From ed0b5a3373add330b60b1850b8212eba681b031d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 14 Sep 2017 19:39:27 -0700 Subject: [PATCH] Fixed a bug in repr for strings ending in "]", and added moonscript macros. --- core.nom | 41 +++++++++++++++++++++++++++++++++++++---- nomsu.moon | 28 ++++++++-------------------- utils.moon | 2 +- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/core.nom b/core.nom index baf0278..2dfa2d9 100644 --- a/core.nom +++ b/core.nom @@ -39,12 +39,45 @@ rule "run file %filename": # Macro helper functions rule "%tree as lua block": - lua block [..] - "do return compiler:tree_to_lua(", %tree, ", 'Statement'), true end" + lua block ".." + |do return compiler:tree_to_lua(vars.tree, 'Statement'), true end rule "%tree as lua expr": - lua expr [..] - "compiler:tree_to_lua(", %tree, ", 'Expression')" + lua expr ".." + |compiler:tree_to_lua(vars.tree, 'Expression') + +# Moonscript! +macro block "moonscript block %moonscript_code": + lua block ".." + |do + | local parse, compile = require('moonscript.parse'), require('moonscript.compile') + | local moon_code = compiler:tree_to_value(vars.moonscript_code, vars) + | local tree, err = parse.string(moon_code) + | if not tree then + | compiler:error("Failed to parse moonscript: "..err) + | end + | local lua_code, err, pos = compile.tree(tree) + | if not lua_code then + | compiler:error(compile.format_error(err, pos, moon_code)) + | end + | return "do\\n"..lua_code.."\\nend" + |end + +macro "moonscript %moonscript_code": + lua block ".." + |do + | local parse, compile = require('moonscript.parse'), require('moonscript.compile') + | local moon_code = compiler:tree_to_value(vars.moonscript_code, vars) + | local tree, err = parse.string(moon_code) + | if not tree then + | compiler:error("Failed to parse moonscript: "..err) + | end + | local lua_code, err, pos = compile.tree(tree) + | if not lua_code then + | compiler:error(compile.format_error(err, pos, moon_code)) + | end + | return "(function(compiler, vars)\\n"..lua_code.."\\nend)(compiler, vars)" + |end # String functions rule "join %strs": diff --git a/nomsu.moon b/nomsu.moon index 16ddb47..0c788c0 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -265,16 +265,14 @@ class NomsuCompiler code = to_lua(statement, "Statement") -- Run the fuckers as we go -- TODO: clean up repeated loading of utils? - lua_thunk, err = load(" + lua_code = " local utils = require('utils') - return (function(compiler, vars)\n#{code}\nend)") + return (function(compiler, vars)\n#{code}\nend)" + lua_thunk, err = load(lua_code) if not lua_thunk error("Failed to compile generated code:\n#{code}\n\n#{err}\n\nProduced by statement:\n#{utils.repr(statement)}") - ok,value = pcall(lua_thunk) - if not ok then error(value) - ok,value = pcall(value, self, vars) - if not ok then error! - return_value = value + value = lua_thunk! + return_value = value(self, vars) add code add [[ return ret @@ -492,6 +490,7 @@ class NomsuCompiler return code, retval error: (...)=> + print "ERROR!" print(...) print("Callstack:") for i=#@callstack,1,-1 @@ -531,22 +530,11 @@ class NomsuCompiler @defmacro [[lua block %lua_code]], (vars, kind)=> if kind == "Expression" then error("Expected to be in statement.") - lua_code = vars.lua_code.value - switch lua_code.type - when "List" - -- TODO: handle subexpressions - return table.concat([as_lua_code(@, i.value, vars) for i in *lua_code.value]), true - else - return as_lua_code(@, lua_code, vars), true + return @tree_to_value(vars.lua_code, vars), true @defmacro [[lua expr %lua_code]], (vars, kind)=> lua_code = vars.lua_code.value - switch lua_code.type - when "List" - -- TODO: handle subexpressions - return table.concat([as_lua_code(@, i.value, vars) for i in *lua_code.value]) - else - return as_lua_code(@, lua_code, vars) + return @tree_to_value(vars.lua_code, vars) @def "rule %spec %body", (vars)=> @def vars.spec, vars.body diff --git a/utils.moon b/utils.moon index 1f6d0d7..069b1a0 100644 --- a/utils.moon +++ b/utils.moon @@ -22,7 +22,7 @@ utils = { elseif not x\find[[']] and not x\find"\n" "\'"..x.."\'" else - for i=0,math.huge + for i=1,math.huge eq = ("=")\rep(i) if not x\find"%[#{eq}%[" and not x\find"%]#{eq}%]" -- Stupid bullshit add an extra newline because lua discards first one if it exists