diff options
| author | eunhhu <[email protected]> | 2026-09-25 15:14:08 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-09-25 15:14:08 +0100 |
| commit | c298c2eec0b7ec0fcdacb8f661a9da3c184d2960 (patch) | |
| tree | affe490db2b070f0b064bbb9bf5ade49d45a9293 /src/main.c | |
| parent | 91aa0afcd34cff345f1f7293d82fec7d00082d8b (diff) | |
| download | aup-c298c2eec0b7ec0fcdacb8f661a9da3c184d2960.tar.gz aup-c298c2eec0b7ec0fcdacb8f661a9da3c184d2960.zip | |
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.
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 60 |
1 files changed, 50 insertions, 10 deletions
@@ -5,26 +5,66 @@ #include <string.h> static int usage(void) { - fputs("aup check FILE\naup build FILE -o OUT.pmod\n", stderr); + fputs("aup check FILE\naup build FILE -o OUT.pmod\n" + "A library `use name` is name.aup beside FILE or in a directory of AUP_PATH (':' separated).\n", stderr); return 2; } +/* Read a whole source; NULL if it cannot be read. Longer than the limit is + * left for the compiler to report. */ +static char *read_source(const char *path, size_t *length) { + FILE *file = fopen(path, "rb"); + if (!file) return NULL; + char *source = malloc(AUP_SOURCE_LIMIT + 1); + *length = source ? fread(source, 1, AUP_SOURCE_LIMIT + 1, file) : 0; + bool failed = !source || ferror(file); + fclose(file); + if (failed) { free(source); return NULL; } + return source; +} + +typedef struct { char directory[AUP_PATH]; char *sources[16]; unsigned count; } Libraries; + +static bool try_library(Libraries *l, const char *directory, size_t directory_length, const char *name, + const char **source, size_t *length, char path[AUP_PATH]) { + int written = snprintf(path, AUP_PATH, "%.*s/%s.aup", (int)directory_length, directory, name); + if (written < 0 || written >= AUP_PATH || l->count == sizeof(l->sources) / sizeof(*l->sources)) return false; + char *text = read_source(path, length); + if (!text) return false; + *source = l->sources[l->count++] = text; + return true; +} + +/* Beside the main source first, then each directory of AUP_PATH in order. */ +static bool load_library(void *context, const char *name, const char **source, size_t *length, char path[AUP_PATH]) { + Libraries *l = context; + if (try_library(l, l->directory, strlen(l->directory), name, source, length, path)) return true; + for (const char *at = getenv("AUP_PATH"); at && *at;) { + size_t span = strcspn(at, ":"); + if (span && try_library(l, at, span, name, source, length, path)) return true; + at += span + (at[span] == ':'); + } + return false; +} + int main(int argc, char **argv) { bool build = argc == 5 && !strcmp(argv[1], "build") && !strcmp(argv[3], "-o"); if (!build && !(argc == 3 && !strcmp(argv[1], "check"))) return usage(); const char *path = argv[2]; - FILE *file = fopen(path, "rb"); - if (!file) { perror(path); return 2; } - static char source[AUP_SOURCE_LIMIT + 1]; - size_t length = fread(source, 1, sizeof(source), file); - bool unreadable = ferror(file); - fclose(file); - if (unreadable) { fprintf(stderr, "%s: cannot read\n", path); return 2; } + size_t length; + char *source = read_source(path, &length); + if (!source) { perror(path); return 2; } + Libraries libraries = {.count = 0}; + const char *slash = strrchr(path, '/'); + snprintf(libraries.directory, sizeof(libraries.directory), "%.*s", slash ? (int)(slash - path) : 1, slash ? path : "."); unsigned char *module; size_t size; AupDiagnostic error; - if (!aup_compile(source, length, &module, &size, &error)) { - fprintf(stderr, "%s:%d:%d: %s\n", path, error.line, error.column, error.message); + bool compiled = aup_compile(source, length, load_library, &libraries, &module, &size, &error); + for (unsigned i = 0; i < libraries.count; ++i) free(libraries.sources[i]); + free(source); + if (!compiled) { + fprintf(stderr, "%s:%d:%d: %s\n", *error.path ? error.path : path, error.line, error.column, error.message); return 1; } int status = 0; |
