Skip to content

Instantly share code, notes, and snippets.

@Moligaloo
Created May 22, 2018 15:37
Show Gist options
  • Save Moligaloo/9c1a11dacac1567d9732bd53a689593c to your computer and use it in GitHub Desktop.
Save Moligaloo/9c1a11dacac1567d9732bd53a689593c to your computer and use it in GitHub Desktop.
Compile block to AST using JSON format
#!/usr/bin/env io
Yajl
Regex
Message do(
asMap := method(
if(
self name allMatchesOfRegex("(^[0-9\"])|true|false|nil") isNotEmpty,
return Map with("type", "literal", "value", doString(self name))
)
if(
self name == ";",
return Map with("type", "statement_sep")
)
if(
self arguments size > 0,
Map with("type", "call", "name", self name, "arguments", self arguments),
Map with("type", "var", "name", self name)
)
)
asList := method(
if(self next isNil,
list(self asMap),
self next asList prepend(self asMap)
)
)
asJson := method(
self asList asJson
)
)
Block asJson := method(
m := Map with("body", self message asList)
if(self argumentNames isNotEmpty,
m atPut("params", self argumentNames)
)
m asJson
)
if(
System isLaunchScript,
block(a, b,
c := getInput(2)
d = true
return a+b*c+1
) asJson println
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment