Merge commit '1b64651a9a0c19201cba9c8e993f9760e490289b'

This commit is contained in:
Benjamin Vedder 2022-03-08 17:48:01 +01:00
commit 7e4716f3f3
60 changed files with 132 additions and 127 deletions

View File

@ -88,48 +88,53 @@ Compute 5 % 3, evaluates to 2.
\section sec_comp Comparisons \section sec_comp Comparisons
<a name="="> <h3>=</h3> </a> <a name="eq"> <h3>eq</h3> </a>
Compare expressions for equality. The = implements structural equality. Compare expressions for equality. The eq implements structural equality.
The for of an = expression is <code>(= expr1 ... exprN)</code> The form of an eq expression is <code>(eq expr1 ... exprN)</code>
\note \note
Compare the result of <code>(+ 1 2)</code> with 3. The Compare the result of <code>(+ 1 2)</code> with 3. The
result of this comparison is <code>t</code>. result of this comparison is <code>t</code>.
\code \code
(= (+ 1 2) 3) (eq (+ 1 2) 3)
\endcode \endcode
Multiple expressions can be checked at once. The examples below evaluates to Multiple expressions can be checked at once. The examples below evaluates to
<code>t</code> <code>t</code>
\code \code
(= 1 1 1 1) (eq 1 1 1 1)
(= (+ 3 4) (+ 2 5) (+ 1 6)) (eq (+ 3 4) (+ 2 5) (+ 1 6))
\endcode \endcode
The following examples evaluate to <code>nil</code> representing false. The following examples evaluate to <code>nil</code> representing false.
\code \code
(= 1 1 1 1 2) (eq 1 1 1 1 2)
(= (+ 1 2) (+ 0 2) (+ -1 2)) (eq (+ 1 2) (+ 0 2) (+ -1 2))
\endcode \endcode
The = comparison can be used on tree shaped data. The following expression evaluates to The = comparison can be used on tree shaped data. The following expression evaluates to
<code>t</code>. <code>t</code>.
\code \code
(= '(1 (1 2)) '(1 (1 2))) (eq '(1 (1 2)) '(1 (1 2)))
\endcode \endcode
--- ---
<a name="num-eq"> <h3>num-eq</h3> </a> <a name="#="> <h3>=</h3> </a>
The <code>num-eq</code> operation can only be used on numerical arguments. The <code>=</code> operation can only be used on numerical arguments.
For numerical arguments <code>num-eq</code> behaves like <a href="#=">=</a>.
If you know you are comparing numbers, it will be more efficient to use If you know you are comparing numbers, it will be more efficient to use
<code>num-eq</code>. <code>=</code>.
An important difference between <code>eq</code> and <code>=</code> is
that equals compare the numerical values of the arguments. A 3 is a 3
independent of them being different types. <code>eq</code> on the other
hands compares the representations of the arguments exactly and they must
match in structure, type and value to be considered equal.
\note \note
Example of <code>num-eq</code> comparison. Example of <code>=</code> comparison.
\code \code
(num-eq (+ 2 3) (+ 1 4)) (num-eq (+ 2 3) (+ 1 4))
\endcode \endcode

View File

