From 0c9973ff0363e400d3d284339b77197c40c3f60c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 12 Jun 2018 13:56:15 -0700 Subject: Tidying up exceptions and error reporting. Also simplified the grammar a tiny bit. --- core/control_flow.nom | 42 ++++++++++++++++++++++++++++++------------ core/metaprogramming.nom | 18 +++++++++++++++--- core/scopes.nom | 4 ++-- 3 files changed, 47 insertions(+), 17 deletions(-) (limited to 'core') diff --git a/core/control_flow.nom b/core/control_flow.nom index f5c61e3..73a0f43 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -49,13 +49,13 @@ immediately To see why this is necessary consider: (random()<.5 and false or 99) return Lua value ".." - (function() + ((function() if \(%condition as lua expr) then return \(%when_true_expr as lua expr) else return \(%when_false_expr as lua expr) end - end)() + end)()) # GOTOs immediately @@ -361,31 +361,46 @@ immediately # Try/except immediately compile [..] - try %action and if it succeeds %success or if it barfs %fallback - try %action and if it barfs %fallback or if it succeeds %success + 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 ok, ret = pcall(function() + 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 - else - \(%fallback as lua statements) + 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 @@ -406,17 +421,20 @@ immediately fell_through = true end) \(%final_action as lua statements) - if not ok then error(ret) end + if not ok then error(ret, 0) end if not fell_through then return ret end end # Inline thunk: immediately compile [result of %body] to - Lua value ".." - (function() - \(%body as lua statements) - end)() + %body <- (%body as lua statements) + declare locals in %body + return + Lua value ".." + ((function() + \%body + end)()) # Coroutines: immediately diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 8ad9448..83293c1 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -132,15 +132,25 @@ immediately lua> ".." local lua = nomsu:tree_to_lua(\%tree) if not lua.is_value then - error("Invalid thing to convert to lua expr: "..tostring(\%tree)) + compile_error(\%tree, "Invalid thing to convert to lua expr:\n%s") end return lua action [%tree as lua statements] =lua "nomsu:tree_to_lua(\%tree):as_statements()" + + action [%tree as lua return] + =lua "nomsu:tree_to_lua(\%tree):as_statements('return ')" - action [%tree with vars %vars] - =lua "\%tree:map(\%vars)" +immediately + compile [%tree with vars %vars] to + barf "Deprecated" + + compile [%tree with %t -> %replacement] to + Lua value ".." + \(%tree as lua expr):map(function(\(%t as lua expr)) + \(%replacement as lua return) + end) compile [declare locals in %code] to Lua value "\(%code as lua expr):declare_locals()" @@ -202,6 +212,8 @@ immediately # Error functions immediately + compile [traceback] to: Lua value "debug.traceback()" + compile [traceback %] to: Lua value "debug.traceback('', \(% as lua expr))" compile [barf] to: Lua "error(nil, 0);" compile [barf %msg] to: Lua "error(\(%msg as lua expr), 0);" compile [assume %condition] to diff --git a/core/scopes.nom b/core/scopes.nom index 72631c0..02ad73d 100644 --- a/core/scopes.nom +++ b/core/scopes.nom @@ -30,7 +30,7 @@ compile [using %definitions %body, using %definitions do %body] to \%setup_lua if not ok then ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders - error(ret) + error(ret, 0) end if not fell_through then ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders @@ -42,7 +42,7 @@ compile [using %definitions %body, using %definitions do %body] to \%body_lua ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders if not ok then - error(ret) + error(ret, 0) end if not fell_through then return ret -- cgit v1.2.3