Skip to content

Commit

Permalink
feat: partial list support
Browse files Browse the repository at this point in the history
- declare list variables with square brackets
- print list variables
  • Loading branch information
anirudhgray committed Oct 25, 2024
1 parent 0cac23d commit d853597
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
13 changes: 13 additions & 0 deletions tahini/app/src/main/java/com/tahini/lang/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Visitor<R> {
R visitTernaryExpr(Ternary expr);
R visitVariableExpr(Variable expr);
R visitLogicalExpr(Logical expr);
R visitTahiniListExpr(TahiniList expr);
}
static class Assign extends Expr {
Assign(Token name, Expr value) {
Expand Down Expand Up @@ -142,6 +143,18 @@ <R> R accept(Visitor<R> visitor) {
final Token operator;
final Expr right;
}
static class TahiniList extends Expr {
TahiniList(List<Expr> elements) {
this.elements = elements;
}

@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitTahiniListExpr(this);
}

final List<Expr> elements;
}

abstract <R> R accept(Visitor<R> visitor);
}
9 changes: 9 additions & 0 deletions tahini/app/src/main/java/com/tahini/lang/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ private String stringify(Object object) {
return object.toString();
}

@Override
public Object visitTahiniListExpr(Expr.TahiniList expr) {
List<Object> tahiniList = new ArrayList<>();
for (Expr element : expr.elements) {
tahiniList.add(evaluate(element));
}
return tahiniList;
}

@Override
public Object visitLiteralExpr(Expr.Literal expr) {
return expr.value;
Expand Down
11 changes: 11 additions & 0 deletions tahini/app/src/main/java/com/tahini/lang/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,17 @@ private Expr primary() {
return new Expr.Grouping(expr);
}

if (match(TokenType.LEFT_SQUARE)) {
List<Expr> elements = new ArrayList<>();
if (!check(TokenType.RIGHT_SQUARE)) {
do {
elements.add(expression());
} while (match(TokenType.COMMA));
}
consume(TokenType.RIGHT_SQUARE, "Expect ']' after list elements.");
return new Expr.TahiniList(elements);
}

// Error production for binary operator without left-hand operand
if (match(TokenType.PLUS, TokenType.MINUS, TokenType.STAR, TokenType.SLASH, TokenType.EQUAL_EQUAL, TokenType.BANG_EQUAL, TokenType.GREATER, TokenType.GREATER_EQUAL, TokenType.LESS, TokenType.LESS_EQUAL)) {
Token operator = previous();
Expand Down
4 changes: 4 additions & 0 deletions tahini/app/src/main/java/com/tahini/lang/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ private void scanToken() {
addToken(TokenType.LEFT_BRACE);
case '}' ->
addToken(TokenType.RIGHT_BRACE);
case '[' ->
addToken(TokenType.LEFT_SQUARE);
case ']' ->
addToken(TokenType.RIGHT_SQUARE);
case ',' ->
addToken(TokenType.COMMA);
case '.' ->
Expand Down
2 changes: 1 addition & 1 deletion tahini/app/src/main/java/com/tahini/lang/TokenType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

enum TokenType {
// Single-character tokens.
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE, LEFT_SQUARE, RIGHT_SQUARE,
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR, QUESTION_MARK, COLON,
// One or two character tokens.
BANG, BANG_EQUAL,
Expand Down
3 changes: 2 additions & 1 deletion tahini/app/src/main/java/com/tahini/tool/GenerateAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static void main(String[] args) throws IOException {
"Unary : Token operator, Expr right",
"Ternary : Expr condition, Expr left, Expr right",
"Variable : Token name",
"Logical : Expr left, Token operator, Expr right"
"Logical : Expr left, Token operator, Expr right",
"TahiniList : List<Expr> elements"
));

defineAst(outputDir, "Stmt", Arrays.asList(
Expand Down

0 comments on commit d853597

Please sign in to comment.