1
0
forked from alex-eg/sex
Commit Graph

105 Commits

Author SHA1 Message Date
f0a1b6712f spell ? and ! in names as letters C takes
A Lisp predicate came out as ident_start?, which no C compiler reads.
ʔ and ꜝ are letters that look the part; ǃ looks closer, but clang flags
it as a homoglyph of `!' at every use.

Fixes #49.
2026-10-19 16:04:51 +03:00
ba45069484 walk a define's body, and parenthesise it
The body was pasted as read, so `cast' stayed a call and names kept
their dashes; and fmt-c parenthesised only a function-like body, so
an object-like one regrouped with the operator it was pasted under.

Fixes #55.
2026-10-19 16:04:51 +03:00
8db9246389 convert enumerators written with a value
(wire-varint 0) is a pair, and both the writer and map-fields only
expected a name: the writer passed it out unconverted, and a macro
mapping over the enum was handed the pair.

Fixes #46.
2026-10-19 16:04:51 +03:00
6f29a9c203 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.
2026-10-19 16:04:51 +03:00
bc5f23881f spell an array bound as an expression
It went to fmt-c unwalked, so a bound named CHUNK-AREA kept its dash
while the same name in an expression became CHUNK_AREA.

Fixes #50.
2026-10-19 16:04:51 +03:00
dfdca46e1e tell an array's bound by the shape of its type
Whether a trailing name was the bound hinged on the word before it
being a typedef Sex had seen, which a header's pthread_t never is, and
a grouped (* T) never is either: [pthread-t N] became a two-word type.

Fixes #56, #60.
2026-10-19 16:04:51 +03:00
8bc27cba26 parenthesise casts and conditionals by precedence
Neither took part in it: a cast parenthesised its operand only from a
list of binary heads that missed `|', and never itself, so `->' took
it apart; `?:' was written bare under any operator, swallowing it into
the test. Three of the four still compiled, to wrong values.

Fixes #59, #61, #62.
2026-10-19 16:04:51 +03:00
db5507b180 give a record's docstring a line of its own 2026-10-19 15:56:05 +03:00
b5030e3824 say what the threading test checks, not how -> came free 2026-10-19 15:56:05 +03:00
8f7b26eafa name the encapsulation test after what it covers 2026-10-19 15:56:05 +03:00
a885c21b9b infer a global's type before its braces lose theirs
A global's initializer was stripped to plain braces before `_' was
solved, so #(point : 2 3) left nothing to infer point from.
2026-10-19 15:33:37 +03:00
a0b323980f refuse a match in a case label
case and case/fallthrough walked their label as a statement, so a match
there was taken and wrote `case switch:'.
2026-10-19 15:32:47 +03:00
913af0b463 start a global from braces, not a compound literal
A compound literal is no constant in C11, so a global built by a
constructor or a typed literal failed under -pedantic-errors; gcc says
so, clang lets it pass.
2026-10-15 11:20:53 +03:00
6cda0873c6 hold a literal for an array field to the field's length
A longer one reached cc as excess elements, and a shorter one filled
the rest with zeros where a named array of that length was refused.
2026-10-15 11:15:57 +03:00
0a5eb1e0ce refuse a match in a control form's test
An if, while or for walked every slot as a statement, so a match in
its test came out as a switch inside the condition, which cc refuses.
2026-10-15 11:15:19 +03:00
0a565046af say that a binder is a copy, and where %-> is
A match binder looks like a reference to its field and is not one. And
with `.' reading an untyped receiver as a value, nothing said how to
write the arrow by hand.
2026-10-14 17:27:52 +03:00
9c5f678830 hand an array field its elements
An array field compiled to C that cc refused: an array is neither an
initializer of another nor something a payload literal can be built
from. Both the constructor and the binder now spell it out element by
element, which needs a length sexc can count. A sum holding itself by
value is refused at the declaration rather than as cc's incomplete type.
2026-10-14 17:14:36 +03:00
427177d50f write a sum's constructors in place
A constructor was a static function per variant. One taking an array
field cannot exist, its parameter being a pointer, and building some
variants in place and others by call would make two kinds of
constructor. In place, every one is a constant where one is wanted, an
lvalue a method can take the address of, and nothing a pub sum has to
paste into its importer.

