commit 21a798437a371a9e65277e53be41f82a71b79716
parent e3b7a7ae5df4853ff4cf7e96ee070b12e96a6450
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Thu, 18 Dec 2025 21:03:29 +0000
added propper casts (sorta, pointer casts dont work)
Diffstat:
6 files changed, 26 insertions(+), 18 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -1,8 +1,9 @@
IMPORTANT
=========
-- make casts into real code, should check at the parser level to achieve this
-- add support for structs and other user aliased types
-- add arrays, along side the start of the rt lib
-- add tupples to allow for multiple return types
+- [x] make casts into real code, should check at the parser level to achieve this
+- [ ] scoped vars
+- [ ] add support for structs and other user aliased types
+- [ ] add arrays, along side the start of the rt lib
+- [ ] add tupples to allow for multiple return types
- .............................
diff --git a/cback.c b/cback.c
@@ -100,6 +100,13 @@ compilerexpr_C(rexpr *r, FILE *f) {
compiletype_C(r->type, f);
fprintf(f, " v%d = v%d;\n", vid++, right, r->name);
return vid - 1;
+ case CAST:
+ right = compilerexpr_C(r->args, f);
+ compiletype_C(r->type, f);
+ fprintf(f, " v%d = (", vid++);
+ fprintf(f, "%s", toctype(r->fname));
+ fprintf(f, ")(v%d);\n", right);
+ break;
}
if (isbin(r)) {
diff --git a/lexer.h b/lexer.h
@@ -30,6 +30,7 @@ enum lex_ops {
/* used by the parser, wont be assigned by the lexer */
FUNCALL,
+ CAST,
NEGATE,
DEREF,
};
diff --git a/parse.c b/parse.c
@@ -48,6 +48,14 @@ parserErrtok(const char *msg, tok t) {
}
int
+isbasictype(char *t) {
+ for (int i = 0; i < SIZE(builtintypes); i++)
+ if (SAME(t, builtintypes[i]))
+ return 1;
+ return 0;
+}
+
+int
getpres(tok t) {
switch (t.op) {
case LEOF:
@@ -189,6 +197,9 @@ parsefcall(tok *start, tok *end, mctx *ctx) {
e->type = f->rtype;
e->op = FUNCALL;
+ if (builtin && isbasictype(f->name))
+ e->op = CAST;
+
if (start[1].op != '(')
return NULL;
start = &start[2];
@@ -343,13 +354,7 @@ parserexpr(tok *start, tok *end, mctx *ctx) {
return e;
}
-int
-isbasictype(char *t) {
- for (int i = 0; i < SIZE(builtintypes); i++)
- if (SAME(t, builtintypes[i]))
- return 1;
- return 0;
-}
+
type *
parsetype(lex *l, mctx *ctx) {
diff --git a/parse.h b/parse.h
@@ -4,7 +4,6 @@
#include "lexer.h"
#include "util.h"
-
enum lexpr_ops {
LEXPR_ASSIGN,
LEXPR_FUNC,
diff --git a/test.spl b/test.spl
@@ -1,8 +1,3 @@
-func foo(c *i32) i32 {
- return *c;
-}
-
func main() i32 {
- a i32 = 5 + 5.4;
- b i32 = foo(&a);
+ a i32 = i16(400);
}