aboutsummaryrefslogtreecommitdiff
path: root/nomsu.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-04-08 15:41:05 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-04-08 15:41:55 -0700
commitfd621a1062c12b808efd7f8adf1957e0fe978ecc (patch)
tree6e19a251c58c1187a128989f983073bb214052db /nomsu.moon
parenta49e97f0e3a8b69afbc375fac7e04fe49aaf0591 (diff)
Cleaned up LHS of "." operator to avoid adding unnecessary parens so
Lua's parser doesn't get confused by (x).y = 1.
Diffstat (limited to 'nomsu.moon')
-rwxr-xr-xnomsu.moon16
1 files changed, 13 insertions, 3 deletions
diff --git a/nomsu.moon b/nomsu.moon
index 12a4da7..8e8af08 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -557,7 +557,7 @@ class NomsuCompiler
else
nomsu = inline_expression(bit)
return nil unless nomsu
- buff ..= if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" or bit.type == "IndexChain"
+ buff ..= if bit.type == "Var" or bit.type == "List" or bit.type == "Dict"
"\\"..nomsu
else "\\("..nomsu..")"
return buff
@@ -796,9 +796,18 @@ class NomsuCompiler
error "#{line}: Cannot index #{colored.yellow src}, since it's not an expression.", 0
-- TODO: improve generated code by removing parens and square brackets when possible
if i == 1
- insert items, "(#{lua.expr})"
+ if lua.expr\sub(-1,-1) == "}" or lua.expr\sub(-1,-1) == '"'
+ insert items, "(#{lua.expr})"
+ else
+ insert items, lua.expr
else
- insert items, "[ #{lua.expr}]"
+ -- NOTE: this *must* use a space after the [ to avoid freaking out
+ -- Lua's parser if the inner expression is a long string. Lua
+ -- parses x[[[y]]] as x("[y]"), not as x["y"]
+ if item.type == 'Text' and #item.value == 1 and type(item.value[1]) == 'string' and item.value[1]\match("^[a-zA-Z_][a-zA-Z0-9_]$")
+ insert items, ".#{item.value[1]}"
+ else
+ insert items, "[ #{lua.expr}]"
return expr:concat(items,"")
when "List"
@@ -1169,6 +1178,7 @@ if arg and debug.getinfo(2).func != require
-- Note: xpcall has a slightly different API in Lua <=5.1 vs. >=5.2, but this works
-- for both APIs
+ -- TODO: revert back to old error handler
ldt = require 'ldt'
ldt.guard run
--xpcall(run, err_hand)