Is it important to use JSON in programming languages?

In the article How to Create a Programming Language, it says:

Here is an example of simple code like this:

if (foo == "bar") {
   10 + 10
   10 * 20
}

The output in JSON format looks like this:

{
  "type": "Program",
  "body": [
    {
      "type": "IfStatement",
      "test": {
        "type": "BinaryExpression",
        "operator": "==",
        "left": {
          "type": "Identifier",
          "name": "foo",
          "range": [
            4,
            7
          ]
        },
        "right": {
          "type": "Literal",
          "value": "bar",
          "raw": "\"bar\"",
          "range": [
            11,
            16
          ]
        },
        "range": [
          4,
          16
        ]
      },
      "consequent": {
        "type": "BlockStatement",
        "body": [
          {
            "type": "ExpressionStatement",
            "expression": {
              "type": "BinaryExpression",
              "operator": "+",
              "left": {
                "type": "Literal",
                "value": 10,
                "raw": "10",
                "range": [
                  23,
                  25
                ]
              },
              "right": {
                "type": "Literal",
                "value": 10,
                "raw": "10",
                "range": [
                  28,
                  30
                ]
              },
              "range": [
                23,
                30
              ]
            },
            "range": [
              23,
              30
            ]
          },
          {
            "type": "ExpressionStatement",
            "expression": {
              "type": "BinaryExpression",
              "operator": "*",
              "left": {
                "type": "Literal",
                "value": 10,
                "raw": "10",
                "range": [
                  34,
                  36
                ]
              },
              "right": {
                "type": "Literal",
                "value": 20,
                "raw": "20",
                "range": [
                  39,
                  41
                ]
              },
              "range": [
                34,
                41
              ]
            },
            "range": [
              34,
              41
            ]
          }
        ],
        "range": [
          18,
          43
        ]
      },
      "alternate": null,
      "range": [
        0,
        43
      ]
    }
  ],
  "sourceType": "module",
  "range": [
    0,
    43
  ]
}

I just use split and startsWith for my programming language. Is that incorrect?

Well, it really depends. What it is doing is called “parsing” and “ast tree building”. If you want your language to be really simple, like just a line of words, you can use split and startswith, if you want to do anything more complicated, you should learn how to make parsers and how to use them to build AST (Abstract syntax trees).

3 Likes

But I’ve made a (kinda) complex programming language, and I’ve used split and startsWith. So I should switch to ASTs?

You should definitely use a parser, and AST is usually just a tree structure of classes/types. What language are you making it in? PEG’s are the easiest kind of parser for me to use and I highly recommend them. (Many languages support them, eg JS/Python…)

I’m making it in JS.

Also, what is a parser?

It chops up text into usable data with context. Here is peg.js: https://pegjs.org/

Although, for programming languages, I recommend using a fast compiled language so that your language will not be unbearably slow.

1 Like

Well, this is going to be a debate. I understand and use JSON, but if you ask hardware and embedded people like me (and people around me) the answer is … NO. JSON is a waste of bandwidth and bloated messaging created to help readability for humans.

1 Like

So should I use split and startsWith for my programming language?

To start with I would look at tools that are build to build languages and only when happy make your own compiler/interpreter.
But note I am old school … so what I say here might just be worst practice these days :slight_smile:

2 Likes

Yes, as I said above, use a tree of objects instead of json for storing the AST. I have never seen JSON used for abstract syntax trees.

In my opinion no, still parse and build an AST tree, just do not use JSON.

1 Like

JSON is used to represent ASTs for human reading. You should still use an AST, but you don’t need to serialize it to JSON.

1 Like