1.0.9 • Published 2 years ago

@childrentime/js-interpreter v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

js-interpreter

standard-readme compliant

Background

This project is my graduation project, the main purpose is to implement a simple javascript interpreter.

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.

If you want to add new syntax support, in general, you need to add parsing of the syntax expression in parse, and then interpret it in interperter.

In addition, the interperter of the while statement and for statement in the project is a bit rough.

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

npm i @childrentime/js-interpreter

Usage

import { interpreter } from "@childrentime/js-interpreter";
const result = interpreter(`var a = 1; console.log(a)`);
console.log(result); // [[1]]

The output of each console statement will be contained in an array so the result is a two-dimensional array

Living Demo

You can experience it in real time here.

test cases

Alternatively, you can view its test files here.

API

export { tokenizer, parse, interpreter };
const code = 'var a = 1;';
const tokens: { type: string, value: string | number }[] = tokenizer(code);
const ast = parse(code);
const interpreter: any[][] = interpreter(code);

License

MIT © ChildrenTime

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago