-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { Expr, ExpressionVisitor, visitExpr } from "./ast.js"; | ||
|
||
const printer: ExpressionVisitor<string> = { | ||
binary: (expr) => parenthesize(expr.operator.lexeme, expr.left, expr.right), | ||
grouping: (expr) => parenthesize("group", expr.expr), | ||
literal: (expr) => (expr.value === null ? "nil" : String(expr.value)), | ||
unary: (expr) => parenthesize(expr.operator.lexeme, expr.right), | ||
}; | ||
|
||
function parenthesize(name: string, ...exprs: Expr[]) { | ||
const parts = ["(", name]; | ||
for (const expr of exprs) { | ||
parts.push(" "); | ||
parts.push(visitExpr(expr, printer)); | ||
} | ||
parts.push(")"); | ||
return parts.join(""); | ||
} | ||
|
||
test("AST Visitor", () => { | ||
const expr: Expr = { | ||
kind: "binary", | ||
left: { | ||
kind: "unary", | ||
operator: { lexeme: "-", line: 1, literal: null, type: "-" }, | ||
right: { kind: "literal", value: 123 }, | ||
}, | ||
operator: { lexeme: "*", line: 1, literal: null, type: "*" }, | ||
right: { | ||
expr: { | ||
kind: "literal", | ||
value: 45.67, | ||
}, | ||
kind: "grouping", | ||
}, | ||
}; | ||
const text = visitExpr(expr, printer); | ||
expect(text).toEqual("(* (- 123) (group 45.67))"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Token } from "./token.js"; | ||
|
||
export interface Binary { | ||
kind: "binary"; | ||
left: Expr; | ||
operator: Token; | ||
right: Expr; | ||
} | ||
|
||
export interface Grouping { | ||
expr: Expr; | ||
kind: "grouping"; | ||
} | ||
|
||
export interface Literal { | ||
kind: "literal"; | ||
value: null | number | string; | ||
} | ||
|
||
export interface Unary { | ||
kind: "unary"; | ||
operator: Token; | ||
right: Expr; | ||
} | ||
|
||
export type Expr = Binary | Grouping | Literal | Unary; | ||
|
||
export type ExpressionVisitor<R> = { | ||
[Kind in Expr["kind"]]: (expr: Extract<Expr, { kind: Kind }>) => R; | ||
}; | ||
|
||
export function visitExpr<R>(expr: Expr, visitor: ExpressionVisitor<R>): R { | ||
return visitor[expr.kind](expr as never); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters