This commit is contained in:
nub31
2025-10-21 20:47:00 +02:00
parent 4a01fbc306
commit e3f819411c
6 changed files with 74 additions and 125 deletions

View File

@@ -100,23 +100,19 @@ public record UnaryExpressionNode(List<Token> Tokens, NubType Type, UnaryOperato
public record FuncCallNode(List<Token> Tokens, NubType Type, ExpressionNode Expression, List<ExpressionNode> Parameters) : RValueExpressionNode(Tokens, Type);
public record LValueIdentifierNode(List<Token> Tokens, NubType Type, string Name) : LValueExpressionNode(Tokens, Type);
public record RValueIdentifierNode(List<Token> Tokens, NubType Type, string Name) : RValueExpressionNode(Tokens, Type);
public record VariableIdentifierNode(List<Token> Tokens, NubType Type, string Name) : LValueExpressionNode(Tokens, Type);
public record FuncIdentifierNode(List<Token> Tokens, NubType Type, string Module, string Name, string? ExternSymbol) : RValueExpressionNode(Tokens, Type);
public record ArrayInitializerNode(List<Token> Tokens, NubType Type, ExpressionNode Capacity, NubType ElementType) : RValueExpressionNode(Tokens, Type);
public record ArrayInitializerNode(List<Token> Tokens, NubType Type, ExpressionNode Capacity, NubType ElementType) : LValueExpressionNode(Tokens, Type);
public record ArrayIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Tokens, Type);
public record ConstArrayIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Tokens, Type);
public record SliceIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Tokens, Type);
public record AddressOfNode(List<Token> Tokens, NubType Type, LValueExpressionNode LValue) : RValueExpressionNode(Tokens, Type);
public record StructFieldAccessNode(List<Token> Tokens, NubType Type, LValueExpressionNode Target, string Field) : LValueExpressionNode(Tokens, Type);
public record StructFieldAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, string Field) : LValueExpressionNode(Tokens, Type);
public record StructInitializerNode(List<Token> Tokens, NubStructType StructType, Dictionary<string, ExpressionNode> Initializers) : LValueExpressionNode(Tokens, StructType);

View File

@@ -109,7 +109,7 @@ public sealed class TypeChecker
{
foreach (var parameter in node.Prototype.Parameters)
{
CurrentScope.DeclareVariable(new Variable(parameter.Name, ResolveType(parameter.Type), VariableKind.RValue));
CurrentScope.DeclareVariable(new Variable(parameter.Name, ResolveType(parameter.Type)));
}
var prototype = CheckFuncPrototype(node.Prototype);
@@ -211,7 +211,7 @@ public sealed class TypeChecker
throw new TypeCheckerException(Diagnostic.Error($"Cannot infer type of variable {statement.Name}").At(statement).Build());
}
CurrentScope.DeclareVariable(new Variable(statement.Name, type, VariableKind.LValue));
CurrentScope.DeclareVariable(new Variable(statement.Name, type));
return new VariableDeclarationNode(statement.Tokens, statement.Name, assignmentNode, type);
}
@@ -248,7 +248,6 @@ public sealed class TypeChecker
LocalIdentifierSyntax expression => CheckLocalIdentifier(expression),
ModuleIdentifierSyntax expression => CheckModuleIdentifier(expression),
BoolLiteralSyntax expression => CheckBoolLiteral(expression),
ConstArrayInitializerSyntax expression => CheckConstArrayInitializer(expression),
StringLiteralSyntax expression => CheckStringLiteral(expression, expectedType),
IntLiteralSyntax expression => CheckIntLiteral(expression, expectedType),
FloatLiteralSyntax expression => CheckFloatLiteral(expression, expectedType),
@@ -284,13 +283,6 @@ public sealed class TypeChecker
throw new TypeCheckerException(Diagnostic.Error($"Cannot convert {result.Type} to {expectedType}").At(node).Build());
}
private ArrayInitializerNode CheckConstArrayInitializer(ConstArrayInitializerSyntax expression)
{
var elementType = ResolveType(expression.ElementType);
var type = new NubArrayType(elementType);
return new ArrayInitializerNode(expression.Tokens, type, new IntLiteralNode([], new NubIntType(false, 64), expression.Capacity), elementType);
}
private FloatToIntBuiltinNode CheckFloatToInt(FloatToIntBuiltinSyntax expression)
{
var value = CheckExpression(expression.Value);
@@ -334,8 +326,6 @@ public sealed class TypeChecker
return target.Type switch
{
NubArrayType arrayType => new ArrayIndexAccessNode(expression.Tokens, arrayType.ElementType, target, index),
NubConstArrayType constArrayType => new ConstArrayIndexAccessNode(expression.Tokens, constArrayType.ElementType, target, index),
NubSliceType sliceType => new SliceIndexAccessNode(expression.Tokens, sliceType.ElementType, target, index),
_ => throw new TypeCheckerException(Diagnostic.Error($"Cannot use array indexer on type {target.Type}").At(expression).Build())
};
}
@@ -576,12 +566,7 @@ public sealed class TypeChecker
var scopeIdent = CurrentScope.LookupVariable(expression.Name);
if (scopeIdent != null)
{
return scopeIdent.Kind switch
{
VariableKind.LValue => new LValueIdentifierNode(expression.Tokens, scopeIdent.Type, expression.Name),
VariableKind.RValue => new RValueIdentifierNode(expression.Tokens, scopeIdent.Type, expression.Name),
_ => throw new ArgumentOutOfRangeException()
};
return new VariableIdentifierNode(expression.Tokens, scopeIdent.Type, expression.Name);
}
var module = _importedModules[CurrentScope.Module];
@@ -686,12 +671,7 @@ public sealed class TypeChecker
.Build());
}
if (target is not LValueExpressionNode lValueTarget)
{
throw new TypeCheckerException(Diagnostic.Error("Struct field access is not an lvalue (this should never happen)").At(expression.Target).Build());
}
return new StructFieldAccessNode(expression.Tokens, field.Type, lValueTarget, expression.Member);
return new StructFieldAccessNode(expression.Tokens, field.Type, target, expression.Member);
}
default:
{
@@ -894,13 +874,7 @@ public sealed class TypeChecker
}
}
public enum VariableKind
{
LValue,
RValue
}
public record Variable(string Name, NubType Type, VariableKind Kind);
public record Variable(string Name, NubType Type);
public class Scope(string module, Scope? parent = null)
{