OSDN Git Service

A start on simple functions.
authorsforman <sforman@hushmail.com>
Fri, 4 Aug 2023 17:38:52 +0000 (10:38 -0700)
committersforman <sforman@hushmail.com>
Fri, 4 Aug 2023 17:38:52 +0000 (10:38 -0700)
I'm sure there's a cooler way to do this, but it works.  (This is my
first Scheme code.)

implementations/scheme-chicken/joy.scm

index dd32e72..07c020b 100644 (file)
       (joy (cons (car expression) stack) (cdr expression) dict))))
 
 (define (joy-eval symbol stack expression dict)
-  (values (cons symbol stack) expression dict))
+  (define (is-it? name) (string=? symbol name))
+  (cond
+    ((is-it? "+") (values (joy-add stack) expression dict))
+    ((is-it? "-") (values (joy-sub stack) expression dict))
+    (else (values (cons symbol stack) expression dict))))
+
+(define (joy-add stack) (cons (+ (cadr stack) (car stack)) (cddr stack)))
+(define (joy-sub stack) (cons (- (cadr stack) (car stack)) (cddr stack)))
 
 
 (define (string-replace str from to)
@@ -72,6 +79,6 @@
     (joy '() (text-to-expression text) '())
     (joy-expression-to-string stack)))
 
-(display (doit "ab  cd [[  ]] 234 [true] false"))
+(display (doit "ab  cd [[  ]] 23 4 - [true] false"))
 (newline)