aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/type.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/prc1/type.c')
-rw-r--r--src/prc1/type.c79
1 files changed, 49 insertions, 30 deletions
diff --git a/src/prc1/type.c b/src/prc1/type.c
index c7e43d0..b7f6882 100644
--- a/src/prc1/type.c
+++ b/src/prc1/type.c
@@ -17,8 +17,7 @@ static int t(const struct lex *l, struct prv *p);
static const struct seq seqs[] =
{
- {(struct step[])
- {{ID, "array"}, {ID, "of"}, {ID, "etc"}, {0}}, .fn = aetc},
+ {(struct step[]){{ID, "array"}, {ID, "of"}, {ID, "etc"}, {0}}, .fn = aetc},
{(struct step[]){{ID, "array"}, {ID, "of"}, {NUM}, {0}}, .fn = anum},
{(struct step[]){{ID, "array"}, {ID, "of"}, {ID}, {0}}, .fn = aid},
{(struct step[]){{ID, "pointer"}, {ID, "to"}, {0}}, .fn = p},
@@ -77,8 +76,13 @@ void type_free(struct type *t)
void type_lfree(struct type *t)
{
- if (t->child)
- type_lfree(t->child);
+ if (t)
+ {
+ if (t->child)
+ type_lfree(t->child);
+
+ type_free(t);
+ }
free(t);
}
@@ -186,13 +190,9 @@ const struct type *type_ufind(const struct fn *fn, const char *s)
}
if (td)
- for (size_t i = 0; i < td->ntypes; i++)
- {
- const struct type *t = &td->types[i];
-
+ for (const struct type *t = td->types; t; t = t->next)
if (!strcmp(t->tk->s, s))
return t;
- }
for (size_t i = 0; i < fn->nimps; i++)
{
@@ -215,10 +215,9 @@ const struct type *type_find(const struct fn *fn, const char *s)
return t;
}
-static int queue(struct prv *p, struct type *t)
+static void queue(struct prv *p, struct type *t)
{
struct type *tt;
- struct pos *pos = &p->pos[p->i];
if (!(tt = p->t))
p->t = t;
@@ -230,10 +229,8 @@ static int queue(struct prv *p, struct type *t)
tt->child = t;
}
- pos->seq = pos->stseq;
- pos->step = pos->seq->steps;
p->stk = p->tk;
- return 1;
+ reset(p);
}
static int aetc(const struct lex *l, struct prv *p)
@@ -255,12 +252,13 @@ static int aetc(const struct lex *l, struct prv *p)
if (!(t = malloc(sizeof *t)))
{
- perror("malloc");
+ perror("malloc(3)");
return -1;
}
*t = (struct type){.type = ARY, .u.ary.etc = 1};
- return queue(p, t);
+ queue(p, t);
+ return 0;
}
static int aid(const struct lex *l, struct prv *p)
@@ -294,12 +292,13 @@ static int aid(const struct lex *l, struct prv *p)
}
else if (!(t = malloc(sizeof *t)))
{
- perror("malloc");
+ perror("malloc(3)");
return -1;
}
*t = (struct type){.type = ARY, .u.ary.n = v};
- return queue(p, t);
+ queue(p, t);
+ return 0;
}
static int anum(const struct lex *l, struct prv *p)
@@ -320,12 +319,13 @@ static int anum(const struct lex *l, struct prv *p)
}
else if (!(t = malloc(sizeof *t)))
{
- perror("malloc");
+ perror("malloc(3)");
return -1;
}
*t = (struct type){.type = ARY, .u.ary.n = n};
- return queue(p, t);
+ queue(p, t);
+ return 0;
}
static int p(const struct lex *l, struct prv *p)
@@ -334,24 +334,27 @@ static int p(const struct lex *l, struct prv *p)
if (!t)
{
- perror("malloc");
+ perror("malloc(3)");
return -1;
}
*t = (struct type)
{
.type = PTR,
+ /* TODO: ask target for sizeof (void *) */
.sz = sizeof (void *),
.align = sizeof (void *)
};
if ((tt = p->t) && tt->type == ARY)
{
+ /* TODO: ask target for sizeof (void *) */
tt->sz = tt->u.ary.n * sizeof (void *);
tt->align = sizeof (void *);
}
- return queue(p, t);
+ queue(p, t);
+ return 0;
}
static int aryeq(const struct type *a, const struct type *b)
@@ -372,10 +375,10 @@ static int eq(const struct type *a, const struct type *b)
return !a && !b;
}
-static const struct type *insert(struct prv *p)
+const struct type *type_insert(struct prv *p, struct type *a)
{
struct ast *ast = p->ast;
- struct type *a = p->t, *tt = ast->types;
+ struct type *tt = ast->types;
for (const struct type *b = ast->types; b; b = b->next)
if (eq(a, b))
@@ -434,26 +437,42 @@ static int t(const struct lex *l, struct prv *p)
tt->align = t->align;
}
- if (!(t = insert(p)))
+ if (!(t = type_insert(p, p->t)))
return -1;
-
- p->t = NULL;
}
+ else
+ p->t = (struct type *)t;
+ if (pop(l, p))
+ return -1;
+
+ p->stk = p->tk;
+ p->t = NULL;
return p->type(l, p, t);
}
+static int endtype(const struct lex *l, struct prv *p)
+{
+ if (!p->t)
+ {
+ errloc(p->stk - 1, "expected type");
+ return -1;
+ }
+
+ return 0;
+}
+
int type(const struct lex *l, struct prv *p,
int (*fn)(const struct lex *, struct prv *, const struct type *))
{
struct pos init =
{
+ .name = __func__,
.seq = seqs,
- .stseq = seqs,
- .step = seqs->steps
+ .end = endtype
};
p->stk = p->tk;
p->type = fn;
- return push(&init, p) ? -1 : 1;
+ return push(&init, p);
}