Skip to content

Commit

Permalink
Merge pull request #19 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 1fe24f1 + f39930f commit 7b4a99b
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 68 deletions.
1 change: 0 additions & 1 deletion dodger/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
[_ (error "parse error" s)])]
[_ (error "parse error" s)]))


;; Any -> Boolean
(define (datum? x)
(or (exact-integer? x)
Expand Down
42 changes: 42 additions & 0 deletions dodger/random.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#lang racket
(provide random-expr random-well-defined-expr)
(require "parse.rkt")

;; Randomly generate an expression
;; Note: this will often generate programs with type errors
(define (random-expr)
(parse (contract-random-generate expr/c)))

(define (random-well-defined-expr)
(parse (contract-random-generate expr-good/c)))

(define op1/c
(one-of/c 'add1 'sub1 'zero? 'char? 'integer->char 'char->integer))

(define expr/c
(flat-rec-contract e
boolean?
char?
(integer-in #f #f)
(list/c op1/c e)
(list/c 'if e e e)))

(define expr-good/c
(flat-murec-contract
([e-int (integer-in #f #f)
(list/c 'add1 e-int)
(list/c 'sub1 e-int)
(list/c 'char->integer e-char)
(list/c 'if e-any e-int e-int)]
[e-char char?
(list/c 'integer->char e-codepoint)
(list/c 'if e-any e-char e-char)]
[e-bool boolean?
(list/c 'char? e-any)
(list/c 'zero? e-int)
(list/c 'if e-any e-bool e-bool)]
[e-codepoint (integer-in 0 #xD7FF)
(integer-in #xE000 #x10FFFF)]
[e-any e-int e-char e-bool
(list/c 'if e-any e-any e-any)])
e-any))
1 change: 0 additions & 1 deletion dupe/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
[_ (error "parse error" s)])]
[_ (error "parse error" s)]))


;; Any -> Boolean
(define (datum? x)
(or (exact-integer? x)
Expand Down
12 changes: 12 additions & 0 deletions evildoer/gcd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <inttypes.h>

int64_t gcd(int64_t a, int64_t b) {
int remainder = a % b;

if (remainder == 0) {
return b;
}

return gcd(b, remainder);
}

1 change: 0 additions & 1 deletion evildoer/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
[_ (error "parse error" s)])]
[_ (error "parse error" s)]))


;; Any -> Boolean
(define (datum? x)
(or (exact-integer? x)
Expand Down
73 changes: 73 additions & 0 deletions evildoer/random.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#lang racket
(provide random-expr random-well-defined-expr)
(require "parse.rkt")

;; Randomly generate an expression
;; Note: this will often generate programs with type errors
(define (random-expr)
(parse (contract-random-generate expr/c)))

(define (random-well-defined-expr)
(parse (contract-random-generate expr-good/c)))

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

(define op1/c
(one-of/c 'add1 'sub1 'zero? 'char? 'integer->char 'char->integer
'write-byte 'eof-object?))

(define expr/c
(flat-rec-contract e
boolean?
char?
'eof
(integer-in #f #f)
(list/c op0/c)
(list/c op1/c e)
(list/c 'if e e e)
(list/c 'begin e e)))

(define expr-good/c
(flat-murec-contract
([e-int e-byte
(integer-in #f #f)
(list/c 'add1 e-int)
(list/c 'sub1 e-int)
(list/c 'char->integer e-char)
(list/c 'if e-any e-int e-int)
(list/c 'begin e-any e-int)]
[e-byte (integer-in 0 255)
(list/c 'if
(list/c 'eof-object? (list/c 'peek-byte))
e-byte
(list/c 'read-byte))
(list/c 'if
(list/c 'eof-object? (list/c 'peek-byte))
e-byte
(list/c 'peek-byte))
(list/c 'if e-any e-byte e-byte)
(list/c 'begin e-any e-byte)]
[e-char char?
(list/c 'integer->char e-codepoint)
(list/c 'if e-any e-char e-char)
(list/c 'begin e-any e-char)]
[e-bool boolean?
(list/c 'char? e-any)
(list/c 'zero? e-int)
(list/c 'eof-object? e-any)
(list/c 'if e-any e-bool e-bool)
(list/c 'begin e-any e-bool)]
[e-codepoint (integer-in 0 #xD7FF)
(integer-in #xE000 #x10FFFF)
(list/c 'if e-any e-codepoint e-codepoint)
(list/c 'begin e-any e-codepoint)]
[e-void (list/c 'void)
(list/c 'write-byte e-byte)
(list/c 'if e-any e-void e-void)
(list/c 'begin e-any e-void)]
[e-any e-int e-byte e-char e-bool e-codepoint e-void
'eof
(list/c 'if e-any e-any e-any)
(list/c 'begin e-any e-any)])
e-any))
1 change: 0 additions & 1 deletion extort/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
[_ (error "parse error" s)])]
[_ (error "parse error" s)]))


;; Any -> Boolean
(define (datum? x)
(or (exact-integer? x)
Expand Down
77 changes: 66 additions & 11 deletions extort/random.rkt
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
#lang racket
(provide random-expr)
(provide random-expr random-well-defined-expr)
(require "parse.rkt")

;; Randomly generate an expression
;; Note: this will often generate programs with type errors
(define (random-expr)
(parse
(contract-random-generate
(flat-rec-contract e
#t
#f
(integer-in #f #f)
(list/c 'add1 e)
(list/c 'sub1 e)
(list/c 'zero? e)
(list/c 'if e e e)))))
(parse (contract-random-generate expr/c)))

(define (random-well-defined-expr)
(parse (contract-random-generate expr-good/c)))

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

(define op1/c
(one-of/c 'add1 'sub1 'zero? 'char? 'integer->char 'char->integer
'write-byte 'eof-object?))

(define expr/c
(flat-rec-contract e
boolean?
char?
'eof
(integer-in #f #f)
(list/c op0/c)
(list/c op1/c e)
(list/c 'if e e e)
(list/c 'begin e e)))

(define expr-good/c
(flat-murec-contract
([e-int e-byte
(integer-in #f #f)
(list/c 'add1 e-int)
(list/c 'sub1 e-int)
(list/c 'char->integer e-char)
(list/c 'if e-any e-int e-int)
(list/c 'begin e-any e-int)]
[e-byte (integer-in 0 255)
(list/c 'if
(list/c 'eof-object? (list/c 'peek-byte))
e-byte
(list/c 'read-byte))
(list/c 'if
(list/c 'eof-object? (list/c 'peek-byte))
e-byte
(list/c 'peek-byte))
(list/c 'if e-any e-byte e-byte)
(list/c 'begin e-any e-byte)]
[e-char char?
(list/c 'integer->char e-codepoint)
(list/c 'if e-any e-char e-char)
(list/c 'begin e-any e-char)]
[e-bool boolean?
(list/c 'char? e-any)
(list/c 'zero? e-int)
(list/c 'eof-object? e-any)
(list/c 'if e-any e-bool e-bool)
(list/c 'begin e-any e-bool)]
[e-codepoint (integer-in 0 #xD7FF)
(integer-in #xE000 #x10FFFF)
(list/c 'if e-any e-codepoint e-codepoint)
(list/c 'begin e-any e-codepoint)]
[e-void (list/c 'void)
(list/c 'write-byte e-byte)
(list/c 'if e-any e-void e-void)
(list/c 'begin e-any e-void)]
[e-any e-int e-byte e-char e-bool e-codepoint e-void
'eof
(list/c 'if e-any e-any e-any)
(list/c 'begin e-any e-any)])
e-any))
26 changes: 16 additions & 10 deletions fraud/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
(provide parse parse-closed)
(require "ast.rkt")

;; S-Expr -> Expr
;; s:S-Expr -> e:ClosedExpr
;; Parse s into (a potentially open) expr e
(define (parse s)
(match (parse/acc s '() '())
[(list _ e) e]))

;; S-Expr -> ClosedExpr
;; s:S-Expr -> e:ClosedExpr
;; Parse s into closed expr e; signal an error when e is open
(define (parse-closed s)
(match (parse/acc s '() '())
[(list '() e) e]
[(list fvs e) (error "unbound identifiers" fvs)]))

;; S-Expr [Listof Id] [Listof Id] -> (list [Listof Id] Expr)
;; Parse s into expr and list of free variables
;; assuming bvs are bound, fvs are free
;; s:S-Expr bvs:[Listof Id] fvs:[Listof Id]
;; -> (list fvs-e:[Listof Id] e:Expr)
;; Parse s into expr e and list of free variables fvs-e,
;; assuming variables in bvs are bound and fvs are free.
(define (parse/acc s bvs fvs)
(define (rec s bvs fvs)
(match s
Expand Down Expand Up @@ -50,7 +53,10 @@
[_ (error "parse error" s)]))
(rec s bvs fvs))

;; S-Expr [Listof Id] [Listof Id] -> (list [Listof Id] [Listof Expr])
;; s:S-Expr bvs:[Listof Id] fvs:[Listof Id]
;; -> (list fvs-e:[Listof Id] es:[Listof Expr])
;; Parse s into a list of expr es and list of free variables fvs-e,
;; assuming variables in bvs are bound and fvs are free.
(define (parse-es/acc s bvs fvs)
(match s
['() (list fvs '())]
Expand All @@ -62,10 +68,10 @@
(list fvs (cons e es))])])]
[_ (error "parse error")]))

;; [Listof Any] -> (Any -> Boolean)
(define (not-in m)
(λ (x) (not (memq x m))))

;; xs:[Listof Any] -> p:(x:Any -> Boolean)
;; Produce a predicate p for things not in xs
(define (not-in xs)
(λ (x) (not (memq x xs))))

;; Any -> Boolean
(define (datum? x)
Expand Down
26 changes: 16 additions & 10 deletions hoax/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
(provide parse parse-closed)
(require "ast.rkt")

;; S-Expr -> Expr
;; s:S-Expr -> e:ClosedExpr
;; Parse s into (a potentially open) expr e
(define (parse s)
(match (parse/acc s '() '())
[(list _ e) e]))

;; S-Expr -> ClosedExpr
;; s:S-Expr -> e:ClosedExpr
;; Parse s into closed expr e; signal an error when e is open
(define (parse-closed s)
(match (parse/acc s '() '())
[(list '() e) e]
[(list fvs e) (error "unbound identifiers" fvs)]))

;; S-Expr [Listof Id] [Listof Id] -> (list [Listof Id] Expr)
;; Parse s into expr and list of free variables
;; assuming bvs are bound, fvs are free
;; s:S-Expr bvs:[Listof Id] fvs:[Listof Id]
;; -> (list fvs-e:[Listof Id] e:Expr)
;; Parse s into expr e and list of free variables fvs-e,
;; assuming variables in bvs are bound and fvs are free.
(define (parse/acc s bvs fvs)
(define (rec s bvs fvs)
(match s
Expand Down Expand Up @@ -53,7 +56,10 @@
[_ (error "parse error" s)]))
(rec s bvs fvs))

;; S-Expr [Listof Id] [Listof Id] -> (list [Listof Id] [Listof Expr])
;; s:S-Expr bvs:[Listof Id] fvs:[Listof Id]
;; -> (list fvs-e:[Listof Id] es:[Listof Expr])
;; Parse s into a list of expr es and list of free variables fvs-e,
;; assuming variables in bvs are bound and fvs are free.
(define (parse-es/acc s bvs fvs)
(match s
['() (list fvs '())]
Expand All @@ -65,10 +71,10 @@
(list fvs (cons e es))])])]
[_ (error "parse error")]))

;; [Listof Any] -> (Any -> Boolean)
(define (not-in m)
(λ (x) (not (memq x m))))

;; xs:[Listof Any] -> p:(x:Any -> Boolean)
;; Produce a predicate p for things not in xs
(define (not-in xs)
(λ (x) (not (memq x xs))))

;; Any -> Boolean
(define (datum? x)
Expand Down
7 changes: 1 addition & 6 deletions hustle/heap.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang racket
(provide alloc-box alloc-cons alloc-str heap-ref heap-set)
(provide alloc-box alloc-cons heap-ref heap-set)

;; Value* Heap -> Answer*
(define (alloc-box v h)
Expand All @@ -11,11 +11,6 @@
(cons (cons v2 (cons v1 h))
(list 'cons (length h))))

;; String Heap -> Answer*
(define (alloc-str s h)
(cons (append (reverse (string->list s)) (list (string-length s)) h)
(list 'str (length h))))

;; Heap Address -> Value*
(define (heap-ref h a)
(list-ref h (- (length h) (add1 a))))
Expand Down
Loading

0 comments on commit 7b4a99b

Please sign in to comment.