Commit Graph

41 Commits

Author SHA1 Message Date
b49bb8f716 let a method call one written below it
Each method was registered just before its own body was walked, so an
earlier one could not find it. All are registered first now, and one
called above its definition is declared ahead of the bodies, which C
needs as well.

Fixes #44.
2026-10-14 19:22:34 +03:00
062cb18c60 walk what an initializer holds
The body walk passed a #(...) by as an atom, so nothing inside one was
typed, dispatched or lifted: (. self p) read as self.p with self a
pointer, where the same path outside the literal was right.

Fixes #45.
2026-10-14 19:22:32 +03:00
3e01fe41e8 make a declaring case body a block
A case body shared the switch's scope, so two cases could not each
declare `bits', and a label could not be followed by a declaration
before C23; inference shared the scope too, and typed a later case's
read of an outer name as the earlier case's local.

Fixes #51.
2026-10-14 19:21:13 +03:00
52228e6fc6 write arrays with brackets in tests
The tests spelled out (¤ ...), what [...] reads into, which nobody
writes. The reader's own tests keep it: there it is the point.
2026-10-14 19:12:47 +03:00
08853d2c77 read hex and binary literals
0x80 was not a number to string->number, so it became a symbol the
printer escaped as |0x80|, and #x was rejected outright. Both spellings
now read as the integer, which the C writes in decimal.

Fixes #47.
2026-10-14 19:12:47 +03:00
9d4454afe3 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.
2026-10-14 19:12:47 +03:00
5a887c0654 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-14 19:12:47 +03:00
b5de28342b 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-14 19:12:47 +03:00
f24adfa2db 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-14 19:12:47 +03:00
b0dd2d9f94 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-14 19:12:47 +03:00
169933ff2e 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-14 19:12:47 +03:00
0a565046af say that a binder is a copy, and where %-> is
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m40s
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
0119c85ded link ops.sex against libm
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m28s
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
Some checks failed
Sex CI / build-linux (pull_request) Failing after 5m41s
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
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
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
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
8a3f51e833 implement closures 2026-09-29 21:07:16 +03:00
aa5cfc1b7d remove capture list from lambdas, they are always pure
Explicitly rename capturing lambdas to closures, they will be
implemented later
2026-09-29 16:09:51 +03:00
ca88d9b386 implement compound and designated literals support
All checks were successful
Sex CI / build-linux (pull_request) Successful in 4m46s
Sex CI / build-linux (push) Successful in 4m50s
2026-09-27 19:35:37 +03:00
26e8e6c374 fix lambdas and add a test to prevent rotting
Lambdas rotted because there wasn't any test for them, and after
return type moved to the end, lambdas stayed assumning it in the
front. We fix this and add test program
2026-09-27 16:11:05 +03:00
e0987c1836 read the feature flags in sextest in the way sexc does
All checks were successful
Sex CI / build-linux (pull_request) Successful in 4m54s
Sex CI / build-linux (push) Successful in 4m48s
sextest resolves #+ and #- itself -- it reads the program and prints
what survives to sexc -- so a flag spelling it does not recognise
decides which branch gets compiled
2026-09-23 14:35:56 +03:00
bf83baa508 read-time feature expressions
'#+' and '#-' introduce conditional compilation: the form that follows
is kept only when the feature expression is true, and otherwise is read
and thrown away.  An expression is a feature name, or and / or / not
of them.

They are read time, not compile time.

Default features are the host's software-version, software-type and
machine-type as CHICKEN reports them, plus what --features flag adds.
2026-09-16 17:59:37 +03:00
0b2b97a0c6 add type database and compile-time reflection 2026-09-15 21:28:41 +03:00
d609a0c591 begin -> do
Shorter and doesn't imply existence of `end'
2026-09-15 16:32:55 +03:00
0db4a11a2b add `comments' test program 2026-09-15 16:32:55 +03:00
528f26b7ef migrate to Chicken 6 2026-09-09 12:45:12 +03:00
8ae2346e41 add small programs for compile testing
Some checks failed
Sex CI / build-macos (push) Has been cancelled
Sex CI / build-linux (push) Has been cancelled
2026-05-27 18:00:18 +03:00