Add semantics
This commit is contained in:
parent
572d95f108
commit
f3b2d11279
2 changed files with 49 additions and 1 deletions
7
ast.rkt
7
ast.rkt
|
@ -29,4 +29,9 @@
|
|||
(define-type CoreBinds (Binds Name))
|
||||
(define-type CoreAlts (Alts Name))
|
||||
|
||||
(provide CoreExpr CoreScDefn CoreProgram CoreBind CoreAlt Expr Bind Alt Name Let Arity ConId CoreAlts CoreBinds)
|
||||
(: scdefn-name (All (A) (-> (ScDefn A) Name) ))
|
||||
(define (scdefn-name a)
|
||||
(match a
|
||||
[(list 'define (cons n _) _) n]))
|
||||
|
||||
(provide CoreExpr CoreScDefn CoreProgram CoreBind CoreAlt Expr Bind Alt Name Let Arity ConId CoreAlts CoreBinds scdefn-name)
|
||||
|
|
43
semantics.rkt
Normal file
43
semantics.rkt
Normal file
|
@ -0,0 +1,43 @@
|
|||
#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 ])))
|
Loading…
Add table
Add a link
Reference in a new issue