From c298c2eec0b7ec0fcdacb8f661a9da3c184d2960 Mon Sep 17 00:00:00 2001 From: eunhhu Date: Fri, 25 Sep 2026 15:14:08 +0100 Subject: Add fns and libraries fn name(params) = value declares one expression, inlined where it is called: arguments are computed once onto the stack and the body reads them with PICK, so a fn costs no runtime call and keeps Pure's bound on work. Parameters take any kind of value; recursion is a compile error. use name reads name.aup beside the source or from AUP_PATH. A library declares only fns and uses, sees only its parameters, and is called as name.fn(...). Diagnostics now carry the file they are in, and the compiler takes a loader so that sources come from the host. examples/words.aup is such a library; examples/tasks.aup uses it and still matches its reference model. --- src/aup.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/aup.h') diff --git a/src/aup.h b/src/aup.h index 75047af..98b59b0 100644 --- a/src/aup.h +++ b/src/aup.h @@ -4,16 +4,22 @@ #include /* aup: source text to a Pure module (Pure docs/design/008-module-format.md). */ -enum { AUP_SOURCE_LIMIT = 32768, AUP_MESSAGE = 160 }; +enum { AUP_SOURCE_LIMIT = 32768, AUP_MESSAGE = 256, AUP_PATH = 256 }; typedef struct { + char path[AUP_PATH]; /* the library the error is in, or "" for the main source */ int line, column; char message[AUP_MESSAGE]; } AupDiagnostic; +/* Finds the library that `use name` names: its source, alive until + * aup_compile returns, and a path that diagnostics show. */ +typedef bool (*AupLoad)(void *context, const char *name, const char **source, size_t *length, + char path[AUP_PATH]); + /* Compile source into a newly allocated module (free with free()). The same - * source always produces the same bytes. On failure *module is NULL and the - * diagnostic names the first error. */ -bool aup_compile(const char *source, size_t length, unsigned char **module, size_t *size, - AupDiagnostic *error); + * sources always produce the same bytes. On failure *module is NULL and the + * diagnostic names the first error. load may be NULL when nothing is used. */ +bool aup_compile(const char *source, size_t length, AupLoad load, void *context, + unsigned char **module, size_t *size, AupDiagnostic *error); #endif -- cgit v1.2.3