diff --git a/sample.llm b/sample.llm index 8c72a49..d4eea4f 100644 --- a/sample.llm +++ b/sample.llm @@ -1,31 +1,24 @@ +;; ------------ Boolean Logic -------------------- +;; Boolean logic: and, or, not, nor: -(def cons (lm (a b) (lm (x) (if (eq x 0) a b)))) -;; ------------ Boolean Logic ---------------------------------- (def and (lm (a b) (nand (nand a b) (nand a b)))) -(def or (lm (a b) (nand (nand a a) (nand b 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)))) +;; ------------ Relational Operators ------------- +;; equal, not euql, lower, greater w/ equal: -;; ------------ Boolean Logic ---------------------------------- - -;; greater than: (def gt (lm (a b) (lt b a))) - -;; equal: -(def eq (lm (a b) (and (not (lt a b)) (not (lt b a))))) - -;; not equal: (def neq (lm (a b) (not (eq a b)))) - -;; great equal: (def geq (lm (a b) (or (eq a b) (gt a b)))) - -;; lower equal: +(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)))) -;; ------------ Arithmetic ---------------------------------- -;; Available: Numbers on Z, add, inv +;; ------------ arithmetic operators ------------- +;; Functions to implement arithmetic based on +;; (Z,+,inv). +;; Implemented: sub, mult, mod, div, sum, inc, dec ;; sub: a - b: (def sub (lm (a b) (add a (inv b)))) @@ -45,12 +38,19 @@ (def modsubr (lm (a b last) (if (lt a 0) last (modsubr (sub a b) b a)))) +;; mod: a mod b (def mod (lm (a b) (modsubr a b 0))) -(def sum (lm (a b erg) +;; sum: sum(a, b, 0) +(def sumarg (lm (a b erg) (if (gt a b) erg (sum (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)))) ;; ------------ Open TODO's ---------------------------------- @@ -119,3 +119,8 @@ eval_fn_call: expected function body as list 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/tokenize.l b/tokenize.l index 44bcf20..92ead2a 100644 --- a/tokenize.l +++ b/tokenize.l @@ -1,4 +1,4 @@ -/* llm - litle lisp machinery: +/* ~~~~~~~~~~~~~~~~ llm - The little LISP machinery ~~~~~~~~~~~~~ * * Copyright (c) 2019 Christian Barthel * All rights reserved. @@ -29,9 +29,28 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * + * Goal: evaluate simple LISP forms in an + * read-eval-print stylized loop. supports: * - * Compile: flex tokenize.l - * cc lex.yy.c -lfl + * Data types: + * # Functions + * t,nil boolean (nand ) -> boolean + * "str" Strings (streq ) -> boolean + * abc Symboles (symeq ) -> boolean + * 1234 Number/Integer (inv ) -> -num + * (add ) -> num + * (lt ) -> boolean + * (num2bool ) -> boolean + * + * Special Forms: + * (def ..) ()) + * (if ) + * (quote a) + * + * + * Compile: flex tokenize.l && cc lex.yy.c -lfl -o llm + * Or: make; ./llm */ %{ #define _POSIX_C_SOURCE 200809L @@ -104,28 +123,6 @@ nil { yylval = 0; return BOOL; } . { err(1, "invalid symbol: %s\n", yytext); } %% -/* - * Goal: evaluate simple LISP forms in an - * read-eval-print stylized loop. supports: - * - * Data types: - * # Functions - * t,nil boolean (nand ) -> boolean - * "str" Strings (streq ) -> boolean - * abc Symboles (symeq ) -> boolean - * 1234 Number/Integer (inv ) -> -num - * (add ) -> num - * (lt ) -> boolean - * (num2bool ) -> boolean - * - * Special Forms: - * (def in the current environment to - * the evaluated form. - * (lm ( ..) ( )) - * (if ) - * (quote a) -> a - */ void reassure(int promised, const char *fmt, ...)