...
This commit is contained in:
@@ -112,7 +112,7 @@ public class NubStructType(string module, string name, List<NubStructFieldType>
|
||||
|
||||
public override string ToString() => $"{Module}.{Name}";
|
||||
public override bool Equals(NubType? other) => other is NubStructType structType && Name == structType.Name && Module == structType.Module;
|
||||
public override int GetHashCode() => HashCode.Combine(typeof(NubStructType), Name);
|
||||
public override int GetHashCode() => HashCode.Combine(typeof(NubStructType), Module, Name);
|
||||
}
|
||||
|
||||
public class NubStructFieldType(string name, NubType type, bool hasDefaultValue)
|
||||
|
||||
@@ -298,12 +298,12 @@ public sealed class TypeChecker
|
||||
BinaryExpressionSyntax expression => CheckBinaryExpression(expression),
|
||||
UnaryExpressionSyntax expression => CheckUnaryExpression(expression, expectedType),
|
||||
DereferenceSyntax expression => CheckDereference(expression),
|
||||
DotFuncCallSyntax expression => CheckDotFuncCall(expression),
|
||||
MemberFuncCallSyntax expression => CheckMemberFuncCall(expression),
|
||||
FuncCallSyntax expression => CheckFuncCall(expression),
|
||||
LocalIdentifierSyntax expression => CheckLocalIdentifier(expression),
|
||||
ModuleIdentifierSyntax expression => CheckModuleIdentifier(expression),
|
||||
LiteralSyntax expression => CheckLiteral(expression, expectedType),
|
||||
StructFieldAccessSyntax expression => CheckStructFieldAccess(expression),
|
||||
MemberAccessSyntax expression => CheckMemberAccess(expression),
|
||||
StructInitializerSyntax expression => CheckStructInitializer(expression, expectedType),
|
||||
InterpretBuiltinSyntax expression => CheckExpression(expression.Target) with { Type = ResolveType(expression.Type) },
|
||||
SizeBuiltinSyntax expression => new SizeBuiltinNode(new NubIntType(false, 64), ResolveType(expression.Type)),
|
||||
@@ -610,7 +610,7 @@ public sealed class TypeChecker
|
||||
return new FuncCallNode(funcType.ReturnType, accessor, parameters);
|
||||
}
|
||||
|
||||
private StructFuncCallNode CheckDotFuncCall(DotFuncCallSyntax expression)
|
||||
private StructFuncCallNode CheckMemberFuncCall(MemberFuncCallSyntax expression)
|
||||
{
|
||||
// todo(nub31): When adding interfaces, also support other types than structs
|
||||
var target = CheckExpression(expression.Target);
|
||||
@@ -769,28 +769,32 @@ public sealed class TypeChecker
|
||||
}
|
||||
}
|
||||
|
||||
private StructFieldAccessNode CheckStructFieldAccess(StructFieldAccessSyntax expression)
|
||||
private ExpressionNode CheckMemberAccess(MemberAccessSyntax expression)
|
||||
{
|
||||
var target = CheckExpression(expression.Target);
|
||||
|
||||
if (target.Type is not NubStructType structType)
|
||||
switch (target.Type)
|
||||
{
|
||||
throw new TypeCheckerException(Diagnostic
|
||||
.Error($"Cannot access struct member on non-struct type {target.Type}")
|
||||
.At(expression)
|
||||
.Build());
|
||||
}
|
||||
case NubStructType structType:
|
||||
{
|
||||
var field = structType.Fields.FirstOrDefault(x => x.Name == expression.Member);
|
||||
if (field == null)
|
||||
{
|
||||
throw new TypeCheckerException(Diagnostic
|
||||
.Error($"Struct {target.Type} does not have a field with the name {expression.Member}")
|
||||
.At(expression)
|
||||
.Build());
|
||||
}
|
||||
|
||||
var field = structType.Fields.FirstOrDefault(x => x.Name == expression.Member);
|
||||
if (field == null)
|
||||
{
|
||||
throw new TypeCheckerException(Diagnostic
|
||||
.Error($"Struct {target.Type} does not have a field with the name {expression.Member}")
|
||||
.At(expression)
|
||||
.Build());
|
||||
return new StructFieldAccessNode(field.Type, target, expression.Member);
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new TypeCheckerException(Diagnostic
|
||||
.Error($"Cannot access struct member on non-struct type {target.Type}")
|
||||
.At(expression)
|
||||
.Build());
|
||||
}
|
||||
}
|
||||
|
||||
return new StructFieldAccessNode(field.Type, target, expression.Member);
|
||||
}
|
||||
|
||||
private StructInitializerNode CheckStructInitializer(StructInitializerSyntax expression, NubType? expectedType)
|
||||
|
||||
Reference in New Issue
Block a user