summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-08-30 12:14:04 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-08-30 12:14:04 -0400
commit1929fee97726344c0e83d74258ff27d0488770f9 (patch)
treebc1622766e2f4a1a670c576bfa32a25c9bc95338
parent969a228777ca0f2f624618d48b7ed21e2c25ec29 (diff)
added array access lvalues
-rw-r--r--base_cgen.c4
-rw-r--r--out.c10
-rw-r--r--test.toc7
-rw-r--r--tokenizer.c4
-rw-r--r--types.c8
-rw-r--r--util/arr.c4
6 files changed, 23 insertions, 14 deletions
diff --git a/base_cgen.c b/base_cgen.c
index 8ac73e5..4c8cb72 100644
--- a/base_cgen.c
+++ b/base_cgen.c
@@ -107,7 +107,7 @@ static bool cgen_ident(CGenerator *g, Identifier i, Location *where) {
return false;
}
Declaration *decl = id_decl->decl;
- if (decl->flags & DECL_FLAG_HAS_EXPR) {
+ if ((decl->flags & DECL_FLAG_HAS_EXPR) && (decl->flags & DECL_FLAG_CONST)) {
if (decl->expr.kind == EXPR_FN) {
cgen_fn_name(g, &decl->expr.fn, NULL);
return true;
@@ -196,7 +196,7 @@ static bool cgen_type_post(CGenerator *g, Type *t) {
}
static bool cgen_fn_name(CGenerator *g, FnExpr *f, Location *where) {
- if (f->name && g->block == NULL) {
+ if (f->name) {
if (ident_eq_str(f->name, "main"))
cgen_write(g, "main__");
else
diff --git a/out.c b/out.c
index 95de661..23b0282 100644
--- a/out.c
+++ b/out.c
@@ -1,8 +1,13 @@
#include "out.h"
/* toc */
+static void a___(void);
void main__(void) {
void (*bar[3])(void) = {NULL};
+ void (*foo)(void) = a___;
+ ((bar[1])=foo);
+ ((bar[2])=foo);
+ ((bar[0])=foo);
int64_t i = 0;
void (*x)(void) = (bar[i]);
x();
@@ -12,9 +17,8 @@ void main__(void) {
(i=(i+1));
(x=(bar[i]));
x();
- (i=(i+1));
- (x=(bar[i]));
- x();
+}
+static void a___(void) {
}
int main(void) {
diff --git a/test.toc b/test.toc
index 846011e..4f7056e 100644
--- a/test.toc
+++ b/test.toc
@@ -1,5 +1,9 @@
main @= fn() {
bar : [3]fn();
+ foo := fn() {};
+ bar[1] = foo;
+ bar[2] = foo;
+ bar[0] = foo;
i := 0;
x := bar[i];
x();
@@ -9,7 +13,4 @@ main @= fn() {
i = i + 1;
x = bar[i];
x();
- i = i + 1;
- x = bar[i];
- x();
};
diff --git a/tokenizer.c b/tokenizer.c
index a66129d..a8932e2 100644
--- a/tokenizer.c
+++ b/tokenizer.c
@@ -105,7 +105,7 @@ typedef struct {
-static bool token_is_kw(Token *t, Keyword kw) {
+static inline bool token_is_kw(Token *t, Keyword kw) {
return t->kind == TOKEN_KW && t->kw == kw;
}
@@ -149,7 +149,7 @@ static Token *tokr_add(Tokenizer *t) {
return token;
}
-static void tokr_nextchar(Tokenizer *t) {
+static inline void tokr_nextchar(Tokenizer *t) {
if (*(t->s) == '\n') {
t->line++;
}
diff --git a/types.c b/types.c
index 5470bb9..bd908b0 100644
--- a/types.c
+++ b/types.c
@@ -116,10 +116,14 @@ static bool expr_must_lval(Expression *e) {
return true;
}
+ case EXPR_BINARY_OP:
+ if (e->binary.op == BINARY_AT_INDEX) return true;
+ break;
default:
- err_print(e->where, "Cannot assign to non-lvalue.");
- return false;
+ break;
}
+ err_print(e->where, "Cannot assign to non-lvalue.");
+ return false;
}
static bool type_of_expr(Expression *e, Type *t);
diff --git a/util/arr.c b/util/arr.c
index 97f4bd0..b3e0558 100644
--- a/util/arr.c
+++ b/util/arr.c
@@ -11,13 +11,13 @@ static void arr_create(Array *arr, size_t item_sz) {
arr->data = NULL;
}
-static void arr_reserve(Array *arr, size_t n) {
+static inline void arr_reserve(Array *arr, size_t n) {
arr->cap = n;
arr->data = realloc(arr->data, arr->item_sz * arr->cap);
}
-static void *arr_last(Array *arr) {
+static inline void *arr_last(Array *arr) {
if (arr->data)
return (void*)((char*)arr->data + arr->item_sz * (arr->len - 1));
else