From 13c730f9d8dbeae6d9c571e1ce06bd49bf72c2c8 Mon Sep 17 00:00:00 2001 From: Yiyun Liu Date: Wed, 4 Jun 2025 23:33:02 -0400 Subject: [PATCH] Update the implementation of step-sc so it errors when unsaturated --- semantics.rkt | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/semantics.rkt b/semantics.rkt index 968753a..ab4c45f 100644 --- a/semantics.rkt +++ b/semantics.rkt @@ -90,33 +90,29 @@ (let ([stack (State-stack state)]) (match stack [(cons addr rstack) - (step-node state (lookup-node (State-heap state) addr))] + (step-node rstack state (lookup-node (State-heap state) addr))] ['() (error "Invalid state")]))) -(: step-node (-> State Node State)) -(define (step-node st n) +(: step-node (-> Stack State Node State)) +(define (step-node rstack st n) (match n [(? integer?) (error "A number cannot be stepped any further!")] [(list a _) (update-stack (λ (x) (cons a x)) st)] [(list 'define (list name args ...) body) - (step-sc st args body)])) + (step-sc rstack st args body)])) -(: step-sc (-> State (Listof Name) CoreExpr State)) -(define (step-sc state arg-names body) - (let* ([stack (State-stack state)] - [heap (State-heap state)] - [new-env : Globals - (for/fold ([globals : Globals (State-globals state)]) - ([arg-name : Name arg-names] - [addr : Addr (if (null? stack) - (error "impossible") - (cdr stack))]) - (update-globals globals arg-name addr))] - ;; [new-stack : Stack - ;; (drop stack (add1 (length arg-names)))] - ) - (let-values ([(new-heap addr) (instantiate-body body new-env heap)]) - (let ([new-stack (cons addr (drop stack (add1 (length arg-names))))]) +(: step-sc (-> Stack State (Listof Name) CoreExpr State)) +(define (step-sc rstack state arg-names body) + (let ([heap (State-heap state)]) + (let*-values ([(new-env new-stack) + (for/fold ([globals : Globals (State-globals state)] + [stack : Stack rstack]) + ([arg-name : Name arg-names]) + (if (null? stack) + (error "Not enough arguments to apply the supercombinator") + (values (update-globals globals arg-name (car stack)) (cdr stack))))] + [(new-heap addr) (instantiate-body body new-env heap)]) + (let ([new-stack (cons addr new-stack)]) (struct-copy State state [stack new-stack] [heap new-heap]))))) (: instantiate-body (-> CoreExpr Globals Heap (Values Heap Addr)))