Anonymous struct initializer syntax
This commit is contained in:
@@ -15,8 +15,8 @@ struct Human
|
|||||||
|
|
||||||
func main(args: []cstring): i64
|
func main(args: []cstring): i64
|
||||||
{
|
{
|
||||||
let x: Human = struct {
|
let x: Human = {
|
||||||
name = struct {
|
name = {
|
||||||
first = "bob"
|
first = "bob"
|
||||||
last = "the builder"
|
last = "the builder"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -455,6 +455,7 @@ public sealed class Parser
|
|||||||
Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()),
|
Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()),
|
||||||
Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()),
|
Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()),
|
||||||
Symbol.OpenBracket => ParseArrayInitializer(startIndex),
|
Symbol.OpenBracket => ParseArrayInitializer(startIndex),
|
||||||
|
Symbol.OpenBrace => new StructInitializerSyntax(GetTokens(startIndex), Optional<TypeSyntax>.Empty(), ParseStructInitializerBody()),
|
||||||
Symbol.Struct => ParseStructInitializer(startIndex),
|
Symbol.Struct => ParseStructInitializer(startIndex),
|
||||||
_ => throw new ParseException(Diagnostic
|
_ => throw new ParseException(Diagnostic
|
||||||
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
|
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
|
||||||
@@ -570,6 +571,13 @@ public sealed class Parser
|
|||||||
ExpectSymbol(Symbol.OpenBrace);
|
ExpectSymbol(Symbol.OpenBrace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var initializers = ParseStructInitializerBody();
|
||||||
|
|
||||||
|
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, ExpressionSyntax> ParseStructInitializerBody()
|
||||||
|
{
|
||||||
Dictionary<string, ExpressionSyntax> initializers = [];
|
Dictionary<string, ExpressionSyntax> initializers = [];
|
||||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||||
{
|
{
|
||||||
@@ -579,7 +587,7 @@ public sealed class Parser
|
|||||||
initializers.Add(name, value);
|
initializers.Add(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
return initializers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BlockSyntax ParseBlock()
|
private BlockSyntax ParseBlock()
|
||||||
|
|||||||
Reference in New Issue
Block a user