diff --git a/ASTPrinter.cs b/ASTPrinter.cs index 51bb2c8..5be8aaf 100644 --- a/ASTPrinter.cs +++ b/ASTPrinter.cs @@ -125,4 +125,9 @@ class ASTPrinter : IVisitor } } + + public string visitIndexerExpr(Indexer expr) + { + return parenthesize("index", expr.Left, expr.Index); + } } diff --git a/Expr.g.cs b/Expr.g.cs index 14845e1..a305e6f 100644 --- a/Expr.g.cs +++ b/Expr.g.cs @@ -22,6 +22,7 @@ public interface IVisitor { 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(IVisitor visitor) + { + return visitor.visitIndexerExpr(this); + } +} diff --git a/Parser.cs b/Parser.cs index d804ef9..8f95f91 100644 --- a/Parser.cs +++ b/Parser.cs @@ -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)) diff --git a/ast_classes.fsx b/ast_classes.fsx index 080de40..056d12f 100644 --- a/ast_classes.fsx +++ b/ast_classes.fsx @@ -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}"