aboutsummaryrefslogtreecommitdiff
path: root/nomsu.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-10-13 18:09:04 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-10-13 18:09:04 -0700
commite476bfea5261b04e32cd444208bbffa34bc6f475 (patch)
tree1e298777e70f50e9fb7625b45395d8c5c4c91e38 /nomsu.moon
parentd13bcde2b9306e0c87b055faed88da76bd3ff41e (diff)
Improved code generation for "when" statements using elseif, and
improved code generation for loops by omitting goto labels when not used.
Diffstat (limited to 'nomsu.moon')
-rwxr-xr-xnomsu.moon34
1 files changed, 17 insertions, 17 deletions
diff --git a/nomsu.moon b/nomsu.moon
index d24b27c..435265f 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -329,10 +329,10 @@ class NomsuCompiler
@errorln "#{colored.red "Error occurred in statement:"}\n#{colored.bright colored.yellow statement.src}"
@error(expr)
code_for_statement = ([[
- return (function(nomsu, vars)
- %s
- return %s;
- end);]])\format(statements or "", expr or "ret")
+return (function(nomsu, vars)
+%s
+return %s;
+end);]])\format(statements or "", expr or "ret")
if @debug
@writeln "#{colored.bright "RUNNING LUA:"}\n#{colored.blue colored.bright(code_for_statement)}"
lua_thunk, err = load(code_for_statement)
@@ -342,7 +342,7 @@ class NomsuCompiler
n = n + 1
("\n%-3d|")\format(n)
code = "1 |"..code_for_statement\gsub("\n", fn)
- error("Failed to compile generated code:\n#{colored.bright colored.blue code}\n\n#{err}\n\nProduced by statement:\n#{colored.bright colored.yellow statement.src}")
+ error("Failed to compile generated code:\n#{colored.bright colored.blue colored.onblack code}\n\n#{err}\n\nProduced by statement:\n#{colored.bright colored.yellow statement.src}")
run_statement = lua_thunk!
ok,ret = pcall(run_statement, self, vars)
if expr then return_value = ret
@@ -350,16 +350,16 @@ class NomsuCompiler
@errorln "#{colored.red "Error occurred in statement:"}\n#{colored.yellow statement.src}"
@errorln debug.traceback!
@error(ret)
- insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr}" or ''}"
+ insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr};" or ''}"
if max_operations
debug.sethook!
lua_code = ([[
- return (function(nomsu, vars)
- local ret;
- %s
- return ret;
- end);]])\format(concat(buffer, "\n"))
+return (function(nomsu, vars)
+local ret;
+%s
+return ret;
+end);]])\format(concat(buffer, "\n"))
return return_value, lua_code, vars
tree_to_value: (tree, vars)=>
@@ -368,7 +368,7 @@ class NomsuCompiler
@writeln "#{colored.bright "RUNNING LUA TO GET VALUE:"}\n#{colored.blue colored.bright(code)}"
lua_thunk, err = load(code)
if not lua_thunk
- @error("Failed to compile generated code:\n#{colored.bright colored.blue code}\n\n#{colored.red err}")
+ @error("Failed to compile generated code:\n#{colored.bright colored.blue colored.onblack code}\n\n#{colored.red err}")
return (lua_thunk!)(self, vars or {})
tree_to_lua: (tree)=>
@@ -391,11 +391,11 @@ class NomsuCompiler
if statement then insert lua_bits, statement
if expr then insert lua_bits, "ret = #{expr};"
return ([[
- (function(nomsu, vars)
- local ret;
- %s
- return ret;
- end)]])\format(concat(lua_bits, "\n"))
+(function(nomsu, vars)
+local ret;
+%s
+return ret;
+end)]])\format(concat(lua_bits, "\n"))
when "FunctionCall"
stub = @get_stub(tree)