diff --git a/semen.scm b/semen.scm index 379fdbb..c4b239f 100644 --- a/semen.scm +++ b/semen.scm @@ -571,7 +571,7 @@ (define (walk-statement form env) (cond - ((vector? form) (walk-literal form env)) + ((vector? form) (walk-initializer form env)) ((not (list? form)) form) ((null? form) form) ;; expanded here rather than before the walk, so the macro body can @@ -775,6 +775,17 @@ (append (drop-right parts (length elements)) (map (lambda (element) (walk-operand element env)) elements))))) +;;; #(struct point : (. p x) 2) -- the values are expressions, and the +;;; words before a `:' a type that is left as written +(define (walk-initializer form env) + (let-values (((prefix rest) (break (lambda (e) (eq? e ':)) + (vector->list form)))) + (let ((walk (lambda (es) (map (lambda (e) (walk-statement e env)) es)))) + (list->vector + (if (null? rest) + (walk prefix) + (append prefix (list ':) (walk (cdr rest)))))))) + ;;; 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 diff --git a/tests/codegen.scm b/tests/codegen.scm index b4dc339..d9eeffd 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -798,6 +798,18 @@ whitespace aside, THEN?" ;; `.' 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 + ;; ...and inside a literal, which the walk used to pass by + (test-group "member access in a literal" + (test-assert "compound" + (emits? (in-line "(var q (struct point) #(struct point : (. pp x) (. pp y)))") + "(struct point){pp->x, pp->y}")) + (test-assert "brace initializer" + (emits? (in-line "(var q (struct point) #((. pp x) (. pp y)))") + "{pp->x, pp->y}")) + (test-assert "designated" + (emits? (in-line "(var q (struct point) #(.y (. pp x)))") + "{.y = pp->x}"))) + (test-group "member access" (test-assert "a value reads with a dot" (emits? (in-line "(= (. p x) 1)") "p.x = 1")) diff --git a/tests/sex-programs/compound-literals.sex b/tests/sex-programs/compound-literals.sex index fbe5411..3aad6c5 100644 --- a/tests/sex-programs/compound-literals.sex +++ b/tests/sex-programs/compound-literals.sex @@ -5,7 +5,8 @@ "through a pointer 9" "array 10 20 30" "argument 6" - "mixed 1 5") + "mixed 1 5" + "read through a pointer 1 5") (return 0) ;;; `:' inside #(...) ends a type and makes the rest a C99 compound @@ -46,4 +47,10 @@ (var m (struct point) #(struct point : 1 .y 5)) (printf "mixed %d %d\n" (. m x) (. m y)) + ;; what a literal holds is walked like any expression, so a path in + ;; it reads through a pointer as it does outside one + (var pm (* (struct point)) (& m)) + (var c (struct point) #(struct point : (. pm x) (. pm y))) + (printf "read through a pointer %d %d\n" (. c x) (. c y)) + (return 0))