commit 2b686cbe658d51860a657ac4dc57329f2a10f883
parent f830eda935ab2c8f7917eb263196e16982a26d68
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Sun, 28 Nov 2021 20:07:16 -0600
add str type, allow assignment to variable from str literal
Diffstat:
3 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/main.c b/main.c
@@ -311,6 +311,8 @@ parse(struct token *tok)
if (strncmp(tok->slice.ptr, "i64", 3) == 0) {
decl.type = TYPE_I64;
+ } else if (strncmp(tok->slice.ptr, "str", 3) == 0) {
+ decl.type = TYPE_STR;
} else {
error("unknown type");
}
@@ -337,15 +339,21 @@ void
typecheck(struct items items)
{
for (size_t i = 0; i < items.len; i++) {
+ struct expr *expr;
switch (items.data[i].kind) {
case ITEM_DECL:
struct decl *decl = &decls.data[items.data[i].idx];
switch (decl->type) {
case TYPE_I64:
- struct expr *expr = &exprs.data[decl->val];
+ expr = &exprs.data[decl->val];
// FIXME: we should be able to deal with binary, ident or fcalls
if (expr->kind != EXPR_LIT || expr->d.v.type != P_INT) error("expected integer value for integer declaration");
break;
+ case TYPE_STR:
+ expr = &exprs.data[decl->val];
+ // FIXME: we should be able to deal with ident or fcalls
+ if (expr->kind != EXPR_LIT || expr->d.v.type != P_STR) error("expected string value for string declaration");
+ break;
default:
error("unknown decl type");
}
@@ -494,10 +502,15 @@ main(int argc, char *argv[])
}
} else if (item->kind == ITEM_DECL) {
struct expr *expr = &exprs.data[decls.data[item->idx].val];
- if (expr->kind == EXPR_LIT && expr->d.v.type == P_INT) {
- decls.data[item->idx].addr = data_pushint(expr->d.v.v.val);
+ if (expr->kind == EXPR_LIT) {
+ if (expr->d.v.type == P_INT) {
+ decls.data[item->idx].addr = data_pushint(expr->d.v.v.val);
+ } else if (expr->d.v.type == P_STR) {
+ size_t addr = data_push(expr->d.v.v.s.ptr, expr->d.v.v.s.len);
+ decls.data[item->idx].addr = data_pushint(addr);
+ }
} else {
- error("cannot allocate memory for expression");
+ error("cannot allocate memory for expression");
}
} else {
error("cannot generate code for type");
diff --git a/nooc.h b/nooc.h
@@ -41,7 +41,8 @@ struct fcall {
// FIXME: make a struct for more complex types
enum type {
- TYPE_I64
+ TYPE_I64,
+ TYPE_STR
};
struct decl {
diff --git a/prog.nc b/prog.nc
@@ -2,7 +2,9 @@ write i64 = 1
stdout i64 = 0
exit i64 = 60
len1 i64 = 11
+hello str = "hello "
+world str = "world"
len2 i64 = 6
-syscall(write, stdout, "hello world", (+ 1 (+ 5 5)))
-syscall(write, stdout, " world", len2)
+syscall(write, stdout, hello, 6)
+syscall(write, stdout, world, 5)
syscall(exit, 0)