simulator.rkt skeleton
This commit is contained in:
parent
e8c2d199aa
commit
3b9722dcc6
|
@ -22,10 +22,40 @@
|
||||||
|
|
||||||
(: step (machine -> machine))
|
(: step (machine -> machine))
|
||||||
(define (step m)
|
(define (step m)
|
||||||
|
(let: ([i : Statement (current-instruction m)])
|
||||||
|
(cond
|
||||||
|
[(symbol? i)
|
||||||
|
(increment-pc m)]
|
||||||
|
[(AssignImmediateStatement? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(AssignPrimOpStatement? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(PerformStatement? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(GotoStatement? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(TestAndBranchStatement? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(PopEnvironment? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(PushEnvironment? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(PushControlFrame? i)
|
||||||
|
(error 'step)]
|
||||||
|
[(PopControlFrame? i)
|
||||||
|
(error 'step)])))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(: current-instruction (machine -> Statement))
|
||||||
|
(define (current-instruction m)
|
||||||
(match m
|
(match m
|
||||||
[(struct machine (val proc env control pc text))
|
[(struct machine (val proc env control pc text))
|
||||||
m
|
(vector-ref text pc)]))
|
||||||
]))
|
|
||||||
|
|
||||||
|
|
||||||
(: val-update (machine Any -> machine))
|
(: val-update (machine Any -> machine))
|
||||||
(define (val-update m v)
|
(define (val-update m v)
|
||||||
|
@ -74,3 +104,41 @@
|
||||||
(make-machine val proc (append (take env skip)
|
(make-machine val proc (append (take env skip)
|
||||||
(drop env (+ skip n)))
|
(drop env (+ skip n)))
|
||||||
control pc text)]))
|
control pc text)]))
|
||||||
|
|
||||||
|
|
||||||
|
(: control-push (machine Symbol -> machine))
|
||||||
|
(define (control-push m l)
|
||||||
|
(match m
|
||||||
|
[(struct machine (val proc env control pc text))
|
||||||
|
(make-machine val proc env (cons (make-frame l) control) pc text)]))
|
||||||
|
|
||||||
|
|
||||||
|
(: control-pop (machine Symbol -> (values machine Symbol)))
|
||||||
|
(define (control-pop m l)
|
||||||
|
(match m
|
||||||
|
[(struct machine (val proc env control pc text))
|
||||||
|
(values (make-machine val proc env (rest control) pc text)
|
||||||
|
(frame-return (first control)))]))
|
||||||
|
|
||||||
|
(: increment-pc (machine -> machine))
|
||||||
|
(define (increment-pc m)
|
||||||
|
(match m
|
||||||
|
[(struct machine (val proc env control pc text))
|
||||||
|
(make-machine val proc env control (add1 pc) text)]))
|
||||||
|
|
||||||
|
|
||||||
|
(: jump (machine Symbol -> machine))
|
||||||
|
(define (jump m l)
|
||||||
|
(match m
|
||||||
|
[(struct machine (val proc env control pc text))
|
||||||
|
(make-machine val proc env control (vector-find text l) text)]))
|
||||||
|
|
||||||
|
|
||||||
|
(: vector-find (All (A) (Vectorof A) A -> Natural))
|
||||||
|
(define (vector-find vec x)
|
||||||
|
(let: loop : Natural ([i : Natural 0])
|
||||||
|
(cond
|
||||||
|
[(eq? (vector-ref vec i) x)
|
||||||
|
i]
|
||||||
|
[else
|
||||||
|
(loop (add1 i))])))
|
Loading…
Reference in New Issue
Block a user