-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
473 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))])) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?))) | ||
|
Oops, something went wrong.