• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

j2j: jass <-> json converter

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
539
A little tool for people who don't want to write a jass parser themselves. This tool can take a jass script as input and return a json representation of the jass ast or take a jass ast encoded as json and turn it into valid jass again. I don't know if this is of any value but i wanted to write this for a while. Source is of course available.
Below you can see a small example. But beware there is zero documentation. You have to figure out the json structure yourself. But that shouldn't bee too hard; just feed it with small example scripts.



test.j:
JASS:
globals
    integer array ARR
endglobals

function foo takes integer a, integer b returns integer
    return a * ARR[b]
endfunction

Convert it to json (note im using jq to pretty print it):
Bash:
$ j2j to-json test.j | jq > test.json
JSON:
[
  {
    "def": {
      "kind": "adef",
      "name": "ARR",
      "type": "vardef",
      "vartype": "integer"
    },
    "kind": "global",
    "type": "toplevel"
  },
  {
    "body": [
      {
        "kind": "return",
        "type": "stmt",
        "value": {
          "args": [
            {
              "kind": "svar",
              "name": "a",
              "type": "expr"
            },
            {
              "index": {
                "kind": "svar",
                "name": "b",
                "type": "expr"
              },
              "kind": "avar",
              "name": "ARR",
              "type": "expr"
            }
          ],
          "kind": "call",
          "name": "*",
          "type": "call"
        }
      }
    ],
    "constant": false,
    "kind": "function",
    "name": "foo",
    "parameters": [
      [
        "integer",
        "a"
      ],
      [
        "integer",
        "b"
      ]
    ],
    "returntype": "integer",
    "type": "toplevel"
  }
]

Now do some modifications:
Diff:
20,21c20,21
<               "kind": "svar",
<               "name": "a",
---
>               "kind": "rawcode",
>               "value": "a",
27c27
<                 "name": "b",
---
>                 "name": "idx",
51c51
<         "b"
---
>         "idx"

And convert back to jass
Bash:
$ j2j to-jass test.json > new.j # test.json was modified like above
JASS:
globals
integer array ARR
endglobals

 function foo takes integer a,integer idx returns integer
return ('a' * ARR [idx])
endfunction
 

Attachments

  • j2j.zip
    6.4 MB · Views: 2
Top