From a46cc063350c78f2ec1a08be98bebccbfbf3b6aa Mon Sep 17 00:00:00 2001 From: Yiyun Liu Date: Wed, 28 May 2025 00:00:13 -0400 Subject: [PATCH] Add more helper functions for the stepping relation --- semantics.rkt | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/semantics.rkt b/semantics.rkt index befd578..28e086e 100644 --- a/semantics.rkt +++ b/semantics.rkt @@ -46,11 +46,18 @@ (define (update-stats f mstate) (struct-copy State mstate [stats (f (State-stats mstate))])) +(: update-stack (-> (-> Stack Stack) State State)) +(define (update-stack f mstate) + (struct-copy State mstate [stack (f (State-stack mstate))])) + (: allocate-node (-> Heap Node (Values Heap Addr))) (define (allocate-node heap node) (let ([addr (new-addr)]) (values (hash-set heap addr node) addr))) +(: lookup-node (-> Heap Addr Node)) +(define lookup-node hash-ref) + (: initialize-heap (-> (Listof CoreScDefn) (Values Heap Globals))) (define (initialize-heap scs) (for/fold ([heap : Heap empty-heap] @@ -62,5 +69,36 @@ (: compile-core (-> (Listof CoreScDefn) State)) (define (compile-core scdefs) - (let-values ([(heap globals) (initialize-heap scdefs)]) + (let-values ([(heap globals) (initialize-heap (append scdefs prelude))]) (State empty-stack empty-dump heap globals empty-stats))) + + +(: data-node? (-> Node Boolean)) +(define data-node? integer?) + +(: final-state? (-> State Boolean)) +(define (final-state? st) + (match (State-stack st) + [(list n) (data-node? (lookup-node (State-heap st) n))] + [_ #f])) + +(: step (-> State State)) +(define (step state) + (let ([stack (State-stack state)]) + (match stack + [(cons addr _) + (step-node state (lookup-node (State-heap state) addr))] + ['() (error "Invalid state")]))) + +(: step-node (-> State Node State)) +(define (step-node 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 name args body)])) + +(: step-sc (-> State Name (Listof Name) CoreExpr State)) +(define (step-sc state sc args body) + + (error "to be implemented"))