From 49f1eb3d08caf7605046373b7b3a001f28aa8aab Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 14 Jun 2018 22:17:26 -0700 Subject: [PATCH] Reshuffling some files. --- core/control_flow.nom | 1 + core/errors.nom | 22 ++++++++++ core/io.nom | 20 ++++++++++ core/metaprogramming.nom | 84 ++++++--------------------------------- core/operators.nom | 1 + lib/os.nom | 22 ++++++++++ tests/metaprogramming.nom | 2 - 7 files changed, 78 insertions(+), 74 deletions(-) create mode 100644 core/errors.nom create mode 100644 core/io.nom create mode 100644 lib/os.nom diff --git a/core/control_flow.nom b/core/control_flow.nom index 2c85221..e760b00 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -5,6 +5,7 @@ use "core/metaprogramming.nom" use "core/text.nom" use "core/operators.nom" +use "core/errors.nom" # No-Op immediately diff --git a/core/errors.nom b/core/errors.nom new file mode 100644 index 0000000..dc585be --- /dev/null +++ b/core/errors.nom @@ -0,0 +1,22 @@ +# + This file contains basic error reporting code + +use "core/metaprogramming.nom" + +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 + lua> "local \%assumption = 'Assumption failed: '..tostring(nomsu:tree_to_nomsu(\%condition));" + return + Lua ".." + if not \(%condition as lua expr) then + error(\(quote "\%assumption"), 0); + end + +compile [assume %condition or barf %message] to + Lua ".." + if not \(%condition as lua expr) then + error(\(%message as lua expr), 0); + end diff --git a/core/io.nom b/core/io.nom new file mode 100644 index 0000000..4f12e57 --- /dev/null +++ b/core/io.nom @@ -0,0 +1,20 @@ +# + This file contains basic input/output code + +use "core/metaprogramming.nom" + +compile [say %message] to + lua> ".." + if \%message.type == "Text" then + return Lua(tree.source, "io.write(", \(%message as lua expr), ", '\\\\n');"); + else + return Lua(tree.source, "io.write(tostring(", \(%message as lua expr), "), '\\\\n');"); + end + +compile [ask %prompt] to + lua> ".." + if \%prompt.type == "Text" then + return Lua.Value(tree.source, "(io.write(", \(%prompt as lua expr), ") and io.read())"); + else + return Lua.Value(tree.source, "(io.write(tostring(", \(%prompt as lua expr), ")) and io.read())"); + end diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 15c5817..301c092 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -94,20 +94,6 @@ compile [remove action %action] to COMPILE_TIME[fn] = nil immediately - action [read file %filename] - lua> ".." - local file = io.open(\%filename) - local contents = file:read("*a") - file:close() - return contents - - action [sh> %cmd] - lua> ".." - local result = io.popen(\%cmd) - local contents = result:read("*a") - result:close() - return contents - action [%tree as nomsu] =lua "nomsu:tree_to_nomsu(\%tree)" @@ -133,11 +119,6 @@ immediately action [%var as lua identifier] =lua "type(\%var) == 'string' and string.as_lua_id(\%var) or nomsu:tree_to_lua(\%var)" - -immediately - compile [parse %text] to - Lua value ".." - nomsu:parse(Nomsu("\("\(%text.source)")", \(%text as lua expr))) immediately compile [%tree with %t -> %replacement] to @@ -155,15 +136,6 @@ immediately compile [remove free vars %vars from %code] to Lua "\(%code as lua expr):remove_free_vars(\(%vars as lua expr));" - action [%tree as value] - lua> ".." - if \%tree.type == 'Text' and #\%tree == 1 and type(\%tree[1]) == 'string' then - return \%tree[1] - end - local lua = Lua(\%tree.source, "return ",nomsu:tree_to_lua(\%tree)) - return nomsu:run_lua(lua) - -immediately parse [%lua <-write %code, to %lua write %code] as: lua> "\%lua:append(\%code);" immediately @@ -173,35 +145,23 @@ immediately compile [type of %obj] to: Lua value "type(\(%obj as lua expr))" immediately - compile [nomsu] to: Lua value "nomsu" + compile [parse %text] to + Lua value ".." + nomsu:parse(Nomsu("\("\(%text.source)")", \(%text as lua expr))) -# Compiler tools -immediately - compile [run %code] to - Lua value "nomsu:run(Nomsu(\(quote "\(%code.source)"), \(%code as lua expr)))" + compile [run %nomsu_code] to + Lua value "nomsu:run(Nomsu(\(quote "\(%nomsu_code.source)"), \(%nomsu_code as lua expr)))" -immediately - compile [show lua %block] to + action [run tree %tree, %tree as value] lua> ".." - local \%lua = nomsu:tree_to_lua(\%block); - return Lua(tree.source, "print(", repr(tostring(\%lua)), ");"); - -immediately - compile [say %message] to - lua> ".." - if \%message.type == "Text" then - return Lua(tree.source, "io.write(", \(%message as lua expr), ", '\\\\n');"); - else - return Lua(tree.source, "io.write(tostring(", \(%message as lua expr), "), '\\\\n');"); + if \%tree.type == 'Text' and #\%tree == 1 and type(\%tree[1]) == 'string' then + return \%tree[1] end + local lua = Lua(\%tree.source, "return ",nomsu:tree_to_lua(\%tree)) + return nomsu:run_lua(lua) - compile [ask %prompt] to - lua> ".." - if \%prompt.type == "Text" then - return Lua.Value(tree.source, "(io.write(", \(%prompt as lua expr), ") and io.read())"); - else - return Lua.Value(tree.source, "(io.write(tostring(", \(%prompt as lua expr), ")) and io.read())"); - end + compile [compile %block, compiled %block, %block compiled] to + Lua value "nomsu:tree_to_lua(\(%block as lua))" # Return immediately @@ -210,26 +170,6 @@ immediately compile [return] to: Lua "do return; end" compile [return %return_value] to: Lua "do return \(%return_value as lua expr); end" -# 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 - lua> "local \%assumption = 'Assumption failed: '..tostring(nomsu:tree_to_nomsu(\%condition));" - return - Lua ".." - if not \(%condition as lua expr) then - error(\(quote "\%assumption"), 0); - end - - compile [assume %condition or barf %message] to - Lua ".." - if not \(%condition as lua expr) then - error(\(%message as lua expr), 0); - end - # Literals immediately compile [yes] to: Lua value "true" diff --git a/core/operators.nom b/core/operators.nom index 3ea0fe1..2b44df4 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -2,6 +2,7 @@ This file contains definitions of operators like "+" and "and". use "core/metaprogramming.nom" +use "core/errors.nom" # Comparison Operators immediately diff --git a/lib/os.nom b/lib/os.nom new file mode 100644 index 0000000..be65370 --- /dev/null +++ b/lib/os.nom @@ -0,0 +1,22 @@ +use "core" + +action [sh> %cmd] + lua> ".." + local result = io.popen(\%cmd) + local contents = result:read("*a") + result:close() + return contents + +action [read file %filename] + lua> ".." + local file = io.open(\%filename) + local contents = file:read("*a") + file:close() + return contents + +action [write to file %filename %text, to file %filename write %text] + lua> ".." + local file = io.open(\%filename, 'w') + file:write(\%text) + file:close() + diff --git a/tests/metaprogramming.nom b/tests/metaprogramming.nom index a7a61d4..d5ef07f 100644 --- a/tests/metaprogramming.nom +++ b/tests/metaprogramming.nom @@ -62,8 +62,6 @@ assume ("\(\%x as nomsu)" = "%x") or barf "var source code failed." assume ((type of {}) = "table") or barf "type of failed." -assume ((nomsu) = (=lua "nomsu")) or barf "nomsu failed" - assume ("\(\%x as lua identifier)" = "_x") or barf "converting to identifier failed." assume ((run "return 99") = 99) or barf "run % failed."