What the function did for free is done here instead: arity is checked,
since a short compound literal zero-fills quietly, and an argument is
converted against its field's closure type. The price is that a
constructor is no longer a function value; a lambda makes one.
2026-10-14 17:11:38 +03:00
e91b44dbe1 walk a global's initializer like a local's
Only a local's initializer was walked, so at file scope a path was not
dispatched, a lambda was never lifted, and a constructor written in
place would have been left a call to nothing.
2026-10-14 17:08:42 +03:00
9af9113bf5 walk the values of a brace literal
The walk stopped at a vector, so whatever a literal held reached the
writer as written: #(v : (.get a)) was (v){.get(a)}, and a path through
a pointer inside one read it as a value.
2026-10-14 17:07:20 +03:00
224ef8fb6b file methods, realizations and sums under what a typedef names
A typedef had the fields of its record but none of its methods, and a
typedef of a sum was not a sum to match. A typedef that ends in a
scalar still keeps its own, so (typedef meters float) does not lend
its methods to every float.
2026-10-14 16:54:16 +03:00
bb2f05fa36 type a macro call by what it expands to
A receiver, a scrutinee or the start of a path was typed before the
walk had expanded it, so (.len (the a)) had no type to dispatch on and
(. (the p) x) read a pointer as a value.
2026-10-14 16:40:37 +03:00
3baaafafc3 refuse the matches and sums cc would refuse
Two else arms, a variant declared twice, and a match standing where a
value is wanted all reached cc as broken C; an arm that is not a list
crashed sexc. A variant called tag collided with the tag enum, now
spelled shapeǂ so no variant name can reach it.
2026-10-14 16:37:51 +03:00
5ba55f05a5 read a spread self as the pointer it is
(self * const v) typed the method's self as `self', so a call passed a
value where the method took a pointer, and a spec written one way
refused a realization written the other. A const receiver handed to a
method taking (* v) is now refused rather than left to cc's warning.
2026-10-14 16:24:00 +03:00
f5c53cdd41 tell a spec default by its body, not its length
A required method with a docstring or a comment counted as a default:
the realization could leave it out, and got a prototype with no
definition or a function with no return.
2026-10-14 15:54:07 +03:00
0119c85ded link ops.sex against libm
sqrtf lives in libm on glibc; macOS folds it into libSystem, which hid
the missing -lm until CI linked on Linux.
2026-10-09 15:18:18 +03:00
dfafe8becd lay out threading.sex one step to a line 2026-10-08 11:09:42 +03:00
e9960488fb test an opaque record across a module boundary
A bodyless pub record ahead of a private definition exports the handle
and keeps the fields, the way flecs keeps ecs_world_t. It worked with
nothing added, and nothing said so.
2026-10-08 11:09:42 +03:00
6c8b38b7e2 floor the C target at C11
f5eee71 spelled the closure environment in C99 for a floor nobody meant
to keep: closures were already C11 through _Static_assert. max_align_t
says the alignment in one word again, and the floor test now holds C11.
2026-10-08 11:09:42 +03:00
dd3a4bdac5 implement thread-first ->
`.' now writes whichever of C's member operators the type calls for,
so nothing needs the arrow spelled out and the name is free for the
macro Clojure spells the same way: (-> 5 double-it (add 3)) reads in
the order it happens rather than inside out.

It has no business in the prelude, which every unit pays for whether
or not it threads anything, so it opens the standard library instead
and is imported like anything else. The compiler finds lib/ by asking
the binary where it is -- beside sexc in a work tree, under
../share/sex once installed -- and looks there last, so a module of
one's own by that name still wins.

The C arrow becomes `%->', and that rename is what makes the rest
possible. The closure lifter writes arrows of its own for the capture
record, those forms go back through walk-statement, and a macro named
`->' threads them: (-> ƛcaptures n) turns into a call to n, with
nothing saying so until the C compiler.

fmt-c rendered a bare `->' as the arrow, which left the one spelling
that cannot be right -- threading with no import -- looking like the
operator it stopped being. It goes through c-apply now, the way any
other head nothing knows does.
2026-10-05 02:25:57 +03:00
fb4b8aa1f9 dispatch . on the type it reads from
Which of C's two member operators applies is a question the compiler
can already answer -- (.length v) and (.length p) have picked & or *
off the receiver's type since methods moved into the head -- so making
the programmer answer it again asks them to restate what semen knows,
and to revisit every reader of a field the day it becomes a pointer.

