WIP: dev #1
@@ -234,6 +234,24 @@ public class Parser
|
||||
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();
|
||||
|
||||
if (TryExpectSymbol(Symbol.Equal))
|
||||
@@ -244,6 +262,7 @@ public class Parser
|
||||
|
||||
return new NodeStatementExpression(TokensFrom(startIndex), target);
|
||||
}
|
||||
}
|
||||
|
||||
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 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 class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)
|
||||
|
||||
@@ -389,6 +389,7 @@ public class Tokenizer
|
||||
"struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct),
|
||||
"packed" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Packed),
|
||||
"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),
|
||||
"if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If),
|
||||
"else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else),
|
||||
@@ -536,6 +537,7 @@ public enum Keyword
|
||||
Struct,
|
||||
Packed,
|
||||
Enum,
|
||||
Match,
|
||||
Let,
|
||||
If,
|
||||
Else,
|
||||
@@ -601,6 +603,7 @@ public static class TokenExtensions
|
||||
Keyword.Struct => "struct",
|
||||
Keyword.Packed => "packed",
|
||||
Keyword.Enum => "enum",
|
||||
Keyword.Match => "enum",
|
||||
Keyword.Let => "let",
|
||||
Keyword.If => "if",
|
||||
Keyword.Else => "else",
|
||||
|
||||
Reference in New Issue
Block a user