From 56f014a4884d7df387ff2ffa34fa95f1cc8b1f16 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 13 Oct 2017 18:14:18 -0700 Subject: [PATCH] Improved code generation by changing vars["x"] to vars.x for allowed lua var names. --- nomsu.lua | 6 +++++- nomsu.moon | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/nomsu.lua b/nomsu.lua index 583843f..28bb36d 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -610,7 +610,11 @@ end)]]):format(concat(lua_bits, "\n")) elseif "Number" == _exp_0 then return repr(tree.value), nil elseif "Var" == _exp_0 then - return "vars[" .. tostring(repr(tree.value)) .. "]", nil + if tree.value:match("^[a-zA-Z_][a-zA-Z0-9_]*$") then + return "vars." .. tostring(tree.value), nil + else + return "vars[" .. tostring(repr(tree.value)) .. "]", nil + end else return self:error("Unknown/unimplemented thingy: " .. tostring(tree.type)) end diff --git a/nomsu.moon b/nomsu.moon index 435265f..cd80510 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -461,7 +461,10 @@ end)]])\format(concat(lua_bits, "\n")) return repr(tree.value), nil when "Var" - return "vars[#{repr tree.value}]", nil + if tree.value\match("^[a-zA-Z_][a-zA-Z0-9_]*$") + return "vars.#{tree.value}", nil + else + return "vars[#{repr tree.value}]", nil else @error("Unknown/unimplemented thingy: #{tree.type}")