WIP: dev #1

Draft
nub31 wants to merge 103 commits from dev into master
4 changed files with 42 additions and 67 deletions
Showing only changes of commit 660166221c - Show all commits

View File

@@ -328,8 +328,8 @@ public class Generator
TypedNodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression), TypedNodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression),
TypedNodeExpressionEnumLiteral expression => EmitExpressionEnumLiteral(expression), TypedNodeExpressionEnumLiteral expression => EmitExpressionEnumLiteral(expression),
TypedNodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression), TypedNodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression),
TypedNodeExpressionLocalIdent expression => expression.Value.Ident, TypedNodeExpressionLocalIdent expression => expression.Name,
TypedNodeExpressionModuleIdent expression => EmitExpressionModuleIdent(expression), TypedNodeExpressionGlobalIdent expression => EmitNodeExpressionGlobalIdent(expression),
TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression), TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression),
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null) _ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
}; };
@@ -409,10 +409,10 @@ public class Generator
return $"{target}.{expression.Name.Ident}"; return $"{target}.{expression.Name.Ident}";
} }
private string EmitExpressionModuleIdent(TypedNodeExpressionModuleIdent expression) private string EmitNodeExpressionGlobalIdent(TypedNodeExpressionGlobalIdent expression)
{ {
if (!moduleGraph.TryResolveIdentifier(expression.Module.Ident, expression.Value.Ident, true, out var info)) if (!moduleGraph.TryResolveIdentifier(expression.Module, expression.Name, true, out var info))
throw new UnreachableException($"Module graph does not have info about identifier {expression.Module.Ident}::{expression.Value.Ident}. This should have been caught earlier"); throw new UnreachableException($"Module graph does not have info about identifier {expression.Module}::{expression.Name}. This should have been caught earlier");
return info.MangledName; return info.MangledName;
} }

View File

@@ -334,15 +334,14 @@ public class Parser
} }
else if (TryExpectIdent(out var ident)) else if (TryExpectIdent(out var ident))
{ {
if (TryExpectSymbol(Symbol.ColonColon)) List<TokenIdent> sections = [ident];
while (TryExpectSymbol(Symbol.ColonColon))
{ {
var name = ExpectIdent(); sections.Add(ExpectIdent());
expr = new NodeExpressionModuleIdent(TokensFrom(startIndex), ident, name);
}
else
{
expr = new NodeExpressionLocalIdent(TokensFrom(startIndex), ident);
} }
expr = new NodeExpressionIdent(TokensFrom(startIndex), sections);
} }
else if (TryExpectKeyword(Keyword.Struct)) else if (TryExpectKeyword(Keyword.Struct))
{ {
@@ -817,9 +816,9 @@ public class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral valu
public TokenBoolLiteral Value { get; } = value; public TokenBoolLiteral Value { get; } = value;
} }
public class NodeExpressionStructLiteral(List<Token> tokens, NodeType? type, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens) public class NodeExpressionStructLiteral(List<Token> tokens, NodeType type, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
{ {
public NodeType? Type { get; } = type; public NodeType Type { get; } = type;
public List<Initializer> Initializers { get; } = initializers; public List<Initializer> Initializers { get; } = initializers;
public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens) public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
@@ -849,15 +848,9 @@ public class NodeExpressionFuncCall(List<Token> tokens, NodeExpression target, L
public List<NodeExpression> Parameters { get; } = parameters; public List<NodeExpression> Parameters { get; } = parameters;
} }
public class NodeExpressionLocalIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens) public class NodeExpressionIdent(List<Token> tokens, List<TokenIdent> sections) : NodeExpression(tokens)
{ {
public TokenIdent Value { get; } = value; public List<TokenIdent> Sections { get; } = sections;
}
public class NodeExpressionModuleIdent(List<Token> tokens, TokenIdent module, TokenIdent value) : NodeExpression(tokens)
{
public TokenIdent Module { get; } = module;
public TokenIdent Value { get; } = value;
} }
public class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens) public class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)

View File

