This commit is contained in:
nub31
2025-10-29 18:41:52 +01:00
parent 4f724ddc0c
commit c764857561
10 changed files with 154 additions and 80 deletions

View File

@@ -28,6 +28,7 @@ public sealed class Parser
var startIndex = _tokenIndex;
var exported = TryExpectSymbol(Symbol.Export);
var packed = TryExpectSymbol(Symbol.Packed);
if (TryExpectSymbol(Symbol.Extern))
{
@@ -43,7 +44,7 @@ public sealed class Parser
Symbol.Module => ParseModule(startIndex),
Symbol.Import => ParseImport(startIndex),
Symbol.Func => ParseFunc(startIndex, exported, null),
Symbol.Struct => ParseStruct(startIndex, exported),
Symbol.Struct => ParseStruct(startIndex, exported, packed),
Symbol.Enum => ParseEnum(startIndex, exported),
_ => throw new CompileException(Diagnostic
.Error($"Expected 'func', 'struct', 'enum', 'import' or 'module' but found '{keyword.Symbol}'")
@@ -126,7 +127,7 @@ public sealed class Parser
return new FuncSyntax(GetTokens(startIndex), prototype, body);
}
private StructSyntax ParseStruct(int startIndex, bool exported)
private StructSyntax ParseStruct(int startIndex, bool exported, bool packed)
{
var name = ExpectIdentifier();
@@ -152,7 +153,7 @@ public sealed class Parser
fields.Add(new StructFieldSyntax(GetTokens(memberStartIndex), fieldName, fieldType, fieldValue));
}
return new StructSyntax(GetTokens(startIndex), name, exported, fields);
return new StructSyntax(GetTokens(startIndex), name, exported, packed, fields);
}
private EnumSyntax ParseEnum(int startIndex, bool exported)
@@ -650,7 +651,7 @@ public sealed class Parser
var startIndex = _tokenIndex;
if (TryExpectIdentifier(out var name))
{
if (name.Value[0] == 'u' && int.TryParse(name.Value[1..], out var size))
if (name.Value[0] == 'u' && ulong.TryParse(name.Value[1..], out var size))
{
if (size is not 8 and not 16 and not 32 and not 64)
{
@@ -664,7 +665,7 @@ public sealed class Parser
return new IntTypeSyntax(GetTokens(startIndex), false, size);
}
if (name.Value[0] == 'i' && int.TryParse(name.Value[1..], out size))
if (name.Value[0] == 'i' && ulong.TryParse(name.Value[1..], out size))
{
if (size is not 8 and not 16 and not 32 and not 64)
{
@@ -678,7 +679,7 @@ public sealed class Parser
return new IntTypeSyntax(GetTokens(startIndex), true, size);
}
if (name.Value[0] == 'f' && int.TryParse(name.Value[1..], out size))
if (name.Value[0] == 'f' && ulong.TryParse(name.Value[1..], out size))
{
if (size is not 32 and not 64)
{

View File

@@ -20,7 +20,7 @@ public record FuncSyntax(List<Token> Tokens, FuncPrototypeSyntax Prototype, Bloc
public record StructFieldSyntax(List<Token> Tokens, IdentifierToken NameToken, TypeSyntax Type, ExpressionSyntax? Value) : SyntaxNode(Tokens);
public record StructSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, NameToken, Exported);
public record StructSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, bool Packed, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, NameToken, Exported);
public record EnumFieldSyntax(List<Token> Tokens, IdentifierToken NameToken, IntLiteralToken? ValueToken) : SyntaxNode(Tokens);
@@ -134,9 +134,9 @@ public record PointerTypeSyntax(List<Token> Tokens, TypeSyntax BaseType) : TypeS
public record VoidTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
public record IntTypeSyntax(List<Token> Tokens, bool Signed, int Width) : TypeSyntax(Tokens);
public record IntTypeSyntax(List<Token> Tokens, bool Signed, ulong Width) : TypeSyntax(Tokens);
public record FloatTypeSyntax(List<Token> Tokens, int Width) : TypeSyntax(Tokens);
public record FloatTypeSyntax(List<Token> Tokens, ulong Width) : TypeSyntax(Tokens);
public record BoolTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);

View File

@@ -85,6 +85,7 @@ public enum Symbol
// Modifier
Extern,
Packed,
Export,
Colon,

View File

@@ -248,6 +248,7 @@ public sealed class Tokenizer
"return" => Symbol.Return,
"struct" => Symbol.Struct,
"extern" => Symbol.Extern,
"packed" => Symbol.Packed,
"module" => Symbol.Module,
"export" => Symbol.Export,
"import" => Symbol.Import,

View File

@@ -33,7 +33,7 @@ public sealed class TypedModule
fields.Add(new NubStructFieldType(field.NameToken.Value, typeResolver.ResolveType(field.Type, name), field.Value != null));
}
structTypes.Add(new NubStructType(name, structSyntax.NameToken.Value, fields));
structTypes.Add(new NubStructType(name, structSyntax.NameToken.Value, structSyntax.Packed, fields));
}
return new TypedModule(functionPrototypes, structTypes, module.Imports());