From 874d7dba8389576dd9caf0e1862442b34fd7c0d6 Mon Sep 17 00:00:00 2001 From: Christian Barthel Date: Sun, 28 Jul 2019 19:56:21 +0200 Subject: [PATCH] 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 --- tokenize.l => llm.l | 2 +- sample.llm | 83 +++++++++++----------------------- sample.session.txt | 107 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 58 deletions(-) rename tokenize.l => llm.l (99%) create mode 100644 sample.session.txt diff --git a/tokenize.l b/llm.l similarity index 99% rename from tokenize.l rename to llm.l index adcfccf..1156f21 100644 --- a/tokenize.l +++ b/llm.l @@ -732,7 +732,7 @@ pr(struct ast *a) break; case AST_LIST: printf("#fn\n"); - debug_env(a->env); + // debug_env(a->env); break; default: err(1, "don't know how to print"); diff --git a/sample.llm b/sample.llm index d4eea4f..a17855a 100644 --- a/sample.llm +++ b/sample.llm @@ -30,10 +30,9 @@ (add a (mult a (sub b 1)))))) ;; div: a / b (result: integer) -(def div (lm (a b) - (if (leq a 0) - 0 - (add 1 (div (sub a b) b))))) +(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 @@ -45,42 +44,44 @@ (def sumarg (lm (a b erg) (if (gt a b) 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))) +;; increment, decrement numbers: (def inc (lm (x) (add x 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) -;; (lm (x) (dispatch a b x)))) -(def cons (lm (a b) (lm (x) (if (eq x 0) a b)))) +(def abs (lm (x) (if (lt x 0) (inv x) x))) -(def car (lm (cell) (cell 0))) -(def cdr (lm (cell) (cell 1))) +;; ------------ Data Structure (List) ------------ -;; (define (cons x y) -;; (define (dispatch m) -;; (cond ((= m 0) x) -;; ((= m 1) y) -;; (else (error "Argument not 0 or 1 -- CONS" m)))) -;; dispatch) -;; (define (car z) (z 0)) -;; (define (cdr z) (z 1)) +(def cons (lm (a b) (lm (x) (if x a b)))) +(def car (lm (cell) (cell t))) +(def cdr (lm (cell) (cell nil))) -;; ------------ 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? -;; (def zero (lm (x) (0))) -;; * lexical scoping like: +;; (def zero (lm (x) (0))) +;; (geht:(def c (lm (x) 0)) ) +;; (def zero (lm () (0))) +;; * check if lexical scoping works: ;; (def a (lm (x y z) ;; (def abc (fn args)) ;; (fn abc args)))) +;; * set! is missing ;; Local Variables: @@ -92,35 +93,3 @@ ;; tab-width: 2 ;; c-basic-offset: 2 ;; 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)))))) diff --git a/sample.session.txt b/sample.session.txt new file mode 100644 index 0000000..7e00cfa --- /dev/null +++ b/sample.session.txt @@ -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 + +