Skip to content

Commit

Permalink
Merge pull request #24 from cmsc430/crook
Browse files Browse the repository at this point in the history
Crook
  • Loading branch information
dvanhorn authored Dec 3, 2024
2 parents 7ae6c06 + 1c550f9 commit d5528ca
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
3 changes: 2 additions & 1 deletion hustle/env.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

;; Env Variable Value -> Value
(define (ext r x i)
(cons (list x i) r))
(cons (list x i) r))

1 change: 1 addition & 0 deletions hustle/heap.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
;; Heap Address Value* -> Heap
(define (heap-set h i v)
(list-set h (- (length h) i 1) v))

13 changes: 6 additions & 7 deletions hustle/interp-heap.rkt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#lang racket
(provide interp interp-env-heap)
(require "env.rkt"
"unload.rkt"
"interp-prims-heap.rkt"
"ast.rkt")
(require "env.rkt")
(require "unload.rkt")
(require "interp-prims-heap.rkt")
(require "ast.rkt")

;; type Answer* =
;; | (cons Heap Value*)
Expand Down Expand Up @@ -34,9 +34,7 @@
[(Lit d) (cons h d)]
[(Eof) (cons h eof)]
[(Var x) (cons h (lookup r x))]
[(Prim0 'void) (cons h (void))]
[(Prim0 'peek-byte) (cons h (peek-byte))]
[(Prim0 'read-byte) (cons h (read-byte))]
[(Prim0 p) (interp-prim0 p h)]
[(Prim1 p e)
(match (interp-env-heap e r h)
['err 'err]
Expand Down Expand Up @@ -66,3 +64,4 @@
['err 'err]
[(cons h v)
(interp-env-heap e2 (ext r x v) h)])]))

17 changes: 13 additions & 4 deletions hustle/interp-prims-heap.rkt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#lang racket
(provide interp-prim1 interp-prim2)
(provide interp-prim0 interp-prim1 interp-prim2)
(require "heap.rkt")

;; Op0 Heap -> Answer*
(define (interp-prim0 op h)
(match op
['read-byte (cons h (read-byte))]
['peek-byte (cons h (peek-byte))]
['void (cons h (void))]))

;; Op1 Value* Heap -> Answer*
(define (interp-prim1 p v h)
(match (list p v)
Expand Down Expand Up @@ -29,13 +36,15 @@
[(list '= (? integer? i1) (? integer? i2)) (cons h (= i1 i2))]
[(list 'eq? v1 v2)
(match (list v1 v2)
[(list (list t1 a1) (list t2 a2)) (cons h (and (eq? t1 t2) (= a1 a2)))]
[_ (cons h (eqv? v1 v2))])]
[(list 'cons v1 v2) (alloc-cons v1 v2 h)]
[(list (cons-ptr a1) (cons-ptr a2)) (cons h (= a1 a2))]
[(list (box-ptr a1) (box-ptr a2)) (cons h (= a1 a2))]
[_ (cons h (eqv? v1 v2))])]
[(list 'cons v1 v2) (alloc-cons v1 v2 h)]
[_ 'err]))

;; Any -> Boolean
(define (codepoint? v)
(and (integer? v)
(or (<= 0 v 55295)
(<= 57344 v 1114111))))

10 changes: 6 additions & 4 deletions hustle/test/interp-heap.rkt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#lang racket
(require "test-runner.rkt"
"../parse.rkt"
"../interp-heap.rkt"
"../interp-io.rkt")
(require "test-runner.rkt")
(require "../parse.rkt")
(require "../interp-heap.rkt")
(require "../interp-io.rkt")

(test (λ (e) (interp (parse e))))

;; FIXME: this is not running a heap-based interpreter!
(test/io (λ (s e) (interp/io (parse e) s)))

1 change: 1 addition & 0 deletions hustle/unload.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
[(cons-ptr a)
(cons (unload-value (heap-ref h a) h)
(unload-value (heap-ref h (add1 a)) h))]))

0 comments on commit d5528ca

Please sign in to comment.