Skip to content

Commit

Permalink
Merge pull request #21 from cmsc430/crook
Browse files Browse the repository at this point in the history
Crook
  • Loading branch information
dvanhorn authored Dec 2, 2024
2 parents 23b9110 + 46ddc6d commit 8ade519
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
5 changes: 4 additions & 1 deletion evildoer/random.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang racket
(provide random-expr random-well-defined-expr)
(provide random-expr random-well-defined-expr random-input)
(require "parse.rkt")

;; Randomly generate an expression
Expand All @@ -10,6 +10,9 @@
(define (random-well-defined-expr)
(parse (contract-random-generate expr-good/c)))

(define (random-input)
(contract-random-generate string?))

(define op0/c
(one-of/c 'read-byte 'peek-byte 'void))

Expand Down
5 changes: 4 additions & 1 deletion extort/random.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang racket
(provide random-expr random-well-defined-expr)
(provide random-expr random-well-defined-expr random-input)
(require "parse.rkt")

;; Randomly generate an expression
Expand All @@ -10,6 +10,9 @@
(define (random-well-defined-expr)
(parse (contract-random-generate expr-good/c)))

(define (random-input)
(contract-random-generate string?))

(define op0/c
(one-of/c 'read-byte 'peek-byte 'void))

Expand Down
9 changes: 6 additions & 3 deletions hustle/heap.rkt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#lang racket
(provide alloc-box alloc-cons heap-ref heap-set)
(provide alloc-box alloc-cons heap-ref heap-set box-ptr cons-ptr)

(struct box-ptr (i))
(struct cons-ptr (i))

;; Value* Heap -> Answer*
(define (alloc-box v h)
(cons (cons v h)
(list 'box (length h))))
(box-ptr (length h))))

;; Value* Value* Heap -> Answer*
(define (alloc-cons v1 v2 h)
(cons (cons v2 (cons v1 h))
(list 'cons (length h))))
(cons-ptr (length h))))

;; Heap Address -> Value*
(define (heap-ref h a)
Expand Down
10 changes: 6 additions & 4 deletions hustle/interp-heap.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
;; | Eof
;; | Void
;; | '()
;; | (list 'box Address)
;; | (list 'cons Address)
;; | (box-ptr Address)
;; | (cons-ptr Address)

;; type Address = Natural

;; type Heap = (Listof Value*)
;; type REnv = (Listof (List Id Value*))

;; Expr -> Answer
(define (interp e)
(define (interp e)
(unload (interp-env-heap e '() '())))

;; Expr REnv Heap -> Answer*
Expand Down Expand Up @@ -55,7 +57,7 @@
(if v
(interp-env-heap e1 r h)
(interp-env-heap e2 r h))])]
[(Begin e1 e2)
[(Begin e1 e2)
(match (interp-env-heap e1 r h)
['err 'err]
[(cons h _) (interp-env-heap e2 r h)])]
Expand Down
8 changes: 4 additions & 4 deletions hustle/interp-prims-heap.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
[(list 'eof-object? v) (cons h (eof-object? v))]
[(list 'write-byte (? byte?)) (cons h (write-byte v))]
[(list 'box v) (alloc-box v h)]
[(list 'unbox (list 'box i)) (cons h (heap-ref h i))]
[(list 'car (list 'cons i)) (cons h (heap-ref h i))]
[(list 'cdr (list 'cons i)) (cons h (heap-ref h (add1 i)))]
[(list 'unbox (box-ptr i)) (cons h (heap-ref h i))]
[(list 'car (cons-ptr i)) (cons h (heap-ref h i))]
[(list 'cdr (cons-ptr i)) (cons h (heap-ref h (add1 i)))]
[(list 'empty? v) (cons h (empty? v))]
[_ 'err]))

Expand All @@ -26,7 +26,7 @@
[(list '+ (? integer? i1) (? integer? i2)) (cons h (+ i1 i2))]
[(list '- (? integer? i1) (? integer? i2)) (cons h (- i1 i2))]
[(list '< (? integer? i1) (? integer? i2)) (cons h (< i1 i2))]
[(list '= (? integer? i1) (? integer? i2)) (cons h (= i1 i2))]
[(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)))]
Expand Down
10 changes: 5 additions & 5 deletions hustle/unload.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
(match v
[(? integer?) v]
[(? boolean?) v]
[(? char?) v]
[(? char?) v]
[(? eof-object?) v]
[(? void?) v]
['() '()]
[(list 'box a)
[(? void?) v]
['() '()]
[(box-ptr a)
(box (unload-value (heap-ref h a) h))]
[(list 'cons a)
[(cons-ptr a)
(cons (unload-value (heap-ref h a) h)
(unload-value (heap-ref h (add1 a)) h))]))

0 comments on commit 8ade519

Please sign in to comment.