More cleanup, slowly working through operators/control_flow
This commit is contained in:
parent
ec17442090
commit
dcb380f1f6
@ -8,7 +8,7 @@ use "core/operators.nom"
|
|||||||
|
|
||||||
# No-Op
|
# No-Op
|
||||||
immediately:
|
immediately:
|
||||||
compile [do nothing] to {statements:""}
|
compile [do nothing] to: Lua ""
|
||||||
|
|
||||||
# Conditionals
|
# Conditionals
|
||||||
immediately:
|
immediately:
|
||||||
@ -59,15 +59,15 @@ immediately
|
|||||||
|
|
||||||
# GOTOs
|
# GOTOs
|
||||||
immediately:
|
immediately:
|
||||||
compile [=== %label ===, --- %label ---, *** %label ***] to {..}
|
compile [=== %label ===, --- %label ---, *** %label ***] to
|
||||||
statements:"::label_\(%label as lua identifier)::;"
|
Lua "::label_\(%label as lua identifier)::;"
|
||||||
compile [go to %label] to {..}
|
compile [go to %label] to
|
||||||
statements:"goto label_\(%label as lua identifier);"
|
Lua "goto label_\(%label as lua identifier);"
|
||||||
|
|
||||||
# Basic loop control
|
# Basic loop control
|
||||||
immediately:
|
immediately:
|
||||||
compile [do next] to {statements:"continue;"}
|
compile [do next] to: Lua "continue;"
|
||||||
compile [stop] to {statements:"break;"}
|
compile [stop] to: Lua "break;"
|
||||||
|
|
||||||
# Helper function
|
# Helper function
|
||||||
immediately:
|
immediately:
|
||||||
@ -84,17 +84,17 @@ immediately:
|
|||||||
|
|
||||||
# While loops
|
# While loops
|
||||||
immediately:
|
immediately:
|
||||||
compile [do next repeat] to {statements:"goto continue_repeat;"}
|
compile [do next repeat] to: Lua "goto continue_repeat;"
|
||||||
compile [stop repeating] to {statements:"goto stop_repeat;"}
|
compile [stop repeating] to: Lua "goto stop_repeat;"
|
||||||
compile [repeat while %condition %body] to
|
compile [repeat while %condition %body] to
|
||||||
%lua <-: Lua "while \(%condition as lua expr) do"
|
%lua <-: Lua "while \(%condition as lua expr) do"
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and ((%'s stub) is "do next repeat")
|
(%.type = "Action") and ((%'s stub) is "do next repeat")
|
||||||
..:
|
..:
|
||||||
%lua +<- "\n::continue_repeat::;"
|
%lua <-write "\n::continue_repeat::;"
|
||||||
%lua +<- "\n"
|
%lua <-write "\n"
|
||||||
%lua +<- (%body as lua statements)
|
%lua <-write (%body as lua statements)
|
||||||
%lua +<- "\nend --while-loop"
|
%lua <-write "\nend --while-loop"
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and ((%'s stub) is "stop repeating")
|
(%.type = "Action") and ((%'s stub) is "stop repeating")
|
||||||
..:
|
..:
|
||||||
@ -113,8 +113,8 @@ immediately:
|
|||||||
%lua <-: Lua "for i=1,\(%n as lua expr) do"
|
%lua <-: Lua "for i=1,\(%n as lua expr) do"
|
||||||
if %body has subtree % where
|
if %body has subtree % where
|
||||||
(%.type = "Action") and ((%'s stub) is "do next repeat")
|
(%.type = "Action") and ((%'s stub) is "do next repeat")
|
||||||
..: %lua +<- "\n::continue_repeat::;"
|
..: %lua <-write "\n::continue_repeat::;"
|
||||||
%lua +<- "\n\(%body as lua statements)\nend --numeric for-loop"
|
%lua <-write "\n\(%body as lua statements)\nend --numeric for-loop"
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and ((%'s stub) is "stop repeating")
|
(%.type = "Action") and ((%'s stub) is "stop repeating")
|
||||||
..:
|
..:
|
||||||
@ -128,10 +128,10 @@ immediately:
|
|||||||
|
|
||||||
# For loop control flow:
|
# For loop control flow:
|
||||||
immediately:
|
immediately:
|
||||||
compile [stop %var] to {..}
|
compile [stop %var] to
|
||||||
statements:"goto stop_\(%var as lua identifier);"
|
Lua "goto stop_\(%var as lua identifier);"
|
||||||
compile [do next %var] to {..}
|
compile [do next %var] to
|
||||||
statements:"goto continue_\(%var as lua identifier);"
|
Lua "goto continue_\(%var as lua identifier);"
|
||||||
|
|
||||||
# Numeric range for loops
|
# Numeric range for loops
|
||||||
immediately:
|
immediately:
|
||||||
@ -148,15 +148,15 @@ immediately:
|
|||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "do next %") and
|
((%'s stub) is "do next %") and
|
||||||
%.value.3.value is %var.value
|
%.value.3.value is %var.value
|
||||||
..: %lua write code "\n::continue_\(%var as lua identifier)::;"
|
..: %lua <-write "\n::continue_\(%var as lua identifier)::;"
|
||||||
%lua write code "\n\(%body as lua statements)\nend --numeric for-loop"
|
%lua <-write "\n\(%body as lua statements)\nend --numeric for-loop"
|
||||||
|
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and:
|
(%.type = "Action") and:
|
||||||
((%'s stub) is "stop %") and:
|
((%'s stub) is "stop %") and:
|
||||||
%.value.2.value is %var.value
|
%.value.2.value is %var.value
|
||||||
..:
|
..:
|
||||||
%lua write code ".."
|
%lua <-write ".."
|
||||||
do -- scope for stopping for-loop
|
do -- scope for stopping for-loop
|
||||||
\%lua
|
\%lua
|
||||||
::stop_\(%var as lua identifier)::;
|
::stop_\(%var as lua identifier)::;
|
||||||
@ -182,8 +182,8 @@ immediately:
|
|||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "do next %") and
|
((%'s stub) is "do next %") and
|
||||||
%.value.3.value is %var.value
|
%.value.3.value is %var.value
|
||||||
..: %lua +<- (Lua "\n::continue_\(%var as lua identifier)::;")
|
..: %lua <-write (Lua "\n::continue_\(%var as lua identifier)::;")
|
||||||
%lua +<- (Lua "\n\(%body as lua statements)\nend --foreach-loop")
|
%lua <-write (Lua "\n\(%body as lua statements)\nend --foreach-loop")
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "stop %") and
|
((%'s stub) is "stop %") and
|
||||||
@ -210,27 +210,27 @@ immediately:
|
|||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "do next %") and
|
((%'s stub) is "do next %") and
|
||||||
%.value.3.value is %key.value
|
%.value.3.value is %key.value
|
||||||
..: %lua +<- (Lua "\n::continue_\(%key as lua identifier)::;")
|
..: %lua <-write (Lua "\n::continue_\(%key as lua identifier)::;")
|
||||||
|
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "do next %") and
|
((%'s stub) is "do next %") and
|
||||||
%.value.3.value is %value.value
|
%.value.3.value is %value.value
|
||||||
..: %lua +<- (Lua "\n::continue_\(%value as lua identifier)::;")
|
..: %lua <-write (Lua "\n::continue_\(%value as lua identifier)::;")
|
||||||
%lua +<- (Lua "\n\(%body as lua statements)\nend --foreach-loop")
|
%lua <-write (Lua "\n\(%body as lua statements)\nend --foreach-loop")
|
||||||
|
|
||||||
%stop_labels <- ""
|
%stop_labels <- ""
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "stop %") and
|
((%'s stub) is "stop %") and
|
||||||
%.value.2.value is %key.value
|
%.value.2.value is %key.value
|
||||||
..: %stop_labels +<- "\n::stop_\(%key as lua identifier)::;"
|
..: %stop_labels <-write "\n::stop_\(%key as lua identifier)::;"
|
||||||
|
|
||||||
if %body has subtree % where:
|
if %body has subtree % where:
|
||||||
(%.type = "Action") and
|
(%.type = "Action") and
|
||||||
((%'s stub) is "stop %") and
|
((%'s stub) is "stop %") and
|
||||||
%.value.2.value is %value.value
|
%.value.2.value is %value.value
|
||||||
..: %stop_labels +<- "\n::stop_\(%value as lua identifier)::;"
|
..: %stop_labels <-write "\n::stop_\(%value as lua identifier)::;"
|
||||||
|
|
||||||
if: %stop_labels is not ""
|
if: %stop_labels is not ""
|
||||||
%lua <-
|
%lua <-
|
||||||
@ -271,7 +271,7 @@ immediately:
|
|||||||
|
|
||||||
if: =lua "\%condition.type == 'Word' and \%condition.value == 'else'"
|
if: =lua "\%condition.type == 'Word' and \%condition.value == 'else'"
|
||||||
assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
|
assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
|
||||||
%code +<- ".."
|
%code <-write ".."
|
||||||
|
|
||||||
else
|
else
|
||||||
\%action_statements
|
\%action_statements
|
||||||
@ -280,7 +280,7 @@ immediately:
|
|||||||
assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block"
|
assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block"
|
||||||
lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
|
lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
|
||||||
%condition_code <- (%fallthroughs joined with " or ")
|
%condition_code <- (%fallthroughs joined with " or ")
|
||||||
%code +<- ".."
|
%code <-write ".."
|
||||||
|
|
||||||
\("if" if %is_first else "elseif") \%condition_code then
|
\("if" if %is_first else "elseif") \%condition_code then
|
||||||
\%action_statements
|
\%action_statements
|
||||||
@ -290,7 +290,7 @@ immediately:
|
|||||||
|
|
||||||
assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block"
|
assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block"
|
||||||
if: %code is not ""
|
if: %code is not ""
|
||||||
%code +<- "\nend"
|
%code <-write "\nend"
|
||||||
lua> "utils.deduplicate(\%locals);"
|
lua> "utils.deduplicate(\%locals);"
|
||||||
return {statements:%code, locals:%locals}
|
return {statements:%code, locals:%locals}
|
||||||
|
|
||||||
@ -322,7 +322,7 @@ immediately:
|
|||||||
|
|
||||||
if: =lua "\%condition.type == 'Word' and \%condition.value == 'else'"
|
if: =lua "\%condition.type == 'Word' and \%condition.value == 'else'"
|
||||||
assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
|
assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
|
||||||
%code +<- ".."
|
%code <-write ".."
|
||||||
|
|
||||||
else
|
else
|
||||||
\%action_statements
|
\%action_statements
|
||||||
@ -337,7 +337,7 @@ immediately:
|
|||||||
..else
|
..else
|
||||||
(%i'th in %fallthroughs) <- "utils.equivalent(branch_value, \%)"
|
(%i'th in %fallthroughs) <- "utils.equivalent(branch_value, \%)"
|
||||||
%clause <- (%fallthroughs joined with " or ")
|
%clause <- (%fallthroughs joined with " or ")
|
||||||
%code +<- ".."
|
%code <-write ".."
|
||||||
|
|
||||||
\("if" if %is_first else "elseif") \%clause then
|
\("if" if %is_first else "elseif") \%clause then
|
||||||
\%action_statements
|
\%action_statements
|
||||||
@ -348,7 +348,7 @@ immediately:
|
|||||||
assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block"
|
assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block"
|
||||||
assume (%code is not "") or barf "No body for 'when % = ?' block!"
|
assume (%code is not "") or barf "No body for 'when % = ?' block!"
|
||||||
unless %seen_else
|
unless %seen_else
|
||||||
%code +<- "\nend"
|
%code <-write "\nend"
|
||||||
%code <- ".."
|
%code <- ".."
|
||||||
do --when % = ?
|
do --when % = ?
|
||||||
local branch_value = \(%branch_value as lua expr);\
|
local branch_value = \(%branch_value as lua expr);\
|
||||||
|
@ -26,7 +26,7 @@ immediately:
|
|||||||
local body_lua = \%lua:as_lua(nomsu);
|
local body_lua = \%lua:as_lua(nomsu);
|
||||||
body_lua:convert_to_statements("return ");
|
body_lua:convert_to_statements("return ");
|
||||||
body_lua:declare_locals(args);
|
body_lua:declare_locals(args);
|
||||||
lua:append(")\\n ", body_lua, "\\nend);")
|
lua:append(")\\n ", body_lua, "\\nend);");
|
||||||
return lua;
|
return lua;
|
||||||
end);
|
end);
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ immediately:
|
|||||||
local template;
|
local template;
|
||||||
if \%longhand.type == "Block" then
|
if \%longhand.type == "Block" then
|
||||||
local lines = {};
|
local lines = {};
|
||||||
for i, line in ipairs(\%longhand.value) do lines[i] = line.source:get_text(); end
|
for i, line in ipairs(\%longhand.value) do lines[i] = tostring(line.source:get_text()); end
|
||||||
template = repr(table.concat(lines, "\\n"));
|
template = repr(table.concat(lines, "\\n"));
|
||||||
else
|
else
|
||||||
template = repr(tostring(\%longhand.source:get_text()));
|
template = repr(tostring(\%longhand.source:get_text()));
|
||||||
@ -117,7 +117,7 @@ immediately:
|
|||||||
lua> ".."
|
lua> ".."
|
||||||
local lua = \%tree:as_lua(nomsu);
|
local lua = \%tree:as_lua(nomsu);
|
||||||
lua:convert_to_statements();
|
lua:convert_to_statements();
|
||||||
lua:declare_locals();
|
--lua:declare_locals();
|
||||||
return lua;
|
return lua;
|
||||||
|
|
||||||
action [%tree as value]:
|
action [%tree as value]:
|
||||||
@ -127,7 +127,7 @@ immediately:
|
|||||||
=lua "nomsu:tree_to_stub(\%tree)"
|
=lua "nomsu:tree_to_stub(\%tree)"
|
||||||
|
|
||||||
immediately:
|
immediately:
|
||||||
parse [%var write code %code] as: lua> "\%var:append(\%code);"
|
parse [%var <-write %code] as: lua> "\%var:append(\%code);"
|
||||||
|
|
||||||
immediately:
|
immediately:
|
||||||
compile [%tree's source code, %tree' source code] to: Lua value "\(%tree as lua expr).source:get_text()"
|
compile [%tree's source code, %tree' source code] to: Lua value "\(%tree as lua expr).source:get_text()"
|
||||||
|
@ -52,11 +52,12 @@ immediately:
|
|||||||
lua> "local \%value_lua = nomsu:tree_to_lua(\%value);"
|
lua> "local \%value_lua = nomsu:tree_to_lua(\%value);"
|
||||||
assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)"
|
assume %value_lua.is_value or barf "Invalid value for assignment: \(%value's source code)"
|
||||||
lua> ".."
|
lua> ".."
|
||||||
local \%lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';');
|
local lua = Lua(tree.source, \%var_lua, ' = ', \%value_lua, ';');
|
||||||
if \%var.type == 'Var' then
|
if \%var.type == 'Var' then
|
||||||
\%lua:add_free_vars(nomsu:var_to_lua_identifier(\%var.value));
|
print("Added var from assignment: "..tostring(\%var:as_lua(nomsu)));
|
||||||
|
lua:add_free_vars(\%var);
|
||||||
end
|
end
|
||||||
return \%lua;
|
return lua;
|
||||||
|
|
||||||
immediately:
|
immediately:
|
||||||
# Simultaneous mutli-assignments like: x,y,z = 1,x,3;
|
# Simultaneous mutli-assignments like: x,y,z = 1,x,3;
|
||||||
@ -72,7 +73,7 @@ immediately:
|
|||||||
local value_lua = nomsu:tree_to_lua(value);
|
local value_lua = nomsu:tree_to_lua(value);
|
||||||
if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end
|
if not value_lua.is_value then error("Invalid value for assignment: "..value:get_src()); end
|
||||||
if target.type == "Var" then
|
if target.type == "Var" then
|
||||||
lhs:add_free_vars(nomsu:var_to_lua_identifier(target.value));
|
lhs:add_free_vars(target);
|
||||||
end
|
end
|
||||||
if i > 1 then
|
if i > 1 then
|
||||||
lhs:append(", ");
|
lhs:append(", ");
|
||||||
|
80
lua_obj.lua
80
lua_obj.lua
@ -126,7 +126,7 @@ do
|
|||||||
sub = function(self, start, stop)
|
sub = function(self, start, stop)
|
||||||
local str = tostring(self):sub(start, stop)
|
local str = tostring(self):sub(start, stop)
|
||||||
local cls = self.__class
|
local cls = self.__class
|
||||||
return cls(self.source:sub(start - self.source.start + 1, stop - self.source.stop + 1), str)
|
return cls(self.source:sub(start - self.source.start + 1, stop - self.source.start + 1), str)
|
||||||
end,
|
end,
|
||||||
append = function(self, ...)
|
append = function(self, ...)
|
||||||
local n = select("#", ...)
|
local n = select("#", ...)
|
||||||
@ -195,6 +195,11 @@ do
|
|||||||
end
|
end
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
local var = select(i, ...)
|
local var = select(i, ...)
|
||||||
|
if type(var) == 'userdata' and var.type == "Var" then
|
||||||
|
var = tostring(var:as_lua())
|
||||||
|
elseif type(var) ~= 'string' then
|
||||||
|
var = tostring(var)
|
||||||
|
end
|
||||||
if not (seen[var]) then
|
if not (seen[var]) then
|
||||||
self.free_vars[#self.free_vars + 1] = var
|
self.free_vars[#self.free_vars + 1] = var
|
||||||
seen[var] = true
|
seen[var] = true
|
||||||
@ -235,21 +240,40 @@ do
|
|||||||
skip = _tbl_0
|
skip = _tbl_0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #self.free_vars > 0 then
|
local to_declare = { }
|
||||||
self:prepend("local " .. tostring(concat(self.free_vars, ", ")) .. ";\n")
|
local walk
|
||||||
end
|
walk = function(self)
|
||||||
local _list_0 = self.free_vars
|
local _list_0 = self.free_vars
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_0 do
|
||||||
local var = _list_0[_index_0]
|
local var = _list_0[_index_0]
|
||||||
skip[var] = true
|
if not (skip[var]) then
|
||||||
end
|
skip[var] = true
|
||||||
local _list_1 = self.bits
|
to_declare[#to_declare + 1] = var
|
||||||
for _index_0 = 1, #_list_1 do
|
end
|
||||||
local bit = _list_1[_index_0]
|
end
|
||||||
if type(bit) == Lua then
|
local _list_1 = self.bits
|
||||||
bit:declare_locals(skip)
|
for _index_0 = 1, #_list_1 do
|
||||||
|
local bit = _list_1[_index_0]
|
||||||
|
if bit.__class == Lua then
|
||||||
|
walk(bit)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if #self.free_vars > 0 then
|
||||||
|
self.free_vars = { }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
walk(self)
|
||||||
|
if #to_declare > 0 then
|
||||||
|
self:prepend("local " .. tostring(concat(to_declare, ", ")) .. ";\n")
|
||||||
|
end
|
||||||
|
return [[ if #@free_vars > 0
|
||||||
|
@prepend "local #{concat @free_vars, ", "};\n"
|
||||||
|
for var in *@free_vars do skip[var] = true
|
||||||
|
@free_vars = {}
|
||||||
|
for bit in *@bits
|
||||||
|
if bit.__class == Lua
|
||||||
|
bit\declare_locals(skip)
|
||||||
|
]]
|
||||||
end,
|
end,
|
||||||
__tostring = function(self)
|
__tostring = function(self)
|
||||||
local buff = { }
|
local buff = { }
|
||||||
@ -303,25 +327,23 @@ do
|
|||||||
lua_to_nomsu = { },
|
lua_to_nomsu = { },
|
||||||
nomsu_to_lua = { }
|
nomsu_to_lua = { }
|
||||||
}
|
}
|
||||||
local lua_offset = 1
|
|
||||||
local walk
|
local walk
|
||||||
walk = function(lua)
|
walk = function(lua, output_range)
|
||||||
if type(lua) == 'string' then
|
local pos = 1
|
||||||
lua_offset = lua_offset + #lua
|
local _list_0 = lua.bits
|
||||||
else
|
for _index_0 = 1, #_list_0 do
|
||||||
local lua_start = lua_offset
|
local b = _list_0[_index_0]
|
||||||
local _list_0 = lua.bits
|
if type(b) == 'string' then
|
||||||
for _index_0 = 1, #_list_0 do
|
local output = output_range:sub(pos, pos + #b)
|
||||||
local b = _list_0[_index_0]
|
metadata.lua_to_nomsu[output] = lua.source
|
||||||
walk(b)
|
metadata.nomsu_to_lua[lua.source] = output
|
||||||
|
else
|
||||||
|
walk(b, output_range:sub(pos, pos + #b))
|
||||||
end
|
end
|
||||||
local lua_stop = lua_offset
|
pos = pos + #b
|
||||||
local nomsu_src, lua_src = lua.source, Source(lua_chunkname, lua_start, lua_stop)
|
|
||||||
metadata.lua_to_nomsu[lua_src] = nomsu_src
|
|
||||||
metadata.nomsu_to_lua[nomsu_src] = lua_src
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
walk(self)
|
walk(self, Source(lua_chunkname, 1, #lua_str))
|
||||||
return lua_str, metadata
|
return lua_str, metadata
|
||||||
end,
|
end,
|
||||||
parenthesize = function(self)
|
parenthesize = function(self)
|
||||||
|
52
lua_obj.moon
52
lua_obj.moon
@ -94,7 +94,7 @@ class Code
|
|||||||
sub: (start,stop)=>
|
sub: (start,stop)=>
|
||||||
str = tostring(self)\sub(start,stop)
|
str = tostring(self)\sub(start,stop)
|
||||||
cls = @__class
|
cls = @__class
|
||||||
return cls(@source\sub(start-@source.start+1,stop-@source.stop+1), str)
|
return cls(@source\sub(start-@source.start+1,stop-@source.start+1), str)
|
||||||
|
|
||||||
append: (...)=>
|
append: (...)=>
|
||||||
n = select("#",...)
|
n = select("#",...)
|
||||||
@ -125,6 +125,10 @@ class Lua extends Code
|
|||||||
seen = {[v]:true for v in *@free_vars}
|
seen = {[v]:true for v in *@free_vars}
|
||||||
for i=1,select("#",...)
|
for i=1,select("#",...)
|
||||||
var = select(i, ...)
|
var = select(i, ...)
|
||||||
|
if type(var) == 'userdata' and var.type == "Var"
|
||||||
|
var = tostring(var\as_lua!)
|
||||||
|
elseif type(var) != 'string'
|
||||||
|
var = tostring(var)
|
||||||
unless seen[var]
|
unless seen[var]
|
||||||
@free_vars[#@free_vars+1] = var
|
@free_vars[#@free_vars+1] = var
|
||||||
seen[var] = true
|
seen[var] = true
|
||||||
@ -140,12 +144,29 @@ class Lua extends Code
|
|||||||
declare_locals: (skip={})=>
|
declare_locals: (skip={})=>
|
||||||
if next(skip) == 1
|
if next(skip) == 1
|
||||||
skip = {[s]:true for s in *skip}
|
skip = {[s]:true for s in *skip}
|
||||||
|
to_declare = {}
|
||||||
|
walk = =>
|
||||||
|
for var in *@free_vars
|
||||||
|
unless skip[var]
|
||||||
|
skip[var] = true
|
||||||
|
to_declare[#to_declare+1] = var
|
||||||
|
for bit in *@bits
|
||||||
|
if bit.__class == Lua
|
||||||
|
walk bit
|
||||||
|
if #@free_vars > 0
|
||||||
|
@free_vars = {}
|
||||||
|
walk self
|
||||||
|
if #to_declare > 0
|
||||||
|
@prepend "local #{concat to_declare, ", "};\n"
|
||||||
|
[[
|
||||||
if #@free_vars > 0
|
if #@free_vars > 0
|
||||||
@prepend "local #{concat @free_vars, ", "};\n"
|
@prepend "local #{concat @free_vars, ", "};\n"
|
||||||
for var in *@free_vars do skip[var] = true
|
for var in *@free_vars do skip[var] = true
|
||||||
|
@free_vars = {}
|
||||||
for bit in *@bits
|
for bit in *@bits
|
||||||
if type(bit) == Lua
|
if bit.__class == Lua
|
||||||
bit\declare_locals(skip)
|
bit\declare_locals(skip)
|
||||||
|
]]
|
||||||
|
|
||||||
__tostring: =>
|
__tostring: =>
|
||||||
buff = {}
|
buff = {}
|
||||||
@ -191,19 +212,18 @@ class Lua extends Code
|
|||||||
lua_filename:lua_chunkname, lua_file:lua_str
|
lua_filename:lua_chunkname, lua_file:lua_str
|
||||||
lua_to_nomsu: {}, nomsu_to_lua: {}
|
lua_to_nomsu: {}, nomsu_to_lua: {}
|
||||||
}
|
}
|
||||||
lua_offset = 1
|
walk = (lua, output_range)->
|
||||||
walk = (lua)->
|
pos = 1
|
||||||
if type(lua) == 'string'
|
for b in *lua.bits
|
||||||
lua_offset += #lua
|
if type(b) == 'string'
|
||||||
else
|
output = output_range\sub(pos, pos+#b)
|
||||||
lua_start = lua_offset
|
metadata.lua_to_nomsu[output] = lua.source
|
||||||
for b in *lua.bits
|
metadata.nomsu_to_lua[lua.source] = output
|
||||||
walk b
|
else
|
||||||
lua_stop = lua_offset
|
walk b, output_range\sub(pos, pos+#b)
|
||||||
nomsu_src, lua_src = lua.source, Source(lua_chunkname, lua_start, lua_stop)
|
pos += #b
|
||||||
metadata.lua_to_nomsu[lua_src] = nomsu_src
|
|
||||||
metadata.nomsu_to_lua[nomsu_src] = lua_src
|
walk self, Source(lua_chunkname, 1, #lua_str)
|
||||||
walk self
|
|
||||||
return lua_str, metadata
|
return lua_str, metadata
|
||||||
|
|
||||||
parenthesize: =>
|
parenthesize: =>
|
||||||
|
37
nomsu.lua
37
nomsu.lua
@ -372,7 +372,7 @@ do
|
|||||||
end
|
end
|
||||||
if filename:match(".*%.lua") then
|
if filename:match(".*%.lua") then
|
||||||
local file = assert(FILE_CACHE[filename], "Could not find file: " .. tostring(filename))
|
local file = assert(FILE_CACHE[filename], "Could not find file: " .. tostring(filename))
|
||||||
return self:run_lua(file, filename)
|
return self:run_lua(file)
|
||||||
end
|
end
|
||||||
if filename:match(".*%.nom") then
|
if filename:match(".*%.nom") then
|
||||||
if not self.skip_precompiled then
|
if not self.skip_precompiled then
|
||||||
@ -443,7 +443,7 @@ do
|
|||||||
return tree.value[1]
|
return tree.value[1]
|
||||||
end
|
end
|
||||||
local lua = Lua(tree.source, "return ", self:tree_to_lua(tree), ";")
|
local lua = Lua(tree.source, "return ", self:tree_to_lua(tree), ";")
|
||||||
return self:run_lua(lua, tree.source.filename)
|
return self:run_lua(lua)
|
||||||
end,
|
end,
|
||||||
tree_to_nomsu = function(self, tree, indentation, max_line, expr_type)
|
tree_to_nomsu = function(self, tree, indentation, max_line, expr_type)
|
||||||
if indentation == nil then
|
if indentation == nil then
|
||||||
@ -940,7 +940,7 @@ do
|
|||||||
tree_with_replaced_vars = function(self, tree, replacements)
|
tree_with_replaced_vars = function(self, tree, replacements)
|
||||||
return self:tree_map(tree, function(t)
|
return self:tree_map(tree, function(t)
|
||||||
if t.type == "Var" then
|
if t.type == "Var" then
|
||||||
local id = self:var_to_lua_identifier(t.value)
|
local id = tostring(t:as_lua(self))
|
||||||
if replacements[id] ~= nil then
|
if replacements[id] ~= nil then
|
||||||
return replacements[id]
|
return replacements[id]
|
||||||
end
|
end
|
||||||
@ -1088,7 +1088,7 @@ do
|
|||||||
end)
|
end)
|
||||||
self:define_compile_action("lua> %code", get_line_no(), function(self, _code)
|
self:define_compile_action("lua> %code", get_line_no(), function(self, _code)
|
||||||
if _code.type ~= "Text" then
|
if _code.type ~= "Text" then
|
||||||
return Lua.Value(self.source, "nomsu:run_lua(", nomsu:tree_to_lua(_code), ")")
|
return Lua.Value(self.source, "nomsu:run_lua(Lua(", repr(_code.source), ", ", repr(tostring(nomsu:tree_to_lua(_code))), "))")
|
||||||
end
|
end
|
||||||
local lua = Lua(_code.source)
|
local lua = Lua(_code.source)
|
||||||
local _list_0 = _code.value
|
local _list_0 = _code.value
|
||||||
@ -1109,7 +1109,7 @@ do
|
|||||||
end)
|
end)
|
||||||
self:define_compile_action("=lua %code", get_line_no(), function(self, _code)
|
self:define_compile_action("=lua %code", get_line_no(), function(self, _code)
|
||||||
if _code.type ~= "Text" then
|
if _code.type ~= "Text" then
|
||||||
return Lua.Value(self.source, "nomsu:run_lua(", nomsu:tree_to_lua(_code), ")")
|
return Lua.Value(self.source, "nomsu:run_lua(Lua(", repr(_code.source), ", ", repr(tostring(nomsu:tree_to_lua(_code))), "))")
|
||||||
end
|
end
|
||||||
local lua = Lua.Value(self.source)
|
local lua = Lua.Value(self.source)
|
||||||
local _list_0 = _code.value
|
local _list_0 = _code.value
|
||||||
@ -1303,7 +1303,7 @@ if arg and debug_getinfo(2).func ~= require then
|
|||||||
return line_table
|
return line_table
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
debug.getinfoXXXXXX = function(thread, f, what)
|
debug.getinfo = function(thread, f, what)
|
||||||
if what == nil then
|
if what == nil then
|
||||||
f, what, thread = thread, f, nil
|
f, what, thread = thread, f, nil
|
||||||
end
|
end
|
||||||
@ -1324,21 +1324,16 @@ if arg and debug_getinfo(2).func ~= require then
|
|||||||
local metadata = nomsu.action_metadata[info.func]
|
local metadata = nomsu.action_metadata[info.func]
|
||||||
if metadata then
|
if metadata then
|
||||||
info.name = metadata.aliases[1]
|
info.name = metadata.aliases[1]
|
||||||
local filename
|
local _ = [=[ filename = if type(metadata.source) == 'string'
|
||||||
if type(metadata.source) == 'string' then
|
metadata.source\match("^[^[:]*")
|
||||||
filename = metadata.source:match("^[^[:]*")
|
else metadata.source.filename
|
||||||
else
|
info.short_src = filename
|
||||||
filename = metadata.source.filename
|
info.source = FILE_CACHE[filename]
|
||||||
end
|
ok, linedefined = pcall(lua_line_to_nomsu_line, info.short_src, info.linedefined)
|
||||||
info.short_src = filename
|
if ok then info.linedefined = linedefined
|
||||||
info.source = FILE_CACHE[filename]
|
ok, currentline = pcall(lua_line_to_nomsu_line, info.short_src, info.currentline)
|
||||||
local linedefined
|
--if ok then info.currentline = currentline
|
||||||
ok, linedefined = pcall(lua_line_to_nomsu_line, info.short_src, info.linedefined)
|
]=]
|
||||||
if ok then
|
|
||||||
info.linedefined = linedefined
|
|
||||||
end
|
|
||||||
local currentline
|
|
||||||
ok, currentline = pcall(lua_line_to_nomsu_line, info.short_src, info.currentline)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
16
nomsu.moon
16
nomsu.moon
@ -333,7 +333,7 @@ class NomsuCompiler
|
|||||||
|
|
||||||
if filename\match(".*%.lua")
|
if filename\match(".*%.lua")
|
||||||
file = assert(FILE_CACHE[filename], "Could not find file: #{filename}")
|
file = assert(FILE_CACHE[filename], "Could not find file: #{filename}")
|
||||||
return @run_lua(file, filename)
|
return @run_lua(file)
|
||||||
if filename\match(".*%.nom")
|
if filename\match(".*%.nom")
|
||||||
if not @skip_precompiled -- Look for precompiled version
|
if not @skip_precompiled -- Look for precompiled version
|
||||||
lua_filename = filename\gsub("%.nom$", ".lua")
|
lua_filename = filename\gsub("%.nom$", ".lua")
|
||||||
@ -383,7 +383,7 @@ class NomsuCompiler
|
|||||||
if tree.type == 'Text' and #tree.value == 1 and type(tree.value[1]) == 'string'
|
if tree.type == 'Text' and #tree.value == 1 and type(tree.value[1]) == 'string'
|
||||||
return tree.value[1]
|
return tree.value[1]
|
||||||
lua = Lua(tree.source, "return ",@tree_to_lua(tree),";")
|
lua = Lua(tree.source, "return ",@tree_to_lua(tree),";")
|
||||||
return @run_lua(lua, tree.source.filename)
|
return @run_lua(lua)
|
||||||
|
|
||||||
tree_to_nomsu: (tree, indentation="", max_line=80, expr_type=nil)=>
|
tree_to_nomsu: (tree, indentation="", max_line=80, expr_type=nil)=>
|
||||||
-- Convert a tree into nomsu code that satisfies the max line requirement or nil
|
-- Convert a tree into nomsu code that satisfies the max line requirement or nil
|
||||||
@ -734,7 +734,7 @@ class NomsuCompiler
|
|||||||
tree_with_replaced_vars: (tree, replacements)=>
|
tree_with_replaced_vars: (tree, replacements)=>
|
||||||
return @tree_map tree, (t)->
|
return @tree_map tree, (t)->
|
||||||
if t.type == "Var"
|
if t.type == "Var"
|
||||||
id = @var_to_lua_identifier t.value
|
id = tostring(t\as_lua(self))
|
||||||
if replacements[id] != nil
|
if replacements[id] != nil
|
||||||
return replacements[id]
|
return replacements[id]
|
||||||
|
|
||||||
@ -836,7 +836,8 @@ class NomsuCompiler
|
|||||||
|
|
||||||
@define_compile_action "lua> %code", get_line_no!, (_code)=>
|
@define_compile_action "lua> %code", get_line_no!, (_code)=>
|
||||||
if _code.type != "Text"
|
if _code.type != "Text"
|
||||||
return Lua.Value(@source, "nomsu:run_lua(",nomsu\tree_to_lua(_code),")")
|
return Lua.Value @source, "nomsu:run_lua(Lua(",repr(_code.source),
|
||||||
|
", ",repr(tostring(nomsu\tree_to_lua(_code))),"))"
|
||||||
|
|
||||||
lua = Lua(_code.source)
|
lua = Lua(_code.source)
|
||||||
for bit in *_code.value
|
for bit in *_code.value
|
||||||
@ -852,7 +853,8 @@ class NomsuCompiler
|
|||||||
|
|
||||||
@define_compile_action "=lua %code", get_line_no!, (_code)=>
|
@define_compile_action "=lua %code", get_line_no!, (_code)=>
|
||||||
if _code.type != "Text"
|
if _code.type != "Text"
|
||||||
return Lua.Value(@source, "nomsu:run_lua(",nomsu\tree_to_lua(_code),")")
|
return Lua.Value @source, "nomsu:run_lua(Lua(",repr(_code.source),
|
||||||
|
", ",repr(tostring(nomsu\tree_to_lua(_code))),"))"
|
||||||
|
|
||||||
lua = Lua.Value(@source)
|
lua = Lua.Value(@source)
|
||||||
for bit in *_code.value
|
for bit in *_code.value
|
||||||
@ -906,7 +908,7 @@ if arg and debug_getinfo(2).func != require
|
|||||||
return line_table
|
return line_table
|
||||||
}
|
}
|
||||||
|
|
||||||
debug.getinfoXXXXXX = (thread,f,what)->
|
debug.getinfo = (thread,f,what)->
|
||||||
if what == nil
|
if what == nil
|
||||||
f,what,thread = thread,f,nil
|
f,what,thread = thread,f,nil
|
||||||
if type(f) == 'number' then f += 1 -- Account for this wrapper function
|
if type(f) == 'number' then f += 1 -- Account for this wrapper function
|
||||||
@ -917,6 +919,7 @@ if arg and debug_getinfo(2).func != require
|
|||||||
if info.short_src or info.source or info.linedefine or info.currentline
|
if info.short_src or info.source or info.linedefine or info.currentline
|
||||||
if metadata = nomsu.action_metadata[info.func]
|
if metadata = nomsu.action_metadata[info.func]
|
||||||
info.name = metadata.aliases[1]
|
info.name = metadata.aliases[1]
|
||||||
|
[=[
|
||||||
filename = if type(metadata.source) == 'string'
|
filename = if type(metadata.source) == 'string'
|
||||||
metadata.source\match("^[^[:]*")
|
metadata.source\match("^[^[:]*")
|
||||||
else metadata.source.filename
|
else metadata.source.filename
|
||||||
@ -926,6 +929,7 @@ if arg and debug_getinfo(2).func != require
|
|||||||
if ok then info.linedefined = linedefined
|
if ok then info.linedefined = linedefined
|
||||||
ok, currentline = pcall(lua_line_to_nomsu_line, info.short_src, info.currentline)
|
ok, currentline = pcall(lua_line_to_nomsu_line, info.short_src, info.currentline)
|
||||||
--if ok then info.currentline = currentline
|
--if ok then info.currentline = currentline
|
||||||
|
]=]
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ Tree "File",
|
|||||||
as_lua: (nomsu)=>
|
as_lua: (nomsu)=>
|
||||||
if #@value == 1
|
if #@value == 1
|
||||||
return @value[1]\as_lua(nomsu)
|
return @value[1]\as_lua(nomsu)
|
||||||
declared_locals = {}
|
|
||||||
lua = Lua(@source)
|
lua = Lua(@source)
|
||||||
for i, line in ipairs @value
|
for i, line in ipairs @value
|
||||||
line_lua = line\as_lua(nomsu)
|
line_lua = line\as_lua(nomsu)
|
||||||
|
Loading…
Reference in New Issue
Block a user