From 8bb8953816c84c5e720d9db040f98f3bd419656f Mon Sep 17 00:00:00 2001 From: sforman Date: Sat, 21 Oct 2023 11:08:01 -0700 Subject: [PATCH] pop-any and pop-list. Runtime type-checking. --- implementations/scheme-chicken/joy.scm | 34 +++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/implementations/scheme-chicken/joy.scm b/implementations/scheme-chicken/joy.scm index 44c6881..919257f 100644 --- a/implementations/scheme-chicken/joy.scm +++ b/implementations/scheme-chicken/joy.scm @@ -108,14 +108,26 @@ (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 (joy-rest stack0) + (receive (el stack) (pop-list stack0) + (if (null-list? el) + (abort "Cannot take rest of empty list.") + (cons (cdr el) stack)))) + + +(define (pop-any stack) + (if (null-list? stack) + (abort "Not enough values on Stack") + (car+cdr stack))) + +(define (pop-list stack) + (receive (term rest) (pop-any stack) + (if (list? term) + (values term rest) + (abort "Not a list.")))) + + + ; ██████╗ ██████╗ ███╗ ███╗██████╗ ██╗███╗ ██╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗ @@ -254,14 +266,14 @@ (define (main-loop stack0 dict0) (let ((text (prompt))) - (if (not (eof-object? text)) + (if (eof-object? text) + (print) (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)))) + (main-loop stack dict))))) (define (joy-trace stack expression) (print (conc (joy-expression->string (reverse stack)) " . " (joy-expression->string expression))))