diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-11 01:03:52 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-11 01:04:17 -0800 |
| commit | c0333ca31532f14ec8ebd841a5f68b2f35e9cc80 (patch) | |
| tree | cca1698b04eec2afb6adc7de55860caf12e90dca /lib | |
| parent | 53a9d4eae888d2b09c68fcd5dc14ae51f5d07c31 (diff) | |
Overhaul of error reporting and removing nomsu:call(stub, line_no, ...) in favor of nomsu.defs[stub].fn(...)
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/metaprogramming.nom | 23 | ||||
| -rw-r--r-- | lib/operators.nom | 22 | ||||
| -rw-r--r-- | lib/utils.nom | 2 |
3 files changed, 30 insertions, 17 deletions
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom index 4b3c615..d396ac3 100644 --- a/lib/metaprogramming.nom +++ b/lib/metaprogramming.nom @@ -19,7 +19,7 @@ immediately: immediately: lua> ".." - nomsu:defmacro("compile %macro_def to %body", function(nomsu, \%macro_def, \%body) + nomsu:defmacro("compile %macro_def to %body", \(__line_no__), function(nomsu, \%macro_def, \%body) nomsu:assert(\%macro_def.type == "List", "Invalid type for compile definition signature. Expected List, but got: "..tostring(\%macro_def.type)); nomsu:assert(\%body.type == "Block", @@ -33,13 +33,14 @@ immediately: %s end local function macro_wrapper(...) return {expr=macro(...)}; end - nomsu:defmacro(%s, macro_wrapper, %s); - end]]):format(args, body_lua, signature, nomsu:repr(("compile %s\\n..to code %s"):format(\%macro_def.src, \%body.src))); + nomsu:defmacro(%s, %s, macro_wrapper, %s); + end]]):format(args, body_lua, signature, nomsu:repr(\%macro_def:get_line_no()), + nomsu:repr(("compile %s\\n..to code %s"):format(\%macro_def.src, \%body.src))); return {statements=lua}; end, \(__src__ 1)); lua> ".." - nomsu:defmacro("compile %macro_def to code %body", function(nomsu, \%macro_def, \%body) + nomsu:defmacro("compile %macro_def to code %body", \(__line_no__), function(nomsu, \%macro_def, \%body) nomsu:assert(\%macro_def.type == "List", "Invalid type for compile definition signature. Expected List, but got: "..tostring(\%macro_def.type)); nomsu:assert(\%body.type == "Block", @@ -53,8 +54,9 @@ immediately: %s end local function macro_wrapper(...) return {statements=macro(...)}; end - nomsu:defmacro(%s, macro_wrapper, %s); - end]]):format(args, body_lua, signature, nomsu:repr(("compile %s\\n..to code %s"):format(\%macro_def.src, \%body.src))); + nomsu:defmacro(%s, %s, macro_wrapper, %s); + end]]):format(args, body_lua, signature, nomsu:repr(\%macro_def:get_line_no()), + nomsu:repr(("compile %s\\n..to code %s"):format(\%macro_def.src, \%body.src))); return {statements=lua}; end, \(__src__ 1)); @@ -71,7 +73,7 @@ immediately: body_lua = body_lua.statements or ("return "..body_lua.expr..";"); local src = nomsu:dedent(nomsu:source_code(0)); local def_lua = ([[ - nomsu:def(%s, function(%s) + nomsu:def(%s, \(__line_no__), function(%s) %s end, %s);]]):format(signature, args, body_lua, nomsu:repr(src)); return def_lua; @@ -79,7 +81,7 @@ immediately: # Rule to make nomsu macros: immediately: lua> ".." - nomsu:defmacro("parse %shorthand as %longhand", (function(nomsu, \%shorthand, \%longhand) + nomsu:defmacro("parse %shorthand as %longhand", \(__line_no__), (function(nomsu, \%shorthand, \%longhand) nomsu:assert(\%shorthand.type == "List", "Invalid type for parse definition signature. Expected List, but got: "..tostring(\%shorthand.type)); nomsu:assert(\%longhand.type == "Block", @@ -95,11 +97,12 @@ immediately: for i, a in ipairs(arg_names) do replacements[i] = "["..nomsu:repr(a).."]=_"..nomsu:var_to_lua_identifier(a); end replacements = "{"..table.concat(replacements, ", ").."}"; local lua_code = ([[ - nomsu:defmacro(%s, (function(%s) + nomsu:defmacro(%s, %s, (function(%s) local template = nomsu:parse(%s, %s); local replacement = nomsu:replaced_vars(template, %s); return nomsu:tree_to_lua(replacement); - end), %s)]]):format(signature, args, template, nomsu:repr(\%shorthand:get_line_no()), replacements, nomsu:repr(nomsu:source_code(0))); + end), %s)]]):format(signature, nomsu:repr(\%shorthand:get_line_no()), args, template, + nomsu:repr(\%shorthand:get_line_no()), replacements, nomsu:repr(nomsu:source_code(0))); return {statements=lua_code}; end), \(__src__ 1)); diff --git a/lib/operators.nom b/lib/operators.nom index 1670191..7eada51 100644 --- a/lib/operators.nom +++ b/lib/operators.nom @@ -19,14 +19,24 @@ compile [..] %when_true_expr if %condition otherwise %when_false_expr %when_false_expr unless %condition else %when_true_expr %when_false_expr unless %condition then %when_true_expr -..to: ".." - (function(nomsu, condition) - if condition then - return \(%when_true_expr as lua); +..to: + lua> ".." + local condition = nomsu:tree_to_lua(\%condition).expr; + local when_true = nomsu:tree_to_lua(\%when_true_expr).expr; + local when_false = nomsu:tree_to_lua(\%when_false_expr).expr; + local safe = {String=true, List=true, Dict=true, Number=true}; + if safe[\%when_true_expr.type] then + return "("..condition.." and "..when_true.." or "..when_false..")"; else - return \(%when_false_expr as lua); + return ([[ + (function(nomsu) + if %s then + return %s; + else + return %s; + end + end)(nomsu)]]):format(condition, when_true, when_false); end - end)(nomsu, \(%condition as lua)) parse [..] %true if %x == %y else %false, %true if %x == %y otherwise %false %false unless %x == %y else %true, %false unless %x == %y otherwise %true diff --git a/lib/utils.nom b/lib/utils.nom index ce6dc9d..6af38c8 100644 --- a/lib/utils.nom +++ b/lib/utils.nom @@ -113,7 +113,7 @@ lua> ".." }; for name,code in pairs(colors) do local escape = "\\"\\\\27["..tostring(code).."m\\"" - nomsu:defmacro(name, function() return escape end, ""); + nomsu:defmacro(name, \(__line_no__), function() return escape end, ""); end end |
