3 (define (castable? from to)
8 (foldl (lambda (x y) (or x (castable? from y)))
10 '(byte int8 int16 int32)))
13 ;; TODO ajouter casts vers byte, int16, etc, probably not needed since operations are done on ints, and useless operations (on bytes that would not exist) are optimized away
16 (define operators '())
18 (define (define-op1 six-id id type-rule constant-fold code-gen)
20 (cons (make-op1 six-id id type-rule constant-fold code-gen)
23 (define (define-op2 six-id id type-rule constant-fold code-gen)
25 (cons (make-op2 six-id id type-rule constant-fold code-gen)
28 (define (type-rule-int-op1 ast)
29 (let ((t1 (expr-type (subast1 ast))))
30 (cond ((castable? t1 'int)
33 (error "int-op1: type error" ast)))))
35 (define (largest t1 t2) ;; TODO might be used more than juste for int-op2
36 (let loop ((l '(int int32 int16 int8 byte)))
38 (error "largest: unknown type")
45 (define (type-rule-int-op2 ast)
46 (let ((t1 (expr-type (subast1 ast)))
47 (t2 (expr-type (subast2 ast))))
48 (cond ((and (castable? t1 'int) (castable? t2 'int))
51 (error "int-op2: type error" ast)))))
53 (define (type-rule-int-assign ast) ;; TODO why the int in the name ?
54 (let ((t1 (expr-type (subast1 ast)))
55 (t2 (expr-type (subast2 ast))))
56 (if (not (castable? t2 t1)) ; the rhs must fit in the lhs
57 (error "int-assign: type error" ast))
60 (define (type-rule-int-comp-op2 ast)
61 (let ((t1 (expr-type (subast1 ast)))
62 (t2 (expr-type (subast2 ast))))
63 (cond ((and (castable? t1 'int) (castable? t2 'int))
66 (error "int-comp-op2: type error" ast)))))
68 (define (type-rule-bool-op2 ast)
69 (let ((t1 (expr-type (subast1 ast)))
70 (t2 (expr-type (subast2 ast))))
71 (cond ((and (castable? t1 'bool) (castable? t2 'bool))
74 (error "bool-op2: type error" ast)))))
76 (define-op1 'six.!x '!x
78 (lambda (ast) ;; TODO implement these
83 (define-op1 'six.++x '++x
90 (define-op1 'six.x++ 'x++
97 (define-op1 'six.--x '--x
104 (define-op1 'six.x-- 'x--
111 (define-op1 'six.~x '~x
118 (define-op2 'six.x%y 'x%y
125 (define-op2 'six.x*y 'x*y
132 (define-op1 'six.*x '*x
140 (define-op2 'six.x/y 'x/y
147 (define-op2 'six.x+y 'x+y
154 (define-op1 'six.+x '+x
161 (define-op2 'six.x-y 'x-y
168 (define-op1 'six.-x '-x
175 (define-op2 'six.x<<y 'x<<y
182 (define-op2 'six.x>>y 'x>>y
189 (define-op2 'six.x<y 'x<y
190 type-rule-int-comp-op2
196 (define-op2 'six.x<=y 'x<=y
197 type-rule-int-comp-op2
203 (define-op2 'six.x>y 'x>y
204 type-rule-int-comp-op2
210 (define-op2 'six.x>=y 'x>=y
211 type-rule-int-comp-op2
217 (define-op2 'six.x!=y 'x!=y
218 type-rule-int-comp-op2
224 (define-op2 'six.x==y 'x==y
225 type-rule-int-comp-op2
231 (define-op2 'six.x&y 'x&y
238 (define-op1 'six.&x '&x
246 (define-op2 'six.x^y 'x^y
253 (define-op2 '|six.x\|y| '|x\|y|
260 (define-op2 'six.x&&y 'x&&y
267 (define-op2 '|six.x\|\|y| '|x\|\|y|
274 (define-op2 'six.x?y:z 'x?y:z
282 (define-op2 'six.x:y 'x:y
290 (define-op2 'six.x%=y 'x%=y
297 (define-op2 'six.x&=y 'x&=y
304 (define-op2 'six.x*=y 'x*=y
311 (define-op2 'six.x+=y 'x+=y
318 (define-op2 'six.x-=y 'x-=y
325 (define-op2 'six.x/=y 'x/=y
332 (define-op2 'six.x<<=y 'x<<=y
339 (define-op2 'six.x=y 'x=y
346 (define-op2 'six.x>>=y 'x>>=y
353 (define-op2 'six.x^=y 'x^=y
360 (define-op2 '|six.x\|=y| '|x\|=y|
367 (define-op2 'six.x:=y 'x:=y
375 (define-op2 '|six.x,y| '|x,y|
383 (define-op2 'six.x:-y 'x:-y
391 (define (operation? source)
393 (let ((x (car source)))
394 (let loop ((lst operators))
397 ((eq? (op-six-id (car lst)) x)
400 (loop (cdr lst))))))))