forked from source-academy/js-slang
-
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.
feat: integrate
Control
with heap (#18)
* refactor: abstract `AstMap` as a class object This is mainly so that we are able to maintain a state to keep track of what is the next `uid` for a AST node. We need to produce more uid since we may do runtime transpilation (e.g. `ForClause` -> `ForCondition`) that introduces more AST nodes. A more straightforward example would be the inject of `main()` call expression to the AST before the execution, where the `CallExpression` needs a uid. * chore: remove `husky` for better DX 🚀 `pre-commit` and `pre-push` hooks are super slow and we don't have any tests right now so I decided to remove it temporarily (we can always revert it later). * feat: implement `AstNode` and `CallOp` support in the heap Added helper functions in the heap to allocate/resolve multiple values. This would prevent having to `H.(alloc/resolve).bind(H)` multiple times in the ECE * feat: integrate AST nodes and `CallOp` with heap Bunch of minor refactors to make code more readable * fix: update uid of `CALL_MAIN` for each evaluate Currently, we are doing a in-place uid assign to `CALL_MAIN` and we only assign a uid if there no uid in the node. However, since `CALL_MAIN` in the outer scope of `evaluate`, we do not assign the right uid on each run of `evaluate`, since the `SourceFile` uid changes. * feat: implement support for `EnvOp` in heap * feat: pass all `Control` values through heap * feat: implement support for `VarDeclOp` in heap Added some helper functions to `AstMap` * feat: implement `UnaryOp` and `BinaryOp` support in heap * feat: implement `AssignOp` support in heap * feat: implement `PopSOp` support in heap * refactor: remove redundant indirection for `BuiltinOp` Instead of having a extra `ApplyBuiltinOp`, we execute the op during the handling of `CallOp` * refactor: add type assertion for clarity
- Loading branch information
1 parent
58d55df
commit 983a274
Showing
11 changed files
with
315 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
import { Node } from '../types' | ||
|
||
export class AstMap extends Map<number, Node> { | ||
private nextAstId: number | ||
|
||
constructor(nextAstId: number) { | ||
super() | ||
this.nextAstId = nextAstId | ||
} | ||
|
||
/** | ||
* Track an AST node | ||
* | ||
* If the node already has a unique identifier, it will be returned as is. | ||
* Otherwise, a new unique identifier will be assigned to the node (in-place). | ||
* | ||
* @param node AST node to track | ||
* @returns AST node with a unique identifier | ||
*/ | ||
public track(node: Node): Node { | ||
node.uid = node.uid ?? this.nextAstId++ | ||
this.set(node.uid, node) | ||
return node | ||
} | ||
|
||
public trackM(nodes: Node[]): Node[] { | ||
return nodes.map(this.track.bind(this)) | ||
} | ||
|
||
public get<T extends Node>(uid: number): T { | ||
return super.get(uid) as T | ||
} | ||
} |
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
Oops, something went wrong.