From f24adfa2db9c039617da78dc707c42ed266f7d61 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 9 Oct 2026 15:58:01 +0300 Subject: [PATCH] write a typedef's target as a type It fell through to the expression walk, which read (* const char) as a dereference and an array as a subscript, and died on a fn type. Fixes #53. --- fmt-c-writer.scm | 3 +++ tests/codegen.scm | 13 +++++++++++++ tests/sex-programs/type-shapes.sex | 12 +++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index cd8545f..94dd599 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -195,6 +195,9 @@ forms, and what remains." ;; name `->' itself being the threading macro (('%-> . rest) (cons '%-> (map walk-expr rest))) (('var . _) (walk-var form)) + ;; semen has already put the target first, the way C writes it + (('typedef type name) + `(typedef ,(walk-type type) ,(atom-to-fmt-c name))) ;; An expression has no room for a statement, so a comment in a ;; cast is dropped rather than relocated. (('cast . rest) diff --git a/tests/codegen.scm b/tests/codegen.scm index 67d7fb1..86a76b6 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -693,6 +693,19 @@ compiles." (test-assert "nor one behind a pointer" (emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]"))) + ;; A typedef's target is a type, and was walked as an expression: a + ;; pointer read as a dereference, an array as a subscript + (test-group "typedefs" + (test-assert "of a name" + (emits? "(typedef small int)" "typedef int small;")) + (test-assert "of a pointer" + (emits? "(typedef cstr (* const char))" "typedef const char * cstr;")) + (test-assert "of an array" + (emits? "(typedef vec4 (¤ float 4))" "typedef float vec4[4];")) + (test-assert "of a function pointer" + (emits? "(typedef colour-fn (fn ((i32)) u32))" + "typedef u32 (*colour_fn)(i32);"))) + ;; `.' is C's `.' or its `->' depending on what is being read from, ;; so a path reads the same however the thing it starts from is held (test-group "member access" diff --git a/tests/sex-programs/type-shapes.sex b/tests/sex-programs/type-shapes.sex index 1c63b36..38d1672 100644 --- a/tests/sex-programs/type-shapes.sex +++ b/tests/sex-programs/type-shapes.sex @@ -6,7 +6,8 @@ "unsized of a typedef: 1 2" "unsized of a pointer: 5" "named bound: 2 4 b" - "unnamed parameters: 7 -1 2") + "unnamed parameters: 7 -1 2" + "typedefs: hi 4 2") (return 0) ;;; Three questions about a written type that used to be answered in @@ -22,6 +23,9 @@ (struct point ((x int) (y int))) (typedef small int) +(typedef cstr (* const char)) +(typedef vec4 (¤ float 4)) +(typedef unary (fn ((small)) small)) (define CAP 2) @@ -79,4 +83,10 @@ ;; takes one parameter, not two (var fp (fn ((unsigned int)) int) width) (printf "unnamed parameters: %d %d %d\n" (fp 7) (sign #\a) (twice 1)) + + ;; a typedef's target is every one of those shapes as well + (var greeting cstr "hi") + (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)) (return 0))