Skip to content

Commit

Permalink
Con and Dupe files.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvanhorn committed Sep 27, 2023
1 parent df7fe03 commit 049889d
Show file tree
Hide file tree
Showing 30 changed files with 473 additions and 3 deletions.
13 changes: 13 additions & 0 deletions langs/con/ast.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#lang racket
(provide Lit Prim1 IfZero)

;; type Expr = (Lit Integer)
;; | (Prim1 Op1 Expr)
;; | (IfZero Expr Expr Expr)

;; type Op1 = 'add1 | 'sub1

(struct Lit (i) #:prefab)
(struct Prim1 (p e) #:prefab)
(struct IfZero (e1 e2 e3) #:prefab)

13 changes: 13 additions & 0 deletions langs/con/compile-ops.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#lang racket
(provide compile-op1)
(require "ast.rkt")
(require a86/ast)

(define rax 'rax)

;; Op1 -> Asm
(define (compile-op1 p)
(match p
['add1 (Add rax 1)]
['sub1 (Sub rax 1)]))

13 changes: 13 additions & 0 deletions langs/con/compile-stdin.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#lang racket
(provide main)
(require "parse.rkt")
(require "compile.rkt")
(require a86/printer)

;; -> Void
;; Compile contents of stdin,
;; emit asm code on stdout
(define (main)
(read-line) ; ignore #lang racket line
(asm-display (compile (parse (read)))))

39 changes: 39 additions & 0 deletions langs/con/compile.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#lang racket
(provide (all-defined-out))
(require "ast.rkt")
(require "compile-ops.rkt")
(require a86/ast)

(define rax 'rax)
;; Expr -> Asm
(define (compile e)
(prog (Global 'entry)
(Label 'entry)
(compile-e e)
(Ret)))

;; Expr -> Asm
(define (compile-e e)
(match e
[(Lit i) (seq (Mov rax i))]
[(Prim1 p e) (compile-prim1 p e)]
[(IfZero e1 e2 e3)
(compile-ifzero e1 e2 e3)]))

;; Op1 Expr -> Asm
(define (compile-prim1 p e)
(seq (compile-e e)
(compile-op1 p)))

;; Expr Expr Expr -> Asm
(define (compile-ifzero e1 e2 e3)
(let ((l1 (gensym 'ifz))
(l2 (gensym 'ifz)))
(seq (compile-e e1)
(Cmp rax 0)
(Jne l1)
(compile-e e2)
(Jmp l2)
(Label l1)
(compile-e e3)
(Label l2))))
9 changes: 9 additions & 0 deletions langs/con/interp-prim.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#lang racket
(provide interp-prim1)

;; Op1 Integer -> Integer
(define (interp-prim1 op i)
(match op
['add1 (add1 i)]
['sub1 (sub1 i)]))

12 changes: 12 additions & 0 deletions langs/con/interp-stdin.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#lang racket
(provide main)
(require "parse.rkt")
(require "interp.rkt")

;; -> Void
;; Parse and interpret contents of stdin,
;; print result on stdout
(define (main)
(read-line) ; ignore #lang racket line
(println (interp (parse (read)))))

16 changes: 16 additions & 0 deletions langs/con/interp.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#lang racket
(provide interp)
(require "ast.rkt")
(require "interp-prim.rkt")

;; Expr -> Integer
(define (interp e)
(match e
[(Lit i) i]
[(Prim1 p e) (interp-prim1 p (interp e))]
[(IfZero e1 e2 e3)
(if (zero? (interp e1))
(interp e2)
(interp e3))]))


13 changes: 13 additions & 0 deletions langs/con/main.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#lang racket
(require "ast.rkt")
(require "parse.rkt")
(require "interp.rkt")
(require "compile.rkt")
(require "run.rkt")
(provide (all-from-out "ast.rkt"))
(provide (all-from-out "parse.rkt"))
(provide (all-from-out "interp.rkt"))
(provide (all-from-out "compile.rkt"))
(provide (all-from-out "run.rkt"))


17 changes: 17 additions & 0 deletions langs/con/parse.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#lang racket
(provide parse)
(require "ast.rkt")

;; S-Expr -> Expr
(define (parse s)
(match s
[(? exact-integer?) (Lit s)]
[(list (? op1? o) e) (Prim1 o (parse e))]
;; NEW:
[(list 'if (list 'zero? e1) e2 e3)
(IfZero (parse e1) (parse e2) (parse e3))]
[_ (error "Parse error")]))

(define (op1? x)
(memq x '(add1 sub1)))

12 changes: 12 additions & 0 deletions langs/con/run-stdin.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#lang racket
(provide main)
(require "parse.rkt")
(require "compile.rkt")
(require "run.rkt")

;; -> Void
;; Compile contents of stdin and use asm-interp to run
(define (main)
(read-line) ; ignore #lang racket line
(run (compile (parse (read)))))

8 changes: 8 additions & 0 deletions langs/con/run.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#lang racket
(require a86/interp)
(provide run)

;; Asm -> Integer
(define (run is)
(asm-interp is))

8 changes: 8 additions & 0 deletions langs/con/test/compile.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#lang racket
(require "../compile.rkt")
(require "../parse.rkt")
(require "../run.rkt")
(require "test-runner.rkt")

(test (λ (e) (run (compile (parse e)))))

7 changes: 7 additions & 0 deletions langs/con/test/interp.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#lang racket
(require "../interp.rkt")
(require "../parse.rkt")
(require "test-runner.rkt")

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

27 changes: 27 additions & 0 deletions langs/con/test/test-runner.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#lang racket
(provide test)
(require rackunit)

(define (test run)
(begin ;; Abscond
(check-equal? (run 7) 7)
(check-equal? (run -8) -8))

(begin ;; Blackmail
(check-equal? (run '(add1 (add1 7))) 9)
(check-equal? (run '(add1 (sub1 7))) 7))

(begin ;; Con
(check-equal? (run '(if (zero? 0) 1 2)) 1)
(check-equal? (run '(if (zero? 1) 1 2)) 2)
(check-equal? (run '(if (zero? -7) 1 2)) 2)
(check-equal? (run '(if (zero? 0)
(if (zero? 1) 1 2)
7))
2)
(check-equal? (run '(if (zero? (if (zero? 0) 1 0))
(if (zero? 1) 1 2)
7))
7)))


16 changes: 16 additions & 0 deletions langs/dupe/ast.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#lang racket
(provide Lit Prim1 If)

;; type Expr = (Lit Datum)
;; | (Prim1 Op1 Expr)
;; | (If Expr Expr Expr)

;; type Datum = Integer
;; | Boolean

;; type Op1 = 'add1 | 'sub1
;; | 'zero?

(struct Lit (d) #:prefab)
(struct Prim1 (p e) #:prefab)
(struct If (e1 e2 e3) #:prefab)
20 changes: 20 additions & 0 deletions langs/dupe/compile-ops.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#lang racket
(provide compile-op1)
(require "ast.rkt")
(require "types.rkt")
(require a86/ast)

(define rax 'rax)
(define r9 'r9)

;; Op1 -> Asm
(define (compile-op1 p)
(match p
['add1 (Add rax (value->bits 1))]
['sub1 (Sub rax (value->bits 1))]
['zero?
(seq (Cmp rax 0)
(Mov rax (value->bits #f))
(Mov r9 (value->bits #t))
(Cmove rax r9))]))

13 changes: 13 additions & 0 deletions langs/dupe/compile-stdin.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#lang racket
(provide main)
(require "parse.rkt")
(require "compile.rkt")
(require a86/printer)

;; -> Void
;; Compile contents of stdin,
;; emit asm code on stdout
(define (main)
(read-line) ; ignore #lang racket line
(asm-display (compile (parse (read)))))

46 changes: 46 additions & 0 deletions langs/dupe/compile.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#lang racket
(provide (all-defined-out))
(require "ast.rkt")
(require "compile-ops.rkt")
(require "types.rkt")
(require a86/ast)

(define rax 'rax)

;; Expr -> Asm
(define (compile e)
(prog (Global 'entry)
(Label 'entry)
(compile-e e)
(Ret)))

;; Expr -> Asm
(define (compile-e e)
(match e
[(Lit d) (compile-value d)]
[(Prim1 p e) (compile-prim1 p e)]
[(If e1 e2 e3)
(compile-if e1 e2 e3)]))

;; Value -> Asm
(define (compile-value v)
(seq (Mov rax (value->bits v))))

;; Op1 Expr -> Asm
(define (compile-prim1 p e)
(seq (compile-e e)
(compile-op1 p)))

;; Expr Expr Expr -> Asm
(define (compile-if e1 e2 e3)
(let ((l1 (gensym 'if))
(l2 (gensym 'if)))
(seq (compile-e e1)
(Cmp rax (value->bits #f))
(Je l1)
(compile-e e2)
(Jmp l2)
(Label l1)
(compile-e e3)
(Label l2))))

9 changes: 9 additions & 0 deletions langs/dupe/interp-prim.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#lang racket
(provide interp-prim1)

;; Op1 Value -> Value
(define (interp-prim1 op v)
(match op
['add1 (add1 v)]
['sub1 (sub1 v)]
['zero? (zero? v)]))
12 changes: 12 additions & 0 deletions langs/dupe/interp-stdin.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#lang racket
(provide main)
(require "parse.rkt")
(require "interp.rkt")

;; -> Void
;; Parse and interpret contents of stdin,
;; print result on stdout
(define (main)
(read-line) ; ignore #lang racket line
(println (interp (parse (read)))))

19 changes: 19 additions & 0 deletions langs/dupe/interp.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#lang racket
(provide interp)
(require "ast.rkt")
(require "interp-prim.rkt")

;; type Value =
;; | Integer
;; | Boolean

;; Expr -> Value
(define (interp e)
(match e
[(Lit d) d]
[(Prim1 p e)
(interp-prim1 p (interp e))]
[(If e1 e2 e3)
(if (interp e1)
(interp e2)
(interp e3))]))
13 changes: 13 additions & 0 deletions langs/dupe/main.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#lang racket
(require "ast.rkt")
(require "parse.rkt")
(require "interp.rkt")
(require "compile.rkt")
(require "run.rkt")
(provide (all-from-out "ast.rkt"))
(provide (all-from-out "parse.rkt"))
(provide (all-from-out "interp.rkt"))
(provide (all-from-out "compile.rkt"))
(provide (all-from-out "run.rkt"))


21 changes: 21 additions & 0 deletions langs/dupe/parse.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#lang racket
(provide parse)
(require "ast.rkt")

;; S-Expr -> Expr
(define (parse s)
(match s
[(? datum?) (Lit s)]
[(list (? op1? o) e) (Prim1 o (parse e))]
[(list 'if e1 e2 e3)
(If (parse e1) (parse e2) (parse e3))]
[_ (error "Parse error")]))

;; Any -> Boolean
(define (datum? x)
(or (exact-integer? x)
(boolean? x)))

(define (op1? x)
(memq x '(add1 sub1 zero?)))

Loading

0 comments on commit 049889d

Please sign in to comment.