aboutsummaryrefslogtreecommitdiff
path: root/im.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2026-09-05 17:14:53 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2026-09-26 03:41:55 +0200
commit3c0819236e4f07c8f57aaf1ca46d0d68aa761558 (patch)
treebd8b57cf5420cabc4ec29815052df545b55026f9 /im.c
parenta3f56c76444fee090e62a59f257ed5f60b4e552b (diff)
downloadprc-3c0819236e4f07c8f57aaf1ca46d0d68aa761558.tar.gz
Move source to src/prc1
Diffstat (limited to 'im.c')
-rw-r--r--im.c125
1 files changed, 0 insertions, 125 deletions
diff --git a/im.c b/im.c
deleted file mode 100644
index e9a208c..0000000
--- a/im.c
+++ /dev/null
@@ -1,125 +0,0 @@
-#include "im.h"
-#include "errloc.h"
-#include "fn.h"
-#include "prv.h"
-#include "parse.h"
-#include <errno.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-static int imlit(const struct lex *, struct prv *);
-
-static const struct seq seqs[] =
-{
- {(struct step[]){{LIT, NULL, 1}, {0}}, .fn = imlit},
- {0}
-};
-
-void im_free(struct im *im)
-{
- ast_free(&im->ast);
- lex_free(&im->l);
-}
-
-static int doim(const struct lex *l, const struct tk *tk, struct prv *p)
-{
- int ret = -1;
- FILE *f = NULL;
- struct im *im;
- struct fn *fn = fn_cur(p);
- struct ast *ast;
- size_t n = fn->nimps + 1;
- const char *path = tk->s;
-
- if (!strcmp(l->loc.f, path))
- {
- errloc(tk, "detected recursive import \"%s\"", path);
- goto end;
- }
- /* TODO: honor -I flags */
- if (!(f = fopen(path, "rb")))
- {
- errloc(tk, "failed to open import \"%s\": %s", path, strerror(errno));
- goto end;
- }
- else if (!(im = realloc(fn->imps, n * sizeof *im)))
- {
- perror("realloc(3)");
- goto end;
- }
-
- fn->imps = im;
- im = &fn->imps[fn->nimps++];
- *im = (struct im){.l.loc.f = path};
- ast = &im->ast;
-
- if (!(ast->fns = malloc(sizeof *ast->fns)))
- {
- perror("malloc(3)");
- goto end;
- }
-
- *ast->fns = (struct fn){.tk = tk};
- /* hack: treat imports as a function */
- ast->nfns = 1;
-
- if (lex(&im->l, f) || parse_im(&im->l, ast))
- goto end;
-
- ret = 0;
-
-end:
-
- if (f && fclose(f))
- {
- fprintf(stderr, "fclose(3) %s: %s\n", path, strerror(errno));
- ret = -1;
- }
-
- return ret;
-}
-
-static int imlit(const struct lex *l, struct prv *p)
-{
- const struct tk *tk = p->stk;
-
- while (!lex_eof(l, tk) && !kw(tk->s))
- if (doim(l, tk++, p))
- return -1;
-
- if (pop(l, p))
- return -1;
-
- p->stk = p->tk;
- return 1;
-}
-
-int im(const struct lex *l, struct prv *p)
-{
- struct pos init =
- {
- .seq = seqs,
- .stseq = seqs,
- .step = seqs->steps
- };
-
- struct fn *fn = fn_cur(p);
-
- if (fn->im)
- {
- const struct loc *loc = &fn->im->loc;
-
- errloc(p->stk, "imports section already defined at %s:%d:%d",
- loc->f, loc->line, loc->col);
- return -1;
- }
-
- if (push(&init, p))
- return -1;
-
- fn->im = p->stk;
- p->stk = p->tk;
- return 1;
-}