move file, cleanup sample session
- move tokenize.l file to llm.l (because the whole source is included in this file) - cleanup sample, add sample.session
This commit is contained in:
parent
4b640bd275
commit
874d7dba83
|
@ -732,7 +732,7 @@ pr(struct ast *a)
|
||||||
break;
|
break;
|
||||||
case AST_LIST:
|
case AST_LIST:
|
||||||
printf("#fn\n");
|
printf("#fn\n");
|
||||||
debug_env(a->env);
|
// debug_env(a->env);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
err(1, "don't know how to print");
|
err(1, "don't know how to print");
|
83
sample.llm
83
sample.llm
|
@ -30,10 +30,9 @@
|
||||||
(add a (mult a (sub b 1))))))
|
(add a (mult a (sub b 1))))))
|
||||||
|
|
||||||
;; div: a / b (result: integer)
|
;; div: a / b (result: integer)
|
||||||
(def div (lm (a b)
|
(def div (lm (a b)
|
||||||
(if (leq a 0)
|
(if (geq a b) (add 1 (div (sub a b) b)) 0)))
|
||||||
0
|
|
||||||
(add 1 (div (sub a b) b)))))
|
|
||||||
|
|
||||||
(def modsubr (lm (a b last)
|
(def modsubr (lm (a b last)
|
||||||
(if (lt a 0) last
|
(if (lt a 0) last
|
||||||
|
@ -45,42 +44,44 @@
|
||||||
(def sumarg (lm (a b erg)
|
(def sumarg (lm (a b erg)
|
||||||
(if (gt a b)
|
(if (gt a b)
|
||||||
erg
|
erg
|
||||||
(sum (add a 1) b (add erg a)))))
|
(sumarg (add a 1) b (add erg a)))))
|
||||||
(def sum (lm (x y) (sumarg x y 0)))
|
(def sum (lm (x y) (sumarg x y 0)))
|
||||||
|
|
||||||
|
|
||||||
|
;; increment, decrement numbers:
|
||||||
(def inc (lm (x) (add x 1)))
|
(def inc (lm (x) (add x 1)))
|
||||||
(def dec (lm (x) (add x (inv 1))))
|
(def dec (lm (x) (add x (inv 1))))
|
||||||
|
|
||||||
;; ------------ Open TODO's ----------------------------------
|
;; ------------ Misc Functions -------------
|
||||||
|
|
||||||
(def dispatch (lm (a b which) (if (eq which 0) a b)))
|
(def fac (lm (x)
|
||||||
|
(if (eq x 0) 1
|
||||||
|
(mult x (fac (dec x))))))
|
||||||
|
|
||||||
;; (def cons (lm (a b)
|
(def abs (lm (x) (if (lt x 0) (inv x) x)))
|
||||||
;; (lm (x) (dispatch a b x))))
|
|
||||||
(def cons (lm (a b) (lm (x) (if (eq x 0) a b))))
|
|
||||||
|
|
||||||
(def car (lm (cell) (cell 0)))
|
;; ------------ Data Structure (List) ------------
|
||||||
(def cdr (lm (cell) (cell 1)))
|
|
||||||
|
|
||||||
;; (define (cons x y)
|
(def cons (lm (a b) (lm (x) (if x a b))))
|
||||||
;; (define (dispatch m)
|
(def car (lm (cell) (cell t)))
|
||||||
;; (cond ((= m 0) x)
|
(def cdr (lm (cell) (cell nil)))
|
||||||
;; ((= m 1) y)
|
|
||||||
;; (else (error "Argument not 0 or 1 -- CONS" m))))
|
|
||||||
;; dispatch)
|
|
||||||
;; (define (car z) (z 0))
|
|
||||||
;; (define (cdr z) (z 1))
|
|
||||||
|
|
||||||
;; ------------ Open TODO's ----------------------------------
|
|
||||||
;; * Garbage Collection
|
|
||||||
;; * Multi Threading (each function within it's own thread
|
|
||||||
|
;; ------------ Open TODO's --------------------
|
||||||
|
;; * Garbage Collection ( free() )
|
||||||
|
;; * Multi Threading (each function within it's own
|
||||||
|
;; thread)
|
||||||
;; * Eval with empty body or empty parameter list?
|
;; * Eval with empty body or empty parameter list?
|
||||||
;; (def zero (lm (x) (0)))
|
;; (def zero (lm (x) (0)))
|
||||||
;; * lexical scoping like:
|
;; (geht:(def c (lm (x) 0)) )
|
||||||
|
;; (def zero (lm () (0)))
|
||||||
|
;; * check if lexical scoping works:
|
||||||
;; (def a (lm (x y z)
|
;; (def a (lm (x y z)
|
||||||
;; (def abc (fn args))
|
;; (def abc (fn args))
|
||||||
;; (fn abc args))))
|
;; (fn abc args))))
|
||||||
|
;; * set! is missing
|
||||||
|
|
||||||
|
|
||||||
;; Local Variables:
|
;; Local Variables:
|
||||||
|
@ -92,35 +93,3 @@
|
||||||
;; tab-width: 2
|
;; tab-width: 2
|
||||||
;; c-basic-offset: 2
|
;; c-basic-offset: 2
|
||||||
;; End:
|
;; End:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(def a (lm (x) (lm (y) (add x y))))
|
|
||||||
nil 0x0
|
|
||||||
a
|
|
||||||
#fn
|
|
||||||
(a 1)
|
|
||||||
#fn
|
|
||||||
(def b (a 1))
|
|
||||||
nil 0x0
|
|
||||||
b
|
|
||||||
#fn
|
|
||||||
(b 5)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
((a 1) 4)
|
|
||||||
eval_fn_call: expected function body as list
|
|
||||||
|
|
||||||
|
|
||||||
- mindestens 2 sachen sind noch kaputt:
|
|
||||||
- wenn ich einen funktionsaufruf am anfang habe das eine funktion
|
|
||||||
zurückgibt gehts nicht
|
|
||||||
- und im allgemeinen findet man nicht die variable in einem
|
|
||||||
funktionsaufruf einer zurückgegebenen funktion
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(def tripe (lm (x) (lm (y) (lm (z) (add x (add y z))))))
|
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
$ ./llm
|
||||||
|
;; function definitions:
|
||||||
|
(def and (lm (a b) (nand (nand a b) (nand a b))))
|
||||||
|
(def not (lm (a) (nand a a)))
|
||||||
|
(def or (lm (a b) (nand (nand a a) (nand b b))))
|
||||||
|
(def nor (lm (a b) (not (or a b))))
|
||||||
|
(def gt (lm (a b) (lt b a)))
|
||||||
|
(def neq (lm (a b) (not (eq a b))))
|
||||||
|
(def geq (lm (a b) (or (eq a b) (gt a b))))
|
||||||
|
(def eq (lm (a b) (and (not (lt a b)) (not (lt b a)))))
|
||||||
|
(def leq (lm (a b) (or (eq a b) (lt a b))))
|
||||||
|
(def sub (lm (a b) (add a (inv b))))
|
||||||
|
(def mult (lm (a b)
|
||||||
|
(if (eq b 1)
|
||||||
|
a
|
||||||
|
(add a (mult a (sub b 1))))))
|
||||||
|
(def div (lm (a b)
|
||||||
|
(if (geq a b) (add 1 (div (sub a b) b))
|
||||||
|
0)))
|
||||||
|
(def modsubr (lm (a b last)
|
||||||
|
(if (lt a 0) last
|
||||||
|
(modsubr (sub a b) b a))))
|
||||||
|
(def mod (lm (a b) (modsubr a b 0)))
|
||||||
|
(def sumarg (lm (a b erg)
|
||||||
|
(if (gt a b)
|
||||||
|
erg
|
||||||
|
(sumarg (add a 1) b (add erg a)))))
|
||||||
|
(def sum (lm (x y) (sumarg x y 0)))
|
||||||
|
(def inc (lm (x) (add x 1)))
|
||||||
|
(def dec (lm (x) (add x (inv 1))))
|
||||||
|
(def fac (lm (x)
|
||||||
|
(if (eq x 0) 1
|
||||||
|
(mult x (fac (dec x))))))
|
||||||
|
(def abs (lm (x) (if (lt x 0) (inv x) x)))
|
||||||
|
(def cons (lm (a b) (lm (x) (if x a b))))
|
||||||
|
(def car (lm (cell) (cell t)))
|
||||||
|
(def cdr (lm (cell) (cell nil)))
|
||||||
|
|
||||||
|
(def countfn (lm (x) (if (lt x 0) dec inc)))
|
||||||
|
(def countdir (lm (x) ((countfn x) x)))
|
||||||
|
|
||||||
|
(def execop (lm (x y z) (x y z)))
|
||||||
|
(def myadd (lm (x y) (add x y)))
|
||||||
|
|
||||||
|
;; -------------------------------------------------------
|
||||||
|
;; read eval print loop:
|
||||||
|
(and t t)
|
||||||
|
t
|
||||||
|
(and t nil)
|
||||||
|
nil
|
||||||
|
(nand t nil)
|
||||||
|
t
|
||||||
|
(gt 8 1)
|
||||||
|
t
|
||||||
|
(neq 1 1)
|
||||||
|
nil
|
||||||
|
|
||||||
|
(eq 1 1)
|
||||||
|
t
|
||||||
|
(leq 1 5)
|
||||||
|
t
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(inc 5)
|
||||||
|
6
|
||||||
|
(mult 5 5)
|
||||||
|
25
|
||||||
|
(div 8 2)
|
||||||
|
4
|
||||||
|
|
||||||
|
fac
|
||||||
|
#fn
|
||||||
|
(fac 5)
|
||||||
|
120
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(countdir (inv 1))
|
||||||
|
-2
|
||||||
|
(countdir (countdir 5))
|
||||||
|
7
|
||||||
|
|
||||||
|
|
||||||
|
(myadd 4 3)
|
||||||
|
7
|
||||||
|
(execop myadd 4 3)
|
||||||
|
7
|
||||||
|
|
||||||
|
|
||||||
|
cons
|
||||||
|
#fn
|
||||||
|
(def cell (cons 1 2))
|
||||||
|
(car cell)
|
||||||
|
1
|
||||||
|
(cdr cell)
|
||||||
|
2
|
||||||
|
|
||||||
|
(def cell2 (cons 0 cell))
|
||||||
|
(car cell2)
|
||||||
|
0
|
||||||
|
(car (cdr cell2))
|
||||||
|
1
|
||||||
|
(cdr (cdr cell2))
|
||||||
|
2
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue