commit 2534cc1edbb10f19ce49f8286cf68f5962b9bfb3
parent 8407c770cc7d16e589d4544e664b34014fb2d6c6
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Tue, 9 Dec 2025 19:55:37 +0000
many changes
Diffstat:
6 files changed, 33 insertions(+), 14 deletions(-)
diff --git a/check.c b/check.c
@@ -9,17 +9,16 @@
map *funcs;
map *vars;
-void
+map *
addmap(map *m, void *data, char *id, mctx *ctx) {
if (!m)
m = alloczctx(ctx, sizeof(map));
- if (m->next)
- addmap(m->next, data, id, ctx);
- else {
- m->next = alloczctx(ctx, sizeof(map));
- m->next->id = id;
- m->next->data = data;
- }
+ else if (m->next)
+ return addmap(m->next, data, id, ctx);
+
+ m->id = id;
+ m->data = data;
+ return m;
}
void *
@@ -46,13 +45,24 @@ checkfunc(lexpr *l, mctx *ctx) {
f = alloczctx(ctx, sizeof(fn));
f->name = l->fname;
f->rtype = l->rtype;
- addmap(funcs, f, l->fname, ctx);
+ funcs = addmap(funcs, f, l->fname, ctx);
for (int i = 0; i < l->bodyc; i++)
checklexpr(&l->body[i], l, ctx);
}
void
+checkassign(lexpr *l, mctx *ctx) {
+ var *v;
+ if (lookupmap(vars, l->name))
+ checkErr("var with name already declared");
+ v = alloczctx(ctx, sizeof(var));
+ v->name = l->name;
+ v->type = l->type;
+ vars = addmap(vars, v, l->name, ctx);
+}
+
+void
checklexpr(lexpr *l, lexpr *scope, mctx *ctx) {
switch (l->op) {
case LEXPR_FUNC:
@@ -61,7 +71,7 @@ checklexpr(lexpr *l, lexpr *scope, mctx *ctx) {
checkfunc(l, ctx);
break;
case LEXPR_ASSIGN:
- //checkassign(l, ctx);
+ checkassign(l, ctx);
break;
}
}
diff --git a/check.h b/check.h
@@ -19,10 +19,15 @@ typedef struct fn {
int argc;
} fn;
+typedef struct var {
+ char *name;
+ type *type;
+} var;
+
extern map *funcs;
extern map *vars;
-void addmap(map *m, void *data, char *id, mctx *ctx);
+map *addmap(map *m, void *data, char *id, mctx *ctx);
void *lookupmap(map *m, char *id);
void checkErr(const char *msg);
void checkfunc(lexpr *l, mctx *ctx);
diff --git a/lexer.c b/lexer.c
@@ -64,6 +64,10 @@ next(lex *l) {
t.op = *l->ptr++;
return t;
}
+ if (memcmp(l->ptr, ":=", 2) == 0) {
+ l->ptr += 2;
+ t.op = WALRUS;
+ }
if (memcmp(l->ptr, "func", 4) == 0) {
l->ptr += 4;
t.op = FUNC;
diff --git a/lexer.h b/lexer.h
@@ -23,6 +23,7 @@ enum lex_ops {
FUNC = 256,
NEGATE,
NAME,
+ WALRUS,
};
typedef struct tok {
diff --git a/parse.c b/parse.c
@@ -239,6 +239,7 @@ shortdec:
return le;
}
+
lexpr *
parsefunc(lex *l, mctx *ctx) {
SAVE(l);
diff --git a/test.spl b/test.spl
@@ -1,6 +1,4 @@
func main(arg1 int, arg2 int) int {
a int = 0;
- func foo() int {
- a int = 3;
- }
+ a int = 1;
}