forked from alex-eg/sex
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.
39 lines
1.5 KiB
Makefile
39 lines
1.5 KiB
Makefile
# Multi-module linking.
|
|
#
|
|
# Modules are only testable end to end, and nothing else in the suite
|
|
# links more than one translation unit. Three things have to hold at
|
|
# once: an imported `pub fn' comes out as a prototype with external
|
|
# linkage, an imported `pub var' as an extern declaration, and the
|
|
# module's object survives being passed after `--'. Get any of them
|
|
# wrong and this fails to link -- or, in the `pub var' case, links and
|
|
# quietly counts into a private copy.
|
|
#
|
|
# It also checks what only a second translation unit can check: that an
|
|
# imported type reaches the type database, by expanding a macro that
|
|
# reads the imported struct's fields; and that a closure type crossing
|
|
# the boundary works both ways -- one built in the module and called
|
|
# here, one built here and called there, through a code pointer that is
|
|
# static in the other object.
|
|
#
|
|
# The public forms carry comments in their headers, which the reduction
|
|
# to a prototype and to an extern both have to look past.
|
|
|
|
SEXC ?= ../../sexc
|
|
|
|
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\ncolour 123
|
|
|
|
check:
|
|
@$(SEXC) greet.sex -c -o greet.o
|
|
@$(SEXC) greet-app.sex -o greet-app -- greet.o
|
|
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
|
|
echo "modules ok"; \
|
|
else \
|
|
echo "modules FAILED, got:"; ./greet-app; $(MAKE) clean; exit 1; \
|
|
fi
|
|
@$(MAKE) --no-print-directory clean
|
|
|
|
clean:
|
|
@rm -f greet.o greet-app
|
|
|
|
.PHONY: check clean
|