diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-06-14 23:25:05 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-06-14 23:25:34 -0700 |
| commit | b12744d831c4158671fc22401590eaac00f7c141 (patch) | |
| tree | 203b80de68d89c7333172337f8def46ba11294d3 /core | |
| parent | 49f1eb3d08caf7605046373b7b3a001f28aa8aab (diff) | |
Some cleanup and fixes. Simplifying a lot of code, and extending the
flexibility of scoping. Redesigned Object system too.
Diffstat (limited to 'core')
| -rw-r--r-- | core/control_flow.nom | 59 | ||||
| -rw-r--r-- | core/coroutines.nom | 17 | ||||
| -rw-r--r-- | core/errors.nom | 46 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 8 | ||||
| -rw-r--r-- | core/operators.nom | 2 | ||||
| -rw-r--r-- | core/scopes.nom | 66 |
6 files changed, 93 insertions, 105 deletions
diff --git a/core/control_flow.nom b/core/control_flow.nom index e760b00..15c1ca1 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -359,51 +359,6 @@ immediately end --when % = ? return %code -# Try/except -immediately - compile [..] - try %action and if it succeeds %success or if it barfs %msg %fallback - try %action and if it barfs %msg %fallback or if it succeeds %success - ..to - Lua ".." - do - local fell_through = false - local err, erred = nil, false - local ok, ret = xpcall(function() - \(%action as lua statements) - fell_through = true - end, function(\(%msg as lua expr)) - local ok, ret = pcall(function() - \(%fallback as lua statements) - end) - if not ok then err, erred = ret, true end - end) - if ok then - \(%success as lua statements) - if not fell_through then - return ret - end - elseif erred then - error(err, 0) - end - end -immediately - parse [..] - try %action and if it succeeds %success or if it barfs %fallback - try %action and if it barfs %fallback or if it succeeds %success - ..as: try %action and if it succeeds %success or if it barfs (=lua "") %fallback -immediately - parse [try %action] as - try %action and if it succeeds: do nothing - ..or if it barfs: do nothing - parse [try %action and if it barfs %fallback] as - try %action and if it succeeds: do nothing - ..or if it barfs %fallback - parse [try %action and if it barfs %msg %fallback] as - try %action and if it succeeds: do nothing - ..or if it barfs %msg %fallback - parse [try %action and if it succeeds %success] as - try %action and if it succeeds %success or if it barfs: do nothing # Do/finally immediately @@ -433,17 +388,7 @@ immediately declare locals in %body return Lua value ".." - ((function() + (function() \%body - end)()) + end)() -# Coroutines: -immediately - compile [values %body, coroutine %body, generator %body] to - Lua value ".." - (function() - \(%body as lua statements) - end) - compile [->] to: Lua "coroutine.yield(true);" - compile [-> %] to: Lua "coroutine.yield(true, \(% as lua expr));" - compile [-> %k = %v] to: Lua "coroutine.yield(\(%k as lua expr), \(%v as lua expr));" diff --git a/core/coroutines.nom b/core/coroutines.nom new file mode 100644 index 0000000..d0d9c36 --- /dev/null +++ b/core/coroutines.nom @@ -0,0 +1,17 @@ +# + This file defines the code that creates and manipulates coroutines + +use "core/metaprogramming.nom" + +compile [coroutine %body, generator %body] to + Lua value ".." + (function() + \(%body as lua statements) + end) +compile [->] to: Lua value "coroutine.yield(true)" +compile [-> %] to: Lua value "coroutine.yield(true, \(% as lua expr))" +compile [for % in coroutine %co %body] to + Lua ".." + for junk,\(% as lua expr) in coroutine.wrap(\(%co as lua expr)) do + \(%body as lua statements) + end diff --git a/core/errors.nom b/core/errors.nom index dc585be..8c6261a 100644 --- a/core/errors.nom +++ b/core/errors.nom @@ -20,3 +20,49 @@ compile [assume %condition or barf %message] to if not \(%condition as lua expr) then error(\(%message as lua expr), 0); end + +# Try/except +immediately + compile [..] + try %action and if it succeeds %success or if it barfs %msg %fallback + try %action and if it barfs %msg %fallback or if it succeeds %success + ..to + Lua ".." + do + local fell_through = false + local err, erred = nil, false + local ok, ret = xpcall(function() + \(%action as lua statements) + fell_through = true + end, function(\(%msg as lua expr)) + local ok, ret = pcall(function() + \(%fallback as lua statements) + end) + if not ok then err, erred = ret, true end + end) + if ok then + \(%success as lua statements) + if not fell_through then + return ret + end + elseif erred then + error(err, 0) + end + end +immediately + parse [..] + try %action and if it succeeds %success or if it barfs %fallback + try %action and if it barfs %fallback or if it succeeds %success + ..as: try %action and if it succeeds %success or if it barfs (=lua "") %fallback +immediately + parse [try %action] as + try %action and if it succeeds: do nothing + ..or if it barfs: do nothing + parse [try %action and if it barfs %fallback] as + try %action and if it succeeds: do nothing + ..or if it barfs %fallback + parse [try %action and if it barfs %msg %fallback] as + try %action and if it succeeds: do nothing + ..or if it barfs %msg %fallback + parse [try %action and if it succeeds %success] as + try %action and if it succeeds %success or if it barfs: do nothing diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 301c092..82447b3 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -117,8 +117,12 @@ immediately action [%tree as lua return] =lua "nomsu:tree_to_lua(\%tree):as_statements('return ')" - action [%var as lua identifier] - =lua "type(\%var) == 'string' and string.as_lua_id(\%var) or nomsu:tree_to_lua(\%var)" + action [%var as lua identifier, %var as lua id] + lua> ".." + if type(\%var) == 'string' then return string.as_lua_id(\%var) + elseif \%var.type == 'Var' then return string.as_lua_id(\%var[1]) + elseif \%var.type == 'Action' then return "A"..string.as_lua_id(\%var.stub) + end immediately compile [%tree with %t -> %replacement] to diff --git a/core/operators.nom b/core/operators.nom index 2b44df4..f07ab3f 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -40,7 +40,7 @@ immediately immediately # Simultaneous mutli-assignments like: x,y,z = 1,x,3; - compile [<- %assignments] to + compile [<- %assignments, assign %assignments] to assume (%assignments.type is "Dict") or barf ".." Expected a Dict for the assignments part of '<- %' statement, not \%assignments lua> ".." diff --git a/core/scopes.nom b/core/scopes.nom index 02ad73d..b5fdded 100644 --- a/core/scopes.nom +++ b/core/scopes.nom @@ -1,54 +1,30 @@ use "core/metaprogramming.nom" -use "core/text.nom" use "core/operators.nom" use "core/collections.nom" use "core/control_flow.nom" -compile [using %definitions %body, using %definitions do %body] to - %setup_lua <- - Lua ".." - local fell_through = false - local ok, ret = pcall(function() - \(%definitions as lua statements) - fell_through = true - end) - %body_lua <- - Lua ".." - local fell_through = false - local ok, ret = pcall(function() - \(%body as lua statements) - fell_through = true - end) - remove free vars (declare locals in %setup_lua) from %body_lua - %lua <- +compile [with local %locals %body, with local %locals do %body] to + %body_lua <- (%body as lua statements) + when %locals.type = ? + * "Dict" + %body_lua <- + Lua ".." + \(=lua "A_assign_1(\%locals, \%locals)") + \%body_lua + declare locals + (%.1 as lua id) for % in %locals + .. in %body_lua + * "List" + declare locals + (% as lua id) for % in %locals + .. in %body_lua + * "Var" + * "Action" + declare locals [%locals as lua id] in %body_lua + * else + barf "Unexpected local: \(%locals as nomsu)" + return 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}) - \%setup_lua - if not ok then - ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders - error(ret, 0) - end - if not fell_through then - 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 \%body_lua - ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders - if not ok then - error(ret, 0) - end - if not fell_through then - return ret - end end - declare locals in %lua - return %lua - -parse [using %] as: using % (do nothing) |