A path resolves a step at a time, since only the type in hand answers
for the next field. A receiver nothing can type is read as a value and
warned about rather than refused: refusing leaves no spelling at all
for a struct out of a header nobody has parsed.

Two things had to come along. A parameter written (p * struct point)
was recorded with nothing but the `*' for a type, which left half the
paths in list-macros unanswerable. And fmt-c parenthesised a member
access written inside another, which went unnoticed while only match
nested them -- its bindings have been reading ((s.u).circle).r since
sums went in.
2026-10-05 02:08:15 +03:00
95f39e4b44 space toplevel forms the way the source does
Every toplevel form was followed by a blank line whether or not one
was written, so the prelude's run of typedefs arrived at twice the
height. A function had no location of its own to measure from either,
having lost it where its header and body are stitched together.
2026-10-05 02:08:15 +03:00
7f3f22e853 type a method call, so one can be another's receiver
expression-type knew nothing about a .method head, so the receiver of
(.length-sq (.sub a b)) had no type and the outer call could not
resolve. Vector arithmetic is nothing but nested calls like that one.
2026-10-04 14:04:01 +03:00
f071684aec do not mangle an include's target
A target is a filename, and the ː separator turned GL/gl.h into
GLːgl.h, so every program including a header from a subdirectory
stopped compiling the moment qualified names got their separator.
2026-10-04 13:59:18 +03:00
737864dadc keep the worked example honest
Encapsulation.org's example was written before any of it was built
and still called methods the way the first draft did. It compiles
now, so it lives here as well, and the doc's block and this file are
the same text.
2026-10-04 13:25:17 +03:00
6181a6c8d4 implement spec and realize
A spec is a contract that emits nothing; a realization is that
contract's methods for one type, mangled typeːspecːmethod. A default
is copied into every realizing type with the parameter substituted, so
the `show' a default calls is that type's own and the whole thing
stays static dispatch with nothing left at runtime.

The methods land in the ops registry beside inherent ones, which is
what lets (.show v) find them with no second lookup -- and makes the
duplicate check already there catch a spec method colliding with an
ops method or with another spec's. Coherence came free.

A realization's signatures are checked against the spec, arguments and
return type both, after substituting the parameter; a missing required
method is named, and so is one the spec has not got. That is what
makes a spec a contract rather than a naming convention.

