Skip to content

Java-based interpreter for Featherweight JavaScript built with ANTLR. Implements a minimal JavaScript subset with functions, closures, conditionals, loops, and operators.

Notifications You must be signed in to change notification settings

satam2/fwjs-interpreter

Repository files navigation

Featherweight JavaScript (FWJS) Interpreter

A Java-based interpreter for Featherweight JavaScript (FWJS), a minimal but powerful subset of JavaScript. This project is built using ANTLR 4 for lexing and parsing, and implements a tree-walking interpreter using the Visitor pattern.

The interpreter supports functional programming concepts (first-class functions, closures) as well as imperative control flow, making it a robust academic example of language implementation.

🚀 Features

The FWJS language supports the following core features:

  • Variables & Lexical Scoping:
    • var declarations with optional initialization.
    • Strict lexical scoping (variables are resolved in the environment where the function was defined).
    • Support for closures and nested environments.
  • Functions:
    • First-class anonymous functions (function(x) { ... }).
    • Lexical closures (functions capture their defining environment).
  • Control Structures:
    • if-then-else conditionals.
    • while loops.
  • Expressions & Operators:
    • Arithmetic: +, -, *, /, %.
    • Comparison: <, >, <=, >=, ==.
    • Values: Integers, Booleans (true/false), null, and Function Closures.
  • Block Scoping: Statements grouped in { ... } blocks.

📂 Project Structure

  • src/edu/sjsu/fwjs/: Contains the core Java implementation.
    • Interpreter.java: The entry point (main method) that reads script files and kicks off execution.
    • Expression.java: Defines the AST nodes (ValueExpr, BinOpExpr, IfExpr, etc.) and their evaluate logic.
    • ExpressionBuilderVisitor.java: Converts the ANTLR ParseTree into our custom Expression AST.
    • Environment.java: Manages variable scopes and lookups (symbol table).
    • Value.java: Defines runtime values (IntVal, BoolVal, ClosureVal, NullVal).
  • FeatherweightJavaScript.g4: The grammar file defining the syntax rules for ANTLR.
  • fwjsScripts/: A collection of example scripts (.fwjs) to test the interpreter (e.g., factorial.fwjs, lists.fwjs).
  • lib/: Contains required JAR dependencies (ANTLR, JUnit).
  • generatedSrc/: (Ignored by Git) Contains source code automatically generated by ANTLR.

💻 Language Examples

Variable Declaration & Arithmetic:

var x = 10;
var y = 20;
print(x + y * 2); // Output: 50

Functions & Closures:

var makeCounter = function() {
  var count = 0;
  function() {
    count = count + 1;
    count;
  };
};

var c = makeCounter();
print(c()); // 1
print(c()); // 2

Control Flow:

var x = 10;
while (x > 0) {
  print(x);
  x = x - 1;
}

🛠 Dependencies

The project is self-contained. All necessary libraries are included in the lib/ directory.

  • Java Development Kit (JDK): Version 8 or higher.
  • Make: Used for the build process (Windows/Linux/macOS).

Included Libraries:

  • antlr-4.13.2-complete.jar: The parser generator runtime.
  • junit-4.12.jar & hamcrest-core-1.3.jar: Unit testing framework.

⚙️ How to Build and Run

We use a Makefile to simplify the build process.

1. Build the Project

This command generates the parser from the .g4 grammar file and compiles all Java sources.

make

2. Run Example Scripts

To execute all test scripts provided in the fwjsScripts/ folder:

make run

You can also run a specific script manually:

java -cp "build;lib/antlr-4.13.2-complete.jar" edu.sjsu.fwjs.Interpreter fwjsScripts/factorial.fwjs

3. Clean Project

To remove compiled .class files and generated sources:

make clean

About

Java-based interpreter for Featherweight JavaScript built with ANTLR. Implements a minimal JavaScript subset with functions, closures, conditionals, loops, and operators.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published