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:
@@ -703,7 +703,29 @@ compiles."
|
||||
(test-assert "nor one behind a pointer"
|
||||
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
|
||||
|
||||
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||
;; `sizeof' takes a type or an expression, and `(* x)' is either.
|
||||
;; What is in scope says which: a name C alone knows is a type
|
||||
(test-group "sizeof"
|
||||
(test-assert "a pointer type"
|
||||
(emits? (in-fn "(var n size-t (sizeof (* char)))") "sizeof(char *)"))
|
||||
(test-assert "a qualified one"
|
||||
(emits? (in-fn "(var n size-t (sizeof (* const char)))") "sizeof(const char *)"))
|
||||
(test-assert "to a type only C knows of"
|
||||
(emits? (in-fn "(var n size-t (sizeof (* size-t)))") "sizeof(size_t *)"))
|
||||
(test-assert "a multi-word type"
|
||||
(emits? (in-fn "(var n size-t (sizeof (unsigned int)))") "sizeof(unsigned int)"))
|
||||
(test-assert "an array type"
|
||||
(emits? (in-fn "(var n size-t (sizeof (¤ int 4)))") "sizeof(int [4])"))
|
||||
(test-assert "a dereference of a local"
|
||||
(emits? (in-fn "(var p (* int) 0) (var n size-t (sizeof (* p)))") "sizeof(*p)"))
|
||||
(test-assert "of a parameter"
|
||||
(emits? "(fn f ((p (* int))) void (var n size-t (sizeof (* p))))" "sizeof(*p)"))
|
||||
(test-assert "of a call"
|
||||
(emits? (in-fn "(var n size-t (sizeof (* (g))))") "sizeof(*g())"))
|
||||
(test-assert "a subscript"
|
||||
(emits? (in-fn "(var xs (¤ int 4)) (var n size-t (sizeof (¤ xs 0)))") "sizeof(xs[0])")))
|
||||
|
||||
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||
(test-group "defines"
|
||||
(test-assert "an atom is left bare"
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
"unsized of a pointer: 5"
|
||||
"named bound: 2 4 b"
|
||||
"unnamed parameters: 7 -1 2"
|
||||
"typedefs: hi 4 2")
|
||||
"typedefs: hi 4 2"
|
||||
"sizeof a pointer type: 1 1")
|
||||
(return 0)
|
||||
|
||||
;;; Three questions about a written type that used to be answered in
|
||||
@@ -89,4 +90,9 @@
|
||||
(var v vec4 #(1.0 2.0 3.0 4.0))
|
||||
(var op unary twice)
|
||||
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof (¤ v 0))) (op 1))
|
||||
|
||||
;; `(* T)' is a type or a dereference, and only what is in scope says
|
||||
(printf "sizeof a pointer type: %d %d\n"
|
||||
(== (sizeof (* const char)) (sizeof greeting))
|
||||
(== (sizeof (* greeting)) 1))
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user