Parse function calls
This commit is contained in:
parent
6f742ec577
commit
57cd5b3a9e
@ -130,4 +130,24 @@ class ASTPrinter : IVisitor<string>
|
||||
{
|
||||
return parenthesize("index", expr.Left, expr.Index);
|
||||
}
|
||||
|
||||
public string visitCallExpr(Call expr)
|
||||
{
|
||||
var w = new StringWriter();
|
||||
w.Write("(call ");
|
||||
w.Write(print(expr.Left));
|
||||
foreach (var arg in expr.Arguments)
|
||||
{
|
||||
if (arg == null)
|
||||
{
|
||||
w.Write(" _");
|
||||
}
|
||||
else
|
||||
{
|
||||
w.Write($" {print(arg)}");
|
||||
}
|
||||
}
|
||||
w.Write(')');
|
||||
return w.ToString();
|
||||
}
|
||||
}
|
||||
|
10
Expr.g.cs
10
Expr.g.cs
@ -23,6 +23,7 @@ public interface IVisitor<T> {
|
||||
T visitRecordExpr(Record expr);
|
||||
T visitSelectorExpr(Selector expr);
|
||||
T visitIndexerExpr(Indexer expr);
|
||||
T visitCallExpr(Call expr);
|
||||
}
|
||||
public class Sequence : Expr
|
||||
{
|
||||
@ -130,3 +131,12 @@ public class Indexer : Expr
|
||||
return visitor.visitIndexerExpr(this);
|
||||
}
|
||||
}
|
||||
public class Call : Expr
|
||||
{
|
||||
public required Expr Left { get; init; }
|
||||
public required Expr?[] Arguments { get; init; }
|
||||
public override T accept<T>(IVisitor<T> visitor)
|
||||
{
|
||||
return visitor.visitCallExpr(this);
|
||||
}
|
||||
}
|
||||
|
19
Parser.cs
19
Parser.cs
@ -227,7 +227,24 @@ class Parser
|
||||
|
||||
if (match(TokenType.LParen))
|
||||
{
|
||||
throw new NotImplementedException("TODO apply");
|
||||
var args = new List<Expr?>();
|
||||
while (!check(TokenType.RParen))
|
||||
{
|
||||
if (match(TokenType.Blank))
|
||||
{
|
||||
args.Add(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Add(expression());
|
||||
}
|
||||
if (!match(TokenType.Comma))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
consume(TokenType.RParen, "Expect '(' after arguments.");
|
||||
return new Call { Left = expr, Arguments = args.ToArray() };
|
||||
}
|
||||
|
||||
return expr;
|
||||
|
@ -36,7 +36,9 @@ let types =
|
||||
{ Name = "Selector"
|
||||
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Name"; Name = "FieldName" } ] }
|
||||
{ Name = "Indexer"
|
||||
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ] } ]
|
||||
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ] }
|
||||
{ Name = "Call"
|
||||
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr?[]"; Name = "Arguments" } ] } ]
|
||||
|
||||
let visitorMethod baseName t = $"visit{t.Name}{baseName}"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user