...
This commit is contained in:
@@ -87,6 +87,7 @@ public class TypeChecker
|
||||
NodeStatementReturn statement => CheckStatementReturn(statement),
|
||||
NodeStatementVariableDeclaration statement => CheckStatementVariableDeclaration(statement),
|
||||
NodeStatementWhile statement => CheckStatementWhile(statement),
|
||||
NodeStatementMatch statement => CheckStatementMatch(statement),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
@@ -138,7 +139,26 @@ public class TypeChecker
|
||||
|
||||
private TypedNodeStatementWhile CheckStatementWhile(NodeStatementWhile statement)
|
||||
{
|
||||
return new TypedNodeStatementWhile(statement.Tokens, CheckExpression(statement.Condition), CheckStatement(statement.Block));
|
||||
return new TypedNodeStatementWhile(statement.Tokens, CheckExpression(statement.Condition), CheckStatement(statement.Body));
|
||||
}
|
||||
|
||||
private TypedNodeStatementMatch CheckStatementMatch(NodeStatementMatch statement)
|
||||
{
|
||||
var cases = new List<TypedNodeStatementMatch.Case>();
|
||||
var target = CheckExpression(statement.Target);
|
||||
var enumType = (NubTypeEnum)target.Type;
|
||||
|
||||
foreach (var @case in statement.Cases)
|
||||
{
|
||||
using (scope.EnterScope())
|
||||
{
|
||||
scope.DeclareIdentifier(@case.VariableName.Ident, NubTypeEnumVariant.Get(enumType.Module, enumType.Name, @case.Type.Ident));
|
||||
var body = CheckStatement(@case.Body);
|
||||
cases.Add(new TypedNodeStatementMatch.Case(@case.Tokens, @case.Type, @case.VariableName, body));
|
||||
}
|
||||
}
|
||||
|
||||
return new TypedNodeStatementMatch(statement.Tokens, target, cases);
|
||||
}
|
||||
|
||||
private TypedNodeExpression CheckExpression(NodeExpression node)
|
||||
@@ -155,6 +175,7 @@ public class TypeChecker
|
||||
NodeExpressionFuncCall expression => CheckExpressionFuncCall(expression),
|
||||
NodeExpressionStringLiteral expression => CheckExpressionStringLiteral(expression),
|
||||
NodeExpressionStructLiteral expression => CheckExpressionStructLiteral(expression),
|
||||
NodeExpressionEnumLiteral expression => CheckExpressionEnumLiteral(expression),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
@@ -331,6 +352,7 @@ public class TypeChecker
|
||||
switch (target.Type)
|
||||
{
|
||||
case NubTypeStruct structType:
|
||||
{
|
||||
if (!moduleGraph.TryResolveModule(structType.Module, out var module))
|
||||
throw new CompileException(Diagnostic.Error($"Module '{structType.Module}' not found").At(fileName, expression.Target).Build());
|
||||
|
||||
@@ -338,13 +360,35 @@ public class TypeChecker
|
||||
throw new CompileException(Diagnostic.Error($"Type '{structType.Name}' not found in module '{structType.Module}'").At(fileName, expression.Target).Build());
|
||||
|
||||
if (typeDef is not Module.TypeInfoStruct structDef)
|
||||
throw new CompileException(Diagnostic.Error($"Type '{structType.Module}::{structType.Name}' is not a struct").At(fileName, expression.Target).Build());
|
||||
throw new CompileException(Diagnostic.Error($"Type '{target.Type}' is not a struct").At(fileName, expression.Target).Build());
|
||||
|
||||
var field = structDef.Fields.FirstOrDefault(x => x.Name == expression.Name.Ident);
|
||||
if (field == null)
|
||||
throw new CompileException(Diagnostic.Error($"Struct '{target.Type}' does not have a field matching the name '{expression.Name.Ident}'").At(fileName, target).Build());
|
||||
|
||||
return new TypedNodeExpressionMemberAccess(expression.Tokens, field.Type, target, expression.Name);
|
||||
}
|
||||
case NubTypeEnumVariant enumVariantType:
|
||||
{
|
||||
if (!moduleGraph.TryResolveModule(enumVariantType.Module, out var module))
|
||||
throw new CompileException(Diagnostic.Error($"Module '{enumVariantType.Module}' not found").At(fileName, expression.Target).Build());
|
||||
|
||||
if (!module.TryResolveType(enumVariantType.EnumName, moduleName == enumVariantType.Module, out var typeDef))
|
||||
throw new CompileException(Diagnostic.Error($"Type '{enumVariantType.EnumName}' not found in module '{enumVariantType.Module}'").At(fileName, expression.Target).Build());
|
||||
|
||||
if (typeDef is not Module.TypeInfoEnum enumDef)
|
||||
throw new CompileException(Diagnostic.Error($"Type '{enumVariantType.Module}::{enumVariantType.EnumName}' is not an enum").At(fileName, expression.Target).Build());
|
||||
|
||||
var variant = enumDef.Variants.FirstOrDefault(x => x.Name == enumVariantType.VariantName);
|
||||
if (variant == null)
|
||||
throw new CompileException(Diagnostic.Error($"Type '{target.Type}' does not have a variant '{enumVariantType.VariantName}'").At(fileName, expression.Target).Build());
|
||||
|
||||
var field = variant.Fields.FirstOrDefault(x => x.Name == expression.Name.Ident);
|
||||
if (field == null)
|
||||
throw new CompileException(Diagnostic.Error($"Enum variant '{target.Type}' does not have a field matching the name '{expression.Name.Ident}'").At(fileName, target).Build());
|
||||
|
||||
return new TypedNodeExpressionMemberAccess(expression.Tokens, field.Type, target, expression.Name);
|
||||
}
|
||||
default:
|
||||
throw new CompileException(Diagnostic.Error($"{target.Type} has no member '{expression.Name.Ident}'").At(fileName, target).Build());
|
||||
}
|
||||
@@ -384,7 +428,7 @@ public class TypeChecker
|
||||
{
|
||||
var field = structDef.Fields.FirstOrDefault(x => x.Name == initializer.Name.Ident);
|
||||
if (field == null)
|
||||
throw new CompileException(Diagnostic.Error($"Field '{initializer.Name.Ident}' does not exist on struct '{expression.Name.Ident}'").At(fileName, initializer.Name).Build());
|
||||
throw new CompileException(Diagnostic.Error($"Field '{initializer.Name.Ident}' does not exist on struct '{expression.Module.Ident}::{expression.Name.Ident}'").At(fileName, initializer.Name).Build());
|
||||
|
||||
var value = CheckExpression(initializer.Value);
|
||||
if (value.Type != field.Type)
|
||||
@@ -396,6 +440,40 @@ public class TypeChecker
|
||||
return new TypedNodeExpressionStructLiteral(expression.Tokens, NubTypeStruct.Get(expression.Module.Ident, expression.Name.Ident), initializers);
|
||||
}
|
||||
|
||||
private TypedNodeExpressionEnumLiteral CheckExpressionEnumLiteral(NodeExpressionEnumLiteral expression)
|
||||
{
|
||||
if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module))
|
||||
throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build());
|
||||
|
||||
var includePrivate = expression.Module.Ident == moduleName;
|
||||
|
||||
if (!module.TryResolveType(expression.EnumName.Ident, includePrivate, out var typeDef))
|
||||
throw new CompileException(Diagnostic.Error($"Enum '{expression.Module.Ident}::{expression.EnumName.Ident}' not found").At(fileName, expression.EnumName).Build());
|
||||
|
||||
if (typeDef is not Module.TypeInfoEnum enumDef)
|
||||
throw new CompileException(Diagnostic.Error($"Cannot create enum literal of non-enum type '{expression.Module.Ident}::{expression.EnumName.Ident}'").At(fileName, expression.EnumName).Build());
|
||||
|
||||
var variant = enumDef.Variants.FirstOrDefault(x => x.Name == expression.VariantName.Ident);
|
||||
if (variant == null)
|
||||
throw new CompileException(Diagnostic.Error($"Enum '{expression.Module.Ident}::{expression.EnumName.Ident}' does not have a variant '{expression.VariantName.Ident}'").At(fileName, expression.VariantName).Build());
|
||||
|
||||
var initializers = new List<TypedNodeExpressionEnumLiteral.Initializer>();
|
||||
foreach (var initializer in expression.Initializers)
|
||||
{
|
||||
var field = variant.Fields.FirstOrDefault(x => x.Name == initializer.Name.Ident);
|
||||
if (field == null)
|
||||
throw new CompileException(Diagnostic.Error($"Field '{initializer.Name.Ident}' does not exist on enum variant '{expression.Module.Ident}::{expression.EnumName.Ident}.{expression.VariantName.Ident}'").At(fileName, initializer.Name).Build());
|
||||
|
||||
var value = CheckExpression(initializer.Value);
|
||||
if (value.Type != field.Type)
|
||||
throw new CompileException(Diagnostic.Error($"Type of assignment ({value.Type}) does not match expected type of field '{field.Name}' ({field.Type})").At(fileName, initializer.Name).Build());
|
||||
|
||||
initializers.Add(new TypedNodeExpressionEnumLiteral.Initializer(initializer.Tokens, initializer.Name, value));
|
||||
}
|
||||
|
||||
return new TypedNodeExpressionEnumLiteral(expression.Tokens, NubTypeEnumVariant.Get(expression.Module.Ident, expression.EnumName.Ident, expression.VariantName.Ident), initializers);
|
||||
}
|
||||
|
||||
private NubType ResolveType(NodeType node)
|
||||
{
|
||||
return node switch
|
||||
@@ -425,6 +503,7 @@ public class TypeChecker
|
||||
return customType switch
|
||||
{
|
||||
Module.TypeInfoStruct => NubTypeStruct.Get(type.Module.Ident, type.Name.Ident),
|
||||
Module.TypeInfoEnum => NubTypeEnum.Get(type.Module.Ident, type.Name.Ident),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(customType))
|
||||
};
|
||||
}
|
||||
@@ -539,10 +618,23 @@ public class TypedNodeStatementIf(List<Token> tokens, TypedNodeExpression condit
|
||||
public TypedNodeStatement? ElseBlock { get; } = elseBlock;
|
||||
}
|
||||
|
||||
public class TypedNodeStatementWhile(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement block) : TypedNodeStatement(tokens)
|
||||
public class TypedNodeStatementWhile(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement body) : TypedNodeStatement(tokens)
|
||||
{
|
||||
public TypedNodeExpression Condition { get; } = condition;
|
||||
public TypedNodeStatement Block { get; } = block;
|
||||
public TypedNodeStatement Body { get; } = body;
|
||||
}
|
||||
|
||||
public class TypedNodeStatementMatch(List<Token> tokens, TypedNodeExpression target, List<TypedNodeStatementMatch.Case> cases) : TypedNodeStatement(tokens)
|
||||
{
|
||||
public TypedNodeExpression Target { get; } = target;
|
||||
public List<Case> Cases { get; } = cases;
|
||||
|
||||
public class Case(List<Token> tokens, TokenIdent type, TokenIdent variableName, TypedNodeStatement body) : Node(tokens)
|
||||
{
|
||||
public TokenIdent Type { get; } = type;
|
||||
public TokenIdent VariableName { get; } = variableName;
|
||||
public TypedNodeStatement Body { get; } = body;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class TypedNodeExpression(List<Token> tokens, NubType type) : TypedNode(tokens)
|
||||
@@ -576,6 +668,17 @@ public class TypedNodeExpressionStructLiteral(List<Token> tokens, NubType type,
|
||||
}
|
||||
}
|
||||
|
||||
public class TypedNodeExpressionEnumLiteral(List<Token> tokens, NubType type, List<TypedNodeExpressionEnumLiteral.Initializer> initializers) : TypedNodeExpression(tokens, type)
|
||||
{
|
||||
public List<Initializer> Initializers { get; } = initializers;
|
||||
|
||||
public class Initializer(List<Token> tokens, TokenIdent name, TypedNodeExpression value) : Node(tokens)
|
||||
{
|
||||
public TokenIdent Name { get; } = name;
|
||||
public TypedNodeExpression Value { get; } = value;
|
||||
}
|
||||
}
|
||||
|
||||
public class TypedNodeExpressionMemberAccess(List<Token> tokens, NubType type, TypedNodeExpression target, TokenIdent name) : TypedNodeExpression(tokens, type)
|
||||
{
|
||||
public TypedNodeExpression Target { get; } = target;
|
||||
|
||||
Reference in New Issue
Block a user