LemonCake

A programming language that combines the speed of Ruby, with the flexibility of x86-64 Assembly, and the consistency of Lisp.

Operators

(= + (~ (reduce add $args))) (= - (~ (reduce sub $args))) (= * (~ (reduce mul $args))) (= / (~ (reduce div $args))) (= % (~ (reduce mod $args))) (= | (~ (reduce binor $args))) (= & (~ (reduce binand $args))) (= join (~ (reduce join-lists $args))) (= < (~ (reduce lt $args))) (= <= (~ (reduce lte $args))) (= > (~ (reduce gt $args))) (= >= (~ (reduce gte $args)))

Builtin functions

min

Returns the smallest element.

Example:

> (min 5 8 3)
3
(= min (~ (reduce (~ (a b) (if (< a b) a b)) $args)))

max

Returns the maximum element.

Example:

> (max 5 8 3)
8
(= max (~ (reduce (~ (a b) (if (> a b) a b)) $args)))

identity

The identity function. It returns what it receives.

Example:

> (identity 'something-here')
'something-here'
(= identity (~ (x) x))

partial

Creates a partial function.

Example:

> (= f (partial + 1 2))
> (f 4 1)
8

Which is the same as running:

> (+ 1 2 4 1)
8
(= partial (~ (fn) (last (= args (tail $args)) (~ (apply fn (join args $args))) )))

tie

Joins strings using a separator.

Example:

> (tie '_' :('a' 'b' 'c'))
'a_b_c'
(= tie (~ (str xs) (apply cat (tail (apply join (map xs (~ (x) (list str x)) )))) ))

repeat

Creates a list contaning `n` repetitions of a given value.

Example:

> (repeat 'a' 3)
('a' 'a' 'a')
(= repeat (~ (x n) (map (range n) (~ x))))

at

Get the value from a list that's at zero-based index `i`.

Example:

> (at :(1 2 3) 2)
3
(= at (~ (l i) (head ((apply comp (repeat tail i)) l))))

comp

Generates a function that composes multiple functions that only accept one argument.

So given functions `f`, `g`, `h`, these two lines are equivalent:

(~ (x) (f (g (h x))))
(comp h g f)

Note that the functions are applied in the order in which they are given.

TODO Fix the order.

Example:

> (= plus1 (~ (x) (+ x 1)))
> (= double (~ (x) (list x x)))
> (= fn (comp plus1 double double))
> (fn 2)
((3 3) (3 3))
(= comp (~ (eval (list :~ :(x) (join :(last) (map $args (~ (fn) (list := :x (list fn :x)) )) :(x) )) )))

list-dict

(= list-dict (~ (xs) (last (= ret (dict)) (map xs (~ (x i) (set ret i x))) ret )))

zip

Pairs elements of lists together.

Example:

> (= names :('John' 'Bill' 'Alice'))
('John' 'Bill' 'Alice')
> (= ages :(31 21 27))
(31 21 27)
> (zip names ages)
(('John' 31) ('Bill' 21) ('Alice' 27))

Tests:

zip-001.in
(zip :(1 2) :(a b))

zip-001.out
((1 a) (2 b))

zip-002.in
(zip :(1 2) :(a b) :(A B))

zip-002.out
((1 a A) (2 b B))

zip-003.in
(zip :(1 2 c) :(a b) :(A B))

zip-003.out
((1 a A) (2 b B))

zip-004.in
(zip :(1 2 c) :(a b c) :(A B))

zip-004.out
((1 a A) (2 b B))

zip-005.in
(zip :(1 2 c) :(a b c) :(A B c))

zip-005.out
((1 a A) (2 b B) (c c c))
(= zip (~ (last (= lists (map $args list-dict)) (= n-args (len lists)) (= min-len (apply min (map lists len))) (map (range min-len) (~ (i) (map lists (~ (d) (get d i)) ))) )))

Util functions

doc-code

