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