diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-30 17:20:22 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-30 17:21:19 -0700 |
| commit | b53516c47c0dd1f9325f9f721f561487510cca98 (patch) | |
| tree | 92961e19bc94eec3ab8b0f19357c57399c205b7d /core | |
| parent | 5637676bc45ce9aa3015726485f63a2a5745a45a (diff) | |
Simplified and correctified lib/object (though the codegen still need
streamlining), added a .stub member to Action trees, and switched
Source's repr to be @filename[start:stop] instead of
"filename[start:stop]"
Diffstat (limited to 'core')
| -rw-r--r-- | core/control_flow.nom | 98 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 49 | ||||
| -rw-r--r-- | core/operators.nom | 4 | ||||
| -rw-r--r-- | core/scopes.nom | 41 |
4 files changed, 95 insertions, 97 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom index e54f10a..9120356 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -51,23 +51,23 @@ immediately Lua value ".." (function() if \(%condition as lua expr) then - return \(%when_true_expr as lua expr); + return \(%when_true_expr as lua expr) else - return \(%when_false_expr as lua expr); + return \(%when_false_expr as lua expr) end end)() # GOTOs immediately compile [=== %label ===, --- %label ---, *** %label ***] to - Lua "::label_\(%label as lua identifier)::;" + Lua "::label_\(%label as lua identifier)::" compile [go to %label] to - Lua "goto label_\(%label as lua identifier);" + Lua "goto label_\(%label as lua identifier)" # Basic loop control immediately - compile [do next] to: Lua "continue;" - compile [stop] to: Lua "break;" + compile [do next] to: Lua "continue" + compile [stop] to: Lua "break" # Helper function immediately @@ -84,25 +84,25 @@ immediately # While loops immediately - compile [do next repeat] to: Lua "goto continue_repeat;" - compile [stop repeating] to: Lua "goto stop_repeat;" + compile [do next repeat] to: Lua "goto continue_repeat" + compile [stop repeating] to: Lua "goto stop_repeat" compile [repeat while %condition %body] to %lua <- Lua ".." while \(%condition as lua expr) do \(%body as lua statements) if - %body has subtree % where: (%.type = "Action") and ((%'s stub) is "do next repeat") - ..: to %lua write "\n ::continue_repeat::;" + %body has subtree % where: (%.type = "Action") and (%.stub is "do next repeat") + ..: to %lua write "\n ::continue_repeat::" to %lua write "\nend --while-loop" if - %body has subtree % where: (%.type = "Action") and ((%'s stub) is "stop repeating") + %body has subtree % where: (%.type = "Action") and (%.stub is "stop repeating") .. %lua <- Lua ".." do -- scope of "stop repeating" label \%lua - ::stop_repeat::; + ::stop_repeat:: end -- end of "stop repeating" label scope return %lua parse [repeat %body] as: repeat while (yes) %body @@ -116,26 +116,26 @@ immediately for i=1,\(%n as lua expr) do \(%body as lua statements) if - %body has subtree % where: (%.type = "Action") and ((%'s stub) is "do next repeat") - ..: to %lua write "\n ::continue_repeat::;" + %body has subtree % where: (%.type = "Action") and (%.stub is "do next repeat") + ..: to %lua write "\n ::continue_repeat::" to %lua write "\nend --numeric for-loop" if - %body has subtree % where: (%.type = "Action") and ((%'s stub) is "stop repeating") + %body has subtree % where: (%.type = "Action") and (%.stub is "stop repeating") .. %lua <- Lua ".." do -- scope of "stop repeating" label \%lua - ::stop_repeat::; + ::stop_repeat:: end -- end of "stop repeating" label scope return %lua # For loop control flow immediately compile [stop %var] to - Lua "goto stop_\(%var as lua identifier);" + Lua "goto stop_\(%var as lua identifier)" compile [do next %var] to - Lua "goto continue_\(%var as lua identifier);" + Lua "goto continue_\(%var as lua identifier)" # Numeric range for loops immediately @@ -152,22 +152,22 @@ immediately if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "do next %") and + (%.stub is "do next %") and %.3 = %var - ..: to %lua write "\n ::continue_\(%var as lua identifier)::;" + ..: to %lua write "\n ::continue_\(%var as lua identifier)::" to %lua write "\nend --numeric for-loop" if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "stop %") and + (%.stub is "stop %") and %.2 = %var .. %lua <- Lua ".." do -- scope for stopping for-loop \%lua - ::stop_\(%var as lua identifier)::; + ::stop_\(%var as lua identifier):: end -- end of scope for stopping for-loop return %lua @@ -187,21 +187,21 @@ immediately if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "do next %") and + (%.stub is "do next %") and %.value.3.value = %var.value - ..: to %lua write (Lua "\n ::continue_\(%var as lua identifier)::;") + ..: to %lua write (Lua "\n ::continue_\(%var as lua identifier)::") to %lua write "\nend --foreach-loop" if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "stop %") and + (%.stub is "stop %") and %.value.2.value = %var.value .. %lua <- Lua ".." do -- scope for stopping for-loop \%lua - ::stop_\(%var as lua identifier)::; + ::stop_\(%var as lua identifier):: end -- end of scope for stopping for-loop return %lua @@ -221,32 +221,32 @@ immediately if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "do next %") and + (%.stub is "do next %") and %.value.3.value = %key.value - ..: to %lua write (Lua "\n ::continue_\(%key as lua identifier)::;") + ..: to %lua write (Lua "\n ::continue_\(%key as lua identifier)::") if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "do next %") and + (%.stub is "do next %") and %.value.3.value = %value.value - ..: to %lua write (Lua "\n ::continue_\(%value as lua identifier)::;") + ..: to %lua write (Lua "\n ::continue_\(%value as lua identifier)::") to %lua write "\nend --foreach-loop" %stop_labels <- (Lua "") if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "stop %") and + (%.stub is "stop %") and %.value.2.value = %key.value - ..: to %stop_labels write "\n::stop_\(%key as lua identifier)::;" + ..: to %stop_labels write "\n::stop_\(%key as lua identifier)::" if %body has subtree % where (%.type = "Action") and - ((%'s stub) is "stop %") and + (%.stub is "stop %") and %.value.2.value = %value.value - ..: to %stop_labels write "\n::stop_\(%value as lua identifier)::;" + ..: to %stop_labels write "\n::stop_\(%value as lua identifier)::" if: (length of %stop_labels) > 0 %lua <- @@ -279,7 +279,7 @@ immediately assume %condition or barf ".." Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else" if: %action is (nil) - lua> "table.insert(\%fallthroughs, \(%condition as lua expr));" + lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" do next %func_call if: %condition = "else" @@ -289,7 +289,7 @@ immediately %seen_else <- (yes) ..else assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block" - lua> "table.insert(\%fallthroughs, \(%condition as lua expr));" + lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" to %code write "\("if" if %is_first else "\nelseif") " for %i = %condition in %fallthroughs if (%i > 1): to %code write " or " @@ -333,7 +333,7 @@ immediately ..else assume (not %seen_else) or barf "'else' clause needs to be last in 'when % = ?' block" to %code write "\("if" if %is_first else "\nelseif") " - lua> "table.insert(\%fallthroughs, \(%condition as lua expr));" + lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" for %i = % in %fallthroughs if: %i > 1 to %code write " or " @@ -353,7 +353,7 @@ immediately %code <- Lua ".." do --when % = ? - local branch_value = \(%branch_value as lua expr); + local branch_value = \(%branch_value as lua expr) \%code end --when % = ? return %code @@ -366,18 +366,18 @@ immediately ..to Lua ".." do - local fell_through = false; + local fell_through = false local ok, ret = pcall(function() \(%action as lua statements) - fell_through = true; - end); + fell_through = true + end) if ok then \(%success as lua statements) end if not ok then \(%fallback as lua statements) elseif not fell_through then - return ret; + return ret end end parse [try %action] as @@ -400,18 +400,18 @@ immediately compile [do %action then always %final_action] to Lua ".." do - local fell_through = false; + local fell_through = false local ok, ret1 = pcall(function() \(%action as lua statements) - fell_through = true; - end); + fell_through = true + end) local ok2, ret2 = pcall(function() \(%final_action as lua statements) - end); - if not ok then error(ret1); end - if not ok2 then error(ret2); end + end) + if not ok then error(ret1) end + if not ok2 then error(ret2) end if not fell_through then - return ret1; + return ret1 end end --do-then-always diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 36f7d34..c343ae9 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -7,15 +7,15 @@ immediately lua> ".." nomsu:define_compile_action("compile %actions to %lua", function(tree, \%actions, \%lua) local lua = Lua(tree.source, "nomsu:define_compile_action(") - local stubs = {} + local specs = {} for i, action in ipairs(\%actions.value) do - stubs[i] = action:get_stub(true) + specs[i] = action:get_spec() end - stubs = repr(stubs) - if #stubs > 80 then - lua:append("\n ",stubs,",\n ") + specs = repr(specs) + if #specs > 80 then + lua:append("\n ",specs,",\n ") else - lua:append(stubs,", ") + lua:append(specs,", ") end lua:append("function(tree") local args = {} @@ -37,15 +37,15 @@ immediately compile [action %actions %body] to lua> ".." local lua = Lua(tree.source, "nomsu:define_action(") - local stubs = {} + local specs = {} for i, action in ipairs(\%actions.value) do - stubs[i] = action:get_stub(true) + specs[i] = action:get_spec() end - stubs = repr(stubs) - if #stubs > 80 then - lua:append("\n ",stubs,",\n ") + specs = repr(specs) + if #specs > 80 then + lua:append("\n ",specs,",\n ") else - lua:append(stubs,", ") + lua:append(specs,", ") end lua:append("function(") local args = {} @@ -67,15 +67,15 @@ immediately compile [parse %shorthand as %longhand] to lua> ".." local lua = Lua(tree.source, "nomsu:define_compile_action(") - local stubs = {} + local specs = {} for i, action in ipairs(\%shorthand.value) do - stubs[i] = action:get_stub(true) + specs[i] = action:get_spec() end - stubs = repr(stubs) - if #stubs > 80 then - lua:append("\n ",stubs,",\n ") + specs = repr(specs) + if #specs > 80 then + lua:append("\n ",specs,",\n ") else - lua:append(stubs,", ") + lua:append(specs,", ") end lua:append("function(tree") local replacements = {} @@ -93,15 +93,15 @@ immediately elseif t.type == 'Var' and replacements[t.value] then return replacements[t.value] elseif t.type == 'Var' then - return t.type.."("..repr(t.value.."#"..tostring(MANGLE_INDEX))..", "..repr(t.source)..")" + return t.type.."("..repr(t.value.."#"..tostring(MANGLE_INDEX))..", "..repr(tostring(t.source))..")" elseif t.is_multi then local bits = {} for i, entry in ipairs(t.value) do bits[i] = make_tree(entry) end - return t.type.."(Tuple("..table.concat(bits, ", ").."), "..repr(t.source)..")" + return t.type.."(Tuple("..table.concat(bits, ", ").."), "..repr(tostring(t.source))..")" else - return t.type.."("..repr(t.value)..", "..repr(t.source)..")" + return t.type.."("..repr(t.value)..", "..repr(tostring(t.source))..")" end end lua:append(")\n local tree = ", make_tree(\%longhand), "\n return nomsu:tree_to_lua(tree)\nend);") @@ -110,7 +110,7 @@ immediately compile [remove action %action] to Lua ".." do - local fn = ACTIONS[\(=lua "repr(\%action:get_stub())")] + local fn = ACTIONS[\(=lua "repr(\%action.stub)")] local stubs = ARG_ORDERS[fn] for stub in pairs(stubs) do ACTIONS[stub] = nil @@ -159,9 +159,6 @@ immediately local lua = Lua(\%tree.source, "return ",nomsu:tree_to_lua(\%tree)) return nomsu:run_lua(lua) - action [%tree's stub] - =lua "\%tree:get_stub()" - immediately parse [%var <-write %code] as: lua> "\%var:append(\%code);" parse [to %var write %code] as: lua> "\%var:append(\%code);" @@ -178,7 +175,7 @@ immediately # Compiler tools immediately compile [run %code] to - Lua "nomsu:run(Nomsu(\(%code.source as text), \(%code as lua expr)))" + Lua "nomsu:run(Nomsu(\"\(%code.source as text)\", \(%code as lua expr)))" immediately compile [show lua %block] to diff --git a/core/operators.nom b/core/operators.nom index c3b56c6..9b6b68a 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -51,7 +51,7 @@ immediately assume %var_lua.is_value or barf "Invalid target for assignment: \%var" lua> ".." \%value = \%value:map(function(t) - if Action:is_instance(t) and t:get_stub() == "?" then + if Action:is_instance(t) and t.stub == "?" then return \%var end end) @@ -75,7 +75,7 @@ immediately for i, item in ipairs(\%assignments.value) do local \%target, \%value = item.value[1], item.value[2] \%value = \%value:map(function(t) - if Action:is_instance(t) and t:get_stub() == "?" then + if Action:is_instance(t) and t.stub == "?" then return \%target end end) diff --git a/core/scopes.nom b/core/scopes.nom index e4e574a..72631c0 100644 --- a/core/scopes.nom +++ b/core/scopes.nom @@ -7,47 +7,48 @@ use "core/control_flow.nom" compile [using %definitions %body, using %definitions do %body] to %setup_lua <- Lua ".." - local fell_through = false; + local fell_through = false local ok, ret = pcall(function() \(%definitions as lua statements) - fell_through = true; - end); + fell_through = true + end) %body_lua <- Lua ".." - local fell_through = false; + local fell_through = false local ok, ret = pcall(function() \(%body as lua statements) - fell_through = true; - end); + fell_through = true + end) remove free vars (declare locals in %setup_lua) from %body_lua %lua <- Lua ".." do - local old_actions, old_compile_actions, old_arg_orders = ACTIONS, COMPILE_ACTIONS, ARG_ORDERS; - ACTIONS = setmetatable({}, {__index=old_actions}); - COMPILE_ACTIONS = setmetatable({}, {__index=old_compile_actions}); - ARG_ORDERS = setmetatable({}, {__index=old_arg_orders}); + local old_actions, old_compile_actions, old_arg_orders = ACTIONS, COMPILE_ACTIONS, ARG_ORDERS + ACTIONS = setmetatable({}, {__index=old_actions}) + COMPILE_ACTIONS = setmetatable({}, {__index=old_compile_actions}) + ARG_ORDERS = setmetatable({}, {__index=old_arg_orders}) \%setup_lua if not ok then - ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders; - error(ret); + ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders + error(ret) end if not fell_through then - ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders; - return ret; + ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders + return ret end - getmetatable(ACTIONS).__newindex = old_actions; - getmetatable(COMPILE_ACTIONS).__newindex = old_compile_actions; - getmetatable(ARG_ORDERS).__newindex = old_arg_orders; + getmetatable(ACTIONS).__newindex = old_actions + getmetatable(COMPILE_ACTIONS).__newindex = old_compile_actions + getmetatable(ARG_ORDERS).__newindex = old_arg_orders \%body_lua - ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders; + ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders if not ok then - error(ret); + error(ret) end if not fell_through then - return ret; + return ret end end declare locals in %lua return %lua +parse [using %] as: using % (do nothing) |
