Implement closures+upvalues, rework virtual machine
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
(assert (= (->string 1234) "1234"))
|
||||
(assert (= (->string '(1 2)) "(1 2)"))
|
||||
(assert (= (->string #t) "#T"))
|
||||
|
||||
(assert (= 1234 (string->number "1234")))
|
||||
(assert (= -1234 (string->number "-1234")))
|
||||
(assert (= 0x1234 (string->number "1234" 16)))
|
||||
(assert (= 0o1234 (string->number "1234" 8)))
|
||||
(assert (= (/ 1 2) (string->number "0.5")))
|
||||
(assert (= (/ -1 2) (string->number "-0.5")))
|
||||
(assert (= (/ -1 2) (string->number "-5e-1")))
|
||||
@@ -0,0 +1 @@
|
||||
(print (explain explain))
|
||||
+11
-3
@@ -1,4 +1,12 @@
|
||||
;; vi:ft=lisp:sw=2:ts=2
|
||||
(print "Argument count:" (length *args*))
|
||||
(print "Arguments as a list:" *args*)
|
||||
|
||||
(print (length *args*))
|
||||
(print *args*)
|
||||
(print "Iterated:")
|
||||
(let
|
||||
(index 0 current *args*)
|
||||
(while current
|
||||
(print index ":" (car current))
|
||||
(setq index (+ index 1))
|
||||
(setq current (cdr current))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
;; vi:ft=lisp:sw=2:ts=2
|
||||
|
||||
(defun factorial (x)
|
||||
(if (= x 0)
|
||||
1
|
||||
|
||||
+28
-20
@@ -1,32 +1,40 @@
|
||||
;; vi:ft=lisp:sw=2:ts=2
|
||||
|
||||
;; loop, broken by (return)
|
||||
(setq looped-loop (let (i 0)
|
||||
(loop
|
||||
(if (< i 10)
|
||||
(setq i (+ i 1))
|
||||
(return)
|
||||
(setq
|
||||
looped-loop
|
||||
(let (i 0)
|
||||
(loop
|
||||
(if (< i 10)
|
||||
(setq i (+ i 1))
|
||||
(break)
|
||||
)
|
||||
)
|
||||
i
|
||||
)
|
||||
i
|
||||
))
|
||||
)
|
||||
|
||||
;; while, broken prematurely
|
||||
(setq looped-while (let (i 0)
|
||||
(while (< i 20)
|
||||
(setq i (+ i 1))
|
||||
(if (= i 10) (return))
|
||||
(setq
|
||||
looped-while
|
||||
(let (i 0)
|
||||
(while (< i 20)
|
||||
(setq i (+ i 1))
|
||||
(if (= i 10) (break))
|
||||
)
|
||||
i
|
||||
)
|
||||
i
|
||||
))
|
||||
)
|
||||
|
||||
;; while, broken by condition
|
||||
(setq looped-while-full (let (i 0)
|
||||
(while (< i 10)
|
||||
(setq i (+ i 1))
|
||||
(setq
|
||||
looped-while-full
|
||||
(let (i 0)
|
||||
(while (< i 10)
|
||||
(setq i (+ i 1))
|
||||
)
|
||||
i
|
||||
)
|
||||
i
|
||||
))
|
||||
)
|
||||
|
||||
;; All loops execute the same count of times
|
||||
(assert (= looped-loop looped-while looped-while-full))
|
||||
(print "Test succeeded")
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
; add
|
||||
(assert (= (+ 1 2 3) 6))
|
||||
(assert (= (+) 0))
|
||||
(assert (= (+ 1) 1))
|
||||
|
||||
; sub
|
||||
(assert (= (- 1) -1))
|
||||
(assert (= (- 1 2) -1))
|
||||
|
||||
; mul
|
||||
(assert (= (*) 1))
|
||||
(assert (= (* 2) 2))
|
||||
(assert (= (* 2 3 4) 24))
|
||||
|
||||
; div
|
||||
(assert (= (/ 1 0) #inf))
|
||||
(assert (= (/ -1 0) -#inf))
|
||||
(assert (= (/ 6 2) 3))
|
||||
(assert (= (/ 12 3 2) 2))
|
||||
(assert (= (/ 6 4) (/ 3 2)))
|
||||
(assert (= (/ 4) (/ 25 100))) ; reciprocal
|
||||
|
||||
; rem
|
||||
(assert (= (% 6 2) 0))
|
||||
(assert (= (% 6 4) 2))
|
||||
|
||||
;;;; NaN/infinity handling
|
||||
(assert (≠ #nan #nan))
|
||||
(assert (= #inf #inf))
|
||||
(assert (≠ -#inf #inf))
|
||||
(assert (> #inf 0 -#inf))
|
||||
|
||||
;;;; Ordering
|
||||
(assert (< 1 2 3))
|
||||
(assert (not (< 1 1 2 3)))
|
||||
(assert (<= 1 1 2 3 3))
|
||||
(assert (> 3 2 1))
|
||||
(assert (not (> 3 3 2 1)))
|
||||
(assert (>= 3 3 2 1))
|
||||
|
||||
(assert (= 1 1 1 1))
|
||||
(assert (not (= 1 1 2 1)))
|
||||
|
||||
(assert (≠ 1 2 3 4))
|
||||
(assert (not (≠ 1 2 2 3)))
|
||||
+1
-3
@@ -1,5 +1,3 @@
|
||||
;; vi:ft=lisp:sw=2:ts=2
|
||||
|
||||
(defun cadr (x) (car (cdr x)))
|
||||
|
||||
(defun map-ok-err (f-ok f-err result)
|
||||
@@ -29,7 +27,7 @@
|
||||
|
||||
(loop
|
||||
(let (expression (read))
|
||||
(if expression NIL (return))
|
||||
(if expression NIL (break))
|
||||
(setq expression (unquote expression))
|
||||
(map-ok-err repl-eval-print repl-read-error expression)
|
||||
)
|
||||
|
||||
+23
-2
@@ -1,5 +1,3 @@
|
||||
;; vi:ft=lisp:sw=2:ts=2
|
||||
|
||||
(defun replace-argument (x) (let (x 1) x))
|
||||
|
||||
(defun replace-let ()
|
||||
@@ -23,3 +21,26 @@
|
||||
(assert (= (replace-argument 3) 1))
|
||||
(assert (= (replace-let) 2))
|
||||
(assert (= (reassignment-in-a-loop 4) 16))
|
||||
|
||||
;; capture upvalue
|
||||
(assert
|
||||
(=
|
||||
123
|
||||
(let
|
||||
(loc0 123)
|
||||
((lambda () loc0))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; mutate upvalue
|
||||
(assert
|
||||
(=
|
||||
321
|
||||
(let
|
||||
(loc0 123)
|
||||
((lambda () (setq loc0 321)))
|
||||
loc0
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
;; Conditional
|
||||
(if #t 1 2)
|
||||
(cond
|
||||
[(= 1 2 3) #f]
|
||||
[(= 1 1 1) #t]
|
||||
[`otherwise 1234]
|
||||
)
|
||||
|
||||
;; Loops
|
||||
(while #f
|
||||
1
|
||||
2
|
||||
3)
|
||||
(loop
|
||||
1
|
||||
2
|
||||
3
|
||||
(break))
|
||||
|
||||
;; Functions
|
||||
(defun a (x y z w) 1 2 3 x)
|
||||
(lambda (x y z w) 1 2 3 x)
|
||||
|
||||
;; Macros
|
||||
(defmacro my-quote (x) `(quote ,x))
|
||||
(print (my-quote (a b c)))
|
||||
|
||||
;; vectors
|
||||
#[1 2 3]
|
||||
|
||||
;; progn
|
||||
(progn
|
||||
1
|
||||
2
|
||||
3)
|
||||
|
||||
;; setq
|
||||
(setq glob-value "my-global")
|
||||
|
||||
;; let
|
||||
(let
|
||||
(x 1 y 2)
|
||||
x
|
||||
)
|
||||
|
||||
(let* (x 1 y x) y)
|
||||
|
||||
;; Quoting
|
||||
`(ok ,glob-value)
|
||||
Reference in New Issue
Block a user