diff options
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parse.c | 8 | ||||
-rw-r--r-- | tests/ptr_arithmetic.toc | 6 | ||||
-rw-r--r-- | types.c | 189 | ||||
-rw-r--r-- | types.h | 6 |
5 files changed, 100 insertions, 110 deletions
@@ -10,6 +10,7 @@ see development.md for development information @TODO: #for figure out how printf is gonna work +when putf is done, migrate tests to new std make a global table of builtin types, so if you ever need a pointer to one you can just point to the table improve type_to_str: Foo ::= struct(t::Type) {} @@ -2395,14 +2395,10 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { if (!cond_success) return false; w->body.kind = BLOCK_WHILE; } break; - case KW_FOR: - for_stmt: { + case KW_FOR: { s->kind = STMT_FOR; For *fo = s->for_ = parser_malloc(p, sizeof *fo); fo->flags = 0; - if (t->token->kind == TOKEN_DIRECT) { - fo->flags |= FOR_STATIC; - } Block *prev_block = p->block; fo->body.parent = p->block; p->block = &fo->body; @@ -2518,8 +2514,6 @@ static Status parse_stmt(Parser *p, Statement *s, bool *was_a_statement) { } break; case DIRECT_IF: goto if_stmt; - case DIRECT_FOR: - goto for_stmt; case DIRECT_ERROR: case DIRECT_WARN: case DIRECT_INFO: { diff --git a/tests/ptr_arithmetic.toc b/tests/ptr_arithmetic.toc index 05975e8..521bdd2 100644 --- a/tests/ptr_arithmetic.toc +++ b/tests/ptr_arithmetic.toc @@ -1,4 +1,4 @@ -#include "std/io.toc"; +#include "std/io.toc", io; #include "std/mem.toc"; ptr_arithmetic_test ::= fn() total := 0 { @@ -28,6 +28,6 @@ ptr_arithmetic_test ::= fn() total := 0 { main ::= fn() { x ::= ptr_arithmetic_test(); y := ptr_arithmetic_test(); - puti(x); - puti(y); + io.puti(x); + io.puti(y); } @@ -3553,99 +3553,11 @@ top: if (!index_type->flags) { construct_resolved_builtin_type(index_type, BUILTIN_I64); } + + if (header->flags & DECL_IS_CONST) { + // static for + // @TODO ASDF - if (flags & FOR_STATIC) { - static_for: - // exit for body - tr->block = prev_block; - arr_remove_lasta(tr->in_decls, tr->allocr); - // create one block, containing a block for each vararg - // e.g. for x := varargs { total += x; } => { { x := varargs[0]; total += x; } { x := varargs[0]; total += x; } } - assert(fo->of->kind == EXPR_IDENT); - Identifier varargs_ident = fo->of->ident; - Declaration *idecl = varargs_ident->decl; - VarArg *varargs = idecl->val.varargs; - size_t nvarargs = arr_len(varargs); - // create surrounding block - s->kind = STMT_BLOCK; - Block *b = s->block = typer_calloc(tr, 1, sizeof *s->block); - idents_create(&b->idents, tr->allocr, b); - b->stmts = NULL; - b->parent = tr->block; - b->where = s->where; - arr_set_lena(b->stmts, nvarargs, tr->allocr); - Statement *stmt = b->stmts; - size_t nstmts = arr_len(fo->body.stmts); - Declaration *header_decl = &fo->header; - - assert(arr_len(header_decl->idents) == 2); - Identifier val_ident = header_decl->idents[0]; - Identifier index_ident = header_decl->idents[1]; - bool has_val = !ident_eq_str(val_ident, "_"); - bool has_index = !ident_eq_str(index_ident, "_"); - - for (size_t i = 0; i < nvarargs; ++i, ++stmt) { - // create sub-block #i - memset(stmt, 0, sizeof *stmt); - stmt->kind = STMT_BLOCK; - Block *sub = stmt->block = typer_calloc(tr, 1, sizeof *sub); - sub->parent = b; - idents_create(&sub->idents, tr->allocr, sub); - sub->stmts = NULL; - sub->where = s->where; - size_t total_nstmts = nstmts + has_val + has_index; - arr_set_lena(sub->stmts, total_nstmts, tr->allocr); - Copier copier = copier_create(tr->allocr, sub); - if (has_val) { - // @TODO(eventually): don't put a decl in each block, just put one at the start - Statement *decl_stmt = &sub->stmts[0]; - decl_stmt->flags = 0; - decl_stmt->kind = STMT_DECL; - decl_stmt->where = s->where; - - // declare value - Declaration *decl = decl_stmt->decl = typer_calloc(tr, 1, sizeof *decl); - decl->where = fo->of->where; - Identifier ident = ident_translate_forced(val_ident, &sub->idents); - typer_arr_add(tr, decl->idents, ident); - ident->decl = decl; - - decl->flags |= DECL_HAS_EXPR; - decl->expr.kind = EXPR_BINARY_OP; - decl->expr.binary.op = BINARY_AT_INDEX; - decl->expr.binary.lhs = fo->of; - decl->expr.where = fo->of->where; - Expression *index = decl->expr.binary.rhs = typer_calloc(tr, 1, sizeof *decl->expr.binary.rhs); - index->kind = EXPR_LITERAL_INT; - index->intl = (U64)i; - index->where = fo->of->where; - } - if (has_index) { - // @TODO(eventually): don't put a decl in each block, just put one at the start - Statement *decl_stmt = &sub->stmts[has_val]; - decl_stmt->flags = 0; - decl_stmt->kind = STMT_DECL; - decl_stmt->where = s->where; - - // declare value - Declaration *decl = decl_stmt->decl = typer_calloc(tr, 1, sizeof *decl); - decl->where = fo->of->where; - Identifier ident = ident_translate_forced(index_ident, &sub->idents); - typer_arr_add(tr, decl->idents, ident); - ident->decl = decl; - - decl->flags |= DECL_HAS_EXPR; - decl->expr.kind = EXPR_LITERAL_INT; - decl->expr.intl = (U64)i; - decl->expr.where = fo->of->where; - } - - size_t start = total_nstmts - nstmts; - for (size_t idx = start; idx < total_nstmts; ++idx) { - copy_stmt(&copier, &sub->stmts[idx], &fo->body.stmts[idx-start]); - } - } - goto top; } if (is_range) { @@ -3751,9 +3663,91 @@ top: break; case TYPE_BUILTIN: switch (iter_type->builtin) { - case BUILTIN_VARARGS: { - fo->flags = flags |= FOR_STATIC; - goto static_for; + case BUILTIN_VARARGS: { // varargs for loop + // exit for body + tr->block = prev_block; + arr_remove_lasta(tr->in_decls, tr->allocr); + // create one inline block, containing a block for each vararg + // e.g. for x := varargs { total += x; } => { x := varargs[0]; total += x; } { x := varargs[0]; total += x; } + assert(fo->of->kind == EXPR_IDENT); + Identifier varargs_ident = fo->of->ident; + Declaration *idecl = varargs_ident->decl; + VarArg *varargs = idecl->val.varargs; + size_t nvarargs = arr_len(varargs); + // create surrounding block + s->kind = STMT_INLINE_BLOCK; + s->inline_block = NULL; + arr_set_lena(s->inline_block, nvarargs, tr->allocr); + Statement *stmt = s->inline_block; + size_t nstmts = arr_len(fo->body.stmts); + Declaration *header_decl = &fo->header; + + assert(arr_len(header_decl->idents) == 2); + Identifier val_ident = header_decl->idents[0]; + Identifier index_ident = header_decl->idents[1]; + bool has_val = !ident_eq_str(val_ident, "_"); + bool has_index = !ident_eq_str(index_ident, "_"); + + for (size_t i = 0; i < nvarargs; ++i, ++stmt) { + // create block #i + memset(stmt, 0, sizeof *stmt); + stmt->kind = STMT_BLOCK; + Block *b = stmt->block = typer_calloc(tr, 1, sizeof *b); + b->parent = tr->block; + idents_create(&b->idents, tr->allocr, b); + b->stmts = NULL; + b->where = s->where; + size_t total_nstmts = nstmts + has_val + has_index; + arr_set_lena(b->stmts, total_nstmts, tr->allocr); + Copier copier = copier_create(tr->allocr, b); + if (has_val) { + Statement *decl_stmt = &b->stmts[0]; + decl_stmt->flags = 0; + decl_stmt->kind = STMT_DECL; + decl_stmt->where = s->where; + + // declare value + Declaration *decl = decl_stmt->decl = typer_calloc(tr, 1, sizeof *decl); + decl->where = fo->of->where; + Identifier ident = ident_translate_forced(val_ident, &b->idents); + typer_arr_add(tr, decl->idents, ident); + ident->decl = decl; + + decl->flags |= DECL_HAS_EXPR; + decl->expr.kind = EXPR_BINARY_OP; + decl->expr.binary.op = BINARY_AT_INDEX; + decl->expr.binary.lhs = fo->of; + decl->expr.where = fo->of->where; + Expression *index = decl->expr.binary.rhs = typer_calloc(tr, 1, sizeof *decl->expr.binary.rhs); + index->kind = EXPR_LITERAL_INT; + index->intl = (U64)i; + index->where = fo->of->where; + } + if (has_index) { + Statement *decl_stmt = &b->stmts[has_val]; + decl_stmt->flags = 0; + decl_stmt->kind = STMT_DECL; + decl_stmt->where = s->where; + + // declare value + Declaration *decl = decl_stmt->decl = typer_calloc(tr, 1, sizeof *decl); + decl->where = fo->of->where; + Identifier ident = ident_translate_forced(index_ident, &b->idents); + typer_arr_add(tr, decl->idents, ident); + ident->decl = decl; + + decl->flags |= DECL_HAS_EXPR; + decl->expr.kind = EXPR_LITERAL_INT; + decl->expr.intl = (U64)i; + decl->expr.where = fo->of->where; + } + + size_t start = total_nstmts - nstmts; + for (size_t idx = start; idx < total_nstmts; ++idx) { + copy_stmt(&copier, &b->stmts[idx], &fo->body.stmts[idx-start]); + } + } + goto top; } default: break; } @@ -4154,7 +4148,10 @@ top: typer_arr_add(tr, tr->uses, u); } break; case STMT_INLINE_BLOCK: - assert(0); // only exists after typing + // this can happen, because static fors and varargs for turn into these + arr_foreach(s->inline_block, Statement, sub) + if (!types_stmt(tr, sub)) + return false; break; } success: @@ -234,7 +234,6 @@ typedef enum { DIRECT_INCLUDE, DIRECT_FORCE, DIRECT_IF, - DIRECT_FOR, DIRECT_ERROR, DIRECT_WARN, DIRECT_INFO, @@ -244,7 +243,7 @@ typedef enum { } Directive; static const char *directives[DIRECT_COUNT] = { - "C", "sizeof", "alignof", "export", "foreign", "builtin", "include", "force", "if", "for", + "C", "sizeof", "alignof", "export", "foreign", "builtin", "include", "force", "if", "error", "warn", "info", "no_warn", "init" }; @@ -875,8 +874,7 @@ typedef struct While { enum { FOR_IS_RANGE = 0x01, FOR_INCLUDES_FROM = 0x02, - FOR_INCLUDES_TO = 0x04, - FOR_STATIC = 0x08 + FOR_INCLUDES_TO = 0x04 }; typedef U8 ForFlags; typedef struct For { |