Skip to content

Instantly share code, notes, and snippets.

@lcanady
Created February 24, 2022 20:09
Show Gist options
  • Save lcanady/2d182d8b06d1f7af723146d228a68d0f to your computer and use it in GitHub Desktop.
Save lcanady/2d182d8b06d1f7af723146d228a68d0f to your computer and use it in GitHub Desktop.
A PEG for making ASTs out of Mushcode!
// mushcode Grammar
// Author: Lemuel Canady, Jr
// This grammar is really basic, but it gets the job done!
blocks = blocks: block* { return blocks.flat() }
block = _"["_ block: args* _ "]"
{
return {block}
} / args
epression = results: args* {
return results.flat()
}
function = _ call: word "(" _ a: (args)? _ ")" _
{
const loc = location()
return {
type: "function",
operator: {type: "word", value:call},
location: loc,
args: Array.isArray(a) ? a : a ? [a] : []
}
} /
"[" _ call: word "(" _ a: (args)? _ ")" _ "]"
{
const loc = location()
return {
type: "function",
operator: {type: "word", value:call},
location: loc,
args: Array.isArray(a) ? a : a ? [a] : []
}
}
args = _ "," _
{
const loc = location();
return {type: "word", value: null, location: loc}
}/
a: arg _ "," _ t: args* {return [a,t.flat()].flat()} /
arg
arg = f: function {return f} /
w: word {
const loc = location();
return {type: "word", value: w, location: loc }
}
word = w:[^\(\),\[\]]+ {return w.join("")}
_ = [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment