commit 4cdb1b4
shrub
·
2026-11-18 19:09:23 +0000 UTC
parent ad70a2b
module fixes and system modules
M
kav.h
+1,
-0
1@@ -27,6 +27,7 @@ struct lexer {
2 const char *linestart;
3 const char *file;
4 usize line;
5+ int afteruse;
6 };
7
8 void lex(struct lexer *, struct token *);
M
lex.re
+10,
-0
1@@ -9,11 +9,15 @@ static void settok(struct lexer *l, struct token *t, enum tokenkind k) {
2 t->loc.line = l->line;
3 t->loc.col = l->tok - l->linestart + 1;
4 t->loc.file = l->file;
5+ if(k != TNONE)
6+ l->afteruse = k == TUSE;
7 }
8
9 void lex(struct lexer *l, struct token *t) {
10 loop:
11 l->tok = l->cur;
12+ if(l->afteruse && *l->cur == '<')
13+ goto usepath;
14 /*!re2c
15 re2c:define:YYCTYPE = char;
16 re2c:define:YYCURSOR = "l->cur";
17@@ -43,6 +47,12 @@ void lex(struct lexer *l, struct token *t) {
18 "\x00" { errorat(&t->loc, "unterminated comment"); }
19 * { goto comment; }
20 */
21+ usepath:
22+ /*!re2c
23+ "<" [^>\n\x00]* ">" { settok(l, t, TUSEPATH); if(t->length == 2) errorat(&t->loc, "empty module path"); return; }
24+ "<" [^>\n\x00]* { settok(l, t, TUSEPATH); errorat(&t->loc, "unterminated module path"); }
25+ * { settok(l, t, TNONE); errorat(&t->loc, "expected system module path"); }
26+ */
27 }
28
29 static const char *const tokname[] = {
M
module.c
+66,
-5
1@@ -1,3 +1,6 @@
2+#define _XOPEN_SOURCE 700
3+
4+#include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7
8@@ -8,12 +11,51 @@ static struct module *seen;
9 static struct module *head;
10 static struct module **end = &head;
11
12-static const char *
13+static char *
14+kavpath(struct token name) {
15+ const char *dir = getenv("KAVPATH");
16+ const char *colon;
17+ char *canonical;
18+ stralloc sa = {0};
19+ usize len;
20+
21+ if(!dir)
22+ dir = "/usr/lib/kav";
23+ if(name.str[1] == '/')
24+ errorat(&name.loc, "system module paths must be relative");
25+ for(;;) {
26+ colon = strchr(dir, ':');
27+ len = colon ? (usize)(colon - dir) : strlen(dir);
28+ if(len) {
29+ sa.len = 0;
30+ sacatb(&sa, dir, len);
31+ sacatl(&sa, "/");
32+ sacatb(&sa, name.str + 1, name.length - 2);
33+ sa0(&sa);
34+ canonical = realpath(sa.s, nil);
35+ if(canonical) {
36+ free(sa.s);
37+ return canonical;
38+ }
39+ if(errno != ENOENT && errno != ENOTDIR)
40+ errorat(&name.loc, "unable to resolve ", sa.s, ": ", strerror(errno));
41+ }
42+ if(!colon)
43+ break;
44+ dir = colon + 1;
45+ }
46+ free(sa.s);
47+ errorat(&name.loc, "module ", tokdup(name), " not found");
48+}
49+
50+static char *
51 usepath(const char *from, struct token name) {
52 stralloc sa = {0};
53 const char *slash;
54
55- if(slash = strrchr(from, '/'))
56+ if(name.kind == TUSEPATH)
57+ return kavpath(name);
58+ if(name.str[1] != '/' && (slash = strrchr(from, '/')))
59 sacatb(&sa, from, slash - from + 1);
60 sacatb(&sa, name.str + 1, name.length - 2);
61 sa0(&sa);
62@@ -25,11 +67,21 @@ load(const char *path, const struct location *at) {
63 struct module *m;
64 struct module *s;
65 struct decl *d;
66+ char *canonical;
67+ char *importpath;
68
69+ canonical = realpath(path, nil);
70+ if(!canonical) {
71+ if(!at)
72+ diesys("unable to resolve ", path);
73+ errorat(at, "unable to resolve ", path, ": ", strerror(errno));
74+ }
75+ path = canonical;
76 for(s = seen; s; s = s->nextseen) {
77 if(strcmp(s->path, path) == 0) {
78 if(s->state == 1)
79 die("import cycle through ", path);
80+ free(canonical);
81 return s;
82 }
83 }
84@@ -38,13 +90,19 @@ load(const char *path, const struct location *at) {
85 m->nextseen = seen;
86 seen = m;
87 m->path = path;
88- if(!openreadclose(path, &m->src))
89+ if(!openreadclose(path, &m->src)) {
90+ if(!at)
91+ diesys("unable to read ", path);
92 errorat(at, "unable to read ", path, ": ", strerror(errno));
93+ }
94 sa0(&m->src);
95 m->decls = parse(path, m->src.s);
96 for(d = m->decls; d; d = d->next)
97- if(d->kind == DUSE)
98- d->mod = load(usepath(path, d->name), &d->name.loc);
99+ if(d->kind == DUSE) {
100+ importpath = usepath(path, d->name);
101+ d->mod = load(importpath, &d->name.loc);
102+ free(importpath);
103+ }
104 m->state = 2;
105 *end = m;
106 end = &m->next;
107@@ -53,6 +111,9 @@ load(const char *path, const struct location *at) {
108
109 struct module *
110 loadall(const char *path) {
111+ seen = nil;
112+ head = nil;
113+ end = &head;
114 load(path, nil);
115 return head;
116 }
M
parse.c
+4,
-1
1@@ -559,7 +559,9 @@ usedecl(void) {
2
3 expect(TUSE);
4 d = mkdecl(DUSE, tok);
5- expect(TSTRING);
6+ if(tok.kind != TSTRING && tok.kind != TUSEPATH)
7+ errorat(&tok.loc, "expected module path");
8+ next();
9 if(consume(TAS)) {
10 d->alias = tok;
11 expect(TIDENT);
12@@ -615,6 +617,7 @@ parse(const char *file, const char *src) {
13 lx.linestart = src;
14 lx.line = 1;
15 lx.file = file;
16+ lx.afteruse = 0;
17
18 next();
19 while(tok.kind != TEOF) {
M
tokens.h
+1,
-0
1@@ -3,6 +3,7 @@ TOKEN(TEOF, )
2 TOKEN(TIDENT, )
3 TOKEN(TNUMBER, )
4 TOKEN(TSTRING, )
5+TOKEN(TUSEPATH, )
6
7 #include "kws.h"
8 #include "punc.h"