Removed some debugging, cleaned up.

This commit is contained in:
Bruce Hill 2017-08-23 14:02:36 -07:00
parent ea69b18198
commit 86c0fd47eb
3 changed files with 35 additions and 13 deletions

View File

@ -11,6 +11,9 @@ g\def [[printf %str]], (args)=>
for s in *args.str do io.write(utils.repr(s))
io.write("\n")
g\def [[quote %str]], (vars)=>
return utils.repr(vars.str, true)
g\defmacro "return %retval", (vars,helpers,ftype)=>
with helpers
switch ftype
@ -25,7 +28,7 @@ g\defmacro "return %retval", (vars,helpers,ftype)=>
g\defmacro "let %varname = %value", (vars, helpers, ftype)=>
with helpers
if ftype == "Expression" then error("Cannot set a variable in an expression.")
.lua "vars[#{.ded(.transform(vars.varname))} = #{.ded(.transform(vars.value))}"
.lua "vars[#{.ded(.transform(vars.varname))}] = #{.ded(.transform(vars.value))}"
return nil
singleton = (aliases, value)->
@ -188,11 +191,15 @@ g\def [[do %action]], (vars)=> return vars.action(self,vars)
g\defmacro [[lua %lua_code]], (vars,helpers,ftype)=>
with helpers
lua_code = vars.lua_code.value
escapes = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r"
unescape = (s)-> s\gsub("\\(.)", ((c)-> escapes[c] or c))
switch lua_code.type
when "List"
.lua table.concat[i.value.value for i in *lua_code.value]
else
.lua(lua_code.value)
-- TODO: handle subexpressions
.lua table.concat[unescape(i.value.value) for i in *lua_code.value]
when "String"
.lua(unescape(lua_code.value))
else error("Unknown type: #{lua_code.type}")
return nil
g\defmacro [[macro %spec %body]], (vars,helpers,ftype)=>

View File

@ -247,3 +247,13 @@ if (1 == 1):
unless (1 > 2):
say "This one too!"
]]
g\simplemacro [[smet %varname = %value]],[[
lua ["vars[\"", %varname, "\"] = ", %value]
lua ["vars[\"", %varname, "\"] = 2*vars[\"", %varname, "\"]"]
]]
g\run[[
smet "fnord" = 23
say %fnord
]]

View File

@ -60,7 +60,7 @@ add_indent_tokens = (str)->
indentflagger = [=[
file <- line*
line <- ((string / [^%linebreak])* %linebreak) -> process_line
string <- '"' (("\\" .) / [^"])* '"'
string <- '"' (("\" .) / [^"])* '"'
]=]
indentflagger = re.compile indentflagger, defs
indentflagger\match(str.."\n")
@ -100,7 +100,7 @@ lingo = [=[
word <- ({ !number {%wordchar+} }) -> Word
expression <- ({ (string / number / variable / list / thunk / subexpression) }) -> Expression
string <- ({ '"' {(("\\" .) / [^"])*} '"' }) -> String
string <- ({ '"' {(("\" .) / [^"])*} '"' }) -> String
number <- ({ {'-'? [0-9]+ ("." [0-9]+)?} }) -> Number
variable <- ({ ("%" {%wordchar+}) }) -> Var
@ -197,7 +197,7 @@ class Game
replace_grammar = [[
stuff <- {~ (var / string / .)+ ~}
var <- ("%" {%wordchar+}) -> replacer
string <- '"' (("\\" .) / [^"])* '"'
string <- '"' (("\" .) / [^"])* '"'
]]
replacement = add_indent_tokens replacement
fn = (vars, helpers, ftype)=>
@ -206,11 +206,14 @@ class Game
return ret
replacement_grammar = re.compile(replace_grammar, {:wordchar, :replacer})
replaced = replacement_grammar\match(replacement)
tree = lingo\match (replaced)
if not tree
error "Couldn't match:\n#{replaced}"
helpers.lua(helpers.transform(tree.value.body))
return code
tree = lingo\match replaced
if tree.value.errors and #tree.value.errors.value > 0
ret = helpers.transform(tree.value.errors)
return ret
result = helpers.transform(tree.value.body)
helpers.lua(result)
return
self\defmacro spec, fn
run: (text)=>
@ -352,7 +355,9 @@ class Game
comma_separated_items("game:call(", args, ")")
when "String"
lua utils.repr(tree.value, true)
escapes = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r"
unescaped = tree.value\gsub("\\(.)", ((c)-> escapes[c] or c))
lua utils.repr(unescaped, true)
when "Number"
lua tree.value