Add more helper functions for the stepping relation
This commit is contained in:
parent
30133f80e7
commit
a46cc06335
1 changed files with 39 additions and 1 deletions
|
@ -46,11 +46,18 @@
|
||||||
(define (update-stats f mstate)
|
(define (update-stats f mstate)
|
||||||
(struct-copy State mstate [stats (f (State-stats 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)))
|
(: allocate-node (-> Heap Node (Values Heap Addr)))
|
||||||
(define (allocate-node heap node)
|
(define (allocate-node heap node)
|
||||||
(let ([addr (new-addr)])
|
(let ([addr (new-addr)])
|
||||||
(values (hash-set heap addr node) 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)))
|
(: initialize-heap (-> (Listof CoreScDefn) (Values Heap Globals)))
|
||||||
(define (initialize-heap scs)
|
(define (initialize-heap scs)
|
||||||
(for/fold ([heap : Heap empty-heap]
|
(for/fold ([heap : Heap empty-heap]
|
||||||
|
@ -62,5 +69,36 @@
|
||||||
|
|
||||||
(: compile-core (-> (Listof CoreScDefn) State))
|
(: compile-core (-> (Listof CoreScDefn) State))
|
||||||
(define (compile-core scdefs)
|
(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)))
|
(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"))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue