From 933c52ac1ea8c9255e838840169f0d29cd87463c Mon Sep 17 00:00:00 2001 From: nub31 Date: Mon, 29 Sep 2025 15:13:17 +0200 Subject: [PATCH] ... --- compiler/NubLang/Parsing/Parser.cs | 35 +-------------- .../Parsing/Syntax/DefinitionSyntax.cs | 4 -- .../Parsing/Syntax/ExpressionSyntax.cs | 4 +- compiler/NubLang/Tokenization/Token.cs | 1 - compiler/NubLang/Tokenization/Tokenizer.cs | 1 - compiler/NubLang/TypeChecking/Node/NubType.cs | 2 +- compiler/NubLang/TypeChecking/TypeChecker.cs | 44 ++++++++++--------- example/src/main.nub | 8 +--- 8 files changed, 30 insertions(+), 69 deletions(-) diff --git a/compiler/NubLang/Parsing/Parser.cs b/compiler/NubLang/Parsing/Parser.cs index 84c2d8a..d8b96be 100644 --- a/compiler/NubLang/Parsing/Parser.cs +++ b/compiler/NubLang/Parsing/Parser.cs @@ -86,7 +86,6 @@ public sealed class Parser { Symbol.Func => ParseFunc(startIndex, exported, null), Symbol.Struct => ParseStruct(startIndex, exported), - Symbol.Enum => ParseEnum(startIndex, exported), _ => throw new ParseException(Diagnostic .Error($"Expected 'func' or 'struct' but found '{keyword.Symbol}'") .WithHelp("Valid definition keywords are 'func' and 'struct'") @@ -225,36 +224,6 @@ public sealed class Parser return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields, funcs); } - private EnumSyntax ParseEnum(int startIndex, bool exported) - { - var name = ExpectIdentifier(); - - TypeSyntax? type = null; - if (TryExpectSymbol(Symbol.Colon)) - { - type = ParseType(); - } - - var values = new List(); - - ExpectSymbol(Symbol.OpenBrace); - while (!TryExpectSymbol(Symbol.CloseBrace)) - { - var valueStartIndex = _tokenIndex; - var valueName = ExpectIdentifier().Value; - - ExpressionSyntax? valueValue = null; - if (TryExpectSymbol(Symbol.Assign)) - { - valueValue = ParseExpression(); - } - - values.Add(new EnumValueSyntax(GetTokens(valueStartIndex), valueName, valueValue)); - } - - return new EnumSyntax(GetTokens(startIndex), name.Value, exported, type, values); - } - private StatementSyntax ParseStatement() { var startIndex = _tokenIndex; @@ -593,11 +562,11 @@ public sealed class Parser } } - expr = new DotFuncCallSyntax(GetTokens(startIndex), member, expr, parameters); + expr = new MemberFuncCallSyntax(GetTokens(startIndex), member, expr, parameters); continue; } - expr = new StructFieldAccessSyntax(GetTokens(startIndex), expr, member); + expr = new MemberAccessSyntax(GetTokens(startIndex), expr, member); continue; } diff --git a/compiler/NubLang/Parsing/Syntax/DefinitionSyntax.cs b/compiler/NubLang/Parsing/Syntax/DefinitionSyntax.cs index d39ab77..d86b535 100644 --- a/compiler/NubLang/Parsing/Syntax/DefinitionSyntax.cs +++ b/compiler/NubLang/Parsing/Syntax/DefinitionSyntax.cs @@ -17,7 +17,3 @@ public record StructFuncSyntax(List Tokens, string Name, string? Hook, Fu public record StructSyntax(List Tokens, string Name, bool Exported, List Fields, List Functions) : DefinitionSyntax(Tokens, Name, Exported); public record StructTemplateSyntax(List Tokens, List TemplateArguments, string Name, bool Exported, List Fields, List Functions) : DefinitionSyntax(Tokens, Name, Exported); - -public record EnumValueSyntax(List Tokens, string Name, ExpressionSyntax? Value) : SyntaxNode(Tokens); - -public record EnumSyntax(List Tokens, string Name, bool Exported, TypeSyntax? Type, List Values) : DefinitionSyntax(Tokens, Name, Exported); \ No newline at end of file diff --git a/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs b/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs index cb504c6..9acc77b 100644 --- a/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs +++ b/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs @@ -38,7 +38,7 @@ public record UnaryExpressionSyntax(List Tokens, UnaryOperatorSyntax Oper public record FuncCallSyntax(List Tokens, ExpressionSyntax Expression, List Parameters) : ExpressionSyntax(Tokens); -public record DotFuncCallSyntax(List Tokens, string Name, ExpressionSyntax Target, List Parameters) : ExpressionSyntax(Tokens); +public record MemberFuncCallSyntax(List Tokens, string Name, ExpressionSyntax Target, List Parameters) : ExpressionSyntax(Tokens); public record LocalIdentifierSyntax(List Tokens, string Name) : ExpressionSyntax(Tokens); @@ -52,7 +52,7 @@ public record AddressOfSyntax(List Tokens, ExpressionSyntax Target) : Exp public record LiteralSyntax(List Tokens, string Value, LiteralKind Kind) : ExpressionSyntax(Tokens); -public record StructFieldAccessSyntax(List Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens); +public record MemberAccessSyntax(List Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens); public record StructInitializerSyntax(List Tokens, TypeSyntax? StructType, Dictionary Initializers) : ExpressionSyntax(Tokens); diff --git a/compiler/NubLang/Tokenization/Token.cs b/compiler/NubLang/Tokenization/Token.cs index 79580fd..e7ae188 100644 --- a/compiler/NubLang/Tokenization/Token.cs +++ b/compiler/NubLang/Tokenization/Token.cs @@ -61,7 +61,6 @@ public enum Symbol Export, Defer, At, - Enum, } public abstract record Token(string FileName, SourceSpan Span); diff --git a/compiler/NubLang/Tokenization/Tokenizer.cs b/compiler/NubLang/Tokenization/Tokenizer.cs index 597ba8a..4210ac9 100644 --- a/compiler/NubLang/Tokenization/Tokenizer.cs +++ b/compiler/NubLang/Tokenization/Tokenizer.cs @@ -24,7 +24,6 @@ public sealed class Tokenizer ["export"] = Symbol.Export, ["import"] = Symbol.Import, ["defer"] = Symbol.Defer, - ["enum"] = Symbol.Enum, }; private static readonly Dictionary Symbols = new() diff --git a/compiler/NubLang/TypeChecking/Node/NubType.cs b/compiler/NubLang/TypeChecking/Node/NubType.cs index 6394a83..3a2753d 100644 --- a/compiler/NubLang/TypeChecking/Node/NubType.cs +++ b/compiler/NubLang/TypeChecking/Node/NubType.cs @@ -112,7 +112,7 @@ public class NubStructType(string module, string name, List 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) diff --git a/compiler/NubLang/TypeChecking/TypeChecker.cs b/compiler/NubLang/TypeChecking/TypeChecker.cs index 4ad08d0..2f82621 100644 --- a/compiler/NubLang/TypeChecking/TypeChecker.cs +++ b/compiler/NubLang/TypeChecking/TypeChecker.cs @@ -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) diff --git a/example/src/main.nub b/example/src/main.nub index d8b0548..7542ace 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -3,15 +3,9 @@ import "raylib" module "main" -enum State -{ - NONE - TEST - SOMEOTHERVALUE -} - extern "main" func main(args: []cstring): i64 { + raylib::SetConfigFlags(raylib::ConfigFlags::VSYNC_HINT) raylib::InitWindow(1600, 900, "Hi from nub-lang") raylib::SetTargetFPS(240)