...
This commit is contained in:
@@ -243,9 +243,10 @@ public class Parser
|
||||
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||
{
|
||||
var caseStartIndex = index;
|
||||
var name = ExpectIdent();
|
||||
var type = ExpectIdent();
|
||||
var variableName = ExpectIdent();
|
||||
var body = ParseStatement();
|
||||
cases.Add(new NodeStatementMatch.Case(TokensFrom(caseStartIndex), name, body));
|
||||
cases.Add(new NodeStatementMatch.Case(TokensFrom(caseStartIndex), type, variableName, body));
|
||||
}
|
||||
|
||||
return new NodeStatementMatch(TokensFrom(startIndex), target, cases);
|
||||
@@ -378,6 +379,28 @@ public class Parser
|
||||
|
||||
expr = new NodeExpressionStructLiteral(TokensFrom(startIndex), module, name, initializers);
|
||||
}
|
||||
else if (TryExpectKeyword(Keyword.Enum))
|
||||
{
|
||||
var module = ExpectIdent();
|
||||
ExpectSymbol(Symbol.ColonColon);
|
||||
var enumName = ExpectIdent();
|
||||
ExpectSymbol(Symbol.Period);
|
||||
var variantName = ExpectIdent();
|
||||
|
||||
var initializers = new List<NodeExpressionEnumLiteral.Initializer>();
|
||||
|
||||
ExpectSymbol(Symbol.OpenCurly);
|
||||
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||
{
|
||||
var initializerStartIndex = startIndex;
|
||||
var fieldName = ExpectIdent();
|
||||
ExpectSymbol(Symbol.Equal);
|
||||
var fieldValue = ParseExpression();
|
||||
initializers.Add(new NodeExpressionEnumLiteral.Initializer(TokensFrom(initializerStartIndex), fieldName, fieldValue));
|
||||
}
|
||||
|
||||
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), module, enumName, variantName, initializers);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CompileException(Diagnostic.Error("Expected start of expression").At(fileName, Peek()).Build());
|
||||
@@ -766,10 +789,10 @@ public class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeS
|
||||
public NodeStatement? ElseBlock { get; } = elseBlock;
|
||||
}
|
||||
|
||||
public class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens)
|
||||
public class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement body) : NodeStatement(tokens)
|
||||
{
|
||||
public NodeExpression Condition { get; } = condition;
|
||||
public NodeStatement Block { get; } = block;
|
||||
public NodeStatement Body { get; } = body;
|
||||
}
|
||||
|
||||
public class NodeStatementMatch(List<Token> tokens, NodeExpression target, List<NodeStatementMatch.Case> cases) : NodeStatement(tokens)
|
||||
@@ -777,10 +800,11 @@ public class NodeStatementMatch(List<Token> tokens, NodeExpression target, List<
|
||||
public NodeExpression Target { get; } = target;
|
||||
public List<Case> Cases { get; } = cases;
|
||||
|
||||
public class Case(List<Token> tokens, TokenIdent type, NodeStatement block) : Node(tokens)
|
||||
public class Case(List<Token> tokens, TokenIdent type, TokenIdent variableName, NodeStatement body) : Node(tokens)
|
||||
{
|
||||
public TokenIdent Type { get; } = type;
|
||||
public NodeStatement Block { get; } = block;
|
||||
public TokenIdent VariableName { get; } = variableName;
|
||||
public NodeStatement Body { get; } = body;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -814,6 +838,20 @@ public class NodeExpressionStructLiteral(List<Token> tokens, TokenIdent module,
|
||||
}
|
||||
}
|
||||
|
||||
public class NodeExpressionEnumLiteral(List<Token> tokens, TokenIdent module, TokenIdent enumName, TokenIdent variantName, List<NodeExpressionEnumLiteral.Initializer> initializers) : NodeExpression(tokens)
|
||||
{
|
||||
public TokenIdent Module { get; } = module;
|
||||
public TokenIdent EnumName { get; } = enumName;
|
||||
public TokenIdent VariantName { get; } = variantName;
|
||||
public List<Initializer> Initializers { get; } = initializers;
|
||||
|
||||
public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
|
||||
{
|
||||
public TokenIdent Name { get; } = name;
|
||||
public NodeExpression Value { get; } = value;
|
||||
}
|
||||
}
|
||||
|
||||
public class NodeExpressionMemberAccess(List<Token> tokens, NodeExpression target, TokenIdent name) : NodeExpression(tokens)
|
||||
{
|
||||
public NodeExpression Target { get; } = target;
|
||||
|
||||
Reference in New Issue
Block a user