@@ -171,8 +171,7 @@ public class TypeChecker
NodeExpressionBinary expression => CheckExpressionBinary(expression), NodeExpressionBinary expression => CheckExpressionBinary(expression),
NodeExpressionUnary expression => CheckExpressionUnary(expression), NodeExpressionUnary expression => CheckExpressionUnary(expression),
NodeExpressionBoolLiteral expression => CheckExpressionBoolLiteral(expression), NodeExpressionBoolLiteral expression => CheckExpressionBoolLiteral(expression),
NodeExpressionLocalIdent expression => CheckExpressionIdent(expression), NodeExpressionIdent expression => CheckExpressionIdent(expression),
NodeExpressionModuleIdent expression => CheckExpressionModuleIdent(expression),
NodeExpressionIntLiteral expression => CheckExpressionIntLiteral(expression), NodeExpressionIntLiteral expression => CheckExpressionIntLiteral(expression),
NodeExpressionMemberAccess expression => CheckExpressionMemberAccess(expression), NodeExpressionMemberAccess expression => CheckExpressionMemberAccess(expression),
NodeExpressionFuncCall expression => CheckExpressionFuncCall(expression), NodeExpressionFuncCall expression => CheckExpressionFuncCall(expression),
@@ -321,20 +320,29 @@ public class TypeChecker
return new TypedNodeExpressionBoolLiteral(expression.Tokens, NubTypeBool.Instance, expression.Value); return new TypedNodeExpressionBoolLiteral(expression.Tokens, NubTypeBool.Instance, expression.Value);
} }
private TypedNodeExpressionLocalIdent CheckExpressionIdent(NodeExpressionLocalIdent expression) private TypedNodeExpression CheckExpressionIdent(NodeExpressionIdent expression)
{ {
var type = scope.GetIdentifierType(expression.Value.Ident); if (expression.Sections.Count == 1)
if (type is null) {
throw BasicError($"Identifier '{expression.Value.Ident}' is not declared", expression.Value); var name = expression.Sections[0].Ident;
return new TypedNodeExpressionLocalIdent(expression.Tokens, type, expression.Value); var localType = scope.GetIdentifierType(name);
} if (localType is not null)
return new TypedNodeExpressionLocalIdent(expression.Tokens, localType, name);
private TypedNodeExpressionModuleIdent CheckExpressionModuleIdent(NodeExpressionModuleIdent expression) if (moduleGraph.TryResolveIdentifier(currentModule, name, true, out var ident))
{ return new TypedNodeExpressionGlobalIdent(expression.Tokens, ident.Type, currentModule, name);
var module = ResolveModule(expression.Module); }
var info = ResolveModuleIdentifier(module, expression.Value); else if (expression.Sections.Count == 2)
return new TypedNodeExpressionModuleIdent(expression.Tokens, info.Type, expression.Module, expression.Value); {
var module = expression.Sections[0].Ident;
var name = expression.Sections[1].Ident;
if (moduleGraph.TryResolveIdentifier(module, name, true, out var ident))
return new TypedNodeExpressionGlobalIdent(expression.Tokens, ident.Type, module, name);
}
throw BasicError($"Unknown identifier '{string.Join("::", expression.Sections.Select(x => x.Ident))}'", expression);
} }
private TypedNodeExpressionIntLiteral CheckExpressionIntLiteral(NodeExpressionIntLiteral expression) private TypedNodeExpressionIntLiteral CheckExpressionIntLiteral(NodeExpressionIntLiteral expression)
@@ -510,14 +518,6 @@ public class TypeChecker
return module; return module;
} }
private Module.IdentifierInfo ResolveModuleIdentifier(Module module, TokenIdent name)
{
if (!module.TryResolveIdentifier(name.Ident, currentModule == module.Name, out var type))
throw BasicError($"Identifier '{module.Name}::{name.Ident}' not found", name);
return type;
}
private Module.TypeInfo ResolveModuleTypeInfo(Module module, TokenIdent name) private Module.TypeInfo ResolveModuleTypeInfo(Module module, TokenIdent name)
{ {
if (!module.TryResolveType(name.Ident, currentModule == module.Name, out var type)) if (!module.TryResolveType(name.Ident, currentModule == module.Name, out var type))
@@ -526,24 +526,6 @@ public class TypeChecker
return type; return type;
} }
private Module.TypeInfoEnum ResolveModuleEnum(Module module, TokenIdent name)
{
var type = ResolveModuleTypeInfo(module, name);
if (type is not Module.TypeInfoEnum info)
throw BasicError($"'{module.Name}::{name.Ident}' is not an enum", name);
return info;
}
private Module.TypeInfoStruct ResolveModuleStruct(Module module, TokenIdent name)
{
var type = ResolveModuleTypeInfo(module, name);
if (type is not Module.TypeInfoStruct info)
throw BasicError($"'{module.Name}::{name.Ident}' is not a struct", name);
return info;
}
private bool TryResolveEnumVariant(string moduleName, string enumName, string variantName, [NotNullWhen(true)] out NubType? result) private bool TryResolveEnumVariant(string moduleName, string enumName, string variantName, [NotNullWhen(true)] out NubType? result)
{ {
result = null; result = null;
@@ -758,15 +740,15 @@ public class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, Typed
public List<TypedNodeExpression> Parameters { get; } = parameters; public List<TypedNodeExpression> Parameters { get; } = parameters;
} }
public class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, TokenIdent value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, string value) : TypedNodeExpression(tokens, type)
{ {
public TokenIdent Value { get; } = value; public string Name { get; } = value;
} }
public class TypedNodeExpressionModuleIdent(List<Token> tokens, NubType type, TokenIdent module, TokenIdent value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionGlobalIdent(List<Token> tokens, NubType type, string module, string value) : TypedNodeExpression(tokens, type)
{ {
public TokenIdent Module { get; } = module; public string Module { get; } = module;
public TokenIdent Value { get; } = value; public string Name { get; } = value;
} }
public class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type)

View File

@@ -40,7 +40,7 @@ export func add(a: i32 b: i32): i32
} }
} }
return math::add_internal(a b) return add_internal(a b)
} }
func add_internal(a: i32 b: i32): i32 func add_internal(a: i32 b: i32): i32