From 2c080433e0d3d4e44a713a1b125ad07b1738e46b Mon Sep 17 00:00:00 2001 From: Zheyu Shen Date: Sat, 1 Jan 2022 19:09:22 +0800 Subject: [PATCH 1/2] introduce subtraction --- compiler.rkt | 11 +++++++++-- interp-Lint.rkt | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler.rkt b/compiler.rkt index 1385616..f9b786b 100644 --- a/compiler.rkt +++ b/compiler.rkt @@ -18,7 +18,8 @@ [(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 @@ -36,12 +37,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 diff --git a/interp-Lint.rkt b/interp-Lint.rkt index c32bc55..329915b 100644 --- a/interp-Lint.rkt +++ b/interp-Lint.rkt @@ -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) @@ -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) From 452f6d1ecd423d08d46ca1c6c640717eb29190bf Mon Sep 17 00:00:00 2001 From: Zheyu Shen Date: Sun, 27 Feb 2022 13:58:24 +0800 Subject: [PATCH 2/2] add missing Int match branch --- compiler.rkt | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler.rkt b/compiler.rkt index f9b786b..c342d31 100644 --- a/compiler.rkt +++ b/compiler.rkt @@ -15,6 +15,7 @@ ;; 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)))]