diff --git a/demo.modal b/demo.modal new file mode 100644 index 0000000..db8ddec --- /dev/null +++ b/demo.modal @@ -0,0 +1,141 @@ +<> (-- ?x) () +-- ( little endian binary integers ) + +-- ( constants ) +<> zero ((0 nil)) +<> one ((1 nil)) +<> two ((0 (1 nil))) +<> three ((1 (1 nil))) +<> ten ((0 (1 (0 (1 nil))))) + +-- ( decimal digit to binary ) +<> (binary 0) ((0 nil)) +<> (binary 1) ((1 nil)) +<> (binary 2) ((0 (1 nil))) +<> (binary 3) ((1 (1 nil))) +<> (binary 4) ((0 (0 (1 nil)))) +<> (binary 5) ((1 (0 (1 nil)))) +<> (binary 6) ((0 (1 (1 nil)))) +<> (binary 7) ((1 (1 (1 nil)))) +<> (binary 8) ((0 (0 (0 (1 nil))))) +<> (binary 9) ((1 (0 (0 (1 nil))))) + +-- ( binary to decimal digit ) +<> (decimal (0 nil)) (0) +<> (decimal (1 nil)) (1) +<> (decimal (0 (1 nil))) (2) +<> (decimal (1 (1 nil))) (3) +<> (decimal (0 (0 (1 nil)))) (4) +<> (decimal (1 (0 (1 nil)))) (5) +<> (decimal (0 (1 (1 nil)))) (6) +<> (decimal (1 (1 (1 nil)))) (7) +<> (decimal (0 (0 (0 (1 nil))))) (8) +<> (decimal (1 (0 (0 (1 nil))))) (9) + +-- create nil-terminated list +<> (nilify (?h)) ((?h nil)) +<> (nilify (?h ?t)) ((?h nilify ?t)) + +-- reverse nil-terminated list +<> (reverse ?x) (reverse' nil ?x) +<> (reverse' ?a nil) (?a) +<> (reverse' ?a (?h ?t)) (reverse' (?h ?a) ?t) + +-- ( normalize, remove trailing zeros ) +-- ( currently zero is (0 nil) though arguably it could be nil ) +-- ( that change would require auditing our rules ) +<> (normalize (?h ?t)) ((?h normalize' nil ?t)) +<> (normalize' ?s nil) (nil) +<> (normalize' ?s (0 ?t)) (normalize' (0 ?s) ?t) +<> (normalize' nil (1 ?t)) ((1 normalize' nil ?t)) +<> (normalize' (0 ?s) (1 ?t)) ((0 normalize' ?s (1 ?t))) + +-- ( to integer ) +<> ((int ?*)) (sum f (one) g reverse nilify (?*)) +<> (g nil) (nil) +<> (g (?h ?t)) ((binary ?h g ?t)) +<> (f (?u) nil) (nil) +<> (f (?u) (?h ?t)) ((mul ?h ?u f (mul ?u ten) ?t)) + +-- ( to string: TODO, need division for this one ) + +-- ( comparison operartions ) +<> (cmp ?x ?y) (cmpc eq ?x ?y) +<> (cmpc ?e nil nil) (?e) +<> (cmpc ?e (1 ?x) nil) (gt) +<> (cmpc ?e (0 ?x) nil) (cmpc ?e ?x nil) +<> (cmpc ?e nil (1 ?y)) (lt) +<> (cmpc ?e nil (0 ?y)) (cmpc ?e nil ?y) +<> (cmpc ?e (0 ?x) (0 ?y)) (cmpc ?e ?x ?y) +<> (cmpc ?e (1 ?x) (0 ?y)) (cmpc gt ?x ?y) +<> (cmpc ?e (0 ?x) (1 ?y)) (cmpc lt ?x ?y) +<> (cmpc ?e (1 ?x) (1 ?y)) (cmpc ?e ?x ?y) + +-- ( addition ) +<> (add ?x ?y) (addc 0 ?x ?y) +<> (addc 0 nil nil) (nil) +<> (addc 1 nil nil) ((1 nil)) +<> (addc ?c ?x nil) (addc ?c ?x (0 nil)) +<> (addc ?c nil ?y) (addc ?c (0 nil) ?y) +<> (addc 0 (0 ?x) (0 ?y)) ((0 addc 0 ?x ?y)) +<> (addc 0 (0 ?x) (1 ?y)) ((1 addc 0 ?x ?y)) +<> (addc 0 (1 ?x) (0 ?y)) ((1 addc 0 ?x ?y)) +<> (addc 0 (1 ?x) (1 ?y)) ((0 addc 1 ?x ?y)) +<> (addc 1 (0 ?x) (0 ?y)) ((1 addc 0 ?x ?y)) +<> (addc 1 (0 ?x) (1 ?y)) ((0 addc 1 ?x ?y)) +<> (addc 1 (1 ?x) (0 ?y)) ((0 addc 1 ?x ?y)) +<> (addc 1 (1 ?x) (1 ?y)) ((1 addc 1 ?x ?y)) + +-- ( summation ) +<> (sum nil) ((0 nil)) +<> (sum (?a nil)) (?a) +<> (sum (?a (?b ?c))) (sum (add ?a ?b ?c)) + +-- ( multiplication ) +<> (mul ?x ?y) (mulc nil ?x ?y) +<> (mulc ?t nil ?y) (sum ?t) +<> (mulc ?t (0 ?x) ?y) (mulc ?t ?x (0 ?y)) +<> (mulc ?t (1 ?x) ?y) (mulc (?y ?t) ?x (0 ?y)) + +-- ( subtraction ) +<> (sub ?x ?y) (normalize subc 0 ?x ?y) +<> (subc 0 nil nil) (nil) +<> (subc 1 nil nil) (#err) +<> (subc 0 ?x nil) (?x) +<> (subc 1 ?x nil) (subc 1 ?x (0 nil)) +<> (subc ?c nil ?y) (subc ?c (0 nil) ?y) +<> (subc 0 (0 ?x) (0 ?y)) ((0 subc 0 ?x ?y)) +<> (subc 0 (0 ?x) (1 ?y)) ((1 subc 1 ?x ?y)) +<> (subc 0 (1 ?x) (0 ?y)) ((1 subc 0 ?x ?y)) +<> (subc 0 (1 ?x) (1 ?y)) ((0 subc 0 ?x ?y)) +<> (subc 1 (0 ?x) (0 ?y)) ((1 subc 1 ?x ?y)) +<> (subc 1 (0 ?x) (1 ?y)) ((0 subc 1 ?x ?y)) +<> (subc 1 (1 ?x) (0 ?y)) ((0 subc 0 ?x ?y)) +<> (subc 1 (1 ?x) (1 ?y)) ((1 subc 1 ?x ?y)) + +-- ( dec ) +<> (dec (0 nil)) (#err) +<> (dec ?x) (normalize dec' ?x) +<> (dec' (0 ?t)) ((1 dec' ?t)) +<> (dec' (1 ?t)) ((0 ?t)) + +-- ( left shift; lshift x b means x< (lshift ?x (0 nil)) (?x) +<> (lshift ?x (1 nil)) ((0 ?x)) +<> (lshift ?x (?h (?a ?b))) (lshift (0 ?x) dec (?h (?a ?b))) + +-- ( constants ) +<> (a) ((1 (1 nil))) +<> (b) ((0 (1 nil))) +<> (c) ((1 (1 (1 nil)))) +<> (d) (int 135) +<> (e) (int 1913) +-- ( <> (f) (int 4294967295) ) + +-- ( cmp c c ) +-- ( add a b ) +-- ( sum (a (b (c nil))) ) +-- ( mul a c ) + +-- ( add d e ) +-- ( lshift (int 3) (int 1) )