aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--code_obj.lua6
-rw-r--r--code_obj.moon5
-rw-r--r--core/control_flow.nom28
-rw-r--r--core/metaprogramming.nom72
-rw-r--r--core/operators.nom2
-rw-r--r--files.lua7
-rw-r--r--files.moon6
-rw-r--r--lib/os.nom5
-rw-r--r--nomsu_compiler.lua4
-rw-r--r--nomsu_compiler.moon4
-rw-r--r--tests/control_flow.nom9
11 files changed, 95 insertions, 53 deletions
diff --git a/code_obj.lua b/code_obj.lua
index de62c8f..e3f7b16 100644
--- a/code_obj.lua
+++ b/code_obj.lua
@@ -297,6 +297,12 @@ do
end
return statements
end,
+ as_expr = function(self)
+ if self.is_value then
+ return self
+ end
+ return error("Cannot convert to expression: " .. tostring(tostring(self)))
+ end,
__tostring = function(self)
if self.__str == nil then
local buff, indents = { }, self.indents
diff --git a/code_obj.moon b/code_obj.moon
index 73a1ba8..a5c3960 100644
--- a/code_obj.moon
+++ b/code_obj.moon
@@ -177,6 +177,11 @@ class LuaCode extends Code
if suffix != ""
statements\append suffix
return statements
+
+ as_expr: =>
+ if @is_value
+ return self
+ error("Cannot convert to expression: #{tostring self}")
__tostring: =>
if @__str == nil
diff --git a/core/control_flow.nom b/core/control_flow.nom
index 0cc8f4e..61438d1 100644
--- a/core/control_flow.nom
+++ b/core/control_flow.nom
@@ -385,16 +385,18 @@ compile [result of %body] to
Lua value "(\(compile as: [] -> %body))()"
# Recurion control flow
-using
- compile [%var's stack] to: Lua value "stack\(%var as lua id)"
-..compile
- parse [for %var in recursive %structure %body] as
- with local {(%var's stack): [%structure], action: recurse % on %}
- action [recurse %v on %x]
- add %x to (%v's stack)
- repeat while: (length of (%var's stack)) > 0
- %var <- (remove 1 from (%var's stack))
- %body
- === next %var ==
- === stop %var ===
-
+compile [for %var in recursive %structure %body] to
+ with local compile actions
+ compile [recurse %v on %x] to
+ Lua "table.insert(stack\(%v as lua id), \(%x as lua expr))"
+ return
+ Lua ".."
+ do
+ local stack\(%var as lua id) = list{\(%structure as lua expr)}
+ while #stack\(%var as lua id) > 0 do
+ \(%var as lua expr) = table.remove(stack\(%var as lua id), 1)
+ \(%body as lua statements)
+ \(compile as: === next %var ===)
+ end
+ \(compile as: === stop %var ===)
+ end
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index d22a457..242db04 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -61,20 +61,6 @@ compile [call %fn with %args] to
lua:append(")")
return lua
-compile [using %defs compile %body] to
- lua> ".."
- local lua = LuaCode(tree.source)
- lua:append(
- "local old_nomsu = nomsu\n",
- "local nomsu = table.fork(old_nomsu, {COMPILE_ACTIONS=table.fork(old_nomsu.COMPILE_ACTIONS)})")
- lua:append(nomsu:compile(\%defs))
- lua:append("\n")
- lua:append("local ret = nomsu:compile(tree)\n")
- lua:append("return ret")
- nomsu = table.fork(nomsu, {tree=\%body})
- local output = nomsu:run_lua(lua)
- return output
-
compile [local action %actions %body] to
lua> ".."
local fn_name = "A"..string.as_lua_id(\%actions[1].stub)
@@ -138,33 +124,28 @@ compile [parse %actions as %body] to
return ret
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+compile [%tree as lua expr] to
+ Lua value "nomsu:compile(\(=lua "nomsu:compile(\%tree):as_expr()")):as_expr()"
-compile [remove action %action] to
- Lua ".."
- A\(=lua "string.as_lua_id(\(%action.stub))") = nil
-
-action [%tree as nomsu]
- =lua "nomsu:tree_to_nomsu(\%tree)"
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+compile [%tree as lua] to
+ Lua value "nomsu:compile(\(%tree as lua expr))"
-action [%tree as inline nomsu]
- =lua "nomsu:tree_to_nomsu(\%tree, true)"
+compile [%tree as lua statements] to
+ Lua value "nomsu:compile(\(%tree as lua expr)):as_statements()"
-action [%tree as lua]
- =lua "nomsu:compile(\%tree)"
+compile [%tree as lua return] to
+ Lua value "nomsu:compile(\(%tree as lua expr)):as_statements('return ')"
-action [%tree as lua expr]
- lua> ".."
- local lua = nomsu:compile(\%tree)
- if not lua.is_value then
- nomsu:compile_error(\%tree, "Invalid thing to convert to lua expr:\n%s")
- end
- return lua
+compile [remove action %action] to
+ Lua ".."
+ A\(=lua "string.as_lua_id(\(%action.stub))") = nil
-action [%tree as lua statements]
- =lua "nomsu:compile(\%tree):as_statements()"
+compile [%tree as nomsu] to
+ Lua value "nomsu:tree_to_nomsu(\(%tree as lua expr))"
-action [%tree as lua return]
- =lua "nomsu:compile(\%tree):as_statements('return ')"
+compile [%tree as inline nomsu] to
+ Lua value "nomsu:tree_to_nomsu(\(%tree as lua expr), true)"
action [%var as lua identifier, %var as lua id]
lua> ".."
@@ -174,6 +155,12 @@ action [%var as lua identifier, %var as lua id]
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+compile [% is syntax tree] to
+ Lua value "AST.is_syntax_tree(\(% as lua expr))"
+
+compile [% is %kind syntax tree] to
+ Lua value "AST.is_syntax_tree(\(% as lua expr), \(%kind as lua expr))"
compile [%tree with %t -> %replacement] to
Lua value ".."
@@ -215,6 +202,10 @@ compile [parse %text] to
Lua value ".."
nomsu:parse(NomsuCode("\("\(%text.source)")", \(%text as lua expr)))
+compile [parse %text from %filename] to
+ Lua value ".."
+ nomsu:parse(NomsuCode(Source(\(%filename as lua expr), 1, #\(%text as lua expr)), \(%text as lua expr)))
+
compile [run %nomsu_code] to
Lua value "nomsu:run(NomsuCode(\(quote "\(%nomsu_code.source)"), \(%nomsu_code as lua expr)))"
@@ -238,8 +229,19 @@ compile [Nomsu syntax version] to: Lua value "NOMSU_SYNTAX_VERSION"
compile [Nomsu compiler version] to: Lua value "NOMSU_COMPILER_VERSION"
compile [core version] to: Lua value "NOMSU_CORE_VERSION"
compile [lib version] to: Lua value "NOMSU_LIB_VERSION"
+compile [command line args] to: Lua value "arg"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+compile [with local compile actions %body] to
+ Lua ".."
+ do
+ local nomsu = table.fork(nomsu, {COMPILE_ACTIONS=table.fork(nomsu.COMPILE_ACTIONS)})
+ \(%body as lua statements)
+ end
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
action [Nomsu version]
use "lib/version.nom"
return "\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)"
diff --git a/core/operators.nom b/core/operators.nom
index 0756751..d434ea7 100644
--- a/core/operators.nom
+++ b/core/operators.nom
@@ -163,7 +163,7 @@ compile [%x ARSHIFT %shift, %x >> %shift] to: Lua value "(\(%x as lua expr) >> \
# Unary operators
compile [- %] to: Lua value "(- \(% as lua expr))"
compile [not %] to: Lua value "(not \(% as lua expr))"
-compile [length of %list] to: Lua value "(#\(%list as lua expr))"
+compile [length of %list, || %list ||] to: Lua value "(#\(%list as lua expr))"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/files.lua b/files.lua
index ded403a..8163ab4 100644
--- a/files.lua
+++ b/files.lua
@@ -161,4 +161,11 @@ files.get_line_number = function(str, pos)
end
return hi
end
+files.get_line = function(str, line_no)
+ local line_starts = files.get_line_starts(str)
+ return str:sub(line_starts[line_no] or 1, line_starts[line_no + 1] or -1)
+end
+files.get_lines = function(str)
+ return get_lines:match(str)
+end
return files
diff --git a/files.moon b/files.moon
index 1d28e5c..fd8a417 100644
--- a/files.moon
+++ b/files.moon
@@ -117,4 +117,10 @@ files.get_line_number = (str, pos)->
else lo = mid+1
return hi
+files.get_line = (str, line_no)->
+ line_starts = files.get_line_starts(str)
+ return str\sub(line_starts[line_no] or 1, line_starts[line_no+1] or -1)
+
+files.get_lines = (str)-> get_lines\match(str)
+
return files
diff --git a/lib/os.nom b/lib/os.nom
index 8a27168..25b47b6 100644
--- a/lib/os.nom
+++ b/lib/os.nom
@@ -29,3 +29,8 @@ action [write to file %filename %text, to file %filename write %text]
file:write(\%text)
file:close()
+action [line number of %pos in %str]
+ =lua "files.get_line_number(\%str, \%pos)"
+
+action [line %line_num in %str]
+ =lua "files.get_line(\%str, \%line_num)"
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index 137df2e..0505562 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -439,7 +439,7 @@ do
local line_numbered_lua = concat((function()
local _accum_0 = { }
local _len_0 = 1
- for i, line in ipairs(get_lines:match(lua_string)) do
+ for i, line in ipairs(files.get_lines(lua_string)) do
_accum_0[_len_0] = format("%3d|%s", i, line)
_len_0 = _len_0 + 1
end
@@ -870,7 +870,7 @@ do
local nomsu = NomsuCode(tree.source, '".."\n ')
for i, bit in ipairs(tree) do
if type(bit) == 'string' then
- local bit_lines = get_lines:match(bit)
+ local bit_lines = files.get_lines(bit)
for j, line in ipairs(bit_lines) do
if j > 1 then
nomsu:append("\n ")
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index fdadb67..3313aa3 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -288,7 +288,7 @@ with NomsuCompiler
run_lua_fn, err = load(lua_string, nil and tostring(source or lua.source), "t", self)
if not run_lua_fn
line_numbered_lua = concat(
- [format("%3d|%s",i,line) for i, line in ipairs get_lines\match(lua_string)],
+ [format("%3d|%s",i,line) for i, line in ipairs files.get_lines(lua_string)],
"\n")
error("Failed to compile generated code:\n#{colored.bright colored.blue colored.onblack line_numbered_lua}\n\n#{err}", 0)
source_key = tostring(source or lua.source)
@@ -596,7 +596,7 @@ with NomsuCompiler
nomsu = NomsuCode(tree.source, '".."\n ')
for i, bit in ipairs tree
if type(bit) == 'string'
- bit_lines = get_lines\match(bit)
+ bit_lines = files.get_lines(bit)
for j, line in ipairs bit_lines
if j > 1 then nomsu\append "\n "
if #line > 1.25*MAX_LINE
diff --git a/tests/control_flow.nom b/tests/control_flow.nom
index 939d5ee..340bd7d 100644
--- a/tests/control_flow.nom
+++ b/tests/control_flow.nom
@@ -179,4 +179,13 @@ assume
..= 6
..or barf "'result of %' failed"
+
+%t <- [1,[2,[[3],4],5,[[[6]]]]]
+%flat <- []
+for % in recursive %t
+ if: (type of %) is "table"
+ for %2 in %: recurse % on %2
+ ..else: add % to %flat
+assume: (sorted %flat) = [1,2,3,4,5,6]
+
say "Control flow test passed."