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:
2026-10-09 16:04:00 +03:00
parent 5a887c0654
commit 9d4454afe3
5 changed files with 59 additions and 2 deletions

View File

@@ -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"