Skip to content
Andrew Kravchuk edited this page Aug 6, 2019 · 6 revisions

Like Urn, Fennel is a lisp which compiles to Lua and is often used together with the LÖVE framework. It is different from Urn in that it sticks much closer to Lua's semantics; all differences are at compile time, and no runtime overhead is introduced. This results in a dramatically simpler compiler with readable output. In addition, it is much more friendly to interactive development (working from a repl) compared to Urn, though Urn has more features.

Fennel also works natively with TIC-80, a more retro pixel-art-focused gamedev platform. You can see a sample Fennel TIC game where you can press ESC -> close game -> ESC to see the source.


The #fennel channel on Freenode is fairly active.


Example (LÖVE)

;; run `fennel --compile pong.fnl > main.lua; love .` to run this code
(local (speed ball-speed) (values 10 200))
(local state {:x 100 :y 100 :dx 2 :dy 1 :left 10 :right 10})
(local (w h) (love.window.getMode))

(local keys {:a [:left -1] :z [:left 1] :up [:right -1] :down [:right 1]})

(fn on-paddle? []
  (or (and (< state.x 20)
           (< state.left state.y) (< state.y (+ state.left 100)))
      (and (< (- w 20) state.x)
           (< state.right state.y) (< state.y (+ state.right 100)))))

(fn love.update [dt]
  (set state.x (+ state.x (* state.dx dt ball-speed)))
  (set state.y (+ state.y (* state.dy dt ball-speed)))
  (each [key action (pairs keys)]
    (let [[player dir] action]
      (when (love.keyboard.isDown key)
        (tset state player (+ (. state player) (* dir speed))))))

  (when (or (< state.y 0) (> state.y h))
    (set state.dy (- 0 state.dy)))

  (when (on-paddle?)
    (set state.dx (- 0 state.dx)))

  (when (< state.x 0)
    (print "Right player wins")
  (when (> state.x w)
    (print "Left player wins")

(fn love.keypressed [key]
  (when (= "escape" key) (love.event.quit)))

(fn love.draw []
  ( "fill" 10 state.left 10 100)
  ( "fill" (- w 10) state.right 10 100)
  ( "fill" state.x state.y 10))

For an example of TIC-80 see life.fnl

Clone this wiki locally