summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--allocator.c2
-rw-r--r--cgen.c18
-rw-r--r--copy.c10
-rw-r--r--decls_cgen.c2
-rw-r--r--main.c2
-rw-r--r--parse.c8
-rw-r--r--test.toc14
-rw-r--r--toc.c6
-rw-r--r--typedefs_cgen.c16
-rw-r--r--types.c4
10 files changed, 46 insertions, 36 deletions
diff --git a/allocator.c b/allocator.c
index 68a38cf..beb83dd 100644
--- a/allocator.c
+++ b/allocator.c
@@ -1,7 +1,7 @@
static void *err_malloc(size_t bytes);
static void *err_calloc(size_t n, size_t sz);
static void *err_realloc(void *prev, size_t new_size);
-/* #define NO_ALLOCATOR 1 /\* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator *\/ */
+#define NO_ALLOCATOR 1 /* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator */
/* number of bytes a page hold, not including the header */
#define PAGE_BYTES (16384 - sizeof(Page))
#define PAGE_MAX_ALIGNS (PAGE_BYTES / sizeof(MaxAlign))
diff --git a/cgen.c b/cgen.c
index 64b3fe8..169db09 100644
--- a/cgen.c
+++ b/cgen.c
@@ -318,22 +318,6 @@ static bool cgen_type_pre(CGenerator *g, Type *t, Location where) {
} else if (t->struc->c.id) {
cgen_ident_id(g, t->struc->c.id);
} else {
- #if 0
- /* TODO: DELME */
- cgen_write(g, "struct {");
- g->indent_lvl++;
- cgen_nl(g);
- arr_foreach(t->struc.fields, Field, f) {
- if (!cgen_type_pre(g, f->type, where)) return false;
- cgen_write(g, " ");
- cgen_ident(g, f->name);
- if (!cgen_type_post(g, f->type, where)) return false;
- cgen_write(g, ";");
- cgen_nl(g);
- }
- g->indent_lvl--;
- cgen_write(g, "}");
- #endif
assert(0);
}
break;
@@ -1944,6 +1928,8 @@ static bool cgen_defs_block(CGenerator *g, Block *b) {
if (!cgen_defs_stmt(g, s))
return false;
}
+ if (b->ret_expr && !cgen_defs_expr(g, b->ret_expr))
+ return false;
return true;
}
diff --git a/copy.c b/copy.c
index d85dacb..eaa2dc9 100644
--- a/copy.c
+++ b/copy.c
@@ -60,10 +60,9 @@ static void copy_val(Allocator *a, Value *out, Value *in, Type *t) {
}
}
-/* does not work on resolved types */
+/* only works on unresolved types */
static void copy_type(Copier *c, Type *out, Type *in) {
assert(!(in->flags & TYPE_IS_RESOLVED));
-
*out = *in;
switch (in->kind) {
case TYPE_BUILTIN:
@@ -91,7 +90,9 @@ static void copy_type(Copier *c, Type *out, Type *in) {
}
} break;
case TYPE_ARR:
- /* if (!(in->flags & TYPE_IS_RESOLVED)) { */
+ /* if (in->flags & TYPE_IS_RESOLVED) { */
+ /* out->arr.n = in->arr.n; */
+ /* } else { */
out->arr.n_expr = allocr_malloc(c->allocr, sizeof *out->arr.n_expr);
copy_expr(c, out->arr.n_expr, in->arr.n_expr);
/* } */
@@ -111,12 +112,13 @@ static void copy_type(Copier *c, Type *out, Type *in) {
*out->struc = *in->struc;
size_t nfields = arr_len(in->struc->fields);
out->struc->fields = NULL;
+
arr_set_lena(&out->struc->fields, nfields, c->allocr);
for (size_t i = 0; i < nfields; i++) {
Field *fout = &out->struc->fields[i];
Field *fin = &in->struc->fields[i];
*fout = *fin;
- copy_type(c, fout->type, fin->type);
+ copy_type(c, fout->type = allocr_malloc(c->allocr, sizeof *fout->type), fin->type);
}
} break;
}
diff --git a/decls_cgen.c b/decls_cgen.c
index 91ad40a..82bf56f 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -62,6 +62,8 @@ static bool cgen_decls_block(CGenerator *g, Block *b) {
arr_foreach(b->stmts, Statement, s)
if (!cgen_decls_stmt(g, s))
return false;
+ if (b->ret_expr && !cgen_decls_expr(g, b->ret_expr))
+ return false;
cgen_block_exit(g, prev);
return true;
}
diff --git a/main.c b/main.c
index 470f5c2..e98c1be 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,7 @@
/*
TODO:
-fix VBS with structs
test ArrInt @= Arr(int);
+new version of copy_val for copying types
there are probably places where we enter a function and never exit (in typing?) if there's an error
packages
diff --git a/parse.c b/parse.c
index 8c281f4..299e54d 100644
--- a/parse.c
+++ b/parse.c
@@ -167,10 +167,10 @@ static Keyword builtin_type_to_kw(BuiltinType t) {
/* returns the number of characters written, not including the null character */
static size_t type_to_str_(Type *t, char *buffer, size_t bufsize) {
- if ((t->flags & TYPE_IS_RESOLVED) && t->was_expr) {
- /* TODO: improve this (see also: case TYPE_EXPR) */
- return str_copy(buffer, bufsize, "<type expression>");
- }
+ /* if ((t->flags & TYPE_IS_RESOLVED) && t->was_expr) { */
+ /* /\* TODO: improve this (see also: case TYPE_EXPR) *\/ */
+ /* return str_copy(buffer, bufsize, "<type expression>"); */
+ /* } */
switch (t->kind) {
case TYPE_VOID:
return str_copy(buffer, bufsize, "void");
diff --git a/test.toc b/test.toc
index fe137d3..d5e24c4 100644
--- a/test.toc
+++ b/test.toc
@@ -23,11 +23,11 @@ y:s;
};
main @= fn() {
-x : pair(int);
-y : pair(int);
-z : pair(float);
-x[0] = 7;
-puti(x[0]);
-z[0] = 3.3;
-putf(z[0]);
+// a : pair(int);
+b : pair(int);
+c : pair(float);
+// a.x = 5;
+// puti(a.x);
+c.x = 13.3;
+// putf(c.x);
};
diff --git a/toc.c b/toc.c
index 203fddd..844d496 100644
--- a/toc.c
+++ b/toc.c
@@ -1,15 +1,15 @@
/* Includes all of toc's files */
#include <assert.h>
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
-#include <ctype.h>
#include <limits.h>
-#include <inttypes.h>
-#include <stdbool.h>
#include <float.h>
+#include <stdbool.h>
+#include <inttypes.h>
#include "types.h"
#include "allocator.c"
diff --git a/typedefs_cgen.c b/typedefs_cgen.c
index 0feee25..9da3121 100644
--- a/typedefs_cgen.c
+++ b/typedefs_cgen.c
@@ -1,13 +1,17 @@
static bool typedefs_stmt(CGenerator *g, Statement *s);
static bool typedefs_decl(CGenerator *g, Declaration *d);
+static bool typedefs_expr(CGenerator *g, Expression *e);
static bool typedefs_block(CGenerator *g, Block *b) {
Block *prev = g->block;
+ printf("%s\n",b->stmts[0].where.code);
if (!cgen_block_enter(g, b))
return false;
arr_foreach(b->stmts, Statement, s)
if (!typedefs_stmt(g, s))
return false;
+ if (b->ret_expr && !typedefs_expr(g, b->ret_expr))
+ return false;
cgen_block_exit(g, prev);
return true;
}
@@ -18,6 +22,18 @@ static bool typedefs_expr(CGenerator *g, Expression *e) {
/* needs to go before decls_cgen.c... */
e->fn.c.id = g->ident_counter++;
}
+ if (e->kind == EXPR_TYPE && e->typeval.kind == TYPE_STRUCT) {
+ StructDef *sdef = e->typeval.struc;
+ if (sdef->c.id || sdef->c.name) {
+ /* we've already done this */
+ } else {
+ cgen_write(g, "struct ");
+ IdentID id = g->ident_counter++;
+ cgen_ident_id(g, id);
+ sdef->c.id = id;
+ cgen_write(g, ";");
+ }
+ }
return true;
}
diff --git a/types.c b/types.c
index 2b08aa3..7e40d64 100644
--- a/types.c
+++ b/types.c
@@ -1407,6 +1407,10 @@ static bool types_expr(Typer *tr, Expression *e) {
case BINARY_EQ:
case BINARY_NE: {
bool valid = false;
+ assert(lhs_type->flags & TYPE_IS_RESOLVED);
+ assert(rhs_type->flags & TYPE_IS_RESOLVED);
+
+
if (o == BINARY_SET) {
valid = type_eq(lhs_type, rhs_type);
if (lhs_type->kind == TYPE_TYPE) {