...
This commit is contained in:
@@ -171,8 +171,7 @@ public class TypeChecker
|
||||
NodeExpressionBinary expression => CheckExpressionBinary(expression),
|
||||
NodeExpressionUnary expression => CheckExpressionUnary(expression),
|
||||
NodeExpressionBoolLiteral expression => CheckExpressionBoolLiteral(expression),
|
||||
NodeExpressionLocalIdent expression => CheckExpressionIdent(expression),
|
||||
NodeExpressionModuleIdent expression => CheckExpressionModuleIdent(expression),
|
||||
NodeExpressionIdent expression => CheckExpressionIdent(expression),
|
||||
NodeExpressionIntLiteral expression => CheckExpressionIntLiteral(expression),
|
||||
NodeExpressionMemberAccess expression => CheckExpressionMemberAccess(expression),
|
||||
NodeExpressionFuncCall expression => CheckExpressionFuncCall(expression),
|
||||
@@ -321,20 +320,29 @@ public class TypeChecker
|
||||
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 (type is null)
|
||||
throw BasicError($"Identifier '{expression.Value.Ident}' is not declared", expression.Value);
|
||||
if (expression.Sections.Count == 1)
|
||||
{
|
||||
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)
|
||||
{
|
||||
var module = ResolveModule(expression.Module);
|
||||
var info = ResolveModuleIdentifier(module, expression.Value);
|
||||
return new TypedNodeExpressionModuleIdent(expression.Tokens, info.Type, expression.Module, expression.Value);
|
||||
if (moduleGraph.TryResolveIdentifier(currentModule, name, true, out var ident))
|
||||
return new TypedNodeExpressionGlobalIdent(expression.Tokens, ident.Type, currentModule, name);
|
||||
}
|
||||
else if (expression.Sections.Count == 2)
|
||||
{
|
||||
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)
|
||||
@@ -510,14 +518,6 @@ public class TypeChecker
|
||||
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)
|
||||
{
|
||||
if (!module.TryResolveType(name.Ident, currentModule == module.Name, out var type))
|
||||
@@ -526,24 +526,6 @@ public class TypeChecker
|
||||
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)
|
||||
{
|
||||
result = null;
|
||||
@@ -758,15 +740,15 @@ public class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, Typed
|
||||
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 TokenIdent Value { get; } = value;
|
||||
public string Module { get; } = module;
|
||||
public string Name { get; } = value;
|
||||
}
|
||||
|
||||
public class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type)
|
||||
|
||||
Reference in New Issue
Block a user