This commit is contained in:
nub31
2025-10-17 23:39:13 +02:00
parent 1fdeb60eb5
commit 0bd52298fb
11 changed files with 32 additions and 186 deletions

View File

@@ -20,8 +20,6 @@ public record StructFuncNode(List<Token> Tokens, string Name, FuncSignatureNode
public record StructNode(List<Token> Tokens, string Module, string Name, List<StructFieldNode> Fields, List<StructFuncNode> Functions) : DefinitionNode(Tokens, Module, Name);
public record GlobalVariableNode(List<Token> Tokens, string Module, string Name, ExpressionNode Value) : DefinitionNode(Tokens, Module, Name);
#endregion
#region Statements

View File

@@ -29,7 +29,6 @@ public sealed class TypeChecker
public List<DefinitionNode> Definitions { get; } = [];
public List<Diagnostic> Diagnostics { get; } = [];
public List<NubStructType> ReferencedStructTypes { get; } = [];
public void Check()
{
@@ -41,7 +40,6 @@ public sealed class TypeChecker
Diagnostics.Clear();
Definitions.Clear();
ReferencedStructTypes.Clear();
using (BeginRootScope(_syntaxTree.Metadata.ModuleName))
{
@@ -57,9 +55,6 @@ public sealed class TypeChecker
case StructSyntax structSyntax:
Definitions.Add(CheckStructDefinition(structSyntax));
break;
case GlobalVariableSyntax globalVariableSyntax:
Definitions.Add(CheckGlobalVariable(globalVariableSyntax));
break;
case StructTemplateSyntax:
break;
default:
@@ -90,39 +85,7 @@ public sealed class TypeChecker
private ScopeDisposer BeginRootScope(string moduleName)
{
if (!_visibleModules.TryGetValue(moduleName, out var moduleScope))
{
throw new TypeCheckerException(Diagnostic.Error($"Module with name {moduleName} not found").Build());
}
var scope = new Scope(moduleName);
_scopes.Push(scope);
foreach (var globalVariable in moduleScope.GlobalVariables(true))
{
NubType? type;
if (globalVariable.ExplicitType != null)
{
type = ResolveType(globalVariable.ExplicitType);
var valueExpression = CheckExpression(globalVariable.Value, type);
if (valueExpression.Type != ResolveType(globalVariable.ExplicitType))
{
throw new TypeCheckerException(Diagnostic
.Error("Value does not match explicit type of global variable")
.At(globalVariable.Value)
.Build());
}
}
else
{
type = CheckExpression(globalVariable.Value).Type;
}
scope.DeclareVariable(new Variable(globalVariable.Name, type, VariableKind.LValue));
}
_scopes.Push(new Scope(moduleName));
return new ScopeDisposer(this);
}
@@ -224,11 +187,6 @@ public sealed class TypeChecker
return new FuncNode(node.Tokens, CurrentScope.Module, node.Name, node.ExternSymbol, signature, body);
}
private GlobalVariableNode CheckGlobalVariable(GlobalVariableSyntax node)
{
return new GlobalVariableNode(node.Tokens, CurrentScope.Module, node.Name, CheckExpression(node.Value));
}
private AssignmentNode CheckAssignment(AssignmentSyntax statement)
{
var target = CheckExpression(statement.Target);
@@ -702,7 +660,6 @@ public sealed class TypeChecker
private ExpressionNode CheckLocalIdentifier(LocalIdentifierSyntax expression)
{
// First, look in the current scope for a matching identifier
var scopeIdent = CurrentScope.LookupVariable(expression.Name);
if (scopeIdent != null)
{
@@ -714,7 +671,6 @@ public sealed class TypeChecker
};
}
// Second, look in the current module for a function matching the identifier
var module = _visibleModules[CurrentScope.Module];
var function = module.Functions(true).FirstOrDefault(x => x.Name == expression.Name);
@@ -741,20 +697,6 @@ public sealed class TypeChecker
var includePrivate = expression.Module == CurrentScope.Module;
var globalVariable = module.GlobalVariables(includePrivate).FirstOrDefault(x => x.Name == expression.Name);
if (globalVariable != null)
{
// todo(nub31): This should be done in the global scope
NubType? type = null;
if (globalVariable.ExplicitType != null)
{
type = ResolveType(globalVariable.ExplicitType);
}
return CheckExpression(globalVariable.Value, type);
}
// First, look for the exported function in the specified module
var function = module.Functions(includePrivate).FirstOrDefault(x => x.Name == expression.Name);
if (function != null)
{
@@ -1069,8 +1011,6 @@ public sealed class TypeChecker
.ToList();
result.Functions.AddRange(functions);
ReferencedStructTypes.Add(result);
return result;
}
@@ -1146,8 +1086,6 @@ public sealed class TypeChecker
_checkedTemplateStructs.Add($"{structTemplate.Module}.{mangledName}");
}
ReferencedStructTypes.Add(structType);
return structType;
}
}