aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcore.moon15
-rwxr-xr-xgame2.moon10
-rw-r--r--nomic.moon23
3 files changed, 35 insertions, 13 deletions
diff --git a/core.moon b/core.moon
index e514b93..f1847c3 100755
--- a/core.moon
+++ b/core.moon
@@ -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)=>
diff --git a/game2.moon b/game2.moon
index 18c1f51..d26d368 100755
--- a/game2.moon
+++ b/game2.moon
@@ -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
+]]
diff --git a/nomic.moon b/nomic.moon
index 13f99bd..4a7ecf3 100644
--- a/nomic.moon
+++ b/nomic.moon
@@ -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