type a pointer difference as ptrdiff-t
The join took the first pointer, so (- p q) declared the distance between two pointers as a third.
This commit is contained in:
14
semen.scm
14
semen.scm
@@ -1308,7 +1308,13 @@
|
|||||||
`(closure ,(arglist-types (second expr)) ,(third expr))))
|
`(closure ,(arglist-types (second expr)) ,(third expr))))
|
||||||
;; `c-and' and `c-or' are the names from before `&&' and `||'
|
;; `c-and' and `c-or' are the names from before `&&' and `||'
|
||||||
((== != < > <= >= && |\|\|| ! c-and c-or) 'bool)
|
((== != < > <= >= && |\|\|| ! c-and c-or) 'bool)
|
||||||
((+ - / %) (arithmetic-type expr env))
|
;; one pointer from another is how far apart they are
|
||||||
|
((-) (if (and (= 3 (length expr))
|
||||||
|
(every (lambda (operand) (address? (expression-type operand env)))
|
||||||
|
(cdr expr)))
|
||||||
|
'ptrdiff-t
|
||||||
|
(arithmetic-type expr env)))
|
||||||
|
((+ / %) (arithmetic-type expr env))
|
||||||
;; the bitwise operators join like the arithmetic ones
|
;; the bitwise operators join like the arithmetic ones
|
||||||
((^ |\||) (arithmetic-type expr env))
|
((^ |\||) (arithmetic-type expr env))
|
||||||
;; a shift is the promoted left operand, not a join:
|
;; a shift is the promoted left operand, not a join:
|
||||||
@@ -1351,6 +1357,12 @@
|
|||||||
#f
|
#f
|
||||||
(cdr expr)))
|
(cdr expr)))
|
||||||
|
|
||||||
|
;;; A pointer, or an array that is one as an operand
|
||||||
|
(define (address? written)
|
||||||
|
(and written
|
||||||
|
(let ((type (underlying (parse-type written))))
|
||||||
|
(or (ptr-type? type) (array-type? type)))))
|
||||||
|
|
||||||
(define (arith-join left right)
|
(define (arith-join left right)
|
||||||
(cond
|
(cond
|
||||||
((not left) right)
|
((not left) right)
|
||||||
|
|||||||
@@ -623,6 +623,15 @@
|
|||||||
(test-assert "and so is a pointer it gives"
|
(test-assert "and so is a pointer it gives"
|
||||||
(emits? (in-fn "(var s (* char) 0) (var p (const * char) s) (var q _ (+ p 1))")
|
(emits? (in-fn "(var s (* char) 0) (var p (const * char) s) (var q _ (+ p 1))")
|
||||||
"char * q = p + 1"))
|
"char * q = p + 1"))
|
||||||
|
;; the join took the first pointer, and declared the distance one
|
||||||
|
(test-assert "one pointer from another is a distance"
|
||||||
|
(emits? (in-fn "(var p (* int) 0) (var q (* int) 0) (var d _ (- p q))")
|
||||||
|
"ptrdiff_t d = p - q"))
|
||||||
|
(test-assert "an array decays to one for it"
|
||||||
|
(emits? (in-fn "(var xs [int 4]) (var p (* int) xs) (var d _ (- p xs))")
|
||||||
|
"ptrdiff_t d = p - xs"))
|
||||||
|
(test-assert "a number from a pointer is still a pointer"
|
||||||
|
(emits? (in-fn "(var p (* int) 0) (var r _ (- p 1))") "int * r = p - 1"))
|
||||||
;; a compound literal carries its own type, where a brace
|
;; a compound literal carries its own type, where a brace
|
||||||
;; initializer has none and takes one from its context
|
;; initializer has none and takes one from its context
|
||||||
(test-assert "a compound literal answers a bare wildcard"
|
(test-assert "a compound literal answers a bare wildcard"
|
||||||
|
|||||||
Reference in New Issue
Block a user