comments, format, example code updated

format and more notes
This commit is contained in:
Christian Barthel 2019-07-08 20:17:57 +02:00
parent 712dcfc931
commit 293e6b80e4
2 changed files with 45 additions and 43 deletions

View File

@ -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))))))

View File

@ -1,4 +1,4 @@
/* llm - litle lisp machinery:
/* ~~~~~~~~~~~~~~~~ llm - The little LISP machinery ~~~~~~~~~~~~~
*
* Copyright (c) 2019 Christian Barthel <bch@online.de>
* 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 <bool> <bool>) -> boolean
* "str" Strings (streq <string> <string>) -> boolean
* abc Symboles (symeq <sym> <sym>) -> boolean
* 1234 Number/Integer (inv <num>) -> -num
* (add <num> <num>) -> num
* (lt <num> <num>) -> boolean
* (num2bool <num>) -> boolean
*
* Special Forms:
* (def <var> <symbol,number,#function)
* (lm (<symbol> ..) (<symbol>))
* (if <bool> <eval if true> <eval if false>)
* (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 <bool> <bool>) -> boolean
* "str" Strings (streq <string> <string>) -> boolean
* abc Symboles (symeq <sym> <sym>) -> boolean
* 1234 Number/Integer (inv <num>) -> -num
* (add <num> <num>) -> num
* (lt <num> <num>) -> boolean
* (num2bool <num>) -> boolean
*
* Special Forms:
* (def <var> <symbol,number,#function)
* Bind symbol <var> in the current environment to
* the evaluated form.
* (lm (<symbol> ..) (<sym..> ))
* (if <bool> <body> <else>)
* (quote a) -> a
*/
void
reassure(int promised, const char *fmt, ...)