@ -1189,10 +1189,12 @@ static inline void eval_continuation(eval_context_t *ctx) {
} }
static inline void eval_define(eval_context_t *ctx) { static inline void eval_define(eval_context_t *ctx) {
lbm_value key = lbm_car(lbm_cdr(ctx->curr_exp)); lbm_value args = lbm_cdr(ctx->curr_exp);
lbm_value val_exp = lbm_car(lbm_cdr(lbm_cdr(ctx->curr_exp))); lbm_value key = lbm_car(args);
lbm_value rest_args = lbm_cdr(args);
lbm_value val_exp = lbm_car(rest_args);
if (lbm_type_of(key) == LBM_VAL_TYPE_SYMBOL) { if (lbm_is_symbol(key) && lbm_is_symbol_nil(lbm_cdr(rest_args))) {
lbm_uint sym_val = lbm_dec_sym(key); lbm_uint sym_val = lbm_dec_sym(key);
if ((sym_val >= VARIABLE_SYMBOLS_START) && if ((sym_val >= VARIABLE_SYMBOLS_START) &&

View File

@ -2,7 +2,7 @@
(define reverse (define reverse
(lambda (xs) (lambda (xs)
(let ((revacc (lambda (acc xs) (let ((revacc (lambda (acc xs)
(if (= nil xs) (if (eq nil xs)
acc acc
(revacc (cons (car xs) acc) (cdr xs)))))) (revacc (cons (car xs) acc) (cdr xs))))))
(revacc nil xs)))) (revacc nil xs))))
@ -16,7 +16,7 @@
(define length (lambda (xs) (define length (lambda (xs)
(let ((len (lambda (l xs) (let ((len (lambda (l xs)
(if (= xs nil) (if (eq xs nil)
l l
(len (+ l 1) (cdr xs)))))) (len (+ l 1) (cdr xs))))))
(len 0 xs)))) (len 0 xs))))
@ -24,43 +24,43 @@
(define take (lambda (n xs) (define take (lambda (n xs)
(let ((take-tail (let ((take-tail
(lambda (acc n xs) (lambda (acc n xs)
(if (num-eq n 0) (if (= n 0)
acc acc
(take-tail (cons (car xs) acc) (- n 1) (cdr xs)))))) (take-tail (cons (car xs) acc) (- n 1) (cdr xs))))))
(reverse (take-tail nil n xs))))) (reverse (take-tail nil n xs)))))
(define drop (lambda (n xs) (define drop (lambda (n xs)
(if (num-eq n 0) (if (= n 0)
xs xs
(if (= xs nil) (if (eq xs nil)
nil nil
(drop (- n 1) (cdr xs)))))) (drop (- n 1) (cdr xs))))))
(define zip (lambda (xs ys) (define zip (lambda (xs ys)
(if ( = xs nil) (if (eq xs nil)
nil nil
(if ( = ys nil) (if (eq ys nil)
nil nil
(cons (cons (car xs) (car ys)) (zip (cdr xs) (cdr ys))))))) (cons (cons (car xs) (car ys)) (zip (cdr xs) (cdr ys)))))))
(define map (lambda (f xs) (define map (lambda (f xs)
(if (= xs nil) (if (eq xs nil)
nil nil
(cons (f (car xs)) (map f (cdr xs)))))) (cons (f (car xs)) (map f (cdr xs))))))
(define lookup (lambda (x xs) (define lookup (lambda (x xs)
(if (= xs nil) (if (eq xs nil)
nil nil
(if (= (car (car xs)) x) (if (eq (car (car xs)) x)
(car (cdr (car xs))) (car (cdr (car xs)))
(lookup x (cdr xs)))))) (lookup x (cdr xs))))))
(define foldr (lambda (f i xs) (define foldr (lambda (f i xs)
(if (= xs nil) (if (eq xs nil)
i i
(f (car xs) (foldr f i (cdr xs)))))) (f (car xs) (foldr f i (cdr xs))))))
(define foldl (lambda (f i xs) (define foldl (lambda (f i xs)
(if (= xs nil) (if (eq xs nil)
i i
(foldl f (f i (car xs)) (cdr xs))))) (foldl f (f i (car xs)) (cdr xs)))))

View File

@ -117,7 +117,7 @@ special_sym const special_symbols[NUM_SPECIAL_SYMBOLS] = {
{"*" , SYM_MUL}, {"*" , SYM_MUL},
{"/" , SYM_DIV}, {"/" , SYM_DIV},
{"mod" , SYM_MOD}, {"mod" , SYM_MOD},
{"=" , SYM_EQ}, {"=" , SYM_NUMEQ},
{"<" , SYM_LT}, {"<" , SYM_LT},
{">" , SYM_GT}, {">" , SYM_GT},
{"<=" , SYM_LEQ}, {"<=" , SYM_LEQ},
@ -130,7 +130,7 @@ special_sym const special_symbols[NUM_SPECIAL_SYMBOLS] = {
{"yield" , SYM_YIELD}, {"yield" , SYM_YIELD},
{"wait" , SYM_WAIT}, {"wait" , SYM_WAIT},
{"spawn" , SYM_SPAWN}, {"spawn" , SYM_SPAWN},
{"num-eq" , SYM_NUMEQ}, {"eq" , SYM_EQ},
{"car" , SYM_CAR}, {"car" , SYM_CAR},
{"cdr" , SYM_CDR}, {"cdr" , SYM_CDR},
{"cons" , SYM_CONS}, {"cons" , SYM_CONS},

View File

@ -1,2 +1,2 @@
(num-eq (+ 1 0xf) 16u28) (= (+ 1 0xf) 16u28)

View File

@ -11,14 +11,13 @@
(bufset-u8 arr 8 62) (bufset-u8 arr 8 62)
(bufset-u8 arr 9 61) (bufset-u8 arr 9 61)
(= (bufget-u8 arr 0) 70u28) (and (= (bufget-u8 arr 0) 70)
(and (num-eq (bufget-u8 arr 0) 70) (= (bufget-u8 arr 1) 69)
(num-eq (bufget-u8 arr 1) 69) (= (bufget-u8 arr 2) 68)
(num-eq (bufget-u8 arr 2) 68) (= (bufget-u8 arr 3) 67)
(num-eq (bufget-u8 arr 3) 67) (= (bufget-u8 arr 4) 66)
(num-eq (bufget-u8 arr 4) 66) (= (bufget-u8 arr 5) 65)
(num-eq (bufget-u8 arr 5) 65) (= (bufget-u8 arr 6) 64)
(num-eq (bufget-u8 arr 6) 64) (= (bufget-u8 arr 7) 63)
(num-eq (bufget-u8 arr 7) 63) (= (bufget-u8 arr 8) 62)
(num-eq (bufget-u8 arr 8) 62) (= (bufget-u8 arr 9) 61))
(num-eq (bufget-u8 arr 9) 61))

View File

@ -6,9 +6,9 @@
(bufset-u32 arr 8 10) (bufset-u32 arr 8 10)
(bufset-u32 arr 12 0xDEADBEEF) (bufset-u32 arr 12 0xDEADBEEF)
(and (num-eq (bufget-u32 arr 0) 16777215) (and (= (bufget-u32 arr 0) 16777215)
(num-eq (bufget-u32 arr 4) 0xFFFFFFFF) (= (bufget-u32 arr 4) 0xFFFFFFFF)
(num-eq (bufget-u32 arr 8) 10) (= (bufget-u32 arr 8) 10)
(num-eq (bufget-u32 arr 12) 0xDEADBEEF)) (= (bufget-u32 arr 12) 0xDEADBEEF))

View File

@ -5,7 +5,7 @@
(bufset-i32 arr 8 10) (bufset-i32 arr 8 10)
(bufset-i32 arr 12 0xDEADBEEF) (bufset-i32 arr 12 0xDEADBEEF)
(and (num-eq (bufget-i32 arr 0) 16777215) (and (= (bufget-i32 arr 0) 16777215)
(num-eq (bufget-i32 arr 4) 0xFFFFFFFF) (= (bufget-i32 arr 4) 0xFFFFFFFF)
(num-eq (bufget-i32 arr 8) 10) (= (bufget-i32 arr 8) 10)
(num-eq (bufget-i32 arr 12) 0xDEADBEEF)) (= (bufget-i32 arr 12) 0xDEADBEEF))

View File

@ -5,7 +5,7 @@
(bufset-f32 arr 8 100) (bufset-f32 arr 8 100)
(bufset-f32 arr 12 42) (bufset-f32 arr 12 42)
(and (num-eq (bufget-f32 arr 0) 3.14) (and (= (bufget-f32 arr 0) 3.14)
(num-eq (bufget-f32 arr 4) 666.666) (= (bufget-f32 arr 4) 666.666)
(num-eq (bufget-f32 arr 8) 100) (= (bufget-f32 arr 8) 100)
(num-eq (bufget-f32 arr 12) 42)) (= (bufget-f32 arr 12) 42))

View File

@ -3,5 +3,5 @@
(free arr) (free arr)
(and (= (car arr) nil) (and (eq (car arr) nil)
(= (cdr arr) nil)) (eq (cdr arr) nil))

View File

@ -1,2 +1,2 @@
(and (= t (> 1 0)) (and (eq t (> 1 0))
(= nil (< 1 0))) (eq nil (< 1 0)))

View File

@ -1,2 +1,2 @@
(and (= t (>= 1 1)) (and (eq t (>= 1 1))
(= t (<= 1 1))) (eq t (<= 1 1)))

View File

@ -1,2 +1,2 @@
(and (= t (>= 1 0)) (and (eq t (>= 1 0))
(= t (<= 0 1))) (eq t (<= 0 1)))

View File

@ -1,2 +1,2 @@
(and (= nil (>= 0 1)) (and (eq nil (>= 0 1))
(= nil (<= 1 0))) (eq nil (<= 1 0)))

View File

@ -1,2 +1,2 @@
(= '(0u28 0u28 255u28 255u28) (decode (- 65536 1))) (eq '(0u28 0u28 255u28 255u28) (decode (- 65536 1)))

View File

@ -1,3 +1,3 @@
(= (define a 3) 'a) (eq (define a 3) 'a)

View File

@ -1,3 +1,3 @@
(= 'bepa (define bepa 3) ) (eq 'bepa (define bepa 3) )

View File

@ -1,3 +1,3 @@
(= "hello" "hello") (eq "hello" "hello")

View File

@ -1,3 +1,3 @@
(= (= "hello" "hell0") nil) (eq (eq "hello" "hell0") nil)

View File

@ -1,3 +1,3 @@
(= (= 1 2) nil) (eq (= 1 2) nil)

View File

@ -1,11 +1,11 @@
(define f (lambda (x) (define f (lambda (x)
(if (num-eq x 0) (if (= x 0)
'done 'done
(progn (progn
(gc) (gc)
(f (- x 1)))))) (f (- x 1))))))
(= (f 100) 'done) (eq (f 100) 'done)

View File

@ -1 +1 @@
(= (ext-even 2) t) (eq (ext-even 2) t)

View File

@ -1 +1 @@
(= (ext-odd 1) t) (eq (ext-odd 1) t)

View File

@ -1 +1 @@
(= (ext-even 7) nil) (eq (ext-even 7) nil)

View File

@ -1 +1 @@
(= (ext-odd 6) nil) (eq (ext-odd 6) nil)

View File

@ -1,6 +1,6 @@
(define fold (lambda (f i xs) (define fold (lambda (f i xs)
(if (= xs nil) (if (eq xs nil)
i i
(fold f (f i (car xs)) (cdr xs))))) (fold f (f i (car xs)) (cdr xs)))))

View File

@ -1,2 +1,2 @@
(= (if (> 0 1) 'apa 'bepa) 'bepa) (eq (if (> 0 1) 'apa 'bepa) 'bepa)

View File

@ -1,2 +1,2 @@
(= (if (< 0 1) 'apa 'bepa) 'apa) (eq (if (< 0 1) 'apa 'bepa) 'apa)

View File

@ -1 +1 @@
(= nil (ix '(1 2) 100000)) (eq nil (ix '(1 2) 100000))

View File

@ -1,5 +1,5 @@
(let ((x 0) (let ((x 0)
(f (lambda (y) (+ y x))) (f (lambda (y) (+ y x)))
(g (lambda (x) (+ x 1)))) (g (lambda (x) (+ x 1))))
(= (list (f 10) (g 10)) '(10 11))) (eq (list (f 10) (g 10)) '(10 11)))

View File

@ -1,3 +1,3 @@
(= (list 1 2 3) '(1 2 3)) (eq (list 1 2 3) '(1 2 3))

View File

@ -1,3 +1,3 @@
(= (list 1 2 (+ 1 2) 4) '(1 2 3 4)) (eq (list 1 2 (+ 1 2) 4) '(1 2 3 4))

View File

@ -1 +1 @@
(= (and 't 't) 't) (eq (and 't 't) 't)

View File

@ -1 +1 @@
(= (not (not (not (not t)))) t) (eq (not (not (not (not t)))) t)

View File

@ -1 +1 @@
(= (not (not (not (not (not t))))) nil) (eq (not (not (not (not (not t))))) nil)

View File

@ -1 +1 @@
(= (or 't 'nil) 't) (eq (or 't 'nil) 't)

View File

@ -1 +1 @@
(= (and 'nil 't) 'nil) (eq (and 'nil 't) 'nil)

View File

@ -1 +1 @@
(= (or 'nil 'nil) 'nil) (eq (or 'nil 'nil) 'nil)

View File

@ -1 +1 @@
(= (not (and 1 2 3)) nil) (eq (not (and 1 2 3)) nil)

View File

@ -1 +1 @@
(= (not nil) t) (eq (not nil) t)

View File

@ -1 +1 @@
(= (not t) nil) (eq (not t) nil)

View File

@ -1,4 +1,4 @@
(define map (lambda (f xs) (if (= xs nil) nil (cons (f (car xs)) (map f (cdr xs)))))) (define map (lambda (f xs) (if (eq xs nil) nil (cons (f (car xs)) (map f (cdr xs))))))
(= (map (lambda (x) (+ x 1)) (list 1 2 3 4 5)) (list 2 3 4 5 6)) (eq (map (lambda (x) (+ x 1)) (list 1 2 3 4 5)) (list 2 3 4 5 6))

View File

@ -1,10 +1,10 @@
(define map (lambda (f xs) (define map (lambda (f xs)
(let ((accmap (lambda (acc xs) (let ((accmap (lambda (acc xs)
(if (= xs nil) (if (eq xs nil)
acc acc
(accmap (cons (f (car xs)) acc) (cdr xs)))))) (accmap (cons (f (car xs)) acc) (cdr xs))))))
(reverse (accmap nil xs))))) (reverse (accmap nil xs)))))
(= (map (lambda (x) (+ x 1)) (list 1 2 3 4 5 6)) (list 2 3 4 5 6 7)) (eq (map (lambda (x) (+ x 1)) (list 1 2 3 4 5 6)) (list 2 3 4 5 6 7))

View File

@ -1,8 +1,7 @@
(let ((accmap (lambda (f acc xs) (let ((accmap (lambda (f acc xs)
(if (= xs nil) (if (eq xs nil)
acc acc
(accmap f (cons (f (car xs)) acc) (cdr xs)))))) (accmap f (cons (f (car xs)) acc) (cdr xs))))))
(define map (lambda (f xs) (reverse (accmap f nil xs))))) (define map (lambda (f xs) (reverse (accmap f nil xs)))))
(= (map (lambda (x) (+ x 1)) (list 1 2 3 4 5 6)) (list 2 3 4 5 6 7)) (eq (map (lambda (x) (+ x 1)) (list 1 2 3 4 5 6)) (list 2 3 4 5 6 7))

View File

@ -5,4 +5,4 @@
( (?cons c) (+ (car c) (f (cdr c)))) ( (?cons c) (+ (car c) (f (cdr c))))
( _ 'error-not-a-list)))) ( _ 'error-not-a-list))))
(= (f 'kurt) 'error-not-a-list) (eq (f 'kurt) 'error-not-a-list)

View File

@ -1,2 +1,2 @@
(= '(+ 1 2) `(+ 1 2)) (eq '(+ 1 2) `(+ 1 2))

View File

@ -1,2 +1,2 @@
(= `(+ 1 ,(+ 2 3)) '(+ 1 5)) (eq `(+ 1 ,(+ 2 3)) '(+ 1 5))

View File

@ -1,2 +1,2 @@
(= `(+ 1 (+ 2 ,(+ 3 4))) '(+ 1 (+ 2 7))) (eq `(+ 1 (+ 2 ,(+ 3 4))) '(+ 1 (+ 2 7)))

View File

@ -1,2 +1,2 @@
(= `(1 2 ,@'(3 4 5)) '(1 2 3 4 5)) (eq `(1 2 ,@'(3 4 5)) '(1 2 3 4 5))

View File

@ -1 +1 @@
(= 'x 'x) (eq 'x 'x)

View File

@ -1 +1 @@
(= '(1 2 3) (list 1 2 3)) (eq '(1 2 3) (list 1 2 3))

View File

@ -2,4 +2,4 @@
(setcar a (cons 7 8)) (setcar a (cons 7 8))
(= a (cons (cons 7 8) 2)) (eq a (cons (cons 7 8) 2))

View File

@ -2,4 +2,4 @@
(setcdr a (cons 7 8)) (setcdr a (cons 7 8))
(= a (cons 1 (cons 7 8))) (eq a (cons 1 (cons 7 8)))

View File

@ -1,3 +1,3 @@
(let ((a (cons 1 2))) (let ((a (cons 1 2)))
(progn (setcdr a 10) (progn (setcdr a 10)
(= (and (car a 1) (cdr a 10))))) (and (= (car a) 1) (= (cdr a) 10))))

View File

@ -1,8 +1,8 @@
(define sumtree (define sumtree
(lambda (x) (lambda (x)
(if (= (type-of x) type-i28) (if (eq (type-of x) type-i28)
x x
(if (= x 'nil) (if (eq x 'nil)
0 0
(let ((a (sumtree (car x))) (let ((a (sumtree (car x)))
(b (sumtree (cdr x)))) (b (sumtree (cdr x))))

View File

@ -1,14 +1,14 @@
(define is-number (define is-number
(lambda (x) (lambda (x)
(if (= (type-of x) type-i28) (if (eq (type-of x) type-i28)
't 't
(if (= (type-of x) type-u28) (if (eq (type-of x) type-u28)
't 't
(if (= (type-of x) type-float) (if (eq (type-of x) type-float)
't 't
(if (= (type-of x) type-i32) (if (eq (type-of x) type-i32)
't 't
(if (= (type-of x) type-u32) (if (eq (type-of x) type-u32)
't 't
'nil))))))) 'nil)))))))
@ -17,7 +17,7 @@
(lambda (x) (lambda (x)
(if (is-number x) (if (is-number x)
x x
(if (= x 'nil) (if (eq x 'nil)
0 0
(let ((a (sumtree (car x))) (let ((a (sumtree (car x)))
(b (sumtree (cdr x)))) (b (sumtree (cdr x))))

View File

@ -1,10 +1,10 @@
(define is-number (define is-number
(lambda (x) (lambda (x)
(or (= (type-of x) type-i28) (or (eq (type-of x) type-i28)
(= (type-of x) type-u28) (eq (type-of x) type-u28)
(= (type-of x) type-float) (eq (type-of x) type-float)
(= (type-of x) type-i32) (eq (type-of x) type-i32)
(= (type-of x) type-u32)) (eq (type-of x) type-u32))
)) ))
@ -12,7 +12,7 @@
(lambda (x) (lambda (x)
(if (is-number x) (if (is-number x)
x x
(if (= x 'nil) (if (eq x 'nil)
0 0
(let ((a (sumtree (car x))) (let ((a (sumtree (car x)))
(b (sumtree (cdr x)))) (b (sumtree (cdr x))))

View File

@ -1,3 +1,3 @@
(define f (lambda (acc n) (if (num-eq n 0) acc (f (+ acc n) (- n 1))))) (define f (lambda (acc n) (if (= n 0) acc (f (+ acc n) (- n 1)))))
( = (f 0 10000u32) 50005000u32) ( = (f 0 10000u32) 50005000u32)

View File

@ -1,3 +1,3 @@
(define sum (lambda (s rest) (if (= rest nil) s (sum (+ s (car rest)) (cdr rest))))) (define sum (lambda (s rest) (if (eq rest nil) s (sum (+ s (car rest)) (cdr rest)))))
( = (sum 0 (list 2.0 2.0 1.0 1.0 3.0 3.0)) 12.0) ( = (sum 0 (list 2.0 2.0 1.0 1.0 3.0 3.0)) 12.0)

View File

@ -1,2 +1,2 @@
(= (take 100 (iota 1000)) (iota 99)) (eq (take 100 (iota 1000)) (iota 99))