From 355bf2caab5c519c0aabe958d2d9ee7194392104 Mon Sep 17 00:00:00 2001 From: David Van Horn Date: Thu, 22 Jun 2023 08:12:20 -0400 Subject: [PATCH] WIP: reworking literals a la #114 through Hustle. --- langs/abscond/ast.rkt | 6 +++--- langs/abscond/compile.rkt | 2 +- langs/abscond/interp.rkt | 2 +- langs/abscond/parse.rkt | 2 +- langs/blackmail/ast.rkt | 6 +++--- langs/blackmail/compile.rkt | 2 +- langs/blackmail/interp.rkt | 6 +++--- langs/blackmail/parse.rkt | 2 +- langs/con/ast.rkt | 6 +++--- langs/con/compile.rkt | 2 +- langs/con/interp.rkt | 2 +- langs/con/parse.rkt | 2 +- langs/dodger/ast.rkt | 12 +++++------- langs/dodger/compile.rkt | 4 +--- langs/dodger/interp-bits.rkt | 4 +--- langs/dodger/interp.rkt | 4 +--- langs/dodger/parse.rkt | 10 +++++++--- langs/dupe/ast.rkt | 10 +++++----- langs/dupe/compile.rkt | 3 +-- langs/dupe/interp-bits-wrap.rkt | 3 +-- langs/dupe/interp-bits.rkt | 3 +-- langs/dupe/interp.rkt | 3 +-- langs/dupe/parse.rkt | 8 ++++++-- langs/dupe/types.rkt | 6 +++--- langs/evildoer/ast.rkt | 11 ++++------- langs/evildoer/compile.rkt | 4 +--- langs/evildoer/interp.rkt | 4 +--- langs/evildoer/parse.rkt | 10 +++++++--- langs/extort/ast.rkt | 11 ++++------- langs/extort/compile.rkt | 4 +--- langs/extort/interp.rkt | 6 ++---- langs/extort/parse.rkt | 20 +++++++++++--------- langs/fraud/ast.rkt | 11 ++++------- langs/fraud/compile.rkt | 4 +--- langs/fraud/interp-lexical.rkt | 6 ++---- langs/fraud/interp.rkt | 6 ++---- langs/fraud/parse.rkt | 9 ++++++--- langs/fraud/test/translate.rkt | 8 ++++---- langs/fraud/translate.rkt | 10 +++------- langs/hustle/ast.rkt | 13 ++++--------- langs/hustle/compile.rkt | 5 +---- langs/hustle/interp-heap-bits.rkt | 5 +---- langs/hustle/interp-heap.rkt | 5 +---- langs/hustle/interp.rkt | 5 +---- langs/hustle/parse.rkt | 12 ++++++++---- 45 files changed, 122 insertions(+), 157 deletions(-) diff --git a/langs/abscond/ast.rkt b/langs/abscond/ast.rkt index fa3d7869..721978d8 100644 --- a/langs/abscond/ast.rkt +++ b/langs/abscond/ast.rkt @@ -1,5 +1,5 @@ #lang racket -(provide Int) +(provide Lit) -;; type Expr = (Int Integer) -(struct Int (i) #:prefab) +;; type Expr = (Lit Integer) +(struct Lit (i) #:prefab) diff --git a/langs/abscond/compile.rkt b/langs/abscond/compile.rkt index b0410af3..622c406e 100644 --- a/langs/abscond/compile.rkt +++ b/langs/abscond/compile.rkt @@ -12,4 +12,4 @@ ;; Expr -> Asm (define (compile-e e) (match e - [(Int i) (seq (Mov 'rax i))])) + [(Lit i) (seq (Mov 'rax i))])) diff --git a/langs/abscond/interp.rkt b/langs/abscond/interp.rkt index 55a7ca2a..02503f93 100644 --- a/langs/abscond/interp.rkt +++ b/langs/abscond/interp.rkt @@ -6,4 +6,4 @@ ;; Interpret given expression (define (interp e) (match e - [(Int i) i])) + [(Lit i) i])) diff --git a/langs/abscond/parse.rkt b/langs/abscond/parse.rkt index 3d00101a..fb1b6198 100644 --- a/langs/abscond/parse.rkt +++ b/langs/abscond/parse.rkt @@ -5,5 +5,5 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] + [(? exact-integer?) (Lit s)] [_ (error "Parse error")])) diff --git a/langs/blackmail/ast.rkt b/langs/blackmail/ast.rkt index 2531b081..2c8962c1 100644 --- a/langs/blackmail/ast.rkt +++ b/langs/blackmail/ast.rkt @@ -1,9 +1,9 @@ #lang racket -(provide Int Prim1) +(provide Lit Prim1) ;; type Expr = -;; | (Int Integer) +;; | (Lit Integer) ;; | (Prim1 Op Expr) ;; type Op = 'add1 | 'sub1 -(struct Int (i) #:prefab) +(struct Lit (i) #:prefab) (struct Prim1 (p e) #:prefab) diff --git a/langs/blackmail/compile.rkt b/langs/blackmail/compile.rkt index dfc10aa5..974d8827 100644 --- a/langs/blackmail/compile.rkt +++ b/langs/blackmail/compile.rkt @@ -13,7 +13,7 @@ (define (compile-e e) (match e [(Prim1 p e) (compile-prim1 p e)] - [(Int i) (compile-integer i)])) + [(Lit i) (compile-integer i)])) ;; Op Expr -> Asm (define (compile-prim1 p e) diff --git a/langs/blackmail/interp.rkt b/langs/blackmail/interp.rkt index f23a9595..7ddbe38e 100644 --- a/langs/blackmail/interp.rkt +++ b/langs/blackmail/interp.rkt @@ -2,13 +2,13 @@ (provide interp) (require "ast.rkt") -;; Expr -> Integer +;; Expr -> Liteger (define (interp e) (match e - [(Int i) i] + [(Lit i) i] [(Prim1 p e) (interp-prim1 p (interp e))])) -;; Op Integer -> Integer +;; Op Liteger -> Liteger (define (interp-prim1 op i) (match op ['add1 (add1 i)] diff --git a/langs/blackmail/parse.rkt b/langs/blackmail/parse.rkt index 3169fdeb..b3d4717d 100644 --- a/langs/blackmail/parse.rkt +++ b/langs/blackmail/parse.rkt @@ -5,7 +5,7 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] + [(? exact-integer?) (Lit s)] [(list (? op1? o) e) (Prim1 o (parse e))] [_ (error "Parse error")])) diff --git a/langs/con/ast.rkt b/langs/con/ast.rkt index 5f47c42d..49d4cb9f 100644 --- a/langs/con/ast.rkt +++ b/langs/con/ast.rkt @@ -1,11 +1,11 @@ #lang racket -(provide Int Prim1 IfZero) +(provide Lit Prim1 IfZero) ;; type Expr = -;; | (Int Integer) +;; | (Lit Integer) ;; | (Prim1 Op Expr) ;; | (IfZero Expr Expr Expr) ;; type Op = 'add1 | 'sub1 -(struct Int (i) #:prefab) +(struct Lit (i) #:prefab) (struct Prim1 (p e) #:prefab) (struct IfZero (e1 e2 e3) #:prefab) diff --git a/langs/con/compile.rkt b/langs/con/compile.rkt index 9479064a..83a9c0ad 100644 --- a/langs/con/compile.rkt +++ b/langs/con/compile.rkt @@ -12,7 +12,7 @@ ;; Expr -> Asm (define (compile-e e) (match e - [(Int i) (compile-integer i)] + [(Lit i) (compile-integer i)] [(Prim1 p e) (compile-prim1 p (compile-e e))] [(IfZero e1 e2 e3) (compile-ifzero e1 e2 e3)])) diff --git a/langs/con/interp.rkt b/langs/con/interp.rkt index ee7bacb2..a783a5c8 100644 --- a/langs/con/interp.rkt +++ b/langs/con/interp.rkt @@ -5,7 +5,7 @@ ;; Expr -> Integer (define (interp e) (match e - [(Int i) i] + [(Lit i) i] [(Prim1 p e) (interp-prim1 p (interp e))] [(IfZero e1 e2 e3) diff --git a/langs/con/parse.rkt b/langs/con/parse.rkt index 566531e7..1ce518ec 100644 --- a/langs/con/parse.rkt +++ b/langs/con/parse.rkt @@ -5,7 +5,7 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] + [(? exact-integer?) (Lit s)] [(list (? op1? o) e) (Prim1 o (parse e))] [(list 'if (list 'zero? e1) e2 e3) (IfZero (parse e1) (parse e2) (parse e3))] diff --git a/langs/dodger/ast.rkt b/langs/dodger/ast.rkt index 0a546fba..ed245852 100644 --- a/langs/dodger/ast.rkt +++ b/langs/dodger/ast.rkt @@ -1,16 +1,14 @@ #lang racket -(provide Int Bool Char Prim1 If) +(provide Lit Prim1 If) ;; type Expr = -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) +;; | (Lit Datum) ;; | (Prim1 Op Expr) ;; | (If Expr Expr Expr) +;; type Datum = Boolean | Integer | Char ;; type Op = 'add1 | 'sub1 | 'zero? ;; | 'char? | 'integer->char | 'char->integer -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) -(struct Char (c) #:prefab) + +(struct Lit (d) #:prefab) (struct Prim1 (p e) #:prefab) (struct If (e1 e2 e3) #:prefab) diff --git a/langs/dodger/compile.rkt b/langs/dodger/compile.rkt index b292268a..0154197c 100644 --- a/langs/dodger/compile.rkt +++ b/langs/dodger/compile.rkt @@ -14,9 +14,7 @@ ;; Expr -> Asm (define (compile-e e) (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] - [(Char c) (compile-value c)] + [(Lit d) (compile-value d)] [(Prim1 p e) (compile-prim1 p e)] [(If e1 e2 e3) (compile-if e1 e2 e3)])) diff --git a/langs/dodger/interp-bits.rkt b/langs/dodger/interp-bits.rkt index 36631798..6f65d551 100644 --- a/langs/dodger/interp-bits.rkt +++ b/langs/dodger/interp-bits.rkt @@ -16,9 +16,7 @@ ;; Expr -> Bits (define (interp-bits e) (match e - [(Int i) (value->bits i)] - [(Char c) (value->bits c)] - [(Bool b) (value->bits b)] + [(Lit d) (value->bits d)] [(Prim1 'add1 e0) (+ (interp-bits e0) (value->bits 1))] [(Prim1 'sub1 e0) diff --git a/langs/dodger/interp.rkt b/langs/dodger/interp.rkt index 7feaef77..10f127a3 100644 --- a/langs/dodger/interp.rkt +++ b/langs/dodger/interp.rkt @@ -10,9 +10,7 @@ ;; Expr -> Value (define (interp e) (match e - [(Int i) i] - [(Bool b) b] - [(Char c) c] + [(Lit d) d] [(Prim1 p e) (interp-prim1 p (interp e))] [(If e1 e2 e3) diff --git a/langs/dodger/parse.rkt b/langs/dodger/parse.rkt index e9a00b05..f1b7e239 100644 --- a/langs/dodger/parse.rkt +++ b/langs/dodger/parse.rkt @@ -5,14 +5,18 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] - [(? boolean?) (Bool s)] - [(? char?) (Char 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) + (char? x))) + ;; Any -> Boolean (define (op1? x) (memq x '(add1 sub1 zero? char? integer->char char->integer))) diff --git a/langs/dupe/ast.rkt b/langs/dupe/ast.rkt index 00a2eb83..fab4070f 100644 --- a/langs/dupe/ast.rkt +++ b/langs/dupe/ast.rkt @@ -1,13 +1,13 @@ #lang racket -(provide Int Bool Prim1 If) +(provide Lit Prim1 If) ;; type Expr = -;; | (Int Integer) -;; | (Bool Boolean) +;; | (Lit Datum) ;; | (Prim1 Op Expr) ;; | (If Expr Expr Expr) +;; type Datum = Boolean | Integer ;; type Op = 'add1 | 'sub1 | 'zero? -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) + +(struct Lit (d) #:prefab) (struct Prim1 (p e) #:prefab) (struct If (e1 e2 e3) #:prefab) diff --git a/langs/dupe/compile.rkt b/langs/dupe/compile.rkt index 15fc8ad4..0154197c 100644 --- a/langs/dupe/compile.rkt +++ b/langs/dupe/compile.rkt @@ -14,8 +14,7 @@ ;; Expr -> Asm (define (compile-e e) (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] + [(Lit d) (compile-value d)] [(Prim1 p e) (compile-prim1 p e)] [(If e1 e2 e3) (compile-if e1 e2 e3)])) diff --git a/langs/dupe/interp-bits-wrap.rkt b/langs/dupe/interp-bits-wrap.rkt index e22859c1..6c71a47a 100644 --- a/langs/dupe/interp-bits-wrap.rkt +++ b/langs/dupe/interp-bits-wrap.rkt @@ -15,8 +15,7 @@ ;; Expr -> Bits (define (interp-bits-wrap e) (match e - [(Int i) (value->bits i)] - [(Bool b) (value->bits b)] + [(Lit i) (value->bits i)] [(Prim1 'add1 e0) (wrap (add1 (interp-bits-wrap e0)))] [(Prim1 'sub1 e0) diff --git a/langs/dupe/interp-bits.rkt b/langs/dupe/interp-bits.rkt index 1907df37..9d475632 100644 --- a/langs/dupe/interp-bits.rkt +++ b/langs/dupe/interp-bits.rkt @@ -9,8 +9,7 @@ ;; Expr -> Bits (define (interp-bits e) (match e - [(Int i) (value->bits i)] - [(Bool b) (value->bits b)] + [(Lit d) (value->bits d)] [(Prim1 p e) (interp-prim1-bits p (interp-bits e))] [(If e1 e2 e3) diff --git a/langs/dupe/interp.rkt b/langs/dupe/interp.rkt index c9aadb75..0a82f318 100644 --- a/langs/dupe/interp.rkt +++ b/langs/dupe/interp.rkt @@ -9,8 +9,7 @@ ;; Expr -> Value (define (interp e) (match e - [(Int i) i] - [(Bool b) b] + [(Lit d) d] [(Prim1 p e) (interp-prim1 p (interp e))] [(If e1 e2 e3) diff --git a/langs/dupe/parse.rkt b/langs/dupe/parse.rkt index 2be39cd3..b443d23f 100644 --- a/langs/dupe/parse.rkt +++ b/langs/dupe/parse.rkt @@ -5,13 +5,17 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] - [(? boolean?) (Bool 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))) + ;; Any -> Boolean (define (op1? x) (memq x '(add1 sub1 zero?))) diff --git a/langs/dupe/types.rkt b/langs/dupe/types.rkt index 77e13957..157bf54b 100644 --- a/langs/dupe/types.rkt +++ b/langs/dupe/types.rkt @@ -6,15 +6,15 @@ (define type-int #b0) (define (bits->value b) - (cond [(= b (value->bits #t)) #t] + (cond [(= b (value->bits #t)) #t] [(= b (value->bits #f)) #f] [(int-bits? b) (arithmetic-shift b (- int-shift))] [else (error "invalid bits")])) (define (value->bits v) - (cond [(eq? v #t) #b011] - [(eq? v #f) #b111] + (cond [(eq? v #t) #b01] + [(eq? v #f) #b11] [(integer? v) (arithmetic-shift v int-shift)])) (define (int-bits? v) diff --git a/langs/evildoer/ast.rkt b/langs/evildoer/ast.rkt index adfb4ba9..9095b2df 100644 --- a/langs/evildoer/ast.rkt +++ b/langs/evildoer/ast.rkt @@ -1,23 +1,20 @@ #lang racket -(provide Eof Int Bool Char Prim0 Prim1 If Begin) +(provide Eof Lit Prim0 Prim1 If Begin) ;; type Expr = ;; | (Eof) -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) +;; | (Lit Datum) ;; | (Prim0 Op0) ;; | (Prim1 Op1 Expr) ;; | (If Expr Expr Expr) ;; | (Begin Expr Expr) +;; type Datum = Boolean | Integer | Char ;; type Op0 = 'read-byte | 'peek-byte | 'void ;; type Op1 = 'add1 | 'sub1 | 'zero? ;; | 'char? | 'integer->char | 'char->integer ;; | 'write-byte | 'eof-object? (struct Eof () #:prefab) -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) -(struct Char (c) #:prefab) +(struct Lit (d) #:prefab) (struct Prim0 (p) #:prefab) (struct Prim1 (p e) #:prefab) (struct If (e1 e2 e3) #:prefab) diff --git a/langs/evildoer/compile.rkt b/langs/evildoer/compile.rkt index 56e53ae4..1a121fc0 100644 --- a/langs/evildoer/compile.rkt +++ b/langs/evildoer/compile.rkt @@ -21,9 +21,7 @@ ;; Expr -> Asm (define (compile-e e) (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] - [(Char c) (compile-value c)] + [(Lit d) (compile-value d)] [(Eof) (compile-value eof)] [(Prim0 p) (compile-prim0 p)] [(Prim1 p e) (compile-prim1 p e)] diff --git a/langs/evildoer/interp.rkt b/langs/evildoer/interp.rkt index 39778c6b..42a5fea0 100644 --- a/langs/evildoer/interp.rkt +++ b/langs/evildoer/interp.rkt @@ -12,9 +12,7 @@ ;; Expr -> Value (define (interp e) (match e - [(Int i) i] - [(Bool b) b] - [(Char c) c] + [(Lit d) d] [(Eof) eof] [(Prim0 p) (interp-prim0 p)] diff --git a/langs/evildoer/parse.rkt b/langs/evildoer/parse.rkt index 76a905bb..a1d42af6 100644 --- a/langs/evildoer/parse.rkt +++ b/langs/evildoer/parse.rkt @@ -6,9 +6,7 @@ (define (parse s) (match s ['eof (Eof)] - [(? exact-integer?) (Int s)] - [(? boolean?) (Bool s)] - [(? char?) (Char s)] + [(? datum?) (Lit s)] [(list (? op0? o)) (Prim0 o)] [(list (? op1? o) e) (Prim1 o (parse e))] [(list 'begin e1 e2) (Begin (parse e1) (parse e2))] @@ -16,6 +14,12 @@ (If (parse e1) (parse e2) (parse e3))] [_ (error "Parse error")])) +;; Any -> Boolean +(define (datum? x) + (or (exact-integer? x) + (boolean? x) + (char? x))) + ;; Any -> Boolean (define (op0? x) (memq x '(read-byte peek-byte void))) diff --git a/langs/extort/ast.rkt b/langs/extort/ast.rkt index adfb4ba9..9095b2df 100644 --- a/langs/extort/ast.rkt +++ b/langs/extort/ast.rkt @@ -1,23 +1,20 @@ #lang racket -(provide Eof Int Bool Char Prim0 Prim1 If Begin) +(provide Eof Lit Prim0 Prim1 If Begin) ;; type Expr = ;; | (Eof) -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) +;; | (Lit Datum) ;; | (Prim0 Op0) ;; | (Prim1 Op1 Expr) ;; | (If Expr Expr Expr) ;; | (Begin Expr Expr) +;; type Datum = Boolean | Integer | Char ;; type Op0 = 'read-byte | 'peek-byte | 'void ;; type Op1 = 'add1 | 'sub1 | 'zero? ;; | 'char? | 'integer->char | 'char->integer ;; | 'write-byte | 'eof-object? (struct Eof () #:prefab) -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) -(struct Char (c) #:prefab) +(struct Lit (d) #:prefab) (struct Prim0 (p) #:prefab) (struct Prim1 (p e) #:prefab) (struct If (e1 e2 e3) #:prefab) diff --git a/langs/extort/compile.rkt b/langs/extort/compile.rkt index 9b8cad02..3aaea8ba 100644 --- a/langs/extort/compile.rkt +++ b/langs/extort/compile.rkt @@ -25,9 +25,7 @@ ;; Expr -> Asm (define (compile-e e) (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] - [(Char c) (compile-value c)] + [(Lit d) (compile-value d)] [(Eof) (compile-value eof)] [(Prim0 p) (compile-prim0 p)] [(Prim1 p e) (compile-prim1 p e)] diff --git a/langs/extort/interp.rkt b/langs/extort/interp.rkt index 86836693..325dc45a 100644 --- a/langs/extort/interp.rkt +++ b/langs/extort/interp.rkt @@ -14,10 +14,8 @@ ;; Expr -> Answer (define (interp e) (match e - [(Int i) i] - [(Bool b) b] - [(Char c) c] - [(Eof) eof] + [(Lit d) d] + [(Eof) eof] [(Prim0 p) (interp-prim0 p)] [(Prim1 p e0) diff --git a/langs/extort/parse.rkt b/langs/extort/parse.rkt index 65503e24..a1d42af6 100644 --- a/langs/extort/parse.rkt +++ b/langs/extort/parse.rkt @@ -5,22 +5,24 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer? s) (Int s)] - [(? boolean? s) (Bool s)] - [(? char? s) (Char s)] - ['eof (Eof)] - [(list (? op0? o)) (Prim0 o)] - [(list (? op1? o) e) (Prim1 o (parse e))] - [(list 'begin e1 e2) (Begin (parse e1) (parse e2))] + ['eof (Eof)] + [(? datum?) (Lit s)] + [(list (? op0? o)) (Prim0 o)] + [(list (? op1? o) e) (Prim1 o (parse e))] + [(list 'begin e1 e2) (Begin (parse e1) (parse e2))] [(list 'if e1 e2 e3) (If (parse e1) (parse e2) (parse e3))] [_ (error "Parse error")])) ;; Any -> Boolean -(define (op0? x) - (memq x '(read-byte peek-byte void))) +(define (datum? x) + (or (exact-integer? x) + (boolean? x) + (char? x))) ;; Any -> Boolean +(define (op0? x) + (memq x '(read-byte peek-byte void))) (define (op1? x) (memq x '(add1 sub1 zero? char? integer->char char->integer write-byte eof-object?))) diff --git a/langs/fraud/ast.rkt b/langs/fraud/ast.rkt index 975405a9..88cc3bb2 100644 --- a/langs/fraud/ast.rkt +++ b/langs/fraud/ast.rkt @@ -1,11 +1,9 @@ #lang racket -(provide Eof Int Bool Char Prim0 Prim1 Prim2 If Begin Let Var) +(provide Eof Lit Prim0 Prim1 Prim2 If Begin Let Var) ;; type Expr = ;; | (Eof) -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) +;; | (Lit Datum) ;; | (Prim0 Op0) ;; | (Prim1 Op1 Expr) ;; | (Prim2 Op2 Expr Expr) @@ -14,15 +12,14 @@ ;; | (Let Id Expr Expr) ;; | (Var Id) ;; type Id = Symbol +;; type Datum = Boolean | Integer | Char ;; type Op0 = 'read-byte ;; type Op1 = 'add1 | 'sub1 | 'zero? ;; | 'char? | 'integer->char | 'char->integer ;; | 'write-byte | 'eof-object? ;; type Op2 = '+ | '- | '< | '= (struct Eof () #:prefab) -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) -(struct Char (c) #:prefab) +(struct Lit (b) #:prefab) (struct Prim0 (p) #:prefab) (struct Prim1 (p e) #:prefab) (struct Prim2 (p e1 e2) #:prefab) diff --git a/langs/fraud/compile.rkt b/langs/fraud/compile.rkt index 02c8a728..2098914f 100644 --- a/langs/fraud/compile.rkt +++ b/langs/fraud/compile.rkt @@ -29,9 +29,7 @@ ;; Expr CEnv -> Asm (define (compile-e e c) (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] - [(Char c) (compile-value c)] + [(Lit d) (compile-value d)] [(Eof) (compile-value eof)] [(Var x) (compile-variable x c)] [(Prim0 p) (compile-prim0 p c)] diff --git a/langs/fraud/interp-lexical.rkt b/langs/fraud/interp-lexical.rkt index 88ab41eb..0d121985 100644 --- a/langs/fraud/interp-lexical.rkt +++ b/langs/fraud/interp-lexical.rkt @@ -11,10 +11,8 @@ ;; IExpr VEnv -> Answer (define (interp-env e r) (match e - [(Int i) i] - [(Bool b) b] - [(Char c) c] - [(Eof) eof] + [(Lit d) d] + [(Eof) eof] [(Var a) (list-ref r a)] [(Prim0 p) (interp-prim0 p)] [(Prim1 p e) diff --git a/langs/fraud/interp.rkt b/langs/fraud/interp.rkt index a30bc286..2dbcdf7b 100644 --- a/langs/fraud/interp.rkt +++ b/langs/fraud/interp.rkt @@ -20,10 +20,8 @@ ;; Expr Env -> Answer (define (interp-env e r) (match e - [(Int i) i] - [(Bool b) b] - [(Char c) c] - [(Eof) eof] + [(Lit d) d] + [(Eof) eof] [(Var x) (lookup r x)] [(Prim0 p) (interp-prim0 p)] [(Prim1 p e) diff --git a/langs/fraud/parse.rkt b/langs/fraud/parse.rkt index 2c91a9d1..0a5ac341 100644 --- a/langs/fraud/parse.rkt +++ b/langs/fraud/parse.rkt @@ -5,9 +5,7 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] - [(? boolean? s) (Bool s)] - [(? char? s) (Char s)] + [(? datum?) (Lit s)] ['eof (Eof)] [(? symbol? s) (Var s)] [(list (? op0? o)) (Prim0 o)] @@ -20,6 +18,11 @@ (Let x (parse e1) (parse e2))] [_ (error "Parse error" s)])) +;; Any -> Boolean +(define (datum? x) + (or (exact-integer? x) + (boolean? x) + (char? x))) ;; Any -> Boolean (define (op0? x) diff --git a/langs/fraud/test/translate.rkt b/langs/fraud/test/translate.rkt index 044a938d..f83844b4 100644 --- a/langs/fraud/test/translate.rkt +++ b/langs/fraud/test/translate.rkt @@ -4,12 +4,12 @@ (require "../ast.rkt") (require rackunit) (check-equal? (translate (parse '(let ((x 0)) x))) - (Let '_ (Int 0) (Var 0))) + (Let '_ (Lit 0) (Var 0))) (check-equal? (translate (parse '(let ((x 0)) (let ((y 1)) x)))) - (Let '_ (Int 0) (Let '_ (Int 1) (Var 1)))) + (Let '_ (Lit 0) (Let '_ (Lit 1) (Var 1)))) (check-equal? (translate (parse '(let ((x 0)) (let ((y 1)) y)))) - (Let '_ (Int 0) (Let '_ (Int 1) (Var 0)))) + (Let '_ (Lit 0) (Let '_ (Lit 1) (Var 0)))) (check-equal? (translate (parse '(let ((x 0)) (let ((y x)) y)))) - (Let '_ (Int 0) (Let '_ (Var 0) (Var 0)))) + (Let '_ (Lit 0) (Let '_ (Var 0) (Var 0)))) diff --git a/langs/fraud/translate.rkt b/langs/fraud/translate.rkt index 1a7e333c..7db32222 100644 --- a/langs/fraud/translate.rkt +++ b/langs/fraud/translate.rkt @@ -3,10 +3,8 @@ (require "ast.rkt") ;; type IExpr = +;; | (Lit Datum) ;; | (Eof) -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) ;; | (Prim0 Op0) ;; | (Prim1 Op1 IExpr) ;; | (Prim2 Op2 IExpr IExpr) @@ -25,10 +23,8 @@ ;; Expr LEnv -> IExpr (define (translate-e e r) (match e - [(Eof) e] - [(Int i) e] - [(Bool b) e] - [(Char c) e] + [(Eof) e] + [(Lit d) e] [(Prim0 p) e] [(Prim1 p e0) (Prim1 p (translate-e e0 r))] diff --git a/langs/hustle/ast.rkt b/langs/hustle/ast.rkt index a16cdb21..f4a998a0 100644 --- a/langs/hustle/ast.rkt +++ b/langs/hustle/ast.rkt @@ -1,11 +1,8 @@ #lang racket -(provide Eof Int Bool Char Prim0 Prim1 Prim2 If Begin Let Var Empty) +(provide Eof Lit Prim0 Prim1 Prim2 If Begin Let Var) ;; type Expr = (Eof) -;; | (Empty) -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) +;; | (Lit Datum) ;; | (Prim0 Op0) ;; | (Prim1 Op1 Expr) ;; | (Prim2 Op2 Expr Expr) @@ -14,6 +11,7 @@ ;; | (Let Id Expr Expr) ;; | (Var Id) ;; type Id = Symbol +;; type Datum = Boolean | Integer | Char | '() ;; type Op0 = 'read-byte ;; type Op1 = 'add1 | 'sub1 | 'zero? ;; | 'char? | 'integer->char | 'char->integer @@ -23,10 +21,7 @@ ;; type Op2 = '+ | '- | '< | '= ;; | 'cons (struct Eof () #:prefab) -(struct Empty () #:prefab) -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) -(struct Char (c) #:prefab) +(struct Lit (d) #:prefab) (struct Prim0 (p) #:prefab) (struct Prim1 (p e) #:prefab) (struct Prim2 (p e1 e2) #:prefab) diff --git a/langs/hustle/compile.rkt b/langs/hustle/compile.rkt index c84a83f5..1f564e08 100644 --- a/langs/hustle/compile.rkt +++ b/langs/hustle/compile.rkt @@ -33,11 +33,8 @@ ;; Expr CEnv -> Asm (define (compile-e e c) (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] - [(Char c) (compile-value c)] + [(Lit d) (compile-value d)] [(Eof) (compile-value eof)] - [(Empty) (compile-value '())] [(Var x) (compile-variable x c)] [(Prim0 p) (compile-prim0 p c)] [(Prim1 p e) (compile-prim1 p e c)] diff --git a/langs/hustle/interp-heap-bits.rkt b/langs/hustle/interp-heap-bits.rkt index 19cab8ad..d1d47774 100644 --- a/langs/hustle/interp-heap-bits.rkt +++ b/langs/hustle/interp-heap-bits.rkt @@ -18,11 +18,8 @@ ;; Expr REnv Heap -> Answer (define (interp-env-heap e r h) (match e - [(Int i) (cons h (value->bits i))] - [(Bool b) (cons h (value->bits b))] - [(Char c) (cons h (value->bits c))] + [(Lit d) (cons h (value->bits d))] [(Eof) (cons h (value->bits eof))] - [(Empty) (cons h (value->bits '()))] [(Var x) (cons h (lookup r x))] [(Prim0 'void) (cons h (value->bits (void)))] [(Prim0 'read-byte) (cons h (value->bits (read-byte)))] diff --git a/langs/hustle/interp-heap.rkt b/langs/hustle/interp-heap.rkt index 18805f91..02561715 100644 --- a/langs/hustle/interp-heap.rkt +++ b/langs/hustle/interp-heap.rkt @@ -29,11 +29,8 @@ ;; Expr REnv Heap -> Answer* (define (interp-env-heap e r h) (match e - [(Int i) (cons h i)] - [(Bool b) (cons h b)] - [(Char c) (cons h c)] + [(Lit d) (cons h d)] [(Eof) (cons h eof)] - [(Empty) (cons h '())] [(Var x) (cons h (lookup r x))] [(Prim0 'void) (cons h (void))] [(Prim0 'peek-byte) (cons h (peek-byte))] diff --git a/langs/hustle/interp.rkt b/langs/hustle/interp.rkt index 016781b9..ed1b0aab 100644 --- a/langs/hustle/interp.rkt +++ b/langs/hustle/interp.rkt @@ -25,11 +25,8 @@ ;; Expr Env -> Answer (define (interp-env e r) (match e - [(Int i) i] - [(Bool b) b] - [(Char c) c] + [(Lit d) d] [(Eof) eof] - [(Empty) '()] [(Var x) (lookup r x)] [(Prim0 'void) (void)] [(Prim0 'read-byte) (read-byte)] diff --git a/langs/hustle/parse.rkt b/langs/hustle/parse.rkt index 6baa57ce..56b7e5fc 100644 --- a/langs/hustle/parse.rkt +++ b/langs/hustle/parse.rkt @@ -5,12 +5,10 @@ ;; S-Expr -> Expr (define (parse s) (match s - [(? exact-integer?) (Int s)] - [(? boolean?) (Bool s)] - [(? char?) (Char s)] + [(? datum?) (Lit s)] + [(list 'quote (list)) (Lit '())] ['eof (Eof)] [(? symbol?) (Var s)] - [(list 'quote (list)) (Empty)] [(list (? (op? op0) p0)) (Prim0 p0)] [(list (? (op? op1) p1) e) (Prim1 p1 (parse e))] [(list (? (op? op2) p2) e1 e2) (Prim2 p2 (parse e1) (parse e2))] @@ -22,6 +20,12 @@ (Let x (parse e1) (parse e2))] [_ (error "Parse error" s)])) +;; Any -> Boolean +(define (datum? x) + (or (exact-integer? x) + (boolean? x) + (char? x))) + (define op0 '(read-byte peek-byte void))