Coder Social home page Coder Social logo

marco-valentini / javascript-interpreter Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 94 KB

A basic JavaScript interpreter developed as project work for the exam Formal Languages and Compilers at Politecnico di Bari, for the academic year 2022/23

Python 88.22% JavaScript 11.78%
javascript-interpreter lark python python-implemented-interpreter

javascript-interpreter's Introduction

JavaScript-Interpreter in Python

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

Restriction of the grammar adopted

Data types

  • Number
  • Boolean
  • String
  • Array

Arithmetic Operations (with type coercion simulation)

  • addition (+)
  • subtraction (-)
  • multiplication (*)
  • division (/)

Relational Operations (with type coercion simulation)

  • equal (==)
  • not equal (!=)
  • strict equal (===)
  • strict not equal (!==)
  • greater than (>)
  • less than (<)
  • greater than or equal to (>=)
  • less than or equal to (<=)

Assignment operations (with type coercion simulation)

  • identifier += expression
  • identifier -= expression
  • identifier *= expression
  • identifier /= expression
  • identifier ++ (post-increment)
  • ++ identifier (pre-increment)
  • identifier -- (post-decrement)
  • -- identifier (pre-decrement)

Logical operations (with type coercion simulation)

  • not (!)
  • or (||)
  • and (&&)

Branching operations

  • if
  • if else
  • ternary conditional operator (condition ? true : false)

Loop operations

  • while

Input instruction

  • keyboard input (prompt)

Output instruction:

  • print in console (console.log)
  • template literals

Function declaration

  • function IDENTIFIER (list_of_parameters) { body }

Function call

  • IDENTIFIER (list_of_arguments)

Array

  • array declaration
  • array access (with integer index and non integer index)
  • array length (length)

Comments (ignored)

  • single line comment (//)
  • multi line comment (/* */)

Managed errors

  • 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

Tests (folder javascript_tests)

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.

Instructions to run the interpreter

  1. clone the repository or download the project
  2. install python (our python version: 3.8)
  3. install lark library: pip install lark (our version: 1.1.7)

Script execution

  1. open a terminal
  2. go to the project folder
  3. run the script: python main.py -s (--script) <path_to_script>
  4. the output will be printed in the terminal

Console execution

  1. open a terminal
  2. go to the project folder
  3. run the script: python main.py -c (--console) or python main.py
  4. write the script in the console
  5. press enter
  6. 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.

Executable file execution

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.

javascript-interpreter's People

Contributors

marco-valentini avatar paolomasciullo avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

paolomasciullo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.