A JavaScript Interpreter coded in Python using Lark parsing and lexing library
Lark: https://lark-parser.readthedocs.io/en/stable/#
JavaScript grammar reference: https://tc39.es/ecma262/
JavaScript Error Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors?retiredLocale=it
- Number
- Boolean
- String
- Array
- addition (+)
- subtraction (-)
- multiplication (*)
- division (/)
- equal (==)
- not equal (!=)
- strict equal (===)
- strict not equal (!==)
- greater than (>)
- less than (<)
- greater than or equal to (>=)
- less than or equal to (<=)
- identifier += expression
- identifier -= expression
- identifier *= expression
- identifier /= expression
- identifier ++ (post-increment)
- ++ identifier (pre-increment)
- identifier -- (post-decrement)
- -- identifier (pre-decrement)
- not (!)
- or (||)
- and (&&)
- if
- if else
- ternary conditional operator (condition ? true : false)
- while
- keyboard input (prompt)
- print in console (console.log)
- template literals
- function IDENTIFIER (list_of_parameters) { body }
- IDENTIFIER (list_of_arguments)
- array declaration
- array access (with integer index and non integer index)
- array length (length)
- single line comment (//)
- multi line comment (/* */)
-
Lexical errors (typing error)
-
Syntax errors
- non-matched parenthesis
- identifier already declared
- assignment to constant variable
- redeclaration with different declaration type
- reserved word used as identifier
- unexpected end of input
- missing equal sign in constant declaration
-
Semantic errors
- reference error
- type error
The following test scripts are available:
test_1.js
: test the arithmetic and the relational operations and the type coercion.test_2.js
: test the function declaration and the function call and the array declaration, access and length.test_3.js
: test the branching operations (if, if else) and the loop operations (while) and array declaration, access and length.test_4.js
: generate a lexical errors.test_5.js
: generate a syntax errors.test_6.js
: generate a semantic error.test_7.js
: generate a type error.
- clone the repository or download the project
- install python (our python version: 3.8)
- install lark library:
pip install lark
(our version: 1.1.7)
- open a terminal
- go to the project folder
- run the script:
python main.py -s (--script) <path_to_script>
- the output will be printed in the terminal
- open a terminal
- go to the project folder
- run the script:
python main.py -c (--console)
orpython main.py
- write the script in the console
- press enter
- the output will be printed in the terminal
For both the execution modes, if you specify the flag -d (--debug)
the debug mode will be activated and the Parse Tree will be printed in the terminal.
You can run the executable file for your operating system (Windows or MacOS) by following the instructions in the pre-release v0.1.0-alpha.