aboutsummaryrefslogtreecommitdiff
path: root/nomic.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-09-11 19:23:55 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-09-11 19:23:55 -0700
commitc26db225f3d27d3cea6353246d816b02ff4f2900 (patch)
tree22995bd62da4f067cac21421234d54ec49e0beb1 /nomic.moon
parente0d39996becc7ae379bae2298ee3294eb7c7c802 (diff)
working towards moving more code into nomic.
Diffstat (limited to 'nomic.moon')
-rwxr-xr-xnomic.moon34
1 files changed, 11 insertions, 23 deletions
diff --git a/nomic.moon b/nomic.moon
index f85d548..447cdf9 100755
--- a/nomic.moon
+++ b/nomic.moon
@@ -93,6 +93,8 @@ class Game
fn_info = @defs[fn_name]
if fn_info == nil
error "Attempt to call undefined function: #{fn_name}"
+ if fn_info.is_macro
+ error "Attempt to call macro at runtime: #{fn_name}"
{:fn, :arg_names} = fn_info
args = {name, select(i,...) for i,name in ipairs(arg_names)}
if @debug
@@ -119,32 +121,11 @@ class Game
else arg_names = _arg_names
return invocations, arg_names
- defmacro: (spec, fn)=>
+ defmacro: (spec, lua_gen_fn)=>
invocations,arg_names = self\get_invocations spec
- fn_info = {:fn, :arg_names, :invocations, is_macro:true}
+ fn_info = {fn:lua_gen_fn, :arg_names, :invocations, is_macro:true}
for invocation in *invocations
@defs[invocation] = fn_info
-
- simplemacro: (spec, replacement)=>
- spec = spec\gsub("\r", "")
- replacement = replacement\gsub("\r", "")
- replace_grammar = [=[
- stuff <- {~ (var / longstring / string / .)+ ~}
- var <- ("%" {%wordchar+}) -> replacer
- string <- '"' (("\" .) / [^"])* '"'
- longstring <- ('".."' %ws? %indent {(%new_line "|" [^%nl]*)+} %dedent (%new_line '..')?)
- ]=]
- fn = (vars, kind)=>
- replacer = (varname)->
- ret = vars[varname].src
- return ret
- replacement_grammar = make_parser replace_grammar, {:replacer}
- code = replacement_grammar\match(replacement)
- tree = self\parse(code)
- -- Ugh, this is magic code.
- return @tree_to_lua(tree.value.body.value[1].value.value.value, kind), true
-
- self\defmacro spec, fn
run: (text)=>
if @debug
@@ -228,6 +209,13 @@ class Game
assert tree, "Failed to parse: #{str}"
return tree
+ tree_to_value: (tree)=>
+ code = "return (function(game, vars)\nreturn #{@tree_to_lua(tree)}\nend)"
+ lua_thunk, err = loadstring(code)
+ if not lua_thunk
+ error("Failed to compile generated code:\n#{code}\n\n#{err}")
+ return (lua_thunk!)(self, {})
+
tree_to_lua: (tree, kind="Expression")=>
assert tree, "No tree provided."
indent = ""