This commit is contained in:
nub31
2025-10-22 17:45:53 +02:00
parent c48f7c6244
commit 8e37852df0
9 changed files with 201 additions and 86 deletions

View File

@@ -36,4 +36,12 @@ public sealed class Module
.Where(x => x.Exported || includePrivate)
.ToList();
}
public List<EnumSyntax> Enums(bool includePrivate)
{
return _definitions
.OfType<EnumSyntax>()
.Where(x => x.Exported || includePrivate)
.ToList();
}
}

View File

@@ -78,9 +78,10 @@ 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', 'struct', 'import' or 'module' but found '{keyword.Symbol}'")
.WithHelp("Valid top level statements are 'func', 'struct', 'import' and 'module'")
.Error($"Expected 'func', 'struct', 'enum', 'import' or 'module' but found '{keyword.Symbol}'")
.WithHelp("Valid top level statements are 'func', 'struct', 'enum', 'import' and 'module'")
.At(keyword)
.Build())
};
@@ -176,6 +177,54 @@ public sealed class Parser
return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields);
}
private EnumSyntax ParseEnum(int startIndex, bool exported)
{
var name = ExpectIdentifier();
TypeSyntax? type = null;
if (TryExpectSymbol(Symbol.Colon))
{
type = ParseType();
}
List<EnumFieldSyntax> fields = [];
ExpectSymbol(Symbol.OpenBrace);
long value = -1;
while (!TryExpectSymbol(Symbol.CloseBrace))
{
var memberStartIndex = _tokenIndex;
var fieldName = ExpectIdentifier().Value;
long fieldValue;
if (TryExpectSymbol(Symbol.Assign))
{
if (!TryExpectIntLiteral(out var intLiteralToken))
{
throw new ParseException(Diagnostic
.Error("Value of enum field must be an integer literal")
.At(CurrentToken)
.Build());
}
fieldValue = Convert.ToInt64(intLiteralToken.Value, intLiteralToken.Base);
value = fieldValue;
}
else
{
fieldValue = value + 1;
value = fieldValue;
}
fields.Add(new EnumFieldSyntax(GetTokens(memberStartIndex), fieldName, fieldValue));
}
return new EnumSyntax(GetTokens(startIndex), name.Value, exported, type, fields);
}
private StatementSyntax ParseStatement()
{
var startIndex = _tokenIndex;

View File

@@ -16,6 +16,10 @@ public record StructFieldSyntax(List<Token> Tokens, string Name, TypeSyntax Type
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, Name, Exported);
public record EnumFieldSyntax(List<Token> Tokens, string Name, long Value) : SyntaxNode(Tokens);
public record EnumSyntax(List<Token> Tokens, string Name, bool Exported, TypeSyntax? Type, List<EnumFieldSyntax> Fields) : DefinitionSyntax(Tokens, Name, Exported);
public enum UnaryOperatorSyntax
{
Negate,

View File

@@ -17,11 +17,14 @@ public enum Symbol
// Declaration
Func,
Struct,
Enum,
Import,
Module,
// Modifier
Extern,
Export,
Colon,
DoubleColon,
OpenParen,
@@ -53,10 +56,8 @@ public enum Symbol
Pipe,
And,
Or,
Module,
Import,
At,
QuestionMark
QuestionMark,
}
public abstract record Token(SourceSpan Span);

View File

@@ -20,6 +20,7 @@ public sealed class Tokenizer
["export"] = Symbol.Export,
["import"] = Symbol.Import,
["defer"] = Symbol.Defer,
["enum"] = Symbol.Enum,
};
private static readonly Dictionary<char[], Symbol> Symbols = new()