From 63d8b1cd3f34b15bf86210b99209e8b57e7019bb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 22:15:06 -0700 Subject: Fully working, I think? (with a lot of shims) --- nomnom/compile.nom | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'nomnom/compile.nom') diff --git a/nomnom/compile.nom b/nomnom/compile.nom index 19daa6a..64465bd 100644 --- a/nomnom/compile.nom +++ b/nomnom/compile.nom @@ -4,6 +4,7 @@ use "nomnom/code_obj.nom" use "nomnom/parser.nom" use "nomnom/pretty_errors.nom" +# TODO: use pretty_errors local action [report compile error at %pos %err]: barf "Compile error at \%pos: \%err" @@ -23,7 +24,9 @@ action [compile %tree using %compile_actions]: %compile_action = %compile_actions.%stub # Don't apply compiler actions to methods if (%compile_action and (not %tree.target)): - %args = ["tree", "compile_actions"] + # TODO: restore this: + #%args = [%tree, %compile_actions] + %args = [%nomsu, %tree] for % in (%tree::get args): %args::add % %result = (call %compile_action with %args) if (%result == (nil)): @@ -43,7 +46,7 @@ action [compile %tree using %compile_actions]: %lua = (Lua Value from %tree) if %tree.target: # Method call %target_lua = (compile %tree.target using %compile_actions) - if (("\%target_lua"::matches "^%(.*%)$") or ("\%target_lua"::matches "^[_a-zA-Z][_a-zA-Z0-9]*$")): + if (((%target_lua::as smext)::matches "^%(.*%)$") or ((%target_lua::as smext)::matches "^[_a-zA-Z][_a-zA-Z0-9]*$")): %lua::add [%target_lua, ":"] ..else: %lua::add ["(", %target_lua, "):"] @@ -51,12 +54,13 @@ action [compile %tree using %compile_actions]: %args = [] for %tok in %tree at %i: if (%tok is text): do next %tok - # TODO: maybe translate Lua comments - if (%tok.type == "Comment"): do next %tok + # TODO: maybe don't translate Lua comments + #if (%tok.type == "Comment"): do next %tok if (%tok.type == "Block"): - %values = (..) - (compile %line using %compile_actions) for %line in %tok - ..unless (%line.type == "Comment") + %values = [] + for %line in %tok: + #unless (%line.type == "Comment"): + %values::add (compile %line using %compile_actions) if all of (%.is_value for % in %values): if ((size of %values) == 1): %arg_lua = %values.1 @@ -153,11 +157,11 @@ action [compile %tree using %compile_actions]: unless %value_lua.is_value: report compile error at %tree.2 "\ ..Can't use this as a dict value, since it's not an expression." - %key_str = ("\%key_lua"::matching "^[\"']([a-zA-Z_][a-zA-Z0-9_]*)['\"]$") + %key_str = ((%key_lua::as smext)::matching "^[\"']([a-zA-Z_][a-zA-Z0-9_]*)['\"]$") if: %key_str: return (Lua Code from %tree [%key_str, "=", %value_lua]) - ("\%key_lua".1 == "["): + ((%key_lua::as smext).1 == "["): # NOTE: this *must* use a space after the [ to avoid freaking out Lua's parser if the inner expression is a long string. Lua parses x[[[y]]] as x("[y]"), not as x["y"] @@ -170,7 +174,7 @@ action [compile %tree using %compile_actions]: unless %lua.is_value: report compile error at %tree.1 "\ ..Can't index into this, since it's not an expression." - %first_char = "\%lua".1 + %first_char = (%lua::as smext).1 if (any of [%first_char == "{", %first_char == "\"", %first_char == "["]): %lua::parenthesize @@ -180,7 +184,7 @@ action [compile %tree using %compile_actions]: unless %key_lua.is_value: report compile error at %key "\ ..Can't use this as an index, since it's not an expression." - %key_lua_str = "\%key_lua" + %key_lua_str = (%key_lua::as smext) %lua_id = (%key_lua_str::matching "^['\"]([a-zA-Z_][a-zA-Z0-9_]*)['\"]$") if: %lua_id: @@ -206,8 +210,8 @@ action [compile %tree using %compile_actions]: ..compilation depends on the earlier chunks" "Comment": - # TODO: implement? - return (Lua Code from %tree) + # TODO: de-implement? + return (Lua Code from %tree "-- \(%tree.1::with "\n" -> "\n-- ")") "Error": barf "Can't compile errors" -- cgit v1.2.3