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.
The FWJS language supports the following core features:
- Variables & Lexical Scoping:
vardeclarations 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).
- First-class anonymous functions (
- Control Structures:
if-then-elseconditionals.whileloops.
- Expressions & Operators:
- Arithmetic:
+,-,*,/,%. - Comparison:
<,>,<=,>=,==. - Values: Integers, Booleans (
true/false),null, and Function Closures.
- Arithmetic:
- Block Scoping: Statements grouped in
{ ... }blocks.
src/edu/sjsu/fwjs/: Contains the core Java implementation.Interpreter.java: The entry point (mainmethod) that reads script files and kicks off execution.Expression.java: Defines the AST nodes (ValueExpr,BinOpExpr,IfExpr, etc.) and theirevaluatelogic.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.
Variable Declaration & Arithmetic:
var x = 10;
var y = 20;
print(x + y * 2); // Output: 50Functions & Closures:
var makeCounter = function() {
var count = 0;
function() {
count = count + 1;
count;
};
};
var c = makeCounter();
print(c()); // 1
print(c()); // 2Control Flow:
var x = 10;
while (x > 0) {
print(x);
x = x - 1;
}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.
We use a Makefile to simplify the build process.
This command generates the parser from the .g4 grammar file and compiles all Java sources.
makeTo execute all test scripts provided in the fwjsScripts/ folder:
make runYou can also run a specific script manually:
java -cp "build;lib/antlr-4.13.2-complete.jar" edu.sjsu.fwjs.Interpreter fwjsScripts/factorial.fwjsTo remove compiled .class files and generated sources:
make clean