match parsing

This commit is contained in:
nub31
2026-02-15 04:33:21 +01:00
parent caa3b378b3
commit 1511d5d2b8
2 changed files with 40 additions and 6 deletions

View File

@@ -234,6 +234,24 @@ public class Parser
return new NodeStatementWhile(TokensFrom(startIndex), condition, thenBlock); return new NodeStatementWhile(TokensFrom(startIndex), condition, thenBlock);
} }
if (TryExpectKeyword(Keyword.Match))
{
var target = ParseExpression();
var cases = new List<NodeStatementMatch.Case>();
ExpectSymbol(Symbol.OpenCurly);
while (!TryExpectSymbol(Symbol.CloseCurly))
{
var caseStartIndex = index;
var name = ExpectIdent();
var body = ParseStatement();
cases.Add(new NodeStatementMatch.Case(TokensFrom(caseStartIndex), name, body));
}
return new NodeStatementMatch(TokensFrom(startIndex), target, cases);
}
{
var target = ParseExpression(); var target = ParseExpression();
if (TryExpectSymbol(Symbol.Equal)) if (TryExpectSymbol(Symbol.Equal))
@@ -244,6 +262,7 @@ public class Parser
return new NodeStatementExpression(TokensFrom(startIndex), target); return new NodeStatementExpression(TokensFrom(startIndex), target);
} }
}
private NodeExpression ParseExpression(int minPrecedence = -1) private NodeExpression ParseExpression(int minPrecedence = -1)
{ {
@@ -753,6 +772,18 @@ public class NodeStatementWhile(List<Token> tokens, NodeExpression condition, No
public NodeStatement Block { get; } = block; public NodeStatement Block { get; } = block;
} }
public class NodeStatementMatch(List<Token> tokens, NodeExpression target, List<NodeStatementMatch.Case> cases) : NodeStatement(tokens)
{
public NodeExpression Target { get; } = target;
public List<Case> Cases { get; } = cases;
public class Case(List<Token> tokens, TokenIdent type, NodeStatement block) : Node(tokens)
{
public TokenIdent Type { get; } = type;
public NodeStatement Block { get; } = block;
}
}
public abstract class NodeExpression(List<Token> tokens) : Node(tokens); public abstract class NodeExpression(List<Token> tokens) : Node(tokens);
public class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens) public class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)

View File

@@ -389,6 +389,7 @@ public class Tokenizer
"struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct), "struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct),
"packed" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Packed), "packed" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Packed),
"enum" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Enum), "enum" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Enum),
"match" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Match),
"let" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Let), "let" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Let),
"if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If), "if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If),
"else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else), "else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else),
@@ -536,6 +537,7 @@ public enum Keyword
Struct, Struct,
Packed, Packed,
Enum, Enum,
Match,
Let, Let,
If, If,
Else, Else,
@@ -601,6 +603,7 @@ public static class TokenExtensions
Keyword.Struct => "struct", Keyword.Struct => "struct",
Keyword.Packed => "packed", Keyword.Packed => "packed",
Keyword.Enum => "enum", Keyword.Enum => "enum",
Keyword.Match => "enum",
Keyword.Let => "let", Keyword.Let => "let",
Keyword.If => "if", Keyword.If => "if",
Keyword.Else => "else", Keyword.Else => "else",