diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-04-04 23:23:59 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-04-04 23:23:59 -0400 |
commit | 943922ae04fa5194091606ad717ecea91ad2f646 (patch) | |
tree | 3c10758c2154770d0433a581da8253b29e4d094a | |
parent | 98534503a8fdf8bcb5692c4e9c76adee4a70e5d3 (diff) |
added using structs and its mostly working but found a weird bug when an error happens beforehand
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | test.toc | 25 | ||||
-rw-r--r-- | types.c | 21 |
3 files changed, 31 insertions, 17 deletions
@@ -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 @@ -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)); } @@ -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; |