Skip to content

Instantly share code, notes, and snippets.

@maxov
Created May 16, 2015 08:14
Show Gist options
  • Save maxov/52a41db017254c356893 to your computer and use it in GitHub Desktop.
Save maxov/52a41db017254c356893 to your computer and use it in GitHub Desktop.
; the let macro
(set macros "let" (fn (tree)
(if (== (len tree) 0)
(err "let takes at least one arg")
(let
(expr (last tree))
(body (map (init tree) (fn (tree)
(cons (quote "def") tree))))
(form (push (cons (quote "do") body) expr))
(compileExpr form body)))))
; the cond macro
(set macros "cond" (fn (tree)
; TODO when I get the >= operator in here, use that
; also mod
(if (== (len tree) 0)
(err "cond takes at least two args")
(let
(lt (last tree))
(rest (slice tree 0 (- (len tree) 2)))
(grouped (group2 rest))
(form (foldR grouped (fn (acc con)
(list (quote "if") (head con) (at con 1) acc)) lt))
(compileExpr form)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment