defer and struct hooks

This commit is contained in:
nub31
2025-09-19 23:08:12 +02:00
parent 609e1b5d85
commit ca838e32b8
15 changed files with 240 additions and 97 deletions

View File

@@ -175,13 +175,19 @@ public sealed class Parser
{
var memberStartIndex = _tokenIndex;
string? hook = null;
if (TryExpectSymbol(Symbol.At))
{
hook = ExpectIdentifier().Value;
}
if (TryExpectSymbol(Symbol.Func))
{
var funcName = ExpectIdentifier().Value;
var funcSignature = ParseFuncSignature();
var funcBody = ParseBlock();
funcs.Add(new StructFuncSyntax(GetTokens(memberStartIndex), funcName, funcSignature, funcBody));
funcs.Add(new StructFuncSyntax(GetTokens(memberStartIndex), funcName, hook, funcSignature, funcBody));
}
else
{
@@ -209,6 +215,8 @@ public sealed class Parser
{
switch (symbol.Symbol)
{
case Symbol.OpenBrace:
return ParseBlock();
case Symbol.Return:
return ParseReturn();
case Symbol.If:
@@ -219,6 +227,8 @@ public sealed class Parser
return ParseFor();
case Symbol.Let:
return ParseVariableDeclaration();
case Symbol.Defer:
return ParseDefer();
case Symbol.Break:
return ParseBreak();
case Symbol.Continue:
@@ -264,14 +274,22 @@ public sealed class Parser
return new VariableDeclarationSyntax(GetTokens(startIndex), name, explicitType, assignment);
}
private StatementSyntax ParseBreak()
private DeferSyntax ParseDefer()
{
var startIndex = _tokenIndex;
ExpectSymbol(Symbol.Defer);
var statement = ParseStatement();
return new DeferSyntax(GetTokens(startIndex), statement);
}
private BreakSyntax ParseBreak()
{
var startIndex = _tokenIndex;
ExpectSymbol(Symbol.Break);
return new BreakSyntax(GetTokens(startIndex));
}
private StatementSyntax ParseContinue()
private ContinueSyntax ParseContinue()
{
var startIndex = _tokenIndex;
ExpectSymbol(Symbol.Continue);
@@ -303,9 +321,14 @@ public sealed class Parser
var elseStatement = Optional<Variant<IfSyntax, BlockSyntax>>.Empty();
if (TryExpectSymbol(Symbol.Else))
{
elseStatement = TryExpectSymbol(Symbol.If)
? (Variant<IfSyntax, BlockSyntax>)ParseIf()
: (Variant<IfSyntax, BlockSyntax>)ParseBlock();
if (CurrentToken is SymbolToken { Symbol: Symbol.If })
{
elseStatement = (Variant<IfSyntax, BlockSyntax>)ParseIf();
}
else
{
elseStatement = (Variant<IfSyntax, BlockSyntax>)ParseBlock();
}
}
return new IfSyntax(GetTokens(startIndex), condition, body, elseStatement);

View File

@@ -12,6 +12,6 @@ public record FuncSyntax(IEnumerable<Token> Tokens, string Name, bool Exported,
public record StructFieldSyntax(IEnumerable<Token> Tokens, string Name, TypeSyntax Type, Optional<ExpressionSyntax> Value) : SyntaxNode(Tokens);
public record StructFuncSyntax(IEnumerable<Token> Tokens, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : SyntaxNode(Tokens);
public record StructFuncSyntax(IEnumerable<Token> Tokens, string Name, string? Hook, FuncSignatureSyntax Signature, BlockSyntax Body) : SyntaxNode(Tokens);
public record StructSyntax(IEnumerable<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);

View File

@@ -4,6 +4,8 @@ namespace NubLang.Parsing.Syntax;
public abstract record StatementSyntax(IEnumerable<Token> Tokens) : SyntaxNode(Tokens);
public record BlockSyntax(IEnumerable<Token> Tokens, List<StatementSyntax> Statements) : StatementSyntax(Tokens);
public record StatementExpressionSyntax(IEnumerable<Token> Tokens, ExpressionSyntax Expression) : StatementSyntax(Tokens);
public record ReturnSyntax(IEnumerable<Token> Tokens, Optional<ExpressionSyntax> Value) : StatementSyntax(Tokens);
@@ -18,6 +20,8 @@ public record ContinueSyntax(IEnumerable<Token> Tokens) : StatementSyntax(Tokens
public record BreakSyntax(IEnumerable<Token> Tokens) : StatementSyntax(Tokens);
public record DeferSyntax(IEnumerable<Token> Tokens, StatementSyntax Statement) : StatementSyntax(Tokens);
public record WhileSyntax(IEnumerable<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body) : StatementSyntax(Tokens);
public record ForSyntax(IEnumerable<Token> Tokens, string ElementIdent, string? IndexIdent, ExpressionSyntax Target, BlockSyntax Body) : StatementSyntax(Tokens);

View File

@@ -6,6 +6,4 @@ public abstract record SyntaxNode(IEnumerable<Token> Tokens);
public record SyntaxTreeMetadata(string ModuleName, List<string> Imports);
public record SyntaxTree(List<DefinitionSyntax> Definitions, SyntaxTreeMetadata Metadata);
public record BlockSyntax(IEnumerable<Token> Tokens, List<StatementSyntax> Statements) : SyntaxNode(Tokens);
public record SyntaxTree(List<DefinitionSyntax> Definitions, SyntaxTreeMetadata Metadata);