|
1 |
| -# Motivation |
| 1 | +# js-interpreter |
2 | 2 |
|
3 |
| -This project is about to implement a simple javascript interperter |
| 3 | +[](https://github.com/RichardLitt/standard-readme) |
| 4 | + |
| 5 | +## Background |
| 6 | + |
| 7 | +This project is my graduation project, the main purpose is to implement a simple javascript interpreter. |
| 8 | +<div style = "color: red">Warning: This project only supports simple syntax and has no error handler, For example we don't support new syntax and let const keyword. don't use it for production. |
| 9 | +</div> |
| 10 | + |
| 11 | +But maybe it's a good example for you to learn how to write a javascript interpreter because I try to finish it with minimal code. |
| 12 | + |
| 13 | +If you want to add new syntax support, in general, you need to add parsing of the syntax expression in [parse](./src/parser/parse.ts), and then interpret it in [interperter](./src/interpreter/interpreter.ts). |
| 14 | + |
| 15 | +In addition, the interperter of the while statement and for statement in the project is a bit rough. |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +This project uses node and npm. Go check them out if you don't have them locally installed. |
| 20 | + |
| 21 | +```sh |
| 22 | +npm i @childrentime/js-interpreter |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +```ts |
| 28 | +import { interpreter } from "@childrentime/js-interpreter"; |
| 29 | +const result = interpreter(`var a = 1; console.log(a)`); |
| 30 | +console.log(result); // [[1]] |
| 31 | +``` |
| 32 | + |
| 33 | +The output of each console statement will be contained in an array so the result is a two-dimensional array |
| 34 | + |
| 35 | +### Living Demo |
| 36 | + |
| 37 | +You can experience it in real time [here](https://js-interpreter.vercel.app/). |
| 38 | + |
| 39 | +<div style = "color: red">Warning: It is best not to enter infinite loop code</div> |
| 40 | + |
| 41 | +#### test cases |
| 42 | + |
| 43 | +Alternatively, you can view its test files [here](./src/__test//interprete.test.ts). |
| 44 | + |
| 45 | +## API |
| 46 | + |
| 47 | +```ts |
| 48 | +export { tokenizer, parse, interpreter }; |
| 49 | +const code = 'var a = 1;'; |
| 50 | +const tokens: { type: string, value: string | number }[] = tokenizer(code); |
| 51 | +const ast = parse(code); |
| 52 | +const interpreter: any[][] = interpreter(code); |
| 53 | +``` |
| 54 | + |
| 55 | +## License |
| 56 | + |
| 57 | +[MIT](LICENSE) © ChildrenTime |
0 commit comments