From b116c2c98b1fc3b984e98ce29f25955d041b0349 Mon Sep 17 00:00:00 2001 From: sforman Date: Fri, 20 Oct 2023 11:42:50 -0700 Subject: [PATCH] A start on error reporting. 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 | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/implementations/scheme-chicken/joy.scm b/implementations/scheme-chicken/joy.scm index 8880ae4..8de7620 100644 --- a/implementations/scheme-chicken/joy.scm +++ b/implementations/scheme-chicken/joy.scm @@ -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)) @@ -106,6 +107,16 @@ (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.")))))) + + ; ██████╗ ██████╗ ███╗ ███╗██████╗ ██╗███╗ ██╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗ ;██╔════╝██╔═══██╗████╗ ████║██╔══██╗██║████╗ ██║██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝ ;██║ ██║ ██║██╔████╔██║██████╔╝██║██╔██╗ ██║███████║ ██║ ██║ ██║██████╔╝███████╗ @@ -243,7 +254,11 @@ (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))))