diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-09-14 19:39:27 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-09-14 19:39:27 -0700 |
| commit | ed0b5a3373add330b60b1850b8212eba681b031d (patch) | |
| tree | ece79421fa502f928fde137ca6369944b6bb6f04 | |
| parent | 6225462a1c9c007467787f9ee6e48cdd6a78dcfc (diff) | |
Fixed a bug in repr for strings ending in "]", and added moonscript
macros.
| -rw-r--r-- | core.nom | 41 | ||||
| -rwxr-xr-x | nomsu.moon | 28 | ||||
| -rw-r--r-- | utils.moon | 2 |
3 files changed, 46 insertions, 25 deletions
@@ -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": @@ -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 @@ -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 |
