Files
VS-map-previewer/Sex-field-notes.org
2026-10-08 21:02:38 +03:00

8.6 KiB

Sex field notes

Language bugs and friction met while building vs-map-viewer, against sexc at ~/dev/sex. Newest first. Unless said otherwise, sexc dfafe8b.

-INFINITY is one symbol

(var best double -INFINITY) emits _INFINITY: the reader takes the whole token as a symbol and the leading - is mangled like any other. (- INFINITY) works. Negative literals like -1 are fine, so it's only symbolic constants, but the mangled name reads like a typo.

A cast of a bitwise expression loses its parentheses

(cast (c-bit-or xn 1) float) emits (float)xn | 1, which C reads as ((float)xn) | 1: a compile error here, but with an integer target type it would silently cast only xn. Arithmetic operands are parenthesized; bitwise ones aren't. With the two entries below, that makes three places where operator precedence leaks into the output. In this project they have cost more time than every other entry put together, mostly through (if ...) in arithmetic, which keeps coming back because it reads so naturally.

A cast as the object of . loses its parentheses

(. (cast pa (* const wanted)) distance) emits (const wanted *)pa->distance, which C reads as a cast of pa->distance on a void *. Like the conditional entry below, the writer doesn't parenthesize a lower-precedence operand. Bound to a typed local first, it works.

Arrays of a header type with a symbolic size misparse

(workers [pthread-t MAX_WORKERS]) emits pthread_t MAX_WORKERS workers[];, reading MAX_WORKERS as a second type word. [u16 CHUNK_AREA] works, presumably because u16 is known to Sex and pthread_t only to C. A literal size, [pthread-t 32], works.

A conditional expression as an operand loses its parentheses

(/ (* 100.0 matched) (if (> compared 0) compared 1))

emits (100.0 * matched) / (compared > 0) ? compared : 1, which C parses as ((100.0 * matched) / (compared > 0)) ? compared : 1. Clang only warns (-Wparentheses). This one computes wrong values silently, so it is the most dangerous entry here. Bound to a variable first, it's right.

Clang warns only when the operator ends up in the condition, as above. When the conditional comes first, the rest of the operand moves into the else branch, and that is valid C nothing warns about. A sweep of this project found four such sites that had compiled cleanly with -Wall -Wparentheses:

  • (malloc (* (if (> w h) w h) (sizeof u32))) emitted malloc((w > h) ? w : h * sizeof(u32)): when w > h, a buffer a quarter of the size, so a heap overflow on every wide box blur.
  • (+ (if (< d -4) -4 (if (> d 4) 4 d)) 4) gave an index of -4, an out-of-bounds write that corrupted a neighbouring array of counts and with it a figure in our own docs.
  • (* (if (> depth 40) 40 depth) 255) gave water deeper than 40 blocks the shallow colour.
  • (cast (if ...) float) cast only the condition; harmless there.

Only the generated C showed them. The fix is the same: bind to a variable first.

An import doesn't bring the types its interface needs

Module save takes a (* region-maps) from module maps. Importing save pastes its prototypes but not region-maps, so every importer has to import maps itself, and before save:

save.sex:284:52: error: unknown type name 'region_maps'

vsmv.sex now imports eight modules in dependency order to use three. Imports are at least idempotent (a diamond works).

A pub record with a field of a private record type

(pub record natural-shape (... (slots (* shape-cell)) ...)) with shape-cell not pub compiles in its own module, and in the importer fails with unknown type name 'shape_cell'. Either an error at the definition or exporting the type would do.

[(* T) N] fields break in importers

A pub record field (noises [(* simplex-sum) 32]) compiles in its own module but not when pasted into an importer: Error: pointer chains are written flat, as (* * T), not nested: ((* simplex-sum) MAX_STRATA). The message points at the wrong thing; [* simplex-sum MAX_STRATA] works, while a local (var names [(* const char) 6]) is fine either way.

define bodies are pasted raw

