summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-04-04 23:23:59 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-04-04 23:23:59 -0400
commit943922ae04fa5194091606ad717ecea91ad2f646 (patch)
tree3c10758c2154770d0433a581da8253b29e4d094a
parent98534503a8fdf8bcb5692c4e9c76adee4a70e5d3 (diff)
added using structs and its mostly working but found a weird bug when an error happens beforehand
-rw-r--r--main.c2
-rw-r--r--test.toc25
-rw-r--r--types.c21
3 files changed, 31 insertions, 17 deletions
diff --git a/main.c b/main.c
index 4244664..0986f5a 100644
--- a/main.c
+++ b/main.c
@@ -8,6 +8,8 @@
/*
TODO:
+for accessing struct members (and other things, potentially) with struct["member"], just replace e with
+ a BINARY_DOT
use
- use with a decl, e.g. use p : Point;
- make sure use works with functions and for, e.g. for use p := points
diff --git a/test.toc b/test.toc
index d64d32e..41c91ce 100644
--- a/test.toc
+++ b/test.toc
@@ -1,22 +1,19 @@
-#include "std/io.toc";
+//#include "std/io.toc";
-
-n ::= nms {
- x := 5;
+Point ::= struct {
+ x, y: int;
}
-m ::= nms {
- x := 13;
- y := 108;
+sqdist ::= fn(p: Point) {
+ use p;
+ x * x + y * y
}
main ::= fn() {
- use n;
- use m;
- //x = 7;
- //puti(x);
- puti(y);
- y = 100;
- puti(m.y);
+ p: Point;
+ use p;
+ x = 3;
+ y = 4;
+ //puti(sqdist(p));
}
diff --git a/types.c b/types.c
index b32daac..8bb23a6 100644
--- a/types.c
+++ b/types.c
@@ -1809,6 +1809,7 @@ static Status types_expr(Typer *tr, Expression *e) {
Use *use = *usep;
Expression *used = &use->expr;
Identifier translated_use;
+ bool was_a_struct = false;
if (type_is_builtin(&used->type, BUILTIN_NMS)) {
Value val;
if (!eval_expr(tr->evalr, used, &val))
@@ -1819,13 +1820,13 @@ static Status types_expr(Typer *tr, Expression *e) {
translated_use = ident_translate(i, &body->idents);
} else {
/* it's a struct */
+ was_a_struct = true;
Type *struct_type = &used->type;
if (struct_type->kind == TYPE_PTR)
struct_type = struct_type->ptr;
assert(struct_type->kind == TYPE_STRUCT);
- translated_use = NULL;
- err_print(e->where, "not implemented yet");
- return false;
+ StructDef *struc = struct_type->struc;
+ translated_use = ident_translate(i, &struc->body.idents);
}
if (ident_is_declared(translated_use)) {
if (undeclared) {
@@ -1847,6 +1848,20 @@ static Status types_expr(Typer *tr, Expression *e) {
info_print(use->expr.where, "...and %simported by this use statement.", also);
return false;
}
+ if (was_a_struct) {
+ /* change to BINARY_DOT */
+ e->kind = EXPR_BINARY_OP;
+ e->flags = 0;
+ e->binary.op = BINARY_DOT;
+ e->binary.lhs = used;
+ e->binary.rhs = typer_calloc(tr, 1, sizeof *e->binary.rhs);
+ e->binary.rhs->kind = EXPR_IDENT;
+ e->binary.rhs->ident = i;
+ /* re-type */
+ if (!types_expr(tr, e))
+ return false;
+ return true;
+ }
}
}
if (!undeclared) break;