~technomancy/antifennel

83e85cbaaafa2f68194c8016894b209262365c34 — Phil Hagelberg 5 months ago 2759c1f
Fix local function definitions that set existing locals.

Previously anything that wasn't defining a local function or table
function was misclassified as a global function.
3 files changed, 23 insertions(+), 9 deletions(-)

M anticompiler.fnl
M test.lua
M test_expected.fnl
M anticompiler.fnl => anticompiler.fnl +15 -9
@@ 43,12 43,22 @@
    (list (sym :fn) (sequence (unpack params))
          (unpack (map body (partial compile subscope) true)))))

(fn varize-local! [scope name]
  (match (. scope name)
    {: ast} (tset ast 1 :var)))

(fn declare-function [compile scope ast]
  (let [target (if (or ast.locald (= :MemberExpression ast.id.kind))
                   (compile scope ast.id)
                   (sym (.. "_G." ast.id.name)))]
    (doto (function compile scope ast)
      (table.insert 2 target))))
  (if (or ast.locald (= :MemberExpression ast.id.kind))
      (doto (function compile scope ast)
        (table.insert 2 (compile scope ast.id)))
      (not (. scope ast.id.name))
      (doto (function compile scope ast)
        (table.insert 2 (sym (.. "_G." ast.id.name))))
      (do
        (varize-local! scope ast.id.name)
        (list (sym :set)
              (compile scope ast.id)
              (function compile scope ast)))))

(fn identifier [ast]
  (if (and (ast.name:find "^[-_0-9]+$") (ast.name:find "[0-9]"))


@@ 235,10 245,6 @@
            (compile scope (. left 1 :property)))
        right-out))

(fn varize-local! [scope name]
  (match (. scope name)
    {: ast} (tset ast 1 :var)))

(fn setter-for [scope names]
  (let [kinds (map names #(match (or (. scope $) $) {: kind} kind _ :global))
        kinds (doto (distinct kinds) table.sort)]

M test.lua => test.lua +4 -0
@@ 36,6 36,10 @@ end

f = 59

local determination

function determination(hopes) return hopes+_G.dreams end

do
   -- here
   local boo, twenty = "hoo", 20

M test_expected.fnl => test_expected.fnl +4 -0
@@ 41,6 41,10 @@

(set-forcibly! f 59)

(var determination nil)

(set determination (fn [hopes] (+ hopes _G.dreams)))

(let [;; here
      (boo twenty) (values :hoo 20)
      fifteen 15]