match parsing
This commit is contained in:
@@ -234,15 +234,34 @@ public class Parser
|
|||||||
return new NodeStatementWhile(TokensFrom(startIndex), condition, thenBlock);
|
return new NodeStatementWhile(TokensFrom(startIndex), condition, thenBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
var target = ParseExpression();
|
if (TryExpectKeyword(Keyword.Match))
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Equal))
|
|
||||||
{
|
{
|
||||||
var value = ParseExpression();
|
var target = ParseExpression();
|
||||||
return new NodeStatementAssignment(TokensFrom(startIndex), target, value);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NodeStatementExpression(TokensFrom(startIndex), target);
|
{
|
||||||
|
var target = ParseExpression();
|
||||||
|
|
||||||
|
if (TryExpectSymbol(Symbol.Equal))
|
||||||
|
{
|
||||||
|
var value = ParseExpression();
|
||||||
|
return new NodeStatementAssignment(TokensFrom(startIndex), target, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user