diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-08 16:06:31 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-08 16:06:39 -0700 |
| commit | 4e51768ebdf98c0da545c6598b3d6e9af6c04409 (patch) | |
| tree | 64161dec44a88cc719060d52d80579a237f04033 /nomsu.moon | |
| parent | 680006b25ac9d6f0c25b5a35374a33c6047b21c1 (diff) | |
Improved codegen code.
Diffstat (limited to 'nomsu.moon')
| -rwxr-xr-x | nomsu.moon | 15 |
1 files changed, 10 insertions, 5 deletions
@@ -794,20 +794,22 @@ class NomsuCompiler line = @get_line_number(item) src = @get_source_code(item) 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 if lua.expr\sub(-1,-1) == "}" or lua.expr\sub(-1,-1) == '"' insert items, "(#{lua.expr})" else insert items, lua.expr else - -- 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}]" + -- 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 lua.expr\sub(1,1) == '[' + insert items, "[ #{lua.expr}]" + else + insert items, "[#{lua.expr}]" return expr:concat(items,"") when "List" @@ -841,6 +843,9 @@ class NomsuCompiler if key_str insert items, "#{key_str}=#{value_lua.expr}" elseif key_lua.expr\sub(1,1) == "[" + -- 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"] insert items, "[ #{key_lua.expr}]=#{value_lua.expr}" else insert items, "[#{key_lua.expr}]=#{value_lua.expr}" |
