OSDN Git Service

A start on error reporting.
authorsforman <sforman@hushmail.com>
Fri, 20 Oct 2023 18:42:50 +0000 (11:42 -0700)
committersforman <sforman@hushmail.com>
Fri, 20 Oct 2023 18:42:50 +0000 (11:42 -0700)
I outfitted `rest` with some error reporting and tried out exception
handling.  So far so good.

One issue is that the dictionary is not a persistent datastructure, so
if there was an "inscribe" command (it's not yet implemented for this
Scheme implementation) that might be slightly problematical.  (If an
error were to occur in an evaluation that had already entered new
definitions into the dict then those definitions would "survive" into
the rest of the REPL session (or whatever.))

I'll look into persistent dict for Scheme.

implementations/scheme-chicken/joy.scm

index 8880ae4..8de7620 100644 (file)
@@ -26,6 +26,7 @@
 
 (import (chicken io))
 (import (chicken string))
+(import srfi-12)
 (import srfi-69)
 (import matchable)
 
@@ -79,7 +80,7 @@
     ((concat) (joy-func append stack expression dict))
     ((cons) (joy-func cons stack expression dict))
     ((first) (values (cons (caar stack) (cdr stack)) expression dict))
-    ((rest)  (values (cons (cdar stack) (cdr stack)) expression dict))
+    ((rest)  (values (joy-rest stack) expression dict))
 
     ((i) (joy-i stack expression dict))
     ((dip) (joy-dip stack expression dict))
         (else #t)))
 
 
+(define (joy-rest stack)
+  (match stack
+    (() (abort "Not enough values on Stack"))
+    ((head . tail)
+      (match head
+        (() (abort "Cannot take rest of empty list."))
+        ((_ . the_rest) (cons the_rest tail))
+        (_ (abort "Not a list."))))))
+
+
 ; ██████╗ ██████╗ ███╗   ███╗██████╗ ██╗███╗   ██╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
 ;██╔════╝██╔═══██╗████╗ ████║██╔══██╗██║████╗  ██║██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
 ;██║     ██║   ██║██╔████╔██║██████╔╝██║██╔██╗ ██║███████║   ██║   ██║   ██║██████╔╝███████╗
 (define (main-loop stack0 dict0)
   (let ((text (prompt)))
     (if (not (eof-object? text))
-      (receive (stack dict) (joy stack0 (text->expression text) dict0)
+      (receive (stack dict)
+        (handle-exceptions exn
+          (begin (display exn) (newline)
+            (values stack0 dict0))
+          (joy stack0 (text->expression text) dict0))
         (print (joy-expression->string (reverse stack)))
         (main-loop stack dict))
       (print))))