Skip to content

Xzdes/json-interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

# 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

2. Make sure you have Node.js installed

Recommended version: Node.js v14 or higher

3. Run the example program

node index.js

This will execute examples/example1.json by default and print the output to the console.

4. Use as a module in your own project

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');

JSON AST Program Structure

Supported Node Types

1. Arithmetic Operations

{ "type": "add", "left": <expr>, "right": <expr> }
{ "type": "subtract", "left": <expr>, "right": <expr> }
{ "type": "multiply", "left": <expr>, "right": <expr> }
{ "type": "divide", "left": <expr>, "right": <expr> }

2. Variables

  • Declaration:
{ "type": "declare", "name": "x", "value": { "type": "number", "value": 42 } }
  • Assignment:
{ "type": "assign", "name": "x", "value": <expr> }
  • Usage:
{ "type": "identifier", "name": "x" }

3. Literals

  • Number:
{ "type": "number", "value": 10 }
  • String:
{ "type": "string", "value": "Hello" }

4. Output

  • Print to console:
{
  "type": "call",
  "function": "print",
  "args": [ <expr>, <expr>, ... ]
}

5. Conditional Statement

{
  "type": "if",
  "condition": <expr>,
  "then": [ <statement>, ... ],
  "else": [ <statement>, ... ]
}

6. Program Root

{
  "type": "program",
  "body": [ <statement>, ... ]
}

Example JSON Program

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" }
          ]
        }
      ]
    }
  ]
}

Extending the Interpreter

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!


Troubleshooting

  • "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.

License

MIT License


Author

Xzdes


Feedback & Contributions

Feel free to create issues or pull requests if you want to improve this interpreter!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published