This commit is contained in:
nub31
2026-02-26 20:00:31 +01:00
parent 660166221c
commit aa5bf0b568
3 changed files with 196 additions and 79 deletions

View File

@@ -193,10 +193,14 @@ public class Parser
if (TryExpectKeyword(Keyword.Let))
{
var name = ExpectIdent();
ExpectSymbol(Symbol.Colon);
var type = ParseType();
NodeType? type = null;
if (TryExpectSymbol(Symbol.Colon))
type = ParseType();
ExpectSymbol(Symbol.Equal);
var value = ParseExpression();
return new NodeStatementVariableDeclaration(TokensFrom(startIndex), name, type, value);
}
@@ -315,6 +319,20 @@ public class Parser
var target = ParseExpression();
expr = new NodeExpressionUnary(TokensFrom(startIndex), target, NodeExpressionUnary.Op.Negate);
}
else if (TryExpectSymbol(Symbol.OpenCurly))
{
var initializers = new List<NodeExpressionStructLiteral.Initializer>();
while (!TryExpectSymbol(Symbol.CloseCurly))
{
var initializerStartIndex = startIndex;
var fieldName = ExpectIdent();
ExpectSymbol(Symbol.Equal);
var fieldValue = ParseExpression();
initializers.Add(new NodeExpressionStructLiteral.Initializer(TokensFrom(initializerStartIndex), fieldName, fieldValue));
}
expr = new NodeExpressionStructLiteral(TokensFrom(startIndex), null, initializers);
}
else if (TryExpectSymbol(Symbol.Bang))
{
var target = ParseExpression();
@@ -760,10 +778,10 @@ public class NodeStatementReturn(List<Token> tokens, NodeExpression value) : Nod
public NodeExpression Value { get; } = value;
}
public class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens)
public class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType? type, NodeExpression value) : NodeStatement(tokens)
{
public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type;
public NodeType? Type { get; } = type;
public NodeExpression Value { get; } = value;
}
@@ -816,9 +834,9 @@ public class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral valu
public TokenBoolLiteral Value { get; } = value;
}
public class NodeExpressionStructLiteral(List<Token> tokens, NodeType type, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
public class NodeExpressionStructLiteral(List<Token> tokens, NodeType? type, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
{
public NodeType Type { get; } = type;
public NodeType? Type { get; } = type;
public List<Initializer> Initializers { get; } = initializers;
public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)