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 ;; TODO should we handle array index operator this way instead of a special case
133 (define-op1 'six.*x '*x
135 'byte) ; we only have byte arrays
141 (define-op2 'six.x/y 'x/y
148 (define-op2 'six.x+y 'x+y
155 (define-op1 'six.+x '+x
162 (define-op2 'six.x-y 'x-y
169 (define-op1 'six.-x '-x
176 (define-op2 'six.x<<y 'x<<y
183 (define-op2 'six.x>>y 'x>>y
190 (define-op2 'six.x<y 'x<y
191 type-rule-int-comp-op2
197 (define-op2 'six.x<=y 'x<=y
198 type-rule-int-comp-op2
204 (define-op2 'six.x>y 'x>y
205 type-rule-int-comp-op2
211 (define-op2 'six.x>=y 'x>=y
212 type-rule-int-comp-op2
218 (define-op2 'six.x!=y 'x!=y
219 type-rule-int-comp-op2
225 (define-op2 'six.x==y 'x==y
226 type-rule-int-comp-op2
232 (define-op2 'six.x&y 'x&y
239 (define-op1 'six.&x '&x
247 (define-op2 'six.x^y 'x^y
254 (define-op2 '|six.x\|y| '|x\|y|
261 (define-op2 'six.x&&y 'x&&y
268 (define-op2 '|six.x\|\|y| '|x\|\|y|
275 (define-op2 'six.x?y:z 'x?y:z
283 (define-op2 'six.x:y 'x:y
291 (define-op2 'six.x%=y 'x%=y
298 (define-op2 'six.x&=y 'x&=y
305 (define-op2 'six.x*=y 'x*=y
312 (define-op2 'six.x+=y 'x+=y
319 (define-op2 'six.x-=y 'x-=y
326 (define-op2 'six.x/=y 'x/=y
333 (define-op2 'six.x<<=y 'x<<=y
340 (define-op2 'six.x=y 'x=y
347 (define-op2 'six.x>>=y 'x>>=y
354 (define-op2 'six.x^=y 'x^=y
361 (define-op2 '|six.x\|=y| '|x\|=y|
368 (define-op2 'six.x:=y 'x:=y
376 (define-op2 '|six.x,y| '|x,y|
384 (define-op2 'six.x:-y 'x:-y
392 (define (operation? source)
394 (let ((x (car source)))
395 (let loop ((lst operators))
398 ((eq? (op-six-id (car lst)) x)
401 (loop (cdr lst))))))))