(= doc-code (~ (input output) (last (= encode (~ (d) (replace (replace d '>' '>') '<' '<'))) (= surround (~ (tag line) (cat '<' tag '>' (encode (substr line 2)) '</' tag '>') )) (= clean (~ (l) (if (startswith l '# ') (substr l 2) (substr l 1) ))) (= process-comment-line (~ (line) (last (if (is (len line) 0) '<p>' # If it starts with 4 spaces, sorround the line with a pre tag. (startswith line ' ') (surround 'pre' (substr line 2)) # If it starts with 'file ' (startswith line 'file ') (cat '<div class="file">' '<div class="name">' (substr line 5) '</div>' ) # If it starts with 'endfile' (startswith line 'endfile') '</div>' (startswith line '#') (if (startswith line '##') (if (startswith line '###') (surround 'h3' (substr line 2)) (surround 'h2' (substr line 1)) ) (surround 'h1' line) ) (encode line) ) ))) (= process-comment (~ (c) (last (tie '\n' (map (split '\n' c) process-comment-line)) ))) (= split-parts (~ (str) (last (= left (dict 0 '')) (= right (dict)) (= n 0) (= prev-is-left 1) (each (split '\n' str) (~ (l) (last (= is-left (startswith l '#')) (if is-left (if prev-is-left (set left n (cat (get left n) '\n' (clean l))) (last (= n (+ n 1)) (set left n (clean l)) ) ) (if prev-is-left (set right n l) (set right n (cat (get right n) '\n' l)) ) ) (= prev-is-left is-left) ))) (cat '<div class="doc">' (apply cat (map (range (+ n 1)) (~ (i) (cat '<div class="sect">' '<div class="part left"><div class="text">' (process-comment (get left i)) '</div></div>' (if (get right i) (cat '<div class="part right"><div class="code">' (encode (get right i)) '</div></div>' ) '' ) '</div>' )))) '</div>' ) ))) (fs-write output (cat '<!doctype html>' '<html lang="en">' '<head>' '<meta charset="utf-8">' '<meta http-equiv="x-ua-compatible" content="ie=edge">' '<title>LemonCake</title>' '<meta name="viewport" content="width=device-width, initial-scale=1">' '<style>' 'div{box-sizing:border-box}' 'body{' 'margin:0;' 'background:linear-gradient(' 'to right,#fff 0%,#fff 50%,#f4f4f4 50%,#f4f4f4 100%' ')' '}' '.sect{clear:both}' '.part{width:50%;padding-top:50px}' '.left{float:left}' '.right{float:right;background:#f4f4f4}' 'pre{background:#f4f4f4;margin:0;padding:6px 10px}' 'pre+pre{padding-top:0}' '.text{' 'width:100%;' 'max-width:600px;' 'padding-right:10px;' 'float:right;' 'font-family:sans-serif' '}' 'h1,h2,h3{margin:0}' '@media(max-width:1240px){' '.left,.right,.text{float:none}' '.left,.right{width:100%}' '.text{max-width:none;padding:0 20px 20px}' '.right{padding-top:0}' '.left{padding-top:30px}' '.code{padding:20px;border:1px solid #ddd;border-width:2px 0;overflow-y:scroll}' 'body{background:#fff}' '}' '.code{padding-left:10px;font-family:monospace;white-space:pre}' '.file{border:1px solid #ddd}' '.name{background:#eee;padding:2px 10px;font-family:monospace;color:#777}' '</style>' '</head>' '<body>' (split-parts (fs-read input)) '</body>' '</html>' )) )))

Assembler functions

(= reg0 (dict 'al' 0 'cl' 1 'dl' 2 'bl' 3 'ah' 4 'ch' 5 'dh' 6 'bh' 7 )) (= asm-instr (dict 'add' (~ (a b) (if (and (in a reg0) (in b reg0)) (list 0x00 (| 0xc0 (<< (get reg0 b) 3) (get reg0 a) )) )) ))
add-001.in
(+ 1 2)

add-001.out
3

add-002.in
(+ 8 4)

add-002.out
12

add-003.in
(+ 1 1)
(+ 2 3)
 ( + 44 11 ) #

add-003.out
2
5
55
()

add-004.in
(+ 1 2 3 4 5)
(+ 3 3 3)

add-004.out
15
9

add-005.in
(+ 2)
(+ 1 1 1 (+ 1 1))

add-005.out
2
5

add-006.in
(+ 1 -1)

add-006.out
0

add-007.in
(+ 100 -1000)

add-007.out
-900

add-single-byte-001.eout
:(0x00 0xc0)
:(0x00 0xc8)
:(0x00 0xd0)
:(0x00 0xd8)
:(0x00 0xe0)
:(0x00 0xe8)
:(0x00 0xf0)
:(0x00 0xf8)
:(0x00 0xc1)
:(0x00 0xc9)
:(0x00 0xd1)
:(0x00 0xd9)
:(0x00 0xe1)
:(0x00 0xe9)
:(0x00 0xf1)
:(0x00 0xf9)
:(0x00 0xc2)
:(0x00 0xca)
:(0x00 0xd2)
:(0x00 0xda)
:(0x00 0xe2)
:(0x00 0xea)
:(0x00 0xf2)
:(0x00 0xfa)
:(0x00 0xc3)
:(0x00 0xcb)
:(0x00 0xd3)
:(0x00 0xdb)
:(0x00 0xe3)
:(0x00 0xeb)
:(0x00 0xf3)
:(0x00 0xfb)
:(0x00 0xc4)
:(0x00 0xcc)
:(0x00 0xd4)
:(0x00 0xdc)
:(0x00 0xe4)
:(0x00 0xec)
:(0x00 0xf4)
:(0x00 0xfc)
:(0x00 0xc5)
:(0x00 0xcd)
:(0x00 0xd5)
:(0x00 0xdd)
:(0x00 0xe5)
:(0x00 0xed)
:(0x00 0xf5)
:(0x00 0xfd)
:(0x00 0xc6)
:(0x00 0xce)
:(0x00 0xd6)
:(0x00 0xde)
:(0x00 0xe6)
:(0x00 0xee)
:(0x00 0xf6)
:(0x00 0xfe)
:(0x00 0xc7)
:(0x00 0xcf)
:(0x00 0xd7)
:(0x00 0xdf)
:(0x00 0xe7)
:(0x00 0xef)
:(0x00 0xf7)
:(0x00 0xff)

add-single-byte-001.lc
(= endl (~ (ccall (dynsym 'libc.so' 'printf') '\n')))
(= a (~ (last
  (repr (
    apply (get asm-instr 'add') $args
  ))
  (endl)
)))
(a 'al' 'al')
(a 'al' 'cl')
(a 'al' 'dl')
(a 'al' 'bl')
(a 'al' 'ah')
(a 'al' 'ch')
(a 'al' 'dh')
(a 'al' 'bh')
(a 'cl' 'al')
(a 'cl' 'cl')
(a 'cl' 'dl')
(a 'cl' 'bl')
(a 'cl' 'ah')
(a 'cl' 'ch')
(a 'cl' 'dh')
(a 'cl' 'bh')
(a 'dl' 'al')
(a 'dl' 'cl')
(a 'dl' 'dl')
(a 'dl' 'bl')
(a 'dl' 'ah')
(a 'dl' 'ch')
(a 'dl' 'dh')
(a 'dl' 'bh')
(a 'bl' 'al')
(a 'bl' 'cl')
(a 'bl' 'dl')
(a 'bl' 'bl')
(a 'bl' 'ah')
(a 'bl' 'ch')
(a 'bl' 'dh')
(a 'bl' 'bh')
(a 'ah' 'al')
(a 'ah' 'cl')
(a 'ah' 'dl')
(a 'ah' 'bl')
(a 'ah' 'ah')
(a 'ah' 'ch')
(a 'ah' 'dh')
(a 'ah' 'bh')
(a 'ch' 'al')
(a 'ch' 'cl')
(a 'ch' 'dl')
(a 'ch' 'bl')
(a 'ch' 'ah')
(a 'ch' 'ch')
(a 'ch' 'dh')
(a 'ch' 'bh')
(a 'dh' 'al')
(a 'dh' 'cl')
(a 'dh' 'dl')
(a 'dh' 'bl')
(a 'dh' 'ah')
(a 'dh' 'ch')
(a 'dh' 'dh')
(a 'dh' 'bh')
(a 'bh' 'al')
(a 'bh' 'cl')
(a 'bh' 'dl')
(a 'bh' 'bl')
(a 'bh' 'ah')
(a 'bh' 'ch')
(a 'bh' 'dh')
(a 'bh' 'bh')

and-001.in
(and 1)

and-001.out
1

and-002.in
(and 0 2)

and-002.out
0

and-003.in
(and 2 1)

and-003.out
1

and-004.in
(and 3 :(1) 'asdf' :ffff ())

and-004.out
()

append-001.in
(append (list) 1)

append-001.out
(1)

append-002.in
(append (list 3) 1)

append-002.out
(3 1)

append-003.in
(append (list 1 2) 3)

append-003.out
(1 2 3)

append-004.in
(append () ())

append-004.out
(())

apply-001.in
(apply add :(1 5))

apply-001.out
6

apply-002.in
(apply (~ (a b) (add a b)) :(1 2))

apply-002.out
3

assemble-001.lc
(= putchar-ptr (dynsym 'libc.so' 'putchar'))

(= f (assemble (byte-explode

  # mov rdi, 'A'
  :(1 0xbf 0x41 0x00 0x00 0x00)

  # mov rax, address_of_putchar
  :(1 0x48 0xb8)
  (list 8 putchar-ptr)

  # call rax
  :(1 0xff 0xd0)

  # mov rdi, '\n'
  :(1 0xbf 0x0a 0x00 0x00 0x00)

  # mov rax, address_of_putchar
  :(1 0x48 0xb8)
  (list 8 putchar-ptr)

  # call rax
  :(1 0xff 0xd0)

  # mov rdi, 'B'
  :(1 0xbf 0x42 0x00 0x00 0x00)

  # mov rax, address_of_putchar
  :(1 0x48 0xb8)
  (list 8 putchar-ptr)

  # call rax
  :(1 0xff 0xd0)

  # ret
  :(1 0xc3)

)))

(ccall f)

assemble-001.out
A
B

assemble-to-file-001.lc
(= prog-bytes (byte-explode
  # elf64_ehdr
  :(1 0x7f 0x45 0x4c 0x46 2 1 1 0) # e_ident
  :(8 0)
  :(2 2)        # e_type
  :(2 0x3e)     # e_machine
  :(4 1)        # e_version
  :(8 0x4000b0) # e_entry
  :(8 0x40)     # e_phoff
  :(8 0xe0)     # e_shoff
  :(4 0)        # e_flags
  :(2 0x40)     # e_ehsize
  :(2 0x38)     # e_phentsize
  :(2 2)        # e_phnum
  :(2 0x40)     # e_shentsize
  :(2 4)        # e_shnum
  :(2 3)        # e_shstrndx

  # elf64_phdr
  :(4 1)           # p_type
  :(4 5)           # p_flags
  :(8 0)           # p_offset
  :(8 0x400000)    # p_vaddr
  :(8 0x400000)    # p_paddr
  :(8 0xbc)        # p_filesz
  :(8 0xbc)        # p_memsz
  :(8 0x200000)    # p_align

  :(4 1)           # p_type
  :(4 6)           # p_flags
  :(8 0xbc)        # p_offset
  :(8 0x6000bc)    # p_vaddr
  :(8 0x6000bc)    # p_paddr
  :(8 8)           # p_filesz
  :(8 8)           # p_memsz
  :(8 0x200000)    # p_align

  # .text
  :(1 0xb8 0x3c 0x00 0x00 0x00) # mov rax 60
  :(1 0xbf 0x2a 0x00 0x00 0x00) # mov rdi 42
  :(1 0x0f 0x05) # syscall

  # .data
  :(8 0x0102030405060708)

  # .shstrtab
  :(1 0)
  :(1 0x2e 0x73 0x68 0x73 0x74 0x72 0x74 0x61 0x62 0) # .shstrtab
  :(1 0x2e 0x74 0x65 0x78 0x74 0) # .text
  :(1 0x2e 0x64 0x61 0x74 0x61 0) # .data
  :(1 0 0 0 0 0) # padding

  # null section
  :(4 0) # sh_name
  :(4 0) # sh_type
  :(8 0) # sh_flags
  :(8 0) # sh_addr
  :(8 0) # sh_offset
  :(8 0) # sh_size
  :(4 0) # sh_link
  :(4 0) # sh_info
  :(8 0) # sh_addralign
  :(8 0) # sh_entsize

  # .text section
  :(4 0x0b) # sh_name
  :(4 1) # sh_type
  :(8 0x06) # sh_flags
  :(8 0x4000b0) # sh_addr
  :(8 0xb0) # sh_offset
  :(8 0x0c) # sh_size
  :(4 0) # sh_link
  :(4 0) # sh_info
  :(8 0x10) # sh_addralign
  :(8 0) # sh_entsize

  # .data section
  :(4 0x11) # sh_name
  :(4 1) # sh_type
  :(8 0x03) # sh_flags
  :(8 0x6000bc) # sh_addr
  :(8 0xbc) # sh_offset
  :(8 0x08) # sh_size
  :(4 0) # sh_link
  :(4 0) # sh_info
  :(8 4) # sh_addralign
  :(8 0) # sh_entsize

  # .shstrtab section
  :(4 0x01) # sh_name
  :(4 3) # sh_type
  :(8 0x00) # sh_flags
  :(8 0) # sh_addr
  :(8 0xc4) # sh_offset
  :(8 0x17) # sh_size
  :(4 0) # sh_link
  :(4 0) # sh_info
  :(8 1) # sh_addralign
  :(8 0) # sh_entsize
))
(= prog (apply cat (map prog-bytes chr)))
(fs-write 'tmp_test_dir/prg' prog)
(ccall (dynsym 'libc.so' 'system') 'chmod +x tmp_test_dir/prg; ./tmp_test_dir/prg; echo $?')

assemble-to-file-001.out
42

assign-001.lc
(= a 1)
(repr a)

assign-001.out
1

assign-002.lc
(= a 3)
(= foo-bar 5)
(repr (+ a foo-bar))

assign-002.out
8

assign-003.lc
(= X 505 A 4040 aabbaa 110000)
(repr (+ aabbaa X A))

assign-003.out
114545

assign-004.in
(= a 2)
(= b (+ a 1))
(= c (+ b 100))
c
b
a

assign-004.out
2
3
103
103
3
2

assign-005.in
(= a 1)
a
(= a (+ 1 a))
a
(= a (+ a 1))
a
(= a 'asdf')

assign-005.out
1
1
2
2
3
3
'asdf'

at-001.in
(at :(1 2 3 4) 0)
(at :(1 2 3 4) 1)
(at :(1 2 3 4) 2)
(at :(1 2 3 4) 3)

at-001.out
1
2
3
4

binand-001.in
(& 12 6 13)
(& 0xff00000000000000 0x0f0000ff000000ff)

binand-001.out
4
1080863910568919040

binor-001.in
(| 1 2 4)
(| 0xff000000 0x0f0000ff)
(| 0xff00000000000000 0x0f0000ff000000ff)

binor-001.out
7
4278190335
-72056498821267201

byte-explode-001.in
(byte-explode)

byte-explode-001.out
()

byte-explode-002.in
(byte-explode :(1 1))

byte-explode-002.out
(1)

byte-explode-003.in
(byte-explode :(1 8) :(1 9))

byte-explode-003.out
(8 9)

byte-explode-004.in
(byte-explode :(2 0x1122))

byte-explode-004.out
(34 17)

byte-explode-005.in
(byte-explode :(4 0x55443322) :(1 4))

byte-explode-005.out
(34 51 68 85 4)

byte-explode-006.in
(byte-explode :(8 0xffeeddccccddaaff) :(1 5) :(8 0xffeeddbbccddaaff))

byte-explode-006.out
(255 170 221 204 204 221 238 255 5 255 170 221 204 187 221 238 255)

cat-001.in
(cat 'a' 'b')

cat-001.out
'ab'

cat-002.in
(cat 'aa' 'b')

cat-002.out
'aab'

cat-003.in
(cat '11' '2' '3333')

cat-003.out
'1123333'

ccall-001.lc
(ccall (dynsym 'libc.so' 'printf') 'asdf')

ccall-001.out
asdf

ccall-002.lc
(ccall (dynsym 'libc.so' 'printf') 'asdf')

(ccall (dynsym 'libc.so' 'printf') 'another')

ccall-002.out
asdfanother

ccall-003.lc
(= printf-func (dynsym 'libc.so' 'printf'))
(= printf (~ (a b) (ccall printf-func a b)))

(printf 'hello%lld' 3)
(printf 'hello%lld' 8)
(printf 'hello %s end' 'str')

ccall-003.out
hello3hello8hello str end

ccall-004.lc
(ccall
  (dynsym 'libc.so' 'printf')
  'asdf %s %s %s %s %s'
  'a' 'b' 'c' 'd' 'e'
)

ccall-004.out
asdf a b c d e

ccall-005.lc
(= abs (~ (a) (ccall (dynsym 'libc.so' 'abs') a)))

(repr (abs 5))
(repr ' ')
(repr (abs -7))
(repr ' ')
(repr (abs -2147483647))

ccall-005.out
5' '7' '2147483647

change-env-001.lc
# `$env` is the default variable representing the environment (a dict). Making
# this easier should be done in a macro.

(set $env (quote x) 4)
(repr x)

change-env-001.out
4

change-env-002.lc
(set $env (quote x) 5)
(set $env (quote y) 4)
(set $env (quote z) (+ x y))
(repr z)

change-env-002.out
9

chr-001.in
(chr 65)

chr-001.out
'A'

chr-002.in
(chr 39)

chr-002.out
'\''

comments-001.in
'b'#c

comments-001.out
'b'

comments-002.in
 'bc' #d

comments-002.out
'bc'

comments-003.in
 'bcd' #asdf
8

comments-003.out
'bcd'
8

comp-001.in
((comp (~ (a) (+ a 1)) (~ (a) (* a 3))) 2)

comp-001.out
9

dict-001.in
(dict)

dict-001.out
(dict)

dict-002.in
(dict (quote a) 1 (quote bb) 8 (quote ccc) 123)

dict-002.out
(dict bb 8 ccc 123 a 1)

div-001.in
(/ 10 2)

div-001.out
5

div-002.in
(/ 10 3)

div-002.out
3

dynsym-001.in
(- (dynsym 'libc.so' 'printf') (dynsym 'libc.so' 'printf'))

dynsym-001.out
0

each-001.lc
(= count 0)
(each
  :(a b c)
  (~ (= count (+ count 1)))
)
(repr count)

each-001.out
3

each-002.lc
(= count 0)
(each
  (range 10)
  (~ (x) (if
    (is x 5)
    :$stop
    (= count (+ count 1))
  ))
)
(repr count)

each-002.out
5

end-in-comment.lc
(repr 1)
# nothing
(repr 2) # something
(repr 44)
# end

end-in-comment.out
1244

escapes-001.in
(len 'a\'b')
(len '\'b')
(len 'a\'')
(len '\'')

escapes-001.out
3
2
2
1

escapes-002.in
(len 'a\\b')
(len '\\b')
(len 'a\\')
(len '\\')

escapes-002.out
3
2
2
1

escapes-003.in
(len '\n')
(len 'a\n')
(len '\nb')
(len 'a\nb')

escapes-003.out
1
2
2
3

escapes-004.in
'a\'b'
'\'b'
'a\''
'\''
'a\nb'
'\nb'
'a\n'
'\n'
'a\\b'
'\\b'
'a\\'
'\\'

escapes-004.out
'a\'b'
'\'b'
'a\''
'\''
'a\nb'
'\nb'
'a\n'
'\n'
'a\\b'
'\\b'
'a\\'
'\\'

eval-001.in
(eval 1)
(eval 'a')
(eval ())
(eval (dict 1 2))
(eval :(+ 1 2))
(eval (list :+ 1 3))
((eval :(~ (a b) (* a b))) 3 6)
(eval :(= a 3))
(eval :(- a 1))

eval-001.out
1
'a'
()
(dict 1 2)
3
4
18
3
2

fs-read-001.in
(fs-read 'tests/fs-read-001.in')

fs-read-001.out
'(fs-read \'tests/fs-read-001.in\')\n'

fs-write-001.lc
(fs-write 'tmp_test_dir/wot' 'I am a file. What are you?')
(repr (fs-read 'tmp_test_dir/wot'))

fs-write-001.lc_post
pwd
cd ..
rm -fr tmp_test_dir

fs-write-001.lc_pre
mkdir tmp_test_dir
cd tmp_test_dir

fs-write-001.out
'I am a file. What are you?'

func-001.lc
(= inc (~ (+ 1 (head $args))))
(repr (inc 2))
(repr (inc 5))
(repr (inc 22))

func-001.out
3623

func-002.lc
# Test that you can override values outside of the function.
(= a 1)
(= increase-a (~ (= a (+ a 1))))
(repr a)
(increase-a)
(repr a)
(increase-a)
(repr a)

func-002.out
123

func-003.lc
# Test that variables inside the function don't leak outside.
(= f (~ (= a 100)))
(repr a)
(f)
(repr a)

func-003.out
()()

func-004.lc
# Test IIFE (immediately invoked function expression).
(repr (
  (~ (+ 2 (head $args)))
  2
))

func-004.out
4

func-005.lc
# Function with named arguments.
(= sum (~ (a b) (+ a b)))
(repr (sum 1 4))
(repr (sum 1 2 3))

func-005.out
53

func-006.lc
# Nested functions.

(= funcs ((~ (last
  (= a 1)
  (
    dict
    'inc-a' (~ (= a (+ a 1)))
    'get-a' (~ a)
  )
))))

# 'a' isn't available here.
(repr a)

# Get 1.
(repr ((get funcs 'get-a')))

# Increase.
((get funcs 'inc-a'))

# Get 2.
(repr ((get funcs 'get-a')))

func-006.out
()12

func-007.lc
# Recursive function, yay!

(= fib (~ (n) (if
  (is n 0) 0
  (is n 1) 1
  (+
    (fib (- n 1))
    (fib (- n 2))
  )
)))

(repr (fib 2))
(repr (fib 3))
(repr (fib 4))
(repr (fib 5))
(repr (fib 6))

func-007.out
12358

get-001.in
(get (set (dict) 'name' 'John') 'name')

get-001.out
'John'

get-002.in
(get (set (set (dict) (quote a) 1) 'a' 2) 'a')

get-002.out
2

hashcode-001.in
(hashcode 0)

hashcode-001.out
0

hashcode-002.in
(hashcode 1234)

hashcode-002.out
1234

hashcode-003.in
(hashcode 2395873184555)

hashcode-003.out
2395873184555

hashcode-004.in
(hashcode 'a')

hashcode-004.out
177604

hashcode-005.in
(hashcode 'base')

hashcode-005.out
6382700272

hashcode-006.in
(hashcode (quote base))

hashcode-006.out
6382700272

hashcode-007.in
(hashcode 'besa')

hashcode-007.out
6382695920

hashcode-008.in
(hashcode 'Paul Nechifor')

hashcode-008.out
153306557046378463

head-tail-001.in
(head (list 0 1 2))

head-tail-001.out
0

head-tail-002.in
(head (list 0 1 2))

head-tail-002.out
0

head-tail-003.in
(tail (list 0 1 2))

head-tail-003.out
(1 2)

head-tail-004.in
(head (tail (list 0 1 2)))

head-tail-004.out
1

head-tail-005.in
(tail (tail (list 0 1 2)))

head-tail-005.out
(2)

head-tail-006.in
(head (tail (tail (list 0 1 2))))

head-tail-006.out
2

head-tail-007.in
(tail (tail (tail (list 0 1 2))))

head-tail-007.out
()

identity-001.in
(identity 1234)
(identity :asdf)
(identity (~ (x) (+ 1 x)))

identity-001.out
1234
asdf
(~ (x) (+ 1 x))

if-001.in
(if) # Expecting ().

if-001.out
()

if-002.in
(if 0)

if-002.out
0

if-003.in
(if 'a')

if-003.out
'a'

if-004.in
(if 1 'așa')

if-004.out
'așa'

if-005.in
(if 0 100)

if-005.out
()

if-006.in
(= a '')
(if a 'full' 'empty')

if-006.out
''
'empty'

if-007.in
(if 0 0 '' '' () () (dict) (dict) 'none-of-the-above')

if-007.out
'none-of-the-above'

iife-001.in
((~ (a b) (+ a b)) 5 4)

iife-001.out
9

import-001-add.lcl
(~ (n) (+ n n))

import-001.lc
(= double-it (import './import-001-add.lcl'))
(repr (double-it 23))

import-001.out
46

import-002-level1.lcl
(import './import-002-level2.lcl')

import-002-level2.lcl
(~ (n) (+ n n))

import-002.lc
(= double-it (import './import-002-level1.lcl'))
(repr (double-it 5))

import-002.out
10

in-001.in
(in 10 (dict 10 20))

in-001.out
1

in-002.in
(= d (dict 10 20 20 40 30 60))
(in 20 d)
(in 30 d)
(in 10 d)
(in 40 d)

in-002.out
(dict 10 20 20 40 30 60)
1
1
1
0

is-001.in
(is 4 4)

is-001.out
1

is-002.in
(is 4 3)

is-002.out
0

is-003.in
(is '4' 4)

is-003.out
0

is-004.in
(is 05 00005)

is-004.out
1

is-005.in
(is 'asdf' 'asdf')

is-005.out
1

is-006.in
(is 'asde' 'asdf')

is-006.out
0

is-007.in
(is 'asdf' 'asdfe')

is-007.out
0

join-001.in
(join :(1 2) :(3 4))

join-001.out
(1 2 3 4)

join-002.in
(join :('_' 'a') :('_' 'b') :('_' 'c'))

join-002.out
('_' 'a' '_' 'b' '_' 'c')

last-001.in
(last)

last-001.out
()

last-002.in
(last 1)

last-002.out
1

last-003.in
(last 'a' 'b' 'c')

last-003.out
'c'

last-004.in
(last (+ 1 2))

last-004.out
3

last-005.in
(last (+ 100 1 10) (+ 400 20 5))

last-005.out
425

len-001.in
(len 'a')

len-001.out
1

len-002.in
(len '1234')

len-002.out
4

len-003.in
(len '')

len-003.out
0

len-004.in
(len ())

len-004.out
0

len-005.in
(len (list 1))

len-005.out
1

len-006.in
(len (list 1 2))

len-006.out
2

len-007.in
(len (list 1 2 3))

len-007.out
3

len-008.in
(len (dict))

len-008.out
0

len-009.in
(len (dict))
(len (dict 1 11))
(len (dict 1 11 4 44))

len-009.out
0
1
2

lists-001.in
()

lists-001.out
()

lists-002.in
  () # asdf

lists-002.out
()

lists-003.in
(list )

lists-003.out
()

lists-004.in
(list 1)

lists-004.out
(1)

lists-005.in
(list 1 )

lists-005.out
(1)

lists-006.in
( list 22 )

lists-006.out
(22)

lists-007.in
  ( list 22 ) 

lists-007.out
(22)

lists-008.in
(list 1 2 3)

lists-008.out
(1 2 3)

lists-009.in
(list 1 'asdf' 3)

lists-009.out
(1 'asdf' 3)

lists-010.in
(list 1 (list 22 33) 3)

lists-010.out
(1 (22 33) 3)

lists-011.in
(list (list 1 'df' (list 'asf') () (list 'bb' ) ) 123)

lists-011.out
((1 'df' ('asf') () ('bb')) 123)

lists-012.in
(list 1 2 (quote aaa))

lists-012.out
(1 2 aaa)

lists-013.in
(list 1 (+ 1 2 3) 'asdf')

lists-013.out
(1 6 'asdf')

macros-001.lc
(set $env :two (macro :(+ 1 1)))
(repr (two))

macros-001.out
2

macros-002.lc
(set $env :x 20)
(set $env :y 300)
(set $env :foo (macro :(+ x 1 y)))
(repr (foo))

macros-002.out
321

macros-003.lc
(=
  =~
  (macro
    (name args function)
    (list
      :=
      name
      (list
        :~
        args
        function
      )
    )
  )
)

(=~ sum (a b) (+ a b))

(repr (sum 1 2))

macros-003.out
3

macros-004.lc
(= add-with (macro (base) (list
  :~
  :(n)
  (list :+ :n base)
)))

(= adder-5 (add-with 5))
(repr (adder-5 10))
(repr (adder-5 100))

(= adder-123 (add-with 123))
(repr (adder-123 1000))
(repr (adder-123 1))

macros-004.out
151051123124

map-001.in
(map :(1 2 3) (~ (a) (+ a 1)))

map-001.out
(2 3 4)

map-002.in
(map :(5 6 7 8) (~ (n i) (+ n i)))

map-002.out
(5 7 9 11)

minus-001.in
(- 1 1)
(- 100 10)
(- 5000 -6000)
(- -5000 6000)
(- -10000 -20020)
(- 123 99999)

minus-001.out
0
90
11000
-11000
10020
-99876

mixed-types-001.in
321
'str'
123

mixed-types-001.out
321
'str'
123

mixed-types-002.in
'asdf'
43215

mixed-types-002.out
'asdf'
43215

mul-001.in
(* 4 3)

mul-001.out
12

mul-002.in
(* 2 2 4 100)

mul-002.out
1600

multiple-statements-001.lc
(repr 1)
(repr 2)

multiple-statements-001.out
12

multiple-statements-002.lc
1
(repr 123)

multiple-statements-002.out
123

multiple-statements-003.lc
(repr 'asdf')
(repr 1)
(repr (+ 1 2 3))

multiple-statements-003.out
'asdf'16

multiple-statements-004.lc
()
1234
(repr 1)
()
(repr '
')
8
(repr 8888)

multiple-statements-004.out
1'\n'8888

nested-calls-001.in
(+ 1 2 (+ 3))

nested-calls-001.out
6

nested-calls-002.in
(+ 1 2 (+ 3 4))

nested-calls-002.out
10

nested-calls-003.in
(+ 1 1 (+ 1 (+ 1 (+ 1 1) 1 (+ 1 1))))

nested-calls-003.out
9

null-001.in

null-001.out
()

null-002.in
# Just a comment.

null-002.out
()

null-003.in
    # Indented comment.

null-003.out
()

numbers-001.in
4

numbers-001.out
4

numbers-002.in
1234
5

numbers-002.out
1234
5

numbers-003.in
-123
44
-124
-1
-111111111111111111
1234789012347890

numbers-003.out
-123
44
-124
-1
-111111111111111111
1234789012347890

operators-001.in
(< 1 2)
(< 1 -1)
(< 1 2 0)
(< 1 2 9)
(<= 1 2)
(<= 1 1)
(<= 1 1 0)
(> 2 1)
(> -1 1)
(> 2 1 3)
(> 2 1 -4)
(>= 2 1)
(>= 5 5)
(>= 1 2)

operators-001.out
1
0
0
1
1
1
0
1
0
0
1
1
1
0

or-001.in
(or 1)

or-001.out
1

or-002.in
(or 0)

or-002.out
0

or-003.in
(or :())

or-003.out
()

or-004.in
(or 0 1)

or-004.out
1

or-005.in
(or 1 0)

or-005.out
1

or-006.in
(or 0 :() 0 0 :())

or-006.out
()

pair-001.in
(pair 1 :(2 3))
(pair 1 :())
(pair :(1) :(1))

pair-001.out
(1 2 3)
(1)
((1) 1)

partial-001.lc
(= inc (partial add 1))
(repr (inc 1))
(repr (inc 6))

partial-001.out
27

quote-001.in
(quote x)

quote-001.out
x

quote-002.in
(quote (a b))

quote-002.out
(a b)

range-001.in
(range 5)

range-001.out
(0 1 2 3 4)

range-002.in
(range 10 15)

range-002.out
(10 11 12 13 14)

range-003.in
(range 10 5 -1)

range-003.out
(10 9 8 7 6)

range-004.in
(map (range 5) (~ (a) (* a a)))

range-004.out
(0 1 4 9 16)

reduce-001.in
(reduce add :(1 2))

reduce-001.out
3

reduce-002.in
(reduce add :(1 2 3))

reduce-002.out
6

reduce-003.in
(reduce add :(6) 500)

reduce-003.out
506

reduce-004.in
(reduce add :(1 2 1 2 1 2 1 2) 10000)

reduce-004.out
10012

repeat-001.in
(repeat 9 0)
(repeat 1 1)
(repeat 1 2)
(repeat 'a' 3)

repeat-001.out
()
(1)
(1 1)
('a' 'a' 'a')

replace-001.in
(replace 'aaxa' 'a' '-')

replace-001.out
'--x-'

replace-002.in
(replace 'aaXaaXaaa' 'aa' '[]')

replace-002.out
'[]X[]X[]a'

replace-003.in
(replace 'dog eat dog here' 'dog' 'kitty')

replace-003.out
'kitty eat kitty here'

repr-001.in
(repr 'one')

repr-001.out
'one'()

repr-str.lc
(repr 'str1')

repr-str.out
'str1'

repr_str.out
'str1'

set-001.in
(set (dict) 'name' 'John')

set-001.out
(dict 'name' 'John')

set-002.in
(set (set (dict) 'name' 'John') 1 2)

set-002.out
(dict 1 2 'name' 'John')

set-head.in
(= l :(1 2 3))
(set-head l 5)
l
(set-head (tail l) 6)
l

set-head.out
(1 2 3)
5
(5 2 3)
6
(5 6 3)

set-tail.in
(= l :(1 2 3))
(set-tail l ())
l
(set-tail l :(7))
l
(set-tail (tail l) :(8))
l

set-tail.out
(1 2 3)
()
(1)
(7)
(1 7)
(8)
(1 7 8)

shl-001.in
(<< 1 1)
(<< 1 2)
(<< 1 3)
(<< 0xf000000000000000 1)
(<< 1 63)

shl-001.out
2
4
8
-2305843009213693952
-9223372036854775808

shr-001.out
1
2
16
1

shr-002.out
(>> 2 1)
(>> 16 2)
(>> 0xf000000000000000 60)
(>> 0x8000000000000000 63)

split-001.in
(split ' ' 'a b c')

split-001.out
('a' 'b' 'c')

split-002.in
(split 'aa' 'aaJJJaaaMMMMM')

split-002.out
('' 'JJJ' 'aMMMMM')

split-003.in
(split 'aa' 'aaJJJaaaMMMMMaa')

split-003.out
('' 'JJJ' 'aMMMMM' '')

split-004.in
(split '' 'abcdef')

split-004.out
('a' 'b' 'c' 'd' 'e' 'f')

stitch-001.in
(stitch)

stitch-001.out
()

stitch-002.in
(stitch ())

stitch-002.out
()

stitch-003.in
(stitch () () ())

stitch-003.out
()

stitch-004.in
(stitch :(1 2))

stitch-004.out
(1 2)

stitch-005.in
(stitch :(5 2) :(88 123))

stitch-005.out
(5 2 88 123)

stitch-006.in
(stitch :(1 2 3) :(5 55 555) :(-1 -2 -3))

stitch-006.out
(1 2 3 5 55 555 -1 -2 -3)

string-001.in
'asdf'

string-001.out
'asdf'

string-002.in
'aa  bb  '

string-002.out
'aa  bb  '

string-003.lc
(repr '
  a
  ccc
  b
')

string-003.out
'\n  a\n  ccc\n  b\n'

switch-001.in
(switch 1 1 :one)

switch-001.out
one

switch-002.in
(switch 2 1 :one)

switch-002.out
()

switch-003.in
(switch 2 1 :one :other)

switch-003.out
other

switch-004.in
(switch 2 1 :one 2 :two)

switch-004.out
two

symbols-001.in
(quote asdf)

symbols-001.out
asdf

symbols-002.in
(quote <=>)

symbols-002.out
<=>

symbols-003.in
(list (quote asdf))

symbols-003.out
(asdf)

symbols-004.in
(list (quote asdf) 1 (quote ab))

symbols-004.out
(asdf 1 ab)

symbols-005.in
(list (quote a) (quote s) (quote d) (quote f) 'x' (list 1 (quote ab)))

symbols-005.out
(a s d f 'x' (1 ab))

symbols-006.in
(list (quote xx) 'yy')

symbols-006.out
(xx 'yy')

symbols-007.in
(list (quote a) (quote b))
(list (quote a) (quote b))
(list (quote a) (quote b))

symbols-007.out
(a b)
(a b)
(a b)

symbols-008.in
(list (quote aa) (quote bb) (quote cc))
  (list (quote a) (quote c))
( list (quote a) (quote b) (quote c) ) # Nothing here
(list (quote cc) (quote dd))

symbols-008.out
(aa bb cc)
(a c)
(a b c)
(cc dd)

system-001.lc
(ccall (dynsym 'libc.so' 'system') 'echo hello world')

system-001.out
hello world

trap-001.in
($trap 4)
($trap (+ 1 2))
($trap ($entrap 5))
($trap (+ 1 2 ($entrap 'a')))
($trap (if 1 ($entrap 'b')))
($trap (if 0 ($entrap 'b') (+ 4 4)))
($trap (+ 1 2 ($entrap (+ 8 8))))

trap-001.out
4
3
5
'a'
'b'
8
16

trap-002.lc
(= f (~ (a b) ($trap (last
  (= ret 0)
  (map (range a b) (~ (a) (if (- a 10)
    (= ret (+ ret a))
    ($entrap (list 'will not go above 10' ret a))
  )))
  ret
))))

(repr (f 2 20))
(repr (f 2 5))

trap-002.out
('will not go above 10' 44 10)9

unicode-001.in
(= ă 'litera ă mică')
ă

unicode-001.out
'litera ă mică'
'litera ă mică'

whitespace-001.in
  1234 

whitespace-001.out
1234

whitespace-002.in
     'a' 

whitespace-002.out
'a'

whitespace-003.in
  12
   12345  
       55    

whitespace-003.out
12
12345
55

zip-001.in
(zip :(1 2) :(a b))
(zip :(1 2) :(a b) :(A B))
(zip :(1 2 c) :(a b) :(A B))
(zip :(1 2 c) :(a b c) :(A B))
(zip :(1 2 c) :(a b c) :(A B c))

zip-001.out
((1 a) (2 b))
((1 a A) (2 b B))
((1 a A) (2 b B))
((1 a A) (2 b B))
((1 a A) (2 b B) (c c c))