...
This commit is contained in:
@@ -48,6 +48,24 @@ public sealed class Parser(List<Token> tokens)
|
||||
return new NodeDefinitionFunc(TokensFrom(startIndex), name, parameters, body, returnType);
|
||||
}
|
||||
|
||||
if (TryExpectKeyword(Keyword.Struct))
|
||||
{
|
||||
var name = ExpectIdent();
|
||||
var fields = new List<NodeDefinitionStruct.Field>();
|
||||
|
||||
ExpectSymbol(Symbol.OpenCurly);
|
||||
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||
{
|
||||
var fieldStartIndex = index;
|
||||
var fieldName = ExpectIdent();
|
||||
ExpectSymbol(Symbol.Colon);
|
||||
var fieldType = ParseType();
|
||||
fields.Add(new NodeDefinitionStruct.Field(TokensFrom(fieldStartIndex), fieldName, fieldType));
|
||||
}
|
||||
|
||||
return new NodeDefinitionStruct(TokensFrom(startIndex), name, fields);
|
||||
}
|
||||
|
||||
throw new Exception("Not a valid definition");
|
||||
}
|
||||
|
||||
@@ -172,34 +190,59 @@ public sealed class Parser(List<Token> tokens)
|
||||
{
|
||||
var startIndex = index;
|
||||
|
||||
NodeExpression expr;
|
||||
|
||||
if (TryExpectSymbol(Symbol.OpenParen))
|
||||
{
|
||||
var value = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseParen);
|
||||
return value;
|
||||
expr = value;
|
||||
}
|
||||
|
||||
if (TryExpectIntLiteral(out var intLiteral))
|
||||
else if (TryExpectIntLiteral(out var intLiteral))
|
||||
{
|
||||
return new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral);
|
||||
expr = new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral);
|
||||
}
|
||||
|
||||
if (TryExpectStringLiteral(out var stringLiteral))
|
||||
else if (TryExpectStringLiteral(out var stringLiteral))
|
||||
{
|
||||
return new NodeExpressionStringLiteral(TokensFrom(startIndex), stringLiteral);
|
||||
expr = new NodeExpressionStringLiteral(TokensFrom(startIndex), stringLiteral);
|
||||
}
|
||||
|
||||
if (TryExpectBoolLiteral(out var boolLiteral))
|
||||
else if (TryExpectBoolLiteral(out var boolLiteral))
|
||||
{
|
||||
return new NodeExpressionBoolLiteral(TokensFrom(startIndex), boolLiteral);
|
||||
expr = new NodeExpressionBoolLiteral(TokensFrom(startIndex), boolLiteral);
|
||||
}
|
||||
|
||||
if (TryExpectIdent(out var ident))
|
||||
else if (TryExpectIdent(out var ident))
|
||||
{
|
||||
return new NodeExpressionIdent(TokensFrom(startIndex), ident);
|
||||
expr = new NodeExpressionIdent(TokensFrom(startIndex), ident);
|
||||
}
|
||||
else if (TryExpectKeyword(Keyword.Struct))
|
||||
{
|
||||
var name = ExpectIdent();
|
||||
var initializers = new List<NodeExpressionStructLiteral.Initializer>();
|
||||
|
||||
ExpectSymbol(Symbol.OpenCurly);
|
||||
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), name, initializers);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Not a valid expression leaf");
|
||||
}
|
||||
|
||||
throw new Exception("Not a valid expression leaf");
|
||||
if (TryExpectSymbol(Symbol.Period))
|
||||
{
|
||||
var name = ExpectIdent();
|
||||
expr = new NodeExpressionMemberAccess(TokensFrom(startIndex), expr, name);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
private NodeType ParseType()
|
||||
@@ -462,6 +505,18 @@ public sealed class NodeDefinitionFunc(List<Token> tokens, TokenIdent name, List
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NodeDefinitionStruct(List<Token> tokens, TokenIdent name, List<NodeDefinitionStruct.Field> fields) : NodeDefinition(tokens)
|
||||
{
|
||||
public readonly TokenIdent Name = name;
|
||||
public readonly List<Field> Fields = fields;
|
||||
|
||||
public sealed class Field(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
|
||||
{
|
||||
public readonly TokenIdent Name = name;
|
||||
public readonly NodeType Type = type;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class NodeStatement(List<Token> tokens) : Node(tokens);
|
||||
|
||||
public sealed class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens)
|
||||
@@ -475,32 +530,32 @@ public sealed class NodeStatementFuncCall(List<Token> tokens, NodeExpression tar
|
||||
public readonly List<NodeExpression> Parameters = parameters;
|
||||
}
|
||||
|
||||
public class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens)
|
||||
public sealed class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens)
|
||||
{
|
||||
public readonly NodeExpression Value = value;
|
||||
}
|
||||
|
||||
public class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens)
|
||||
public sealed class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens)
|
||||
{
|
||||
public readonly TokenIdent Name = name;
|
||||
public readonly NodeType Type = type;
|
||||
public readonly NodeExpression Value = value;
|
||||
}
|
||||
|
||||
public class NodeStatementAssignment(List<Token> tokens, NodeExpression target, NodeExpression value) : NodeStatement(tokens)
|
||||
public sealed class NodeStatementAssignment(List<Token> tokens, NodeExpression target, NodeExpression value) : NodeStatement(tokens)
|
||||
{
|
||||
public readonly NodeExpression Target = target;
|
||||
public readonly NodeExpression Value = value;
|
||||
}
|
||||
|
||||
public class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeStatement thenBlock, NodeStatement? elseBlock) : NodeStatement(tokens)
|
||||
public sealed class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeStatement thenBlock, NodeStatement? elseBlock) : NodeStatement(tokens)
|
||||
{
|
||||
public readonly NodeExpression Condition = condition;
|
||||
public readonly NodeStatement ThenBlock = thenBlock;
|
||||
public readonly NodeStatement? ElseBlock = elseBlock;
|
||||
}
|
||||
|
||||
public class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens)
|
||||
public sealed class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens)
|
||||
{
|
||||
public readonly NodeExpression Condition = condition;
|
||||
public readonly NodeStatement Block = block;
|
||||
@@ -523,16 +578,34 @@ public sealed class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiter
|
||||
public readonly TokenBoolLiteral Value = value;
|
||||
}
|
||||
|
||||
public sealed class NodeExpressionStructLiteral(List<Token> tokens, TokenIdent name, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
|
||||
{
|
||||
public readonly TokenIdent Name = name;
|
||||
public readonly List<Initializer> Initializers = initializers;
|
||||
|
||||
public sealed class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
|
||||
{
|
||||
public readonly TokenIdent Name = name;
|
||||
public readonly NodeExpression Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NodeExpressionMemberAccess(List<Token> tokens, NodeExpression target, TokenIdent name) : NodeExpression(tokens)
|
||||
{
|
||||
public readonly NodeExpression Target = target;
|
||||
public readonly TokenIdent Name = name;
|
||||
}
|
||||
|
||||
public sealed class NodeExpressionIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens)
|
||||
{
|
||||
public readonly TokenIdent Value = value;
|
||||
}
|
||||
|
||||
public class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)
|
||||
public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)
|
||||
{
|
||||
public NodeExpression Left { get; } = left;
|
||||
public readonly NodeExpression Left = left;
|
||||
public readonly Op Operation = operation;
|
||||
public NodeExpression Right { get; } = right;
|
||||
public readonly NodeExpression Right = right;
|
||||
|
||||
|
||||
public enum Op
|
||||
|
||||
Reference in New Issue
Block a user