The bound is asked rather than declared: realizes? goes into the macro
API beside type-of, and a generic calls it and raises its own error.
That is the return this document was written for -- `plain does not
realize ord' where the line is, instead of a wall of C errors from a
monomorphization two stages downstream.

A default is static even when the realization is pub, and does not
travel on export: every importing unit synthesizes its own copy, so
sending one too would be a second definition of one symbol. The cost
is that a module whose defaults call printf has to pub include
stdio.h -- which is also why pub include and pub import now work, both
having been accepted by read-public-interface and refused by semen.
2026-10-04 12:56:24 +03:00
07cf85910a test what record, ops, sum and match refuse
The diagnostics are what these forms are for -- a match that leaves a
variant out is the thing C cannot say -- and until now every one of
them was checked by hand and none by the suite.

The two registries refuse a second declaration of one name, which is
per translation unit and not per process, so semen-process clears
them on the way in. sexc compiles one file per process and never
noticed; a suite that compiles forty in one did.
2026-10-03 23:33:38 +03:00
4a649e5bf4 walk the cdr of a form as a list, not as a form
The walker offered walk-fn every cdr as well as every form, so the
tail (t) of (fn g () t) read as a call to a macro named t: any
function whose return type or parameter type shared a name with a
macro expanded that tail and died on the macro's arity.
2026-10-03 23:33:32 +03:00
bbdedb9777 put the method in the head, not in a field position
(. v length) made the shape of a form depend on the receiver's type:
(. a b c d e) was either the path a.b.c.d.e or a/b called on c d e,
and nothing but the ops registry could say which. Resolution had to
arbitrate, a field won, and so adding a field named length to vec2
turned every (. v length) in reach from a call into a read -- silently,
and only sometimes into a type error.

Resolution may need types; shape must not. (.length v) puts the method
where an operator belongs, so (. v x) is always the field x, a field
and a method of one name coexist, and a reader needs no symbol table
to see where the arguments start.

Which of &, * or nothing goes in front of the receiver now follows
from its own type against the method's self, rather than from whether
the caller wrote . or ->, so (.length v) and (.length p) are the same
call and -> is member access again and nothing else.

The reader already allowed it: only a standalone . becomes dot-access,
and .foo is the ordinary symbol designated initializers are made of.
2026-10-03 23:17:44 +03:00
3a10ee099b implement sum and match
A sum is the tag enum, a payload struct per variant that carries
fields, the record holding both, and a constructor each. match is a
switch over the tag whose arms open a block and bind the payload as
locals, so the shape of the generated C is the shape a C programmer
would have written by hand.

What it is for is the refusal: a match that leaves a variant out is an
error naming the ones missing, and so are an arm the sum does not
have, a wrong number of binders, and the same arm written twice. C
cannot say any of that, which is why every switch over a tag in the
wild ends in a defensive default nobody can tell from a real case.

Only the sum is typedef'd. A payload struct would put shapeːcircle in
the ordinary namespace, where the constructor of that name already
lives; as a bare tag it collides with nothing. The sum's own typedef
is emitted ahead of the payload structs, since a variant may hold a
pointer back to the sum, and constructors are static even for a pub
sum, which is pasted into the importer whole.

The scrutinee is read once per binding, so one that is not a path of
names is held in a local first -- otherwise (match (f) ...) would call
f once for the tag and again for every field it binds.
2026-10-03 22:59:15 +03:00
dea36e3c02 implement ops
A method is reachable two ways, spelled out as vec2/length or through
its receiver as (. v length), and both land on the same function. The
registry behind the second one is why this is a form and not a
defmacro: a macro expands and vanishes, while resolution, module
export and the spec bounds still to come all want a declaration site
they can read.

The receiver is adjusted to whatever the method asks for -- & where a
value meets a pointer self, * where a pointer meets a value one -- so
the call reads the same whichever is in hand. A field wins over a
method of the same name, so adding one cannot shadow a field in
silence, and needing & on something that is not an lvalue is refused
here rather than by cc: &(f()) is not C, and an expression has nowhere
to put a temporary.

Qualified names are spelled vec2ːlength. The compiler composes them
and the user does not, so __ would collide with a hand-written
vec2__length, and is reserved in C++ besides. The separator has to be
a letter, C admitting letters in an identifier and not symbols, which
is what ruled out § -- hence U+02D0, on the same grounds as the ƛ a
lifted closure carries.

A block crosses a module boundary as a block, its pub methods cut down
to prototypes. Prototypes alone would say that vec2ːlength exists
without saying that vec2 has a length, and the receiver needs the
second thing; which is also why sex-modules now links against types.
2026-10-03 22:46:39 +03:00
622db8eadc add record, a struct that names itself
(record point ...) emits the struct and a typedef of the same name, so
point stands alone wherever (struct point) had to -- in a var, a
pointer, an arglist, a sizeof. struct, union and enum keep C's
meaning, for a type that came from a header or is laid out to match one.
2026-10-03 22:46:39 +03:00
fe72a109bf read a var's type as a type, not as a call
`walk-parts' walked the whole `var' form, and a `fn' type's parameter
list is shaped like a call: `(fn ((c int)) int)' came out as
`int (*fp)(c int)'. A closure literal had no type, so calling one in
place missed the rewrite; sextest fed sexc symbols Sex cannot read.
2026-09-30 23:51:55 +03:00
e87463be87 type the operators the walk had no rule for
`&&', the bitwise operators, the shifts and `++' each stopped with
`cannot infer'; an array operand came back as the array; a rank tie
went to whichever operand came first; and a toplevel `_' reached the
writer unsolved.
2026-09-30 23:51:55 +03:00
f5eee71eb7 declare the closure environment in c99
`max_align_t' is C11 and the union goes into every unit, so a program
with no closure in it stopped building under -std=c99. `unify' also
bound a rigid variable one way round only.
2026-09-30 23:51:55 +03:00
024e97553b keep closure signatures apart when mangled
The argument list was flattened with one separator throughout, so
`((long long))' and `((long) (long))' named one struct. A capture
borrowing a name looked only at the local scope chain.
2026-09-30 23:51:55 +03:00
578feb6b73 scope and promote the way C does
A `while' or `switch' body is a block with no `do' around it, and its
declarations landed in the enclosing frame. Two operands of one narrow
type skipped the conversions, so `(+ c c)' answered `char'.
2026-09-30 23:51:55 +03:00
2da5b5005b give fmt-c a name for every parameter
fmt-c takes a parameter's name with `cadr', so an unnamed one handed
over bare lost its second word to it: `(* const char)' dropped its
star, and `(int)' had no second word at all.
2026-09-30 23:51:55 +03:00
2b73fcf1c4 answer where a written type ends in one place
An arglist entry, an array's bound and its element type ask one
question, and disagreed: `(unsigned int)' was a name plus a type, a
trailing typedef a bound, a subscript the type's second word.
2026-09-30 23:51:55 +03:00
1f445f0f9b implement type inference
Two things out of one mechanism. `_' as a type means "work it out from
the initializer", so (var n _ (strlen s)) stops needing size-t spelled
out; `type-of' hands a macro the type of an expression, so a macro can
dispatch on what it was handed rather than on what was declared. Both
read the same answers from two sides.

Algorithm W's core, intra-procedural, with the extensions C forces:

  - an unknown type, since (include stdio.h) brings in names we never
    parsed. Unification is consistency rather than equality, so
    anything touching an unparsed declaration stops constraining
    instead of rejecting a program that compiled yesterday;
  - the usual arithmetic conversions, since `+' is not a function of
    one type;
  - checking mode for initializers, since #(0 0) has no type of its own
    and takes one from its context. #(T : ...) is the way out of that.

What it wanted on the way:

  - what type a *name* has, which neither the typedef nor the tag
    database recorded. One table serves functions and variables, since
    a function type already has a surface spelling;
  - a scope chain, so a (var c int 9) inside a do ends with the block;
  - form-type, keyed by cons cell, so one form has one type;
  - macros expanded during the walk rather than before it, so type-of
    is answered in the scope the macro was written in.

Closures take the same machinery: a receiver whose type comes from a
call, captures written (name expr) and typed from the expression, and
conversion from a bare function wherever a closure is expected.

type-match grew `_' on the pattern side, since (closure ((int)) int)
and (closure ((float)) int) were separate clauses for one case.
2026-09-30 00:09:32 +03:00