aboutsummaryrefslogtreecommitdiff
path: root/parse.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 /parse.c
parenta3f56c76444fee090e62a59f257ed5f60b4e552b (diff)
downloadprc-3c0819236e4f07c8f57aaf1ca46d0d68aa761558.tar.gz
Move source to src/prc1
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c183
1 files changed, 0 insertions, 183 deletions
diff --git a/parse.c b/parse.c
deleted file mode 100644
index 1c3aba2..0000000
--- a/parse.c
+++ /dev/null
@@ -1,183 +0,0 @@
-#include "parse.h"
-#include "div.h"
-#include "errloc.h"
-#include "fn.h"
-#include "lex.h"
-#include "lit.h"
-#include "prv.h"
-#include "type.h"
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-
-void ast_free(struct ast *ast)
-{
- struct type *t = ast->types;
-
- while (t)
- {
- struct type *next = t->next;
-
- type_lfree(t);
- t = next;
- }
-
- for (struct lit *l = ast->lits, *next; l; l = next)
- {
- next = l->next;
- lit_free(l);
- free(l);
- }
-
- for (size_t i = 0; i < ast->nfns; i++)
- fn_free(&ast->fns[i]);
-
- free(ast->fns);
-}
-
-static const char *expected(const struct step *s)
-{
- static const char *tmap[] =
- {
- [ID] = "identifier", [LIT] = "literal", [NUM] = "number"
- };
-
- return s->id ? s->id : tmap[s->type];
-}
-
-static int iter(const struct lex *l, struct prv *p)
-{
- struct pos *pos = p->pos;
- const struct step *s;
- const struct tk *tk = p->tk;
-
- if (!pos)
- return p->entry(l, p);
-
- pos = &p->pos[p->i];
-
- if (!(s = pos->step))
- {
- if (lex_eof(l, tk))
- return 0;
- else if (p->n > 1)
- return pop(l, p) ? -1 : 1;
- else
- {
- switch (tk->type)
- {
- case LIT:
- errloc(tk, "unexpected literal \"%s\"", tk->s);
- break;
- case NUM:
- errloc(tk, "unexpected number %s", tk->s);
- break;
- case ID:
- errloc(tk, "unexpected %s %s", kw(tk->s) ?
- "keyword" : "identifier", tk->s);
- break;
- case ANY:
- case UNDEF:
- errloc(tk, "%s: unreachable", __func__);
- break;
- }
-
- return -1;
- }
- }
- else if (lex_eof(l, tk))
- {
- if (p->stk == p->tk)
- return pop(l, p) ? -1 : 1;
- else if (s->type)
- {
- tk--;
-
- if (lex_eof(l, p->stk))
- {
- errloc(tk, "expected %s", expected(pos->step));
- return -1;
- }
- }
- }
-
- if (!s->type)
- return pos->seq->fn(l, p);
- else if ((s->type == ID || s->type == ANY) && !s->id && kw(tk->s)
- && !s->chain)
- {
- if (tk != p->stk || !p->n)
- {
- errloc(tk, "unexpected keyword %s", tk->s);
- return -1;
- }
- else if (pop(l, p))
- return -1;
-
- pos = &p->pos[p->i];
- }
- else if ((s->type != tk->type && s->type != ANY)
- || (s->type == ID && s->id && strcmp(s->id, tk->s))
- || (s->type == ID && kw(tk->s) && s->chain))
- {
- pos->step = (++pos->seq)->steps;
- p->tk = p->stk;
- }
- else
- {
- const struct tk *next = tk + 1;
-
- if (!s->chain)
- pos->step++;
- else if (!lex_eof(l, next))
- {
- if (s->chain && kw(next->s))
- pos->step++;
- }
- else
- pos->step++;
-
- if (!lex_eof(l, p->tk))
- p->tk++;
- }
-
- return 1;
-}
-
-static void free_prv(struct prv *p)
-{
- free(p->tdc);
- free(p->pos);
-}
-
-static int comm(const struct lex *l, struct ast *ast,
- int (*entry)(const struct lex *, struct prv *))
-{
- int ret;
- struct prv prv =
- {
- .ast = ast,
- .tk = l->tokens,
- .stk = l->tokens,
- .entry = entry
- };
-
- if (!prv.stk)
- return 0;
-
- while ((ret = iter(l, &prv)) > 0)
- ;
-
- free_prv(&prv);
- return ret;
-}
-
-int parse(const struct lex *l, struct ast *ast)
-{
- return comm(l, ast, fn);
-}
-
-int parse_im(const struct lex *l, struct ast *ast)
-{
- return comm(l, ast, div_im);
-}