Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subtraction #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
;; flips the arguments of +. -Jeremy
(define (flip-exp e)
(match e
[(Int n) e]
[(Var x) e]
[(Prim 'read '()) (Prim 'read '())]
[(Prim '- (list e1)) (Prim '- (list (flip-exp e1)))]
[(Prim '+ (list e1 e2)) (Prim '+ (list (flip-exp e2) (flip-exp e1)))]))
[(Prim '+ (list e1 e2)) (Prim '+ (list (flip-exp e2) (flip-exp e1)))]
[(Prim '- (list e1 e2)) (Prim '- (list (flip-exp e1) (flip-exp e2)))]))

(define (flip-Lint e)
(match e
Expand All @@ -36,12 +38,18 @@
[((Int n1) (Int n2)) (Int (fx+ n1 n2))]
[(_ _) (Prim '+ (list r1 r2))]))

(define (pe-sub r1 r2)
(match* (r1 r2)
[((Int n1) (Int n2)) (Int (fx- n1 n2))]
[(_ _) (Prim '- (list r1 r2))]))

(define (pe-exp e)
(match e
[(Int n) (Int n)]
[(Prim 'read '()) (Prim 'read '())]
[(Prim '- (list e1)) (pe-neg (pe-exp e1))]
[(Prim '+ (list e1 e2)) (pe-add (pe-exp e1) (pe-exp e2))]))
[(Prim '+ (list e1 e2)) (pe-add (pe-exp e1) (pe-exp e2))]
[(Prim '- (list e1 e2)) (pe-sub (pe-exp e1) (pe-exp e2))]))

(define (pe-Lint p)
(match p
Expand Down
8 changes: 8 additions & 0 deletions interp-Lint.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
(define v1 (interp-exp e1))
(define v2 (interp-exp e2))
(fx+ v1 v2)]
[(Prim '- (list e1 e2))
(define v1 (interp-exp e1))
(define v2 (interp-exp e2))
(fx- v1 v2)]
))

(define (interp-Lint p)
Expand Down Expand Up @@ -55,6 +59,10 @@
(define v1 ((interp-exp env) e1))
(define v2 ((interp-exp env) e2))
(fx+ v1 v2)]
[(Prim '- (list e1 e2))
(define v1 ((interp-exp env) e1))
(define v2 ((interp-exp env) e2))
(fx- v1 v2)]
))

(define/public (interp-program p)
Expand Down