Parse indexing expressions

This commit is contained in:
Brandon Dyck 2023-07-02 11:46:18 -06:00
parent 5f9b4a2fb6
commit aca4f669ad
4 changed files with 23 additions and 3 deletions

View File

@ -125,4 +125,9 @@ class ASTPrinter : IVisitor<string>
}
}
public string visitIndexerExpr(Indexer expr)
{
return parenthesize("index", expr.Left, expr.Index);
}
}

View File

@ -22,6 +22,7 @@ public interface IVisitor<T> {
T visitVariantExpr(Variant expr);
T visitRecordExpr(Record expr);
T visitSelectorExpr(Selector expr);
T visitIndexerExpr(Indexer expr);
}
public class Sequence : Expr
{
@ -120,3 +121,12 @@ public class Selector : Expr
return visitor.visitSelectorExpr(this);
}
}
public class Indexer : Expr
{
public required Expr Left { get; init; }
public required Expr Index { get; init; }
public override T accept<T>(IVisitor<T> visitor)
{
return visitor.visitIndexerExpr(this);
}
}

View File

@ -220,7 +220,9 @@ class Parser
if (match(TokenType.LBracket))
{
throw new NotImplementedException("TODO index");
var index = expression();
consume(TokenType.RBracket, "Expect '[' after expression.");
return new Indexer { Left = expr, Index = index };
}
if (match(TokenType.LParen))

View File

@ -30,10 +30,13 @@ let types =
Fields = [ { Type = "Name"; Name = "Tag" }; { Type = "Expr?"; Name = "Argument" } ] }
{ Name = "Record"
Fields =
[ { Type = "Field[]"; Name = "Extensions" }
[ { Type = "Field[]"
Name = "Extensions" }
{ Type = "BaseRecord?"; Name = "Base" } ] }
{ Name = "Selector"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Name"; Name = "FieldName" } ] } ]
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Name"; Name = "FieldName" } ] }
{ Name = "Indexer"
Fields = [ { Type = "Expr"; Name = "Left" }; { Type = "Expr"; Name = "Index" } ] } ]
let visitorMethod baseName t = $"visit{t.Name}{baseName}"