Parser for a JavaScript-like language. Outputs a JSON abstract syntax tree (AST).
./parse -e 'let x = 2 + 3;'Output:
{
"type": "Program",
"body": [
{
"type": "VariableStatement",
"declarations": [
{
"type": "VariableDeclaration",
"id": {
"type": "Identifier",
"name": "x"
},
"init": {
"type": "BinaryExpression",
"left": {
"type": "NumericLiteral",
"value": 2
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"value": 3
}
}
}
]
}
]
}# Enter your program inline
./parse -e '<your program>'
# Read your program from a file
./parse -f path/to/file-
Math
4 * (5 + 6) / 2;
-
Variable declarations with no explicit typing
let x, y = 10, z = "hello";
-
Supported types: integer (
123), string ("hello"), boolean (true/false),null -
Assignment
x = 10; y += 2;
-
Block scopes
let x = 2; { let y = 3; }
-
If-else statements
if (x > 3) { y = 1; } else { y = 2; }
-
Loops
while (x < 3) { x += 1; } for (let i = 0; i < 3; i += 1) { x += i; }
This project uses CMake.
mkdir build && cd build && cmake .. && makeThe executable created is build/parse.
Run build/run-tests. Tests are defined in tests folder.
Tests are also run on GitHub Actions runners using ubuntu-22.04 and macos-14.