From d827f9f68355b5d2cac7436c6a9f1acf96dfc82c Mon Sep 17 00:00:00 2001 From: Yiyun Liu Date: Sat, 7 Jun 2025 19:05:26 -0400 Subject: [PATCH] Add in-place update --- semantics.rkt | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/semantics.rkt b/semantics.rkt index 650bfd6..4b02bae 100644 --- a/semantics.rkt +++ b/semantics.rkt @@ -150,18 +150,18 @@ (let ([stack (State-stack state)]) (match stack [(cons addr rstack) - (step-node rstack state (lookup-node (State-heap state) addr))] + (step-node addr rstack state (lookup-node (State-heap state) addr))] ['() (error "Invalid state")]))) -(: step-node (-> Stack State Node State)) -(define (step-node rstack st n) +(: step-node (-> Addr Stack State Node State)) +(define (step-node addr rstack st n) (cond [(integer-node? n) (error "A number cannot be stepped any further!")] [(ap-node? n) (update-stack (λ (stack) (push-addr stack (ap-fun n))) st)] [(undef-node? n) (error "An undefined node is being stepped!")] [(sc-node? n) (let-values ([(args body) (match-sc n)]) - (step-sc rstack st args body))] + (step-sc addr rstack st args body))] [(indirect-node? n) (update-stack (λ (_) (push-addr rstack (indirect-addr n))) st)])) @@ -171,18 +171,19 @@ [(list _ a) a] [_ (error "Not an application node")])) -(: step-sc (-> Stack State (Listof Name) CoreExpr State)) -(define (step-sc rstack state arg-names body) +(: step-sc (-> Addr Stack State (Listof Name) CoreExpr State)) +(define (step-sc root rstack state arg-names body) (let ([heap (State-heap state)]) - (let*-values ([(new-env new-stack) + (let*-values ([(new-env new-stack root) (for/fold ([globals : Globals (State-globals state)] - [stack : Stack rstack]) + [stack : Stack rstack] + [root : Addr root]) ([arg-name : Name arg-names]) (if (null? stack) (error "Not enough arguments to apply the supercombinator") - (values (update-globals globals arg-name (get-arg heap (car stack))) (cdr stack))))] - [(heap addr) (instantiate-and-allocate body new-env heap)]) - (let ([new-stack (cons addr new-stack)]) + (values (update-globals globals arg-name (get-arg heap (car stack))) (cdr stack) (car stack))))]) + (let* ([heap (instantiate-and-assign body new-env heap root)] + [new-stack (cons root new-stack)]) (struct-copy State state [stack new-stack] [heap heap]))))) (: instantiate-and-allocate (-> CoreExpr Globals Heap (Values Heap Addr)))