# JSON Interpreter
A simple interpreter for programs described in AST-JSON format.
---
## Features
- **Arithmetic operations**: `add`, `subtract`, `multiply`, `divide`
- **Variables**: declaration (`declare`), assignment (`assign`), usage (`identifier`)
- **Output**: function call (`call` with function `"print"`)
- **Conditional statements**: `if`
- **Programs**: root node type is `program` with an array of statements (`body`)
---
## Directory Structure
json-interpreter/ │ ├── index.js # Main interpreter file ├── examples/ │ └── example1.json # Example program in JSON └── README.md # Documentation
---
## How to Use
### 1. Clone the repository
```bash
git clone <your-repository-url>
cd json-interpreter
Recommended version: Node.js v14 or higher
node index.js
This will execute examples/example1.json
by default and print the output to the console.
You can import and use the interpreter in your own Node.js code:
const { interpret, runFile } = require('./index');
// Run an example JSON program
runFile('./examples/example1.json');
{ "type": "add", "left": <expr>, "right": <expr> }
{ "type": "subtract", "left": <expr>, "right": <expr> }
{ "type": "multiply", "left": <expr>, "right": <expr> }
{ "type": "divide", "left": <expr>, "right": <expr> }
- Declaration:
{ "type": "declare", "name": "x", "value": { "type": "number", "value": 42 } }
- Assignment:
{ "type": "assign", "name": "x", "value": <expr> }
- Usage:
{ "type": "identifier", "name": "x" }
- Number:
{ "type": "number", "value": 10 }
- String:
{ "type": "string", "value": "Hello" }
- Print to console:
{
"type": "call",
"function": "print",
"args": [ <expr>, <expr>, ... ]
}
{
"type": "if",
"condition": <expr>,
"then": [ <statement>, ... ],
"else": [ <statement>, ... ]
}
{
"type": "program",
"body": [ <statement>, ... ]
}
File: examples/example1.json
{
"type": "program",
"body": [
{
"type": "declare",
"name": "x",
"value": { "type": "number", "value": 7 }
},
{
"type": "declare",
"name": "y",
"value": { "type": "number", "value": 3 }
},
{
"type": "declare",
"name": "message",
"value": { "type": "string", "value": "Result:" }
},
{
"type": "call",
"function": "print",
"args": [
{ "type": "identifier", "name": "message" },
{
"type": "add",
"left": { "type": "identifier", "name": "x" },
"right": { "type": "identifier", "name": "y" }
}
]
},
{
"type": "if",
"condition": {
"type": "subtract",
"left": { "type": "identifier", "name": "x" },
"right": { "type": "identifier", "name": "y" }
},
"then": [
{
"type": "call",
"function": "print",
"args": [
{ "type": "string", "value": "x is greater than y" }
]
}
],
"else": [
{
"type": "call",
"function": "print",
"args": [
{ "type": "string", "value": "x is not greater than y" }
]
}
]
}
]
}
You can add more features, such as:
- Support for loops (
while
,for
) - User-defined functions
- Arrays and objects
- More built-in functions
Just add new case
branches to the interpret
function in index.js
!
- "Variable not defined": Make sure you declared variables before use.
- Unknown node type: Check the
type
property in your JSON; it must match one of the supported node types. - Invalid JSON: Your file must be valid JSON; you can check it using any online validator.
MIT License
Xzdes
Feel free to create issues or pull requests if you want to improve this interpreter!