; Library of common functions (def != (a b) () (if (= a b) 0 1)) (def >= (a b) () (if (< a b) 0 1)) (def and (a b) ()(if a b 0)) (def or (a b) () (if a 1 b)) (def not a () (if a 0 1)) (def nop () () 0) (def print a () (sys 1 a)) (def printc c () (sys 2 c)) (def space () () (sys 2 32)) (def nl () () (sys 2 10)) (def exit () () (sys 13)) ; --------- string ---------- ; input output of string functions are pointers ; to dereference it, use (vec s 0) ; print string (def prstr s () (while (vec s 0) (do (printc (vec s 0)) (set s (+ s 1))))) ; test string equal (def str= (s1 s2) (flag i c1 c2) (do (set flag 1) (set i 0) (while flag (do (set c1 (vec s1 i)) (set c2 (vec s2 i)) (if (!= c1 c2) (set flag 0) (if (= c1 0) (set flag 0) (if (= c2 0) (set flag 0)))) (set i (+ i 1)))) (and (= c1 0) (= c2 0)))) ; find string length (def strlen s () (if (vec s 0) (+ 1 (strlen (+ s 1))) 0)) ; copy string (def strcpy (s1 s2) a (do (set a (vec s2 0)) (while a (do (setv s1 0 a) (set s1 (+ s1 1)) (set s2 (+ s2 1)) (set a (vec s2 0)))) (setv s1 0 0))) ; check is string a number, no sign (def isNumber s a (do (set a (vec s 0)) (while (and (> a 47) (< a 58)) ; isdigit a (do (set s (+ s 1)) (set a (vec s 0)))) (= a 0))) ; true if reach end (def isString s () (= (vec s 0) 34)) ; start with quote ; convert string to number, no sign (def atoi s (a v) (do (set v 0) (set a (vec s 0)) (while a (do (set v (- (+ (* v 10) a) 48)) (set s (+ s 1)) (set a (vec s 0)))) v)) (def error s () (do (prstr s) (sys 12) ; line no (sys 13))) ; exit ; ---------- data --------- (def head e () (vec e 0)) (def tail e () (vec e 1)) (def sethead (e v) () (setv e 0 v)) (def settail (e v) () (setv e 1 v)) (def cons (e list) h (do (set h (new 2)) (setv h 0 e) ; set head (setv h 1 list) ; set tail h)) (def arg1 e () (head e)) (def arg2 e () (head (tail e))) (def arg3 e () (head (tail (tail e)))) ; End