~technomancy/antifennel

393e16365b2fe09ba4ab8b0d775446a52d50f35f — Phil Hagelberg a month ago 16e8b2e
Clarify limitations; fix a couple bugs.
3 files changed, 26 insertions(+), 8 deletions(-)

M README.md
M anticompiler.fnl
M antifennel.lua
M README.md => README.md +13 -0
@@ 8,6 8,19 @@ Does the opposite of what the Fennel compiler does.

    $ luajit antifennel.lua targetfile.lua > targetfile.fnl

## Limitations

Very immature.

Currently it does very little validation and will almost certainly
emit Fennel which won't compile. It assumes all locals are vars even
if they are never modified.

Certain Lua constructs are not supported in Fennel such as `goto`,
`repeat`, `break`, and early `return`s.

Setting globals is not supported unless you use `_G.foo = bar` notation.

## Copyright

Copyright © 2020 Phil Hagelberg and Contributors

M anticompiler.fnl => anticompiler.fnl +8 -7
@@ 8,10 8,9 @@
      (table.insert out (f v)))
    out))

(local chunk-mt {1 "CHUNK"})

(fn p [x] (print (view x)))
(fn p [x] (print (view x))) ; debugging

(local chunk-mt ["CHUNK"]) ; not doing anything w this yet; maybe useful later?
(fn chunk [contents]
  (setmetatable contents chunk-mt))



@@ 54,7 53,7 @@
  (list (sym ":")
        (compile receiver)
        method.name
        (map arguments compile)))
        (unpack (map arguments compile))))

(fn member [compile {: object : property : computed}]
  (if computed


@@ 64,6 63,7 @@
(fn if* [compile {: tests : cons : alternate}]
  (if alternate
      (list (sym :if)
            (compile (. tests 1))
            (if (= 1 (# (. cons 1)))
                (compile (. cons 1 1))
                (list (sym :do) (unpack (map (. cons 1) compile))))


@@ 71,6 71,7 @@
                (compile (. alternate 1))
                (list (sym :do) (unpack (map alternate compile)))))
      (list (sym :when)
            (compile (. tests 1))
            (unpack (map (. cons 1) compile)))))

(fn concat [compile {: terms}]


@@ 108,10 109,10 @@

(fn table* [compile {: keyvals}]
  (let [out {}]
    (each [i [k v] (pairs keyvals)]
      (if v
    (each [i [v k] (pairs keyvals)]
      (if k
          (tset out (compile k) (compile v))
          (tset out i (compile k))))
          (tset out i (compile v))))
    out))

(fn do* [compile {: body}]

M antifennel.lua => antifennel.lua +5 -1
@@ 1,7 1,11 @@
local fennel = require('fennel')
local view = require('fennelview')

table.insert(package.loaders or package.searchers, fennel.searcher)
if os.getenv("FNL") then -- prefer Fennel to Lua when both exist
   table.insert(package.loaders or package.searchers, 1, fennel.searcher)
else
   table.insert(package.loaders or package.searchers, fennel.searcher)
end

local lex_setup = require('lang.lexer')
local parse = require('lang.parser')