(define K (/ (cast 255 float) (cast 60 float))) emits #define K cast(255, float) / cast(60, float): not translated, and not parenthesized. With (define D (/ -1.0 6.0)), (* x D) becomes x * -1.0 / 6.0. That's (x * -1.0) / 6.0, which rounds differently from multiplying by the folded constant. Bit-exact code needs literal values or typed =var=s.

#line splits a ternary in a call argument

(if c a b) in expression position becomes c ? a : b. Inside a multi-line call, though, the statement anchor lands between the ternary's parts, and the C doesn't compile:

(.set (& river) x y (if (== d 0) 4219071 (grey (- 255 d))))
dump.sex:128:1: error: expected expression
  128 | , grey(255 - d));

-C output, without the directives, is fine. The build now passes --line-directives=toplevel to every Sex unit.

A typedef of a function pointer type crashes the compiler

(typedef colour-fn (fn ((i32)) u32))
Error: (cdr) bad argument type: u32

No location. The same type written inline in a parameter list works.

sizeof of a pointer type reads as a dereference

(sizeof (* char)) becomes sizeof(*char). There's no way to spell the type; (sizeof x) on an expression works.

case bodies share one scope

A case body isn't braced, so (var bits u64 ...) in one case and (var bits u32 ...) in the next is a C redefinition. Clang also warns "label followed by a declaration is a C23 extension". Wrapping each body in (do ...) works around it; the writer could do that itself.

Constants in array types aren't kebab-converted

(var xs [u16 CHUNK-AREA]) and record fields like (terrain-height [u16 CHUNK-AREA]) emit CHUNK-AREA literally, while CHUNK-AREA in an expression becomes CHUNK_AREA. Writing CHUNK_AREA in the type works around it.

? is not mangled in names

(fn ident-start? ...) emits ident_start?, which isn't C. Lisp predicates want ?; - is mangled to _, so ? could become _p.

Relative SEX_MODULE_PATH entries resolve from the root

SEX_MODULE_PATH=src sexc ... searches /src/. An absolute path works.

No hex literals

0x80 reaches C as |0x80| (the Scheme printer escaping a symbol that looks numeric), and #x80 is rejected by the reader: "Unsupported # syntax: #\x". Masks and code points have to be written in decimal, which hurts in bit-twiddling code: (& c 224) instead of (& c 0xE0).

Explicit enum values emit raw names

(pub enum wire ((wire-varint 0) (wire-fixed64 1)))

emits wire-varint = 0, ..., neither kebab-converted nor accepted by

  1. Plain (enum name (a b c)) works; explicit values need =define=s.

Member access inside a compound literal ignores the operand's type

With self a (* pb), #(pb : (. self p) (. self p) true) emits self.p, not self->p. The same (. self p) outside a literal is right. Bound to a local first, it works.

Methods can only call methods above them

Inside one ops block, calling a method defined further down fails with no such method on pb len. C needs prototypes, so this is consistent, but the block could declare its methods before their bodies.

-o - crashes instead of meaning stdout

--help says -C output defaults to stdout without -o; passing -o - explicitly dies in getopt-long's argument handling.

A malformed for crashes the compiler

sexc dfafe8b. Writing the C-style grouped header

(for ((var i int 0) (< i 4) (++ i))
  ...)

instead of (for (var i int 0) (< i 4) (++ i) ...) dies inside the writer with no source location:

Error: (caddr) bad argument type: ()
	fmt-c-writer.scm:112: scheme#symbol->string

Expected: a diagnostic naming the file, the line and the expected shape of for.

Spurious "cannot type this" on a header pointer

sexc dfafe8b. In src/viewer.sex:

(var font (* struct nk-font) (nk-font-atlas-add-default ...))
(/= (. font handle height) scale)

This warns cannot type this; `.' reads it as a value font, yet the emitted C is the correct font->handle.height / scale;=. The variable's type is written out, so the warning is wrong on both counts: the type is known, and the access isn't read as a value. The nearby (& (. font handle)) gives no warning.

No dependency output

sexc has nothing like cc -MD. An import pastes the imported module's public forms into the importer, so the Makefile has to make every Sex object depend on every .sex source.