Skip to content

Commit 363ac16

Browse files
committed
add types; if clauses and get? need to be finished
1 parent 6357c2e commit 363ac16

File tree

9 files changed

+656
-64
lines changed

9 files changed

+656
-64
lines changed

Diff for: compile-ops.rkt

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@
280280

281281

282282
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283+
284+
(define op1-predicates '(char? eof-object? empty? box? cons? vector? string?))
285+
283286
;; Type checkers
284287
(define (assert-type mask type)
285288
(λ (arg)

Diff for: compile.rkt

+311-35
Large diffs are not rendered by default.

Diff for: compiled/drracket/errortrace/types_rkt.dep

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
("8.3" ta6osx ("ebeeffe473f9bf13afffb87c4e53a36d694ed3af" . "5ad2828c01ddba25d85562848514b87028a8eceb") (collects #"errortrace" #"errortrace-key.rkt") (collects #"racket" #"main.rkt") (collects #"racket" #"runtime-config.rkt") (indirect collects #"racket" #"match" #"gen-match.rkt") (indirect collects #"racket" #"match" #"parse.rkt"))
1+
("8.3" ta6osx ("57ff43be570dda44c970d65e6ef77913b72034fd" . "5ad2828c01ddba25d85562848514b87028a8eceb") (collects #"errortrace" #"errortrace-key.rkt") (collects #"racket" #"main.rkt") (collects #"racket" #"runtime-config.rkt") (indirect collects #"racket" #"match" #"gen-match.rkt") (indirect collects #"racket" #"match" #"parse.rkt"))

Diff for: compiled/drracket/errortrace/types_rkt.zo

7.74 KB
Binary file not shown.

Diff for: test/test-compile.rkt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#lang typed/racket
2+
3+
; (: add-t (-> Integer Integer Integer)
4+
; (define (add p1 p2)
5+
; (let ([p1 #t]) (+ p1 p2)))
6+
7+
; (: add-2 (-> Integer Integer Integer)
8+
; (define (add-2 p1 p2)
9+
; (let ([p3 2]) (+ (+ p1 p2) p3)))
10+
11+
;; Combined Function
12+
(: int-or-string (-> Boolean (U String Integer)))
13+
(define (int-or-string b)
14+
(if b 2 "Hello"))
15+
16+
17+
(: bool-to-int (-> Boolean Integer))
18+
(define (bool-to-int b)
19+
(string-length (int-or-string b)))
20+
21+
;; Combined Function
22+
(: int-or-string-2 (-> (U String Integer) Boolean))
23+
(define (int-or-string-2 b)
24+
(if (string? b) #t #f))
25+
26+
27+
(: int-to-int (-> Integer Integer))
28+
(define (int-to-int b)
29+
(if (int-or-string-2 b) 1 2))
30+
31+
;; Check if
32+
(: go-to-else (-> Integer (U String Integer)))
33+
(define (go-to-else b)
34+
(if (char? b) "Hello" 2))
35+
36+
(: test-1 (-> Integer Integer))
37+
(define (test-1 b)
38+
(go-to-else b))

Diff for: test/test-compile.rkt~

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#lang typed/racket
2+
3+
; (: add-t (-> Integer Integer Integer)
4+
; (define (add p1 p2)
5+
; (let ([p1 #t]) (+ p1 p2)))
6+
7+
; (: add-2 (-> Integer Integer Integer)
8+
; (define (add-2 p1 p2)
9+
; (let ([p3 2]) (+ (+ p1 p2) p3)))
10+
11+
;; Combined Function
12+
(: int-or-string (-> Bool (U String Integer)))
13+
(define (int-or-string b)
14+
(if b 2 "Hello"))
15+
16+
17+
(: bool-to-int (-> Bool Integer))
18+
(define (string-to-int b)
19+
(string-length (int-or-string b)))

Diff for: test/test-type.rkt

+23-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,26 @@
4040
(type-minus '((Listof Int) Bool) '(Listof Int))
4141
(type-minus '((Listof (Int Bool)) Bool) '(Listof Int))
4242
(type-minus 'Any '(Listof Int))
43-
(type-minus 'Any 'Int)
43+
(type-minus 'Any 'Int)
44+
45+
;'()
46+
;'Any
47+
;'Bool
48+
;'Int
49+
;'(Listof Char)
50+
;'(Listof Str)
51+
;'(Char (Listof Bool))
52+
;'((Listof Bool) Char)
53+
;'(Int (Listof Vector))
54+
;'((Listof Vector) Int)
55+
56+
57+
58+
(type-add 'Bool '(Listof Str))
59+
(type-add '(Listof Char) '(Listof Str))
60+
(type-add '(Char (Listof Bool)) '(Listof Str))
61+
(type-add 'Bool '(Int (Listof Vector)))
62+
(type-add '(Listof Char) '(Int (Listof Vector))) ;;
63+
(type-add '(Char (Listof Bool)) '(Int (Listof Vector))) ;;
64+
(type-add 'Bool '(Listof Str))
65+
(type-add '(Listof Char) '(Listof Str))

Diff for: types.rkt

+113-27
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,64 @@
6565
(define (str-bits? v)
6666
(zero? (bitwise-xor (bitwise-and v imm-mask) type-str)))
6767

68+
;; TODO: Expand Int to Byte and UInt
6869
(define types
6970
'(Int Bool Char Str Vector Eof Empty Box Cons Void Any))
7071

7172
(define (is-member e list)
7273
(if (eq? (member e list) #f) #f #t))
7374

75+
(define (append-element lst elem)
76+
(append lst (list elem)))
77+
78+
;; Only consider single item
79+
;; If pass (list type), check each of them
7480
(define (type? types x)
75-
(match x
76-
['() #t]
77-
[(? symbol?) (is-member x types)]
78-
[(list 'Listof s) (type? types s)]
79-
[(cons t ts) (and (type? types t) (type? types ts))]
80-
[_ #f]))
81+
(match x
82+
['() #t]
83+
[(? symbol?) (is-member x types)]
84+
[(list 'Listof s) (type? types s)]
85+
[(cons t ts) (and (type? types t) (type? types ts))]
86+
[_ #f]))
87+
88+
;; (type-replace-id xts id pt)
89+
;; (type-rule-out-id xts id pt)
90+
91+
(define (type-replace-id xts id pt)
92+
(append (reverse (list-tail (reverse xts) (- (length xts) id))) `(,pt) (list-tail xts (+ id 1))))
8193

94+
(define (type-rule-out-id xts id pt)
95+
(append (reverse (list-tail (reverse xts) (- (length xts) id))) `(,(type-minus (list-ref xts id) pt)) (list-tail xts (+ id 1))))
96+
97+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98+
;; TODO: support check (Listof, Cons)
99+
100+
;; Only consider when type-query isn't a list
82101
;; possible type-format:
83102
;; 1. A single type
84103
;; 2. List of type
85104
;; 3. (Listof type)
86105

106+
;; Special:
107+
;; 1. Any contains anything
108+
;; 2. Empty can be contained in Vector/String
109+
;; 3. Listof can be contained in Cons
110+
87111
(define (type-contain type-format type-query)
88112
(match type-format
89113
['() #f]
90114
[(list 'Listof tf)
91-
(match type-query
92-
[(list 'Listof tq) (type-contain tf tq)]
93-
[_ #f])]
94-
[(? symbol?) (if (eq? type-format 'Any) #t (if (eq? type-query 'Empty) (or (eq? type-format 'Vector) (eq? type-format 'Str)) (eq? type-format type-query)))]
115+
(match type-query
116+
[(list 'Listof tq) (type-contain tf tq)]
117+
[_ #f])]
118+
[(? symbol?)
119+
(if (eq? type-format 'Any) #t
120+
(if (eq? type-query 'Empty) (or (eq? type-format 'Vector) (eq? type-format 'Str) (eq? type-format 'Empty))
121+
(if (eq? type-format 'Cons)
122+
(match type-query
123+
[(list 'Listof tq) #t]
124+
['Cons #t]
125+
[_ #f]) (eq? type-format type-query))))]
95126
[(cons type-a type-format) (or (type-contain type-a type-query) (type-contain type-format type-query))]
96127
[_ #f]))
97128

@@ -110,23 +141,78 @@
110141
(match type-origin-list
111142
['() '()]
112143
['Any
113-
(match type-ruled-out
114-
[(? symbol?) (append (remove* '(Any type-ruled-out) types) '((Listof Any)))]
115-
[(list 'Listof tf) (append (remove 'Any types) `((Listof ,(type-minus 'Any tf))))]
116-
[_ (error "type-error" "invalid type: ~a" type-ruled-out)])]
144+
(match type-ruled-out
145+
[(? symbol?) (append (remove* '(Any type-ruled-out) types) '((Listof Any)))]
146+
[(list 'Listof tf) (append (remove 'Any types) `((Listof ,(type-minus 'Any tf))))]
147+
[_ (error "type-error" "invalid type: ~a" type-ruled-out)])]
117148
[(? symbol?) (if (eq? type-origin-list type-ruled-out) '() type-origin-list)]
118149
[(list 'Listof tf)
119-
(match type-ruled-out
120-
[(? symbol?) type-origin-list]
121-
[(list 'Listof tr) (let ([empty-ele-lst (type-minus tf tr)])
122-
(if (eq? empty-ele-lst '()) '()
123-
(if (eq? (length empty-ele-lst) 1)
124-
(list 'Listof (list-ref empty-ele-lst 0))
125-
(list 'Listof empty-ele-lst))))]
126-
[_ (error "type-error" "invalid type: ~a" type-ruled-out)])]
150+
(match type-ruled-out
151+
[(? symbol?) type-origin-list]
152+
[(list 'Listof tr) (let ([empty-ele-lst (type-minus tf tr)])
153+
(if (eq? empty-ele-lst '()) '()
154+
(if (eq? (length empty-ele-lst) 1)
155+
(list 'Listof (list-ref empty-ele-lst 0))
156+
(list 'Listof empty-ele-lst))))]
157+
[_ (error "type-error" "invalid type: ~a" type-ruled-out)])]
127158
[(cons type-first types-remaining)
128-
(let ([ele-listele-lst (type-minus type-first type-ruled-out)])
129-
(match ele-listele-lst
130-
[(? symbol? ele-listele-lst) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
131-
[(list 'Listof _) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
132-
[_ (append ele-listele-lst (type-minus types-remaining type-ruled-out))]))]))
159+
(let ([ele-listele-lst (type-minus type-first type-ruled-out)])
160+
(match ele-listele-lst
161+
[(? symbol? ele-listele-lst) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
162+
[(list 'Listof _) (append `(,ele-listele-lst) (type-minus types-remaining type-ruled-out))]
163+
[_ (append ele-listele-lst (type-minus types-remaining type-ruled-out))]))]))
164+
165+
;; possible Input:
166+
;; 1. '()
167+
;; 2. 'Any
168+
;; 3. 'symbol
169+
;; 4. (Listof xx)
170+
;; 5. (list Type)
171+
172+
(define (type-add type-or-lst-1 type-or-lst-2)
173+
(match type-or-lst-1
174+
['() type-or-lst-2]
175+
['Any 'Any]
176+
[(? symbol?)
177+
(match type-or-lst-2
178+
['() type-or-lst-1]
179+
['Any 'Any]
180+
[(? symbol?) (if (eq? type-or-lst-1 type-or-lst-2) type-or-lst-1 `(,type-or-lst-1 ,type-or-lst-2))]
181+
[(list 'Listof tf) `(,type-or-lst-1 ,type-or-lst-2)]
182+
[(cons type-first types-remaining)
183+
(if (eq? types-remaining '()) `(,type-or-lst-1 ,type-first)
184+
(match type-first
185+
[(? symbol?) (if (type-contain type-first type-or-lst-1) type-or-lst-2 (append `(,type-first) (type-add type-or-lst-1 types-remaining)))]
186+
[(list 'Listof tf) (append `(,type-first) (type-add type-or-lst-1 types-remaining))]))])]
187+
[(list 'Listof tf)
188+
(match type-or-lst-2
189+
['() type-or-lst-1]
190+
['Any 'Any]
191+
[(list 'Listof tf2)
192+
(if (type-contain tf tf2) type-or-lst-1
193+
(if (type-contain tf2 tf) type-or-lst-2
194+
`(Listof ,(type-add tf tf2))))]
195+
[(? symbol?) `(,type-or-lst-1 ,type-or-lst-2)]
196+
[(cons type-first types-remaining)
197+
(match type-first
198+
[(list 'Listof tf2) (if (type-contain type-first type-or-lst-1) type-or-lst-2 (type-add `(Listof ,(type-add tf tf2)) types-remaining))]
199+
[(? symbol?) (if (eq? types-remaining '()) `(,type-first ,type-or-lst-1) (type-add type-first (type-add type-or-lst-1 types-remaining)))])
200+
])]
201+
[(cons type-first types-remaining)
202+
(match type-or-lst-2
203+
['() type-or-lst-1]
204+
['Any 'Any]
205+
[(list 'Listof tf)
206+
(match type-first
207+
[(list 'Listof tf2)
208+
(if (type-contain tf tf2) type-or-lst-1
209+
(if (type-contain tf2 tf) (type-add type-or-lst-2 types-remaining)
210+
(type-add `(Listof ,(type-add tf tf2)) types-remaining)))]
211+
[(? symbol?) (type-add type-first (type-add type-or-lst-2 types-remaining))])]
212+
[(? symbol?)
213+
(match type-first
214+
[(list 'Listof tf2) (append `(,type-first) (type-add type-or-lst-2 types-remaining))]
215+
[(? symbol?) (if (type-contain type-first type-or-lst-2) (type-or-lst-1)
216+
(if (type-contain type-or-lst-2 type-first) (type-add type-or-lst-2 types-remaining)
217+
(append `(,type-first) (type-add type-or-lst-2 types-remaining))))])]
218+
[(cons type-first-2 types-remaining-2) (type-add types-remaining (type-add type-first type-or-lst-2))])]))

0 commit comments

Comments
 (0)