Skip to content

Commit

Permalink
WIP: reworking literals a la #114 through Hustle.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvanhorn committed Jun 22, 2023
1 parent 95b84d9 commit 355bf2c
Show file tree
Hide file tree
Showing 45 changed files with 122 additions and 157 deletions.
6 changes: 3 additions & 3 deletions langs/abscond/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion langs/abscond/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
;; Expr -> Asm
(define (compile-e e)
(match e
[(Int i) (seq (Mov 'rax i))]))
[(Lit i) (seq (Mov 'rax i))]))
2 changes: 1 addition & 1 deletion langs/abscond/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
;; Interpret given expression
(define (interp e)
(match e
[(Int i) i]))
[(Lit i) i]))
2 changes: 1 addition & 1 deletion langs/abscond/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
;; S-Expr -> Expr
(define (parse s)
(match s
[(? exact-integer?) (Int s)]
[(? exact-integer?) (Lit s)]
[_ (error "Parse error")]))
6 changes: 3 additions & 3 deletions langs/blackmail/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion langs/blackmail/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions langs/blackmail/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion langs/blackmail/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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")]))

Expand Down
6 changes: 3 additions & 3 deletions langs/con/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion langs/con/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)]))

Expand Down
2 changes: 1 addition & 1 deletion langs/con/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion langs/con/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
12 changes: 5 additions & 7 deletions langs/dodger/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 1 addition & 3 deletions langs/dodger/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)]))

Expand Down
4 changes: 1 addition & 3 deletions langs/dodger/interp-bits.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions langs/dodger/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions langs/dodger/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
10 changes: 5 additions & 5 deletions langs/dupe/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions langs/dupe/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)]))

Expand Down
3 changes: 1 addition & 2 deletions langs/dupe/interp-bits-wrap.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions langs/dupe/interp-bits.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions langs/dupe/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions langs/dupe/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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?)))
6 changes: 3 additions & 3 deletions langs/dupe/types.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions langs/evildoer/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 1 addition & 3 deletions langs/evildoer/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 1 addition & 3 deletions langs/evildoer/interp.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
10 changes: 7 additions & 3 deletions langs/evildoer/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
(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))]
[(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 (op0? x)
(memq x '(read-byte peek-byte void)))
Expand Down
11 changes: 4 additions & 7 deletions langs/extort/ast.rkt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading

0 comments on commit 355bf2c

Please sign in to comment.