I need a simulator for the intermediate language.
This commit is contained in:
parent
0d4a3f859d
commit
e8c2d199aa
20
simulator-structs.rkt
Normal file
20
simulator-structs.rkt
Normal file
|
@ -0,0 +1,20 @@
|
|||
#lang typed/racket/base
|
||||
|
||||
(provide (all-defined-out))
|
||||
|
||||
(require "il-structs.rkt")
|
||||
|
||||
|
||||
(define-struct: machine ([val : Any]
|
||||
[proc : Any]
|
||||
[env : (Listof Any)]
|
||||
[control : (Listof frame)]
|
||||
|
||||
[pc : Natural] ;; program counter
|
||||
[text : (Vectorof Statement)] ;; text of the program
|
||||
))
|
||||
|
||||
|
||||
(define-struct: frame ([return : Symbol]))
|
||||
|
||||
|
76
simulator.rkt
Normal file
76
simulator.rkt
Normal file
|
@ -0,0 +1,76 @@
|
|||
#lang typed/racket/base
|
||||
|
||||
;; An evaluator for the intermediate language, so I can do experiments.
|
||||
|
||||
(require "il-structs.rkt"
|
||||
"simulator-structs.rkt"
|
||||
racket/list
|
||||
racket/match)
|
||||
|
||||
(provide new-machine can-step? step)
|
||||
|
||||
|
||||
(: new-machine ((Listof Statement) -> machine))
|
||||
(define (new-machine program-text)
|
||||
(make-machine (void) (void) '() '() 0 (list->vector program-text)))
|
||||
|
||||
|
||||
(: can-step? (machine -> Boolean))
|
||||
(define (can-step? m)
|
||||
#t)
|
||||
|
||||
|
||||
(: step (machine -> machine))
|
||||
(define (step m)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
m
|
||||
]))
|
||||
|
||||
(: val-update (machine Any -> machine))
|
||||
(define (val-update m v)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
(make-machine v proc env control pc text)]))
|
||||
|
||||
(: proc-update (machine Any -> machine))
|
||||
(define (proc-update m v)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
(make-machine val v env control pc text)]))
|
||||
|
||||
(: env-push (machine Any -> machine))
|
||||
(define (env-push m v)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
(make-machine val proc (cons v env) control pc text)]))
|
||||
|
||||
(: env-ref (machine Natural -> Any))
|
||||
(define (env-ref m i)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
(list-ref env i)]))
|
||||
|
||||
(: env-mutate (machine Natural Any -> machine))
|
||||
(define (env-mutate m i v)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
(make-machine val proc (list-replace env i v) control pc text)]))
|
||||
|
||||
(: list-replace (All (A) (Listof A) Natural A -> (Listof A)))
|
||||
(define (list-replace l i v)
|
||||
(cond
|
||||
[(= i 0)
|
||||
(cons v (rest l))]
|
||||
[else
|
||||
(cons (first l)
|
||||
(list-replace (rest l) (sub1 i) v))]))
|
||||
|
||||
|
||||
(: env-pop (machine Natural Natural -> machine))
|
||||
(define (env-pop m n skip)
|
||||
(match m
|
||||
[(struct machine (val proc env control pc text))
|
||||
(make-machine val proc (append (take env skip)
|
||||
(drop env (+ skip n)))
|
||||
control pc text)]))
|
Loading…
Reference in New Issue
Block a user