...
This commit is contained in:
@@ -172,56 +172,56 @@ public class TypeChecker
|
||||
case NodeExpressionBinary.Op.Multiply:
|
||||
case NodeExpressionBinary.Op.Divide:
|
||||
case NodeExpressionBinary.Op.Modulo:
|
||||
{
|
||||
if (left.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side arithmetic operation: {left.Type}").At(fileName, left).Build());
|
||||
{
|
||||
if (left.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side arithmetic operation: {left.Type}").At(fileName, left).Build());
|
||||
|
||||
if (right.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side arithmetic operation: {right.Type}").At(fileName, right).Build());
|
||||
if (right.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side arithmetic operation: {right.Type}").At(fileName, right).Build());
|
||||
|
||||
type = left.Type;
|
||||
break;
|
||||
}
|
||||
type = left.Type;
|
||||
break;
|
||||
}
|
||||
case NodeExpressionBinary.Op.LeftShift:
|
||||
case NodeExpressionBinary.Op.RightShift:
|
||||
{
|
||||
if (left.Type is not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side of left/right shift operation: {left.Type}").At(fileName, left).Build());
|
||||
{
|
||||
if (left.Type is not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side of left/right shift operation: {left.Type}").At(fileName, left).Build());
|
||||
|
||||
if (right.Type is not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side of left/right shift operation: {right.Type}").At(fileName, right).Build());
|
||||
if (right.Type is not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side of left/right shift operation: {right.Type}").At(fileName, right).Build());
|
||||
|
||||
type = left.Type;
|
||||
break;
|
||||
}
|
||||
type = left.Type;
|
||||
break;
|
||||
}
|
||||
case NodeExpressionBinary.Op.Equal:
|
||||
case NodeExpressionBinary.Op.NotEqual:
|
||||
case NodeExpressionBinary.Op.LessThan:
|
||||
case NodeExpressionBinary.Op.LessThanOrEqual:
|
||||
case NodeExpressionBinary.Op.GreaterThan:
|
||||
case NodeExpressionBinary.Op.GreaterThanOrEqual:
|
||||
{
|
||||
if (left.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side of comparison: {left.Type}").At(fileName, left).Build());
|
||||
{
|
||||
if (left.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side of comparison: {left.Type}").At(fileName, left).Build());
|
||||
|
||||
if (right.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side of comparison: {right.Type}").At(fileName, right).Build());
|
||||
if (right.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side of comparison: {right.Type}").At(fileName, right).Build());
|
||||
|
||||
type = NubTypeBool.Instance;
|
||||
break;
|
||||
}
|
||||
type = NubTypeBool.Instance;
|
||||
break;
|
||||
}
|
||||
case NodeExpressionBinary.Op.LogicalAnd:
|
||||
case NodeExpressionBinary.Op.LogicalOr:
|
||||
{
|
||||
if (left.Type is not NubTypeBool)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side of logical operation: {left.Type}").At(fileName, left).Build());
|
||||
{
|
||||
if (left.Type is not NubTypeBool)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for left hand side of logical operation: {left.Type}").At(fileName, left).Build());
|
||||
|
||||
if (right.Type is not NubTypeBool)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side of logical operation: {right.Type}").At(fileName, right).Build());
|
||||
if (right.Type is not NubTypeBool)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for right hand side of logical operation: {right.Type}").At(fileName, right).Build());
|
||||
|
||||
type = NubTypeBool.Instance;
|
||||
break;
|
||||
}
|
||||
type = NubTypeBool.Instance;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
@@ -260,21 +260,21 @@ public class TypeChecker
|
||||
switch (expression.Operation)
|
||||
{
|
||||
case NodeExpressionUnary.Op.Negate:
|
||||
{
|
||||
if (target.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for negation: {target.Type}").At(fileName, target).Build());
|
||||
{
|
||||
if (target.Type is not NubTypeSInt and not NubTypeUInt)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for negation: {target.Type}").At(fileName, target).Build());
|
||||
|
||||
type = target.Type;
|
||||
break;
|
||||
}
|
||||
type = target.Type;
|
||||
break;
|
||||
}
|
||||
case NodeExpressionUnary.Op.Invert:
|
||||
{
|
||||
if (target.Type is not NubTypeBool)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for inversion: {target.Type}").At(fileName, target).Build());
|
||||
{
|
||||
if (target.Type is not NubTypeBool)
|
||||
throw new CompileException(Diagnostic.Error($"Unsupported type for inversion: {target.Type}").At(fileName, target).Build());
|
||||
|
||||
type = NubTypeBool.Instance;
|
||||
break;
|
||||
}
|
||||
type = NubTypeBool.Instance;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
@@ -313,10 +313,10 @@ public class TypeChecker
|
||||
|
||||
var includePrivate = expression.Module.Ident == moduleName;
|
||||
|
||||
if (!module.TryResolveIdentifierType(expression.Value.Ident, includePrivate, out var identifierType))
|
||||
if (!module.TryResolveIdentifier(expression.Value.Ident, includePrivate, out var identifierType))
|
||||
throw new CompileException(Diagnostic.Error($"Identifier '{expression.Module.Ident}::{expression.Value.Ident}' not found").At(fileName, expression.Value).Build());
|
||||
|
||||
return new TypedNodeExpressionModuleIdent(expression.Tokens, identifierType, expression.Module, expression.Value);
|
||||
return new TypedNodeExpressionModuleIdent(expression.Tokens, identifierType.Type, expression.Module, expression.Value);
|
||||
}
|
||||
|
||||
private TypedNodeExpressionIntLiteral CheckExpressionIntLiteral(NodeExpressionIntLiteral expression)
|
||||
@@ -327,14 +327,27 @@ public class TypeChecker
|
||||
private TypedNodeExpressionMemberAccess CheckExpressionMemberAccess(NodeExpressionMemberAccess expression)
|
||||
{
|
||||
var target = CheckExpression(expression.Target);
|
||||
if (target.Type is not NubTypeStruct structType)
|
||||
throw new CompileException(Diagnostic.Error($"Cannot access member of non-struct type {target.Type}").At(fileName, target).Build());
|
||||
|
||||
var field = structType.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());
|
||||
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());
|
||||
|
||||
return new TypedNodeExpressionMemberAccess(expression.Tokens, field.Type, target, expression.Name);
|
||||
if (!module.TryResolveType(structType.Name, moduleName == structType.Module, out var typeDef))
|
||||
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());
|
||||
|
||||
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);
|
||||
default:
|
||||
throw new CompileException(Diagnostic.Error($"{target.Type} has no member '{expression.Name.Ident}'").At(fileName, target).Build());
|
||||
}
|
||||
}
|
||||
|
||||
private TypedNodeExpressionFuncCall CheckExpressionFuncCall(NodeExpressionFuncCall expression)
|
||||
@@ -360,16 +373,16 @@ public class TypeChecker
|
||||
|
||||
var includePrivate = expression.Module.Ident == moduleName;
|
||||
|
||||
if (!module.TryResolveCustomType(expression.Name.Ident, includePrivate, out var customType))
|
||||
if (!module.TryResolveType(expression.Name.Ident, includePrivate, out var typeDef))
|
||||
throw new CompileException(Diagnostic.Error($"Struct '{expression.Module.Ident}::{expression.Name.Ident}' not found").At(fileName, expression.Name).Build());
|
||||
|
||||
if (customType is not NubTypeStruct structType)
|
||||
if (typeDef is not Module.TypeInfoStruct structDef)
|
||||
throw new CompileException(Diagnostic.Error($"Cannot create struct literal of non-struct type '{expression.Module.Ident}::{expression.Name.Ident}'").At(fileName, expression.Name).Build());
|
||||
|
||||
var initializers = new List<TypedNodeExpressionStructLiteral.Initializer>();
|
||||
foreach (var initializer in expression.Initializers)
|
||||
{
|
||||
var field = structType.Fields.FirstOrDefault(x => x.Name == initializer.Name.Ident);
|
||||
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());
|
||||
|
||||
@@ -380,7 +393,7 @@ public class TypeChecker
|
||||
initializers.Add(new TypedNodeExpressionStructLiteral.Initializer(initializer.Tokens, initializer.Name, value));
|
||||
}
|
||||
|
||||
return new TypedNodeExpressionStructLiteral(expression.Tokens, structType, initializers);
|
||||
return new TypedNodeExpressionStructLiteral(expression.Tokens, NubTypeStruct.Get(expression.Module.Ident, expression.Name.Ident), initializers);
|
||||
}
|
||||
|
||||
private NubType ResolveType(NodeType node)
|
||||
@@ -406,10 +419,14 @@ public class TypeChecker
|
||||
|
||||
var includePrivate = type.Module.Ident == moduleName;
|
||||
|
||||
if (!module.TryResolveCustomType(type.Name.Ident, includePrivate, out var customType))
|
||||
if (!module.TryResolveType(type.Name.Ident, includePrivate, out var customType))
|
||||
throw new CompileException(Diagnostic.Error($"Custom type '{type.Module.Ident}::{type.Name.Ident}' not found").At(fileName, type.Name).Build());
|
||||
|
||||
return customType;
|
||||
return customType switch
|
||||
{
|
||||
Module.TypeInfoStruct => NubTypeStruct.Get(type.Module.Ident, type.Name.Ident),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(customType))
|
||||
};
|
||||
}
|
||||
|
||||
private sealed class Scope
|
||||
@@ -479,7 +496,7 @@ public class TypedNodeDefinitionFunc(List<Token> tokens, string module, TokenIde
|
||||
|
||||
public string GetMangledName()
|
||||
{
|
||||
return SymbolNameGen.Exported(Module, Name.Ident, GetNubType());
|
||||
return Compiler.Name.Create(Module, Name.Ident, GetNubType());
|
||||
}
|
||||
|
||||
public class Param(List<Token> tokens, TokenIdent name, NubType type) : TypedNode(tokens)
|
||||
|
||||
Reference in New Issue
Block a user