let sizeof take a type that looks like an expression
(* char) is a pointer type and (* p) a dereference, and only the names in scope tell them apart, so semen decides and marks a type for the writer. Before, both were walked as expressions: sizeof(*char). Fixes #52.
This commit is contained in:
25
semen.scm
25
semen.scm
@@ -665,6 +665,14 @@
|
||||
|
||||
((dot-access) (walk-member-path form env))
|
||||
|
||||
((sizeof)
|
||||
(if (and (= 2 (length form)) (written-type? (second form) env))
|
||||
(copy-form-source!
|
||||
form
|
||||
`(sizeof (%type ,(resolve-closure-types
|
||||
(expand-type (second form) env)))))
|
||||
(walk-parts form env)))
|
||||
|
||||
(else
|
||||
(cond
|
||||
((method-head? (car form)) (walk-method-call form env))
|
||||
@@ -772,6 +780,23 @@
|
||||
(append (drop-right parts (length elements))
|
||||
(map (lambda (element) (walk-operand element env)) elements)))))
|
||||
|
||||
;;; Is the operand of `sizeof' a type? `(* x)' and `(¤ x i)' are
|
||||
;;; expressions when x is: a name in scope, or a list that is not
|
||||
;;; itself a type. A name nothing declared is one only C knows of, and
|
||||
;;; there that is a type:
|
||||
;;;
|
||||
;;; (* char) (* const char) (* size-t) (unsigned int) types
|
||||
;;; (* p) (* (g)) (¤ xs 0) expressions
|
||||
(define (written-type? form env)
|
||||
(and (pair? form)
|
||||
;; the length a dereference and a subscript have
|
||||
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
|
||||
(let ((operand (second form)))
|
||||
(if (symbol? operand)
|
||||
(not (or (lookup-name env operand) (get-name-type operand)))
|
||||
(written-type? operand env)))
|
||||
(type-head? form))))
|
||||
|
||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||
(define (resolve-wildcard form env)
|
||||
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
||||
|
||||
Reference in New Issue
Block a user