https://github.com/pixilcode/js-subset-interpreter
https://github.com/pixilcode/js-subset-interpreter
Last synced: 7 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/pixilcode/js-subset-interpreter
- Owner: pixilcode
- Created: 2024-01-30T23:27:06.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-07T19:11:49.000Z (9 months ago)
- Last Synced: 2025-05-07T20:25:02.975Z (9 months ago)
- Language: OCaml
- Size: 83 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScript Interpreter
## System Requirements
* Install OCaml
* [Unix and macOS](https://ocaml.org/docs/installing-ocaml#installation-on-unix-and-macos)
* [Windows](https://ocaml.org/docs/installing-ocaml#installation-on-windows)
* Install `dune`
* `opam install dune`
* Install `acorn`
* `npm install -g acorn` (`-g` implies that it will be installed globally,
excluding `-g` will install it locally)
## Running the parser
For a file containing JavaScript code, let's say `foo.js`, run the following:
```bash
acorn --ecma2024 foo.js | dune exec print_ast
```
## Running the interpreter
For a file containing JavaScript code, let's say `foo.js`, run the following:
```bash
acorn --ecma2024 foo.js | dune exec interpret
```
## JavaScript Subset
This interpreter interprets a subset of JavaScript including:
- booleans (`true`, `false`)
- numbers (`1`, `2.3`)
- unary operators
- boolean (`!`)
- number (`+`, `-`)
- binary operators
- relational (`==`, `<`)
- arithmetic (`+`, `-`, `*`, `/`)
- logical (`&&`, `||`)
- conditional expressions (`true ? 1 : 2`)
- variable declaration (`let x = 1;`)
- variable reassignment expression (`x = 2`)
- single-argument function expressions (`function (x) { return x }`)
- single-argument function calls (`foo(1)`)
For more details, see `test/test_js_print_ast.ml` and `test/test_js_interpret.ml`.