aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-12-14 19:23:26 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-12-14 19:25:03 -0800
commit83a40b7493b19e4b55167784e073e92733344b87 (patch)
tree758bc8986c6985fad9aabfab5a713318ef180410
parenta7adc8cbff9347606fc9c0e8d00d4af2b6b838f5 (diff)
Updating to support multiple method calls.
-rw-r--r--core/metaprogramming.nom3
l---------ldt.lua1
-rw-r--r--nomsu.5.peg8
-rw-r--r--nomsu_compiler.lua35
-rw-r--r--nomsu_compiler.moon34
-rw-r--r--nomsu_decompiler.lua24
-rw-r--r--nomsu_decompiler.moon18
7 files changed, 81 insertions, 42 deletions
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index 77dbd24..9fcf26a 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -270,6 +270,9 @@ externally (%tree as lua expr) means:
..local tree_lua = compile(\%tree)
if \%tree.type == 'Block' then
tree_lua = LuaCode:from(\%tree.source, '(function()\\n ', tree_lua, '\\nend)()')
+ elseif \%tree.type == 'MethodCall' and #\%tree > 2 then
+ compile_error_at(\%tree, "This must be a single value instead of "..(#\%tree - 1).." method calls.",
+ "Replace this with a single method call.")
end
return tree_lua"
diff --git a/ldt.lua b/ldt.lua
new file mode 120000
index 0000000..b28ba32
--- /dev/null
+++ b/ldt.lua
@@ -0,0 +1 @@
+/Users/bruce/Sandbox/lua/ldt/ldt.lua \ No newline at end of file
diff --git a/nomsu.5.peg b/nomsu.5.peg
index 07c1973..e51f72d 100644
--- a/nomsu.5.peg
+++ b/nomsu.5.peg
@@ -122,7 +122,7 @@ inline_action (Action):
inline_arg: inline_expression / inline_block
inline_methodcall (MethodCall):
(index_chain / noindex_inline_expression / "(" inline_block ")")
- "|" inline_action
+ "|" inline_action (ws* ";" ws* inline_action)*
action (Action):
!section_division
@@ -132,7 +132,11 @@ linesplit: (ws* "\")? eol nl_nodent ".." ws*
arg: expression / inline_block / indented_block
methodcall (MethodCall):
(index_chain / noindex_inline_expression / indented_expression / "(" inline_block ")" / indented_block)
- linesplit? "|" linesplit? action
+ linesplit? "|"
+ ((ws* inline_action ws* ";")* ws* action
+ / eol nl_indent
+ (action eol) (nl_nodent action eol)*
+ (%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*)
word: !number { operator_char+ / ident_char+ }
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index d51fc1d..a7c4b9d 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -250,29 +250,32 @@ local compile = setmetatable({
lua:add(")")
return lua
elseif "MethodCall" == _exp_0 then
- local stub = tree[2].stub
local lua = LuaCode:from(tree.source)
local target_lua = compile(tree[1])
local target_text = target_lua:text()
- if target_text:match("^%(.*%)$") or target_text:match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or tree[1].type == "IndexChain" then
- lua:add(target_lua, ":")
- else
- lua:add("(", target_lua, "):")
+ if not (target_text:match("^%(.*%)$") or target_text:match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or tree[1].type == "IndexChain") then
+ target_lua:parenthesize()
end
- assert(tree[2].type == "Action")
- lua:add((stub):as_lua_id(), "(")
- for i, arg in ipairs(tree[2]:get_args()) do
- local arg_lua = compile(arg)
- if arg.type == "Block" then
- arg_lua = LuaCode:from(arg.source, "(function()\n ", arg_lua, "\nend)()")
+ for i = 2, #tree do
+ if i > 2 then
+ lua:add("\n")
end
- if i > 1 then
- lua:add(",")
+ lua:add(target_lua, ":")
+ lua:add((tree[i].stub):as_lua_id(), "(")
+ for argnum, arg in ipairs(tree[i]:get_args()) do
+ local arg_lua = compile(arg)
+ if arg.type == "Block" then
+ arg_lua = LuaCode:from(arg.source, "(function()\n ", arg_lua, "\nend)()")
+ end
+ if lua:trailing_line_len() + #arg_lua:text() > MAX_LINE then
+ lua:add(argnum > 1 and ",\n " or "\n ")
+ elseif argnum > 1 then
+ lua:add(", ")
+ end
+ lua:add(arg_lua)
end
- lua:add(lua:trailing_line_len() + #arg_lua:text() > MAX_LINE and "\n " or " ")
- lua:add(arg_lua)
+ lua:add(")")
end
- lua:add(")")
return lua
elseif "EscapedNomsu" == _exp_0 then
local lua = LuaCode:from(tree.source, "SyntaxTree{")
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 411878f..720fe03 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -185,28 +185,28 @@ compile = setmetatable({
return lua
when "MethodCall"
- stub = tree[2].stub
lua = LuaCode\from tree.source
target_lua = compile tree[1]
target_text = target_lua\text!
-- TODO: this parenthesizing is maybe overly conservative
- if target_text\match("^%(.*%)$") or target_text\match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or
- tree[1].type == "IndexChain"
- lua\add target_lua, ":"
- else
- lua\add "(", target_lua, "):"
+ if not (target_text\match("^%(.*%)$") or target_text\match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or
+ tree[1].type == "IndexChain")
+ target_lua\parenthesize!
- -- TODO: de-duplicate this code
- assert tree[2].type == "Action"
- lua\add((stub)\as_lua_id!,"(")
- for i, arg in ipairs tree[2]\get_args!
- arg_lua = compile(arg)
- if arg.type == "Block"
- arg_lua = LuaCode\from(arg.source, "(function()\n ", arg_lua, "\nend)()")
- lua\add "," if i > 1
- lua\add(lua\trailing_line_len! + #arg_lua\text! > MAX_LINE and "\n " or " ")
- lua\add arg_lua
- lua\add ")"
+ for i=2,#tree
+ lua\add "\n" if i > 2
+ lua\add target_lua, ":"
+ lua\add((tree[i].stub)\as_lua_id!,"(")
+ for argnum, arg in ipairs tree[i]\get_args!
+ arg_lua = compile(arg)
+ if arg.type == "Block"
+ arg_lua = LuaCode\from(arg.source, "(function()\n ", arg_lua, "\nend)()")
+ if lua\trailing_line_len! + #arg_lua\text! > MAX_LINE
+ lua\add(argnum > 1 and ",\n " or "\n ")
+ elseif argnum > 1
+ lua\add ", "
+ lua\add arg_lua
+ lua\add ")"
return lua
when "EscapedNomsu"
diff --git a/nomsu_decompiler.lua b/nomsu_decompiler.lua
index bfd78f9..0666e00 100644
--- a/nomsu_decompiler.lua
+++ b/nomsu_decompiler.lua
@@ -85,7 +85,14 @@ tree_to_inline_nomsu = function(tree)
end
return nomsu
elseif "MethodCall" == _exp_0 then
- return NomsuCode:from(tree.source, tree_to_inline_nomsu(tree[1]), "|", tree_to_inline_nomsu(tree[2]))
+ local nomsu = NomsuCode:from(tree.source, tree_to_inline_nomsu(tree[1]), "|")
+ for i = 2, #tree do
+ if i > 2 then
+ nomsu:add("; ")
+ end
+ nomsu:add(tree_to_inline_nomsu(tree[i]))
+ end
+ return nomsu
elseif "EscapedNomsu" == _exp_0 then
local inner_nomsu = tree_to_inline_nomsu(tree[1])
if not (tree[1].type == "List" or tree[1].type == "Dict" or tree[1].type == "Var") then
@@ -190,7 +197,7 @@ tree_to_inline_nomsu = function(tree)
end
return NomsuCode:from(tree.source, s)
elseif "Var" == _exp_0 then
- local varname = tree[1]:gsub("_", " ")
+ local varname = tree[1]
if varname == "" or is_identifier(varname) then
return NomsuCode:from(tree.source, "$", varname)
else
@@ -340,7 +347,18 @@ tree_to_nomsu = function(tree)
end
nomsu:add(target_nomsu)
nomsu:add(target_nomsu:is_multiline() and "\n..|" or "|")
- nomsu:add(tree_to_nomsu(tree[2]))
+ local inner_nomsu = NomsuCode()
+ for i = 2, #tree do
+ if i > 2 then
+ inner_nomsu:add("\n")
+ end
+ inner_nomsu:add(tree_to_nomsu(tree[i]))
+ end
+ if #tree == 2 and nomsu:trailing_line_len() + #inner_nomsu:text():match("^[^\n]*") < MAX_LINE then
+ nomsu:add(inner_nomsu)
+ else
+ nomsu:add("\n ", inner_nomsu)
+ end
return nomsu
elseif "EscapedNomsu" == _exp_0 then
nomsu = recurse(tree[1])
diff --git a/nomsu_decompiler.moon b/nomsu_decompiler.moon
index cb031e0..f522832 100644
--- a/nomsu_decompiler.moon
+++ b/nomsu_decompiler.moon
@@ -57,7 +57,11 @@ tree_to_inline_nomsu = (tree)->
return nomsu
when "MethodCall"
- return NomsuCode\from(tree.source, tree_to_inline_nomsu(tree[1]), "|", tree_to_inline_nomsu(tree[2]))
+ nomsu = NomsuCode\from(tree.source, tree_to_inline_nomsu(tree[1]), "|")
+ for i=2,#tree
+ nomsu\add "; " if i > 2
+ nomsu\add tree_to_inline_nomsu(tree[i])
+ return nomsu
when "EscapedNomsu"
inner_nomsu = tree_to_inline_nomsu(tree[1])
@@ -139,8 +143,7 @@ tree_to_inline_nomsu = (tree)->
return NomsuCode\from(tree.source, s)
when "Var"
- -- TODO: remove this hack:
- varname = tree[1]\gsub("_", " ")
+ varname = tree[1]
if varname == "" or is_identifier(varname)
return NomsuCode\from(tree.source, "$", varname)
else
@@ -265,7 +268,14 @@ tree_to_nomsu = (tree)->
target_nomsu\parenthesize!
nomsu\add target_nomsu
nomsu\add(target_nomsu\is_multiline! and "\n..|" or "|")
- nomsu\add tree_to_nomsu(tree[2])
+ inner_nomsu = NomsuCode!
+ for i=2,#tree
+ inner_nomsu\add "\n" if i > 2
+ inner_nomsu\add tree_to_nomsu(tree[i])
+ if #tree == 2 and nomsu\trailing_line_len! + #inner_nomsu\text!\match("^[^\n]*") < MAX_LINE
+ nomsu\add inner_nomsu
+ else
+ nomsu\add "\n ", inner_nomsu
return nomsu
when "EscapedNomsu"