core-in-racket/semantics.rkt
2025-05-25 22:16:02 -04:00

43 lines
1.3 KiB
Racket
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#lang typed/racket
(require "ast.rkt")
(struct State ([stack : Stack] [dump : Dump] [heap : Heap] [globals : Globals] [stats : Stats]))
(define-type Addr Symbol)
(define-type Stack (Listof Addr))
(define-type Dump Null)
(define-type Heap (Immutable-HashTable Addr Node))
(define-type Node ( (List Addr Addr) CoreScDefn Integer))
(define-type Globals (Immutable-HashTable Name Addr))
(define-type Stats Nonnegative-Integer)
(: new-addr (-> Addr))
(define (new-addr)
(gensym))
(: initial-dump Dump)
(define initial-dump '())
(: initial-stats Stats)
(define initial-stats 0)
(: incr-stats (-> Stats Stats))
(define incr-stats add1)
(: update-stats (-> (-> Stats Stats) State State))
(define (update-stats f mstate)
(struct-copy State mstate [stats (f (State-stats mstate))]))
(: allocate-node (-> Heap Node (Values Heap Addr)))
(define (allocate-node heap node)
(let ([addr (new-addr)])
(values (hash-set heap addr node) addr)))
(: allocate-sc (-> Heap CoreScDefn (Values Heap (Pair Name Addr))))
(define (allocate-sc heap scdef)
(let-values ([(heap addr) (allocate-node heap scdef)])
(values heap (cons (scdefn-name scdef) addr))))
;; (: initial-heap (-> (Listof CoreScDefn) (Values Heap Globals)))
;; (define (initial-heap scs)
;; (for/fold ([heap ])))