summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-10-31 13:39:23 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-10-31 13:39:23 -0400
commita328c2cb8dac42b87d30bf64963cebc4922fbaa1 (patch)
tree2908741ba7e39f7a88f1130f82847d80ad9c0185
parent8ce406bdb55c19d83abb8f7511212ee0c02cb487 (diff)
got . to work at compile time and fixed some other problems along the way
-rw-r--r--eval.c24
-rw-r--r--main.c3
-rw-r--r--parse.c3
-rw-r--r--test.toc10
4 files changed, 26 insertions, 14 deletions
diff --git a/eval.c b/eval.c
index 18ad9e9..ff92ea9 100644
--- a/eval.c
+++ b/eval.c
@@ -1155,15 +1155,23 @@ static bool eval_decl(Evaluator *ev, Declaration *d) {
long index = 0;
arr_foreach(d->idents, Identifier, i) {
IdentDecl *id = ident_decl(*i);
- if (has_expr && d->type.kind == TYPE_TUPLE) {
- val_copy(d->flags & DECL_FLAG_CONST ? ev : NULL, &id->val, &val.tuple[index], &d->type.tuple[index]);
- index++;
- } else if (!has_expr && d->type.kind == TYPE_ARR) {
- /* "stack" array */
- id->val.arr = err_calloc(d->type.arr.n, compiler_sizeof(d->type.arr.of));
- } else {
- val_copy(d->flags & DECL_FLAG_CONST ? ev : NULL, &id->val, &val, &d->type);
+ Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[index] : &d->type;
+ Value *thisval = NULL;
+ if (has_expr)
+ thisval = d->type.kind == TYPE_TUPLE ? &val.tuple[index] : &val;
+ Type *inner_type = type;
+ while (inner_type->kind == TYPE_USER)
+ inner_type = ident_typeval(type->user.name);
+ if (inner_type->kind == TYPE_STRUCT) {
+ puts("Allocating space for struct");
+ id->val.struc = err_calloc(1, compiler_sizeof(inner_type));
+ } else if (inner_type->kind == TYPE_ARR) {
+ id->val.arr = err_calloc(inner_type->arr.n, compiler_sizeof(inner_type->arr.of));
}
+
+ if (has_expr)
+ val_copy(d->flags & DECL_FLAG_CONST ? ev : NULL, &id->val, thisval, type);
+ index++;
id->flags |= IDECL_FLAG_HAS_VAL;
}
if (has_expr && d->expr.kind == EXPR_TUPLE) {
diff --git a/main.c b/main.c
index 0ffb8e6..80e5371 100644
--- a/main.c
+++ b/main.c
@@ -1,8 +1,5 @@
/*
TODO:
-dot at run time
-dot at compile time
-dot + string
. lvalue
using dot with pointers
length of slice/arr with .len
diff --git a/parse.c b/parse.c
index 5f6a506..3676ad9 100644
--- a/parse.c
+++ b/parse.c
@@ -619,13 +619,14 @@ static bool parser_is_definitely_type(Parser *p, Token **end) {
t->token++;
if (token_is_kw(t->token, KW_LBRACE)) goto end; /* void fn expr */
Type return_type;
+ bool prev = t->token->where.ctx->enabled;
t->token->where.ctx->enabled = false;
if (!parse_type(p, &return_type)) {
/* couldn't parse a return type. void fn type */
ret = true;
goto end;
}
-
+ t->token->where.ctx->enabled = prev;
if (token_is_kw(t->token, KW_LBRACE)) {
/* non-void fn expr */
goto end;
diff --git a/test.toc b/test.toc
index 26b449d..bf0fc19 100644
--- a/test.toc
+++ b/test.toc
@@ -7,7 +7,7 @@ Point @= struct {
x_coordinate, y_coordinate : int;
};
-main @= fn() {
+somefn @= fn() int {
p:Point;
x := p.({
t @= int;
@@ -18,5 +18,11 @@ main @= fn() {
"y_coordinate"
}
});
- puti(x);
+ x
+};
+
+main @= fn() {
+ X @= somefn();
+ puti(X);
+ puti(somefn());
};