Add string debugging
This commit is contained in:
@@ -409,30 +409,45 @@ public class Parser
|
||||
{
|
||||
var type = ParseType();
|
||||
|
||||
if (TryExpectSymbol(Symbol.OpenParen))
|
||||
if (type is NodeTypeString)
|
||||
{
|
||||
ExpectSymbol(Symbol.OpenParen);
|
||||
var value = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseParen);
|
||||
|
||||
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), type, value);
|
||||
expr = new NodeExpressionStringConstructor(TokensFrom(startIndex), value);
|
||||
}
|
||||
else if (TryExpectSymbol(Symbol.OpenCurly))
|
||||
else if (type is NodeTypeNamed namedType)
|
||||
{
|
||||
var initializers = new List<NodeExpressionStructLiteral.Initializer>();
|
||||
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||
if (TryExpectSymbol(Symbol.OpenParen))
|
||||
{
|
||||
var initializerStartIndex = startIndex;
|
||||
var fieldName = ExpectIdent();
|
||||
ExpectSymbol(Symbol.Equal);
|
||||
var fieldValue = ParseExpression();
|
||||
initializers.Add(new NodeExpressionStructLiteral.Initializer(TokensFrom(initializerStartIndex), fieldName, fieldValue));
|
||||
}
|
||||
var value = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseParen);
|
||||
|
||||
expr = new NodeExpressionStructLiteral(TokensFrom(startIndex), null, initializers);
|
||||
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), namedType, value);
|
||||
}
|
||||
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
|
||||
{
|
||||
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), namedType, null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), type, null);
|
||||
throw BasicError($"Expected named type or string", type);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -740,6 +755,11 @@ public class Parser
|
||||
{
|
||||
return new CompileException(Diagnostic.Error(message).At(fileName, ident).Build());
|
||||
}
|
||||
|
||||
private CompileException BasicError(string message, Node? node)
|
||||
{
|
||||
return new CompileException(Diagnostic.Error(message).At(fileName, node).Build());
|
||||
}
|
||||
}
|
||||
|
||||
public class Ast(string fileName, TokenIdent moduleName, List<NodeDefinition> definitions)
|
||||
@@ -893,9 +913,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, NodeTypeNamed? type, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
|
||||
{
|
||||
public NodeType? Type { get; } = type;
|
||||
public NodeTypeNamed? Type { get; } = type;
|
||||
public List<Initializer> Initializers { get; } = initializers;
|
||||
|
||||
public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
|
||||
@@ -905,12 +925,17 @@ public class NodeExpressionStructLiteral(List<Token> tokens, NodeType? type, Lis
|
||||
}
|
||||
}
|
||||
|
||||
public class NodeExpressionEnumLiteral(List<Token> tokens, NodeType type, NodeExpression? value) : NodeExpression(tokens)
|
||||
public class NodeExpressionEnumLiteral(List<Token> tokens, NodeTypeNamed type, NodeExpression? value) : NodeExpression(tokens)
|
||||
{
|
||||
public NodeType Type { get; } = type;
|
||||
public NodeTypeNamed Type { get; } = type;
|
||||
public NodeExpression? Value { get; } = value;
|
||||
}
|
||||
|
||||
public class NodeExpressionStringConstructor(List<Token> tokens, NodeExpression value) : NodeExpression(tokens)
|
||||
{
|
||||
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