aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-08-22 04:15:13 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-08-22 04:15:13 -0700
commitea69b181982a50771af0e6f2be262a63e06790da (patch)
tree376ab7b64c02b09081ffce273afb0c4f65351cd9
parent039c620df270e88f6d50df677b7dedd65b868449 (diff)
Slightly cleaned up macros.
-rwxr-xr-xcore.moon26
-rwxr-xr-xgame2.moon7
-rw-r--r--nomic.moon24
3 files changed, 28 insertions, 29 deletions
diff --git a/core.moon b/core.moon
index fb694ef..e514b93 100755
--- a/core.moon
+++ b/core.moon
@@ -30,10 +30,8 @@ g\defmacro "let %varname = %value", (vars, helpers, ftype)=>
singleton = (aliases, value)->
g\defmacro aliases, (vars,helpers,ftype)=>
- if ftype == "Expression"
- helpers.lua(value)
- else
- helpers.lua("ret = #{value}")
+ if ftype == "Expression" then helpers.lua(value)
+ else helpers.lua("ret = #{value}")
infix = (ops)->
for op in *ops
@@ -41,23 +39,15 @@ infix = (ops)->
if type(op) == 'table'
{alias,op} = op
g\defmacro "%x #{op} %y", (vars,helpers,ftype)=>
- if ftype == "Statement"
- helpers.lua("ret = (#{helpers.var('x')} #{op} #{helpers.var('y')})")
- elseif ftype == "Expression"
- helpers.lua("(#{helpers.var('x')} #{op} #{helpers.var('y')})")
- else error("Unknown: #{ftype}")
+ helpers.lua("(#{helpers.var('x')} #{op} #{helpers.var('y')})")
unary = (ops)->
for op in *ops
g\defmacro "#{op} %x", (vars,helpers,ftype)=>
- if ftype == "Statement"
- helpers.lua("ret = #{op}(#{helpers.var('x')})")
- elseif ftype == "Expression"
- helpers.lua("#{op}(#{helpers.var('x')})")
- else error("Unknown: #{ftype}")
+ helpers.lua("#{op}(#{helpers.var('x')})")
singleton {"true","yes"}, "true"
singleton {"false","no"}, "false"
-singleton {"nil","null"}, "nil"
+singleton {"nil","null","nop","pass"}, "nil"
infix{"+","-","*","/","==",{"!=","~="},"<","<=",">",">=","^","and","or"}
unary{"-","#","not"}
g\def [[%x == %y]], (args)=> utils.equivalent(args.x, args.y)
@@ -185,12 +175,13 @@ g\defmacro "for %varname in %iterable %body", (vars,helpers,ftype)=>
g\simplemacro "if %condition %body", [[
if %condition %body
-..else: nil
+..else: pass
]]
g\simplemacro "unless %condition %body", [[
if (not %condition) %body
-..else: nil]]
+..else: pass
+]]
g\def [[do %action]], (vars)=> return vars.action(self,vars)
@@ -207,5 +198,4 @@ g\defmacro [[lua %lua_code]], (vars,helpers,ftype)=>
g\defmacro [[macro %spec %body]], (vars,helpers,ftype)=>
self\simplemacro vars.spec.value.value, vars.body.value.value.src
-
return g
diff --git a/game2.moon b/game2.moon
index eb1d4c5..18c1f51 100755
--- a/game2.moon
+++ b/game2.moon
@@ -190,9 +190,12 @@ rule "do %thing also %also-thing":
do %thing
do %also-thing
return 99
-
+]]
+g\run[[
do: say "one liner"
..also: say "another one liner"
+]]
+g\run[[
say (..)
do:
@@ -200,6 +203,8 @@ say (..)
return 5
say "bye"
+]]
+g\run[[
say (do: return "wow")
if 1: say "hi1" ..else: say "bye1"
diff --git a/nomic.moon b/nomic.moon
index cbe6c8a..13f99bd 100644
--- a/nomic.moon
+++ b/nomic.moon
@@ -79,6 +79,7 @@ lingo = [=[
(({ {|
(expression (%word_boundary fn_bit)+) / (word (%word_boundary fn_bit)*)
|} }) -> FunctionCall)
+ / (expression)
}) -> Statement)
|} }) -> Block
@@ -222,7 +223,7 @@ class Game
print(code)
lua_thunk, err = loadstring(code)
if not lua_thunk
- error("Failed to compile generated code:\n#{code}")
+ error("Failed to compile generated code:\n#{code}\n\n#{err}")
action = lua_thunk!
if @debug
print("Running...")
@@ -299,7 +300,7 @@ class Game
when "Errors"
-- TODO: Better error reporting via tree.src
- error("\nParse error on: #{tree.value}")
+ error("\nParse error on: #{utils.repr(tree.value\match("[^\n]*"), true)}")
when "Block"
for chunk in *tree.value
@@ -317,14 +318,19 @@ class Game
lua "end)"
when "Statement"
- ret = transform(tree.value)
- return ret
+ if tree.value.type == "FunctionCall"
+ name_bits = {}
+ for token in *tree.value.value
+ table.insert name_bits, if token.type == "Word" then token.value else "%"
+ name = table.concat(name_bits, " ")
+ if @macros[name]
+ lua transform(tree.value)
+ ret = table.concat ret_lines, "\n"
+ return ret
+ lua "ret = #{ded(transform(tree.value))}"
when "Expression"
- ret = transform(tree.value)
- if parent.type == "Statement"
- ret = "ret = "..ded(ret)
- return ret
+ lua transform(tree.value)
when "FunctionCall"
name_bits = {}
@@ -341,8 +347,6 @@ class Game
m = fn(self, args, helpers, parent.type)
if m != nil then return m
else
- if parent.type == "Statement"
- lua "ret ="
args = [ded(transform(a)) for a in *tree.value when a.type != "Word"]
table.insert args, 1, utils.repr(name, true)
comma_separated_items("game:call(", args, ")")