comments, format, example code updated
format and more notes
This commit is contained in:
parent
712dcfc931
commit
293e6b80e4
41
sample.llm
41
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 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 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 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)))
|
(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))))
|
(def neq (lm (a b) (not (eq a b))))
|
||||||
|
|
||||||
;; great equal:
|
|
||||||
(def geq (lm (a b) (or (eq a b) (gt 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)))))
|
||||||
;; lower equal:
|
|
||||||
(def leq (lm (a b) (or (eq a b) (lt a b))))
|
(def leq (lm (a b) (or (eq a b) (lt a b))))
|
||||||
|
|
||||||
;; ------------ Arithmetic ----------------------------------
|
;; ------------ arithmetic operators -------------
|
||||||
;; Available: Numbers on Z, add, inv
|
;; Functions to implement arithmetic based on
|
||||||
|
;; (Z,+,inv).
|
||||||
|
;; Implemented: sub, mult, mod, div, sum, inc, dec
|
||||||
|
|
||||||
;; sub: a - b:
|
;; sub: a - b:
|
||||||
(def sub (lm (a b) (add a (inv b))))
|
(def sub (lm (a b) (add a (inv b))))
|
||||||
|
@ -45,12 +38,19 @@
|
||||||
(def modsubr (lm (a b last)
|
(def modsubr (lm (a b last)
|
||||||
(if (lt a 0) last
|
(if (lt a 0) last
|
||||||
(modsubr (sub a b) b a))))
|
(modsubr (sub a b) b a))))
|
||||||
|
;; mod: a mod b
|
||||||
(def mod (lm (a b) (modsubr a b 0)))
|
(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)
|
(if (gt a b)
|
||||||
erg
|
erg
|
||||||
(sum (add a 1) b (add erg a)))))
|
(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 ----------------------------------
|
;; ------------ Open TODO's ----------------------------------
|
||||||
|
|
||||||
|
@ -119,3 +119,8 @@ eval_fn_call: expected function body as list
|
||||||
zurückgibt gehts nicht
|
zurückgibt gehts nicht
|
||||||
- und im allgemeinen findet man nicht die variable in einem
|
- und im allgemeinen findet man nicht die variable in einem
|
||||||
funktionsaufruf einer zurückgegebenen funktion
|
funktionsaufruf einer zurückgegebenen funktion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(def tripe (lm (x) (lm (y) (lm (z) (add x (add y z))))))
|
||||||
|
|
47
tokenize.l
47
tokenize.l
|
@ -1,4 +1,4 @@
|
||||||
/* llm - litle lisp machinery:
|
/* ~~~~~~~~~~~~~~~~ llm - The little LISP machinery ~~~~~~~~~~~~~
|
||||||
*
|
*
|
||||||
* Copyright (c) 2019 Christian Barthel <bch@online.de>
|
* Copyright (c) 2019 Christian Barthel <bch@online.de>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
@ -29,9 +29,28 @@
|
||||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||||
* OF SUCH DAMAGE.
|
* OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
|
* Goal: evaluate simple LISP forms in an
|
||||||
|
* read-eval-print stylized loop. supports:
|
||||||
*
|
*
|
||||||
* Compile: flex tokenize.l
|
* Data types:
|
||||||
* cc lex.yy.c -lfl
|
* # 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
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
@ -104,28 +123,6 @@ nil { yylval = 0; return BOOL; }
|
||||||
. { err(1, "invalid symbol: %s\n", yytext); }
|
. { 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
|
void
|
||||||
reassure(int promised, const char *fmt, ...)
|
reassure(int promised, const char *fmt, ...)
|
||||||
|
|
Loading…
Reference in New Issue