summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arr.c4
-rw-r--r--blockarr.c4
-rw-r--r--cgen.c40
-rw-r--r--copy.c16
-rw-r--r--decls_cgen.c2
-rw-r--r--eval.c12
-rw-r--r--identifiers.c4
-rw-r--r--instance_table.c18
-rw-r--r--main.c3
-rw-r--r--parse.c2
-rw-r--r--str.c2
-rw-r--r--test.toc4
-rw-r--r--tests.c12
-rw-r--r--tokenizer.c6
-rw-r--r--types.c45
15 files changed, 94 insertions, 80 deletions
diff --git a/arr.c b/arr.c
index 10ad4a8..d25dbf1 100644
--- a/arr.c
+++ b/arr.c
@@ -164,10 +164,10 @@ You shouldn't rely on this, though, e.g. by doing
#ifdef TOC_DEBUG
static void arr_test(void) {
int *foos = NULL;
- for (int i = 0; i < 1000; i++) {
+ for (int i = 0; i < 1000; ++i) {
*(int *)arr_add(&foos) = i;
}
- for (int i = 0; i < (int)arr_len(foos); i++) {
+ for (int i = 0; i < (int)arr_len(foos); ++i) {
assert(foos[i] == i);
}
int lastx = -1;
diff --git a/blockarr.c b/blockarr.c
index 6280fde..5a689fe 100644
--- a/blockarr.c
+++ b/blockarr.c
@@ -57,12 +57,12 @@ static void block_arr_test(void) {
BlockArr a;
int *ps[100];
block_arr_create(&a, 3, sizeof(int));
- for (int i = 0; i < 100; i++) {
+ for (int i = 0; i < 100; ++i) {
int *p = block_arr_add(&a);
*p = i;
ps[i] = p;
}
- for (int i = 0; i < 100; i++) {
+ for (int i = 0; i < 100; ++i) {
assert(*ps[i] == i);
}
block_arr_free(&a);
diff --git a/cgen.c b/cgen.c
index da259d3..2689662 100644
--- a/cgen.c
+++ b/cgen.c
@@ -136,7 +136,7 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d);
FnExpr *fn = &e->fn; \
if (e->type.fn.constness) { \
Instance **data = fn->instances.data; \
- for (U64 i = 0; i < fn->instances.cap; i++) { \
+ for (U64 i = 0; i < fn->instances.cap; ++i) { \
if (fn->instances.occupied[i]) { \
cgen_recurse_subexprs_fn_simple((&(*data)->fn), decl_f, block_f); \
} \
@@ -184,7 +184,7 @@ static inline FILE *cgen_writing_to(CGenerator *g) {
/* indent iff needed */
static inline void cgen_indent(CGenerator *g) {
if (g->will_indent) {
- for (int i = 0; i < g->indent_lvl; i++)
+ for (int i = 0; i < g->indent_lvl; ++i)
fprintf(cgen_writing_to(g), "\t");
g->will_indent = false;
}
@@ -351,7 +351,7 @@ static bool cgen_type_post(CGenerator *g, Type *t, Location where) {
case TYPE_FN: {
bool out_param = cgen_uses_ptr(&t->fn.types[0]);
cgen_write(g, ")(");
- for (size_t i = 1; i < arr_len(t->fn.types); i++) {
+ for (size_t i = 1; i < arr_len(t->fn.types); ++i) {
if (i != 1)
cgen_write(g, ", ");
if (!cgen_type_pre(g, &t->fn.types[i], where))
@@ -513,7 +513,7 @@ static bool cgen_fn_header(CGenerator *g, FnExpr *f, Location where, U64 instanc
any_args = true;
if (f->ret_type.kind == TYPE_TUPLE) {
/* multiple return variables */
- for (size_t i = 0; i < arr_len(f->ret_type.tuple); i++) {
+ for (size_t i = 0; i < arr_len(f->ret_type.tuple); ++i) {
Type *x = &f->ret_type.tuple[i];
if (any_params || i > 0)
cgen_write(g, ", ");
@@ -600,7 +600,7 @@ static bool cgen_set(CGenerator *g, Expression *set_expr, const char *set_str, E
}
cgen_write(g, ";");
cgen_nl(g);
- cgen_write(g, "for (i = 0; i < %lu; i++) arr__out[i] = arr__in[i];", (unsigned long)type->arr.n);
+ cgen_write(g, "for (i = 0; i < %lu; ++i) arr__out[i] = arr__in[i];", (unsigned long)type->arr.n);
cgen_nl(g);
cgen_write(g, "}");
break;
@@ -629,7 +629,7 @@ static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents,
break;
case EXPR_TUPLE:
/* e.g. a, b = 3, 5; */
- for (size_t i = 0; i < arr_len(to->tuple); i++) {
+ for (size_t i = 0; i < arr_len(to->tuple); ++i) {
char *s = NULL, buf[64];
Expression *e = NULL;
if (idents)
@@ -661,12 +661,12 @@ static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents,
if (!cgen_expr(g, arg))
return false;
}
- i++;
+ ++i;
}
/* out params */
size_t len = exprs ? arr_len(exprs) : arr_len(idents);
- for (i = 0; i < (int)len; i++) {
+ for (i = 0; i < (int)len; ++i) {
if (any_args || i > 0)
cgen_write(g, ", ");
if (exprs) {
@@ -695,7 +695,7 @@ static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents,
prefix_id = to->each.c.id;
goto prefixed;
prefixed:
- for (unsigned long i = 0; i < (unsigned long)arr_len(to->type.tuple); i++) {
+ for (unsigned long i = 0; i < (unsigned long)arr_len(to->type.tuple); ++i) {
cgen_write(g, "(");
if (exprs) {
if (!cgen_expr(g, &exprs[i]))
@@ -747,7 +747,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
char *p = ret_name + strlen(ret_name);
if (e->type.kind != TYPE_VOID) {
if (e->type.kind == TYPE_TUPLE) {
- for (unsigned long i = 0; i < arr_len(e->type.tuple); i++) {
+ for (unsigned long i = 0; i < arr_len(e->type.tuple); ++i) {
sprintf(p, "%lu_", i);
if (!cgen_type_pre(g, &e->type.tuple[i], e->where)) return false;
cgen_write(g, " %s", ret_name);
@@ -1003,7 +1003,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
if (!constness || !arg_is_const(arg, constness[i])) {
if (!cgen_expr_pre(g, arg)) return false;
}
- i++;
+ ++i;
}
if (cgen_uses_ptr(&e->type)
&& e->type.kind != TYPE_TUPLE) {
@@ -1027,7 +1027,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
if (!cgen_expr(g, arg))
return false;
}
- i++;
+ ++i;
}
if (any_args) {
cgen_write(g, ", ");
@@ -1387,7 +1387,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) {
if (!cgen_expr(g, arg))
return false;
}
- i++;
+ ++i;
}
cgen_write(g, "))");
}
@@ -1589,7 +1589,7 @@ static bool cgen_fn(CGenerator *g, FnExpr *f, Location where, U64 instance, Valu
element->kind = EXPR_IDENT;
element->type = f->ret_type.tuple[i];
element->ident = *ident;
- i++;
+ ++i;
}
}
}
@@ -1617,7 +1617,7 @@ static bool cgen_val_ptr_pre(CGenerator *g, void *v, Type *t, Location where) {
switch (t->kind) {
case TYPE_SLICE: {
Slice *s = (Slice *)v;
- for (I64 i = 0; i < s->n; i++) {
+ for (I64 i = 0; i < s->n; ++i) {
if (!cgen_val_ptr_pre(g, (char *)s->data + (U64)i * compiler_sizeof(t->slice), t->slice, where))
return false;
}
@@ -1625,7 +1625,7 @@ static bool cgen_val_ptr_pre(CGenerator *g, void *v, Type *t, Location where) {
cgen_write(g, "(d%p_[])", v); /* TODO: improve this somehow? */
if (!cgen_type_post(g, t->slice, where)) return false;
cgen_write(g, " = {");
- for (I64 i = 0; i < s->n; i++) {
+ for (I64 i = 0; i < s->n; ++i) {
if (i) cgen_write(g, ", ");
if (!cgen_val_ptr(g, (char *)s->data + (U64)i * compiler_sizeof(t->slice), t->slice, where))
return false;
@@ -1634,7 +1634,7 @@ static bool cgen_val_ptr_pre(CGenerator *g, void *v, Type *t, Location where) {
cgen_nl(g);
} break;
case TYPE_ARR:
- for (size_t i = 0; i < t->arr.n; i++) {
+ for (size_t i = 0; i < t->arr.n; ++i) {
if (!cgen_val_ptr_pre(g, (char *)*(void **)v + i * compiler_sizeof(t->arr.of), t->arr.of, where))
return false;
}
@@ -1669,7 +1669,7 @@ static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where) {
return false;
case TYPE_ARR:
cgen_write(g, "{");
- for (size_t i = 0; i < t->arr.n; i++) {
+ for (size_t i = 0; i < t->arr.n; ++i) {
if (i) cgen_write(g, ", ");
if (!cgen_val_ptr(g, (char *)v + i * compiler_sizeof(t->arr.of), t->arr.of, where))
return false;
@@ -1874,7 +1874,7 @@ static bool cgen_defs_expr(CGenerator *g, Expression *e) {
FnType *fn_type = &e->type.fn;
bool any_const = false;
if (fn_type->constness) {
- for (size_t i = 0; i < arr_len(fn_type->types)-1; i++) {
+ for (size_t i = 0; i < arr_len(fn_type->types)-1; ++i) {
if (fn_type->constness[i] == CONSTNESS_YES)
any_const = true;
}
@@ -1883,7 +1883,7 @@ static bool cgen_defs_expr(CGenerator *g, Expression *e) {
HashTable *instances = &f->instances;
/* generate each instance */
Instance **is = instances->data;
- for (U64 i = 0; i < instances->cap; i++) {
+ for (U64 i = 0; i < instances->cap; ++i) {
if (instances->occupied[i]) {
/* generate this instance */
if (!cgen_fn(g, &is[i]->fn, e->where, is[i]->c.id, is[i]->val.tuple))
diff --git a/copy.c b/copy.c
index b32e0d9..7ccc0a0 100644
--- a/copy.c
+++ b/copy.c
@@ -82,7 +82,7 @@ static void copy_type(Copier *c, Type *out, Type *in) {
size_t ntypes = arr_len(in->fn.types);
out->fn.types = NULL;
arr_set_lena(&out->fn.types, ntypes, c->allocr);
- for (size_t i = 0; i < ntypes; i++) {
+ for (size_t i = 0; i < ntypes; ++i) {
copy_type(c, &out->fn.types[i], &in->fn.types[i]);
}
} break;
@@ -90,7 +90,7 @@ static void copy_type(Copier *c, Type *out, Type *in) {
size_t ntypes = arr_len(in->tuple);
out->tuple = NULL;
arr_set_lena(&out->tuple, ntypes, c->allocr);
- for (size_t i = 0; i < ntypes; i++) {
+ for (size_t i = 0; i < ntypes; ++i) {
copy_type(c, &out->tuple[i], &in->tuple[i]);
}
} break;
@@ -119,7 +119,7 @@ static void copy_type(Copier *c, Type *out, Type *in) {
out->struc->fields = NULL;
arr_set_lena(&out->struc->fields, nfields, c->allocr);
- for (size_t i = 0; i < nfields; i++) {
+ for (size_t i = 0; i < nfields; ++i) {
Field *fout = &out->struc->fields[i];
Field *fin = &in->struc->fields[i];
*fout = *fin;
@@ -135,13 +135,13 @@ static void copy_fn_expr(Copier *c, FnExpr *fout, FnExpr *fin, bool copy_body) {
size_t nparam_decls = arr_len(fin->params);
fout->params = NULL;
arr_set_lena(&fout->params, nparam_decls, c->allocr);
- for (i = 0; i < nparam_decls; i++)
+ for (i = 0; i < nparam_decls; ++i)
copy_decl(c, fout->params + i, fin->params + i);
size_t nret_decls = arr_len(fin->ret_decls);
if (fin->ret_decls) {
fout->ret_decls = NULL;
arr_set_lena(&fout->ret_decls, nret_decls, c->allocr);
- for (i = 0; i < nret_decls; i++)
+ for (i = 0; i < nret_decls; ++i)
copy_decl(c, fout->ret_decls + i, fin->ret_decls + i);
}
copy_type(c, &fout->ret_type, &fin->ret_type);
@@ -222,7 +222,7 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) {
size_t nargs = arr_len(cin->args);
cout->arg_exprs = NULL;
arr_set_lena(&cout->args, nargs, a);
- for (size_t i = 0; i < nargs; i++) {
+ for (size_t i = 0; i < nargs; ++i) {
Argument *arg_in = &cin->args[i];
Argument *arg_out = &cout->args[i];
*arg_out = *arg_in;
@@ -236,7 +236,7 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) {
size_t nexprs = arr_len(in->tuple);
out->tuple = NULL;
arr_set_lena(&out->tuple, nexprs, a);
- for (size_t i = 0; i < nexprs; i++)
+ for (size_t i = 0; i < nexprs; ++i)
copy_expr(c, out->tuple + i, in->tuple + i);
} break;
case EXPR_C:
@@ -306,7 +306,7 @@ static void copy_block(Copier *c, Block *out, Block *in) {
copy_expr(c, out->ret_expr = allocr_malloc(c->allocr, sizeof *out->ret_expr), in->ret_expr);
arr_set_lena(&out->stmts, nstmts, c->allocr);
- for (size_t i = 0; i < nstmts; i++) {
+ for (size_t i = 0; i < nstmts; ++i) {
copy_stmt(c, &out->stmts[i], &in->stmts[i]);
}
c->block = prev;
diff --git a/decls_cgen.c b/decls_cgen.c
index a3d35d2..f1489d8 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -12,7 +12,7 @@ static bool cgen_decls_fn_instances(CGenerator *g, Expression *e) {
FnExpr *f = &e->fn;
assert(e->type.fn.constness);
Instance **data = f->instances.data;
- for (U64 i = 0; i < f->instances.cap; i++) {
+ for (U64 i = 0; i < f->instances.cap; ++i) {
if (f->instances.occupied[i]) {
if (cgen_should_gen_fn(&(*data)->fn)) {
(*data)->fn.c.name = f->c.name;
diff --git a/eval.c b/eval.c
index 508334e..446b83f 100644
--- a/eval.c
+++ b/eval.c
@@ -299,7 +299,7 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) {
fprintf(f, "["); /* TODO: change? when array initializers are added */
size_t n = t->arr.n;
if (n > 5) n = 5;
- for (size_t i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; ++i) {
if (i) fprintf(f, ", ");
fprint_val_ptr(f, (char *)p + i * compiler_sizeof(t->arr.of), t->arr.of);
}
@@ -316,7 +316,7 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) {
Slice slice = *(Slice *)p;
I64 n = slice.n;
if (n > 5) n = 5;
- for (I64 i = 0; i < n; i++) {
+ for (I64 i = 0; i < n; ++i) {
if (i) fprintf(f, ", ");
fprint_val_ptr(f, (char *)slice.data + i * (I64)compiler_sizeof(t->arr.of), t->arr.of);
}
@@ -348,7 +348,7 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) {
static void fprint_val(FILE *f, Value v, Type *t) {
if (t->kind == TYPE_TUPLE) {
fprintf(f, "(");
- for (size_t i = 0; i < arr_len(t->tuple); i++) {
+ for (size_t i = 0; i < arr_len(t->tuple); ++i) {
fprint_val(f, v.tuple[i], &t->tuple[i]);
}
fprintf(f, ")");
@@ -859,7 +859,7 @@ static bool eval_set(Evaluator *ev, Expression *set, Value *to) {
}
break;
case EXPR_TUPLE:
- for (size_t i = 0; i < arr_len(set->tuple); i++) {
+ for (size_t i = 0; i < arr_len(set->tuple); ++i) {
if (!eval_set(ev, &set->tuple[i], &to->tuple[i]))
return false;
}
@@ -1350,7 +1350,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
size_t i, n = arr_len(e->tuple);
v->tuple = err_malloc(n * sizeof *v->tuple);
*(void **)arr_add(&ev->to_free) = v->tuple;
- for (i = 0; i < n; i++) {
+ for (i = 0; i < n; ++i) {
if (!eval_expr(ev, &e->tuple[i], &v->tuple[i]))
return false;
}
@@ -1408,7 +1408,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
/* OPTIM (NOTE: currently needed for recursion) */
Value *args = NULL;
arr_resv(&args, arr_len(e->call.arg_exprs));
- for (size_t i = 0; i < arr_len(e->call.arg_exprs); i++) {
+ for (size_t i = 0; i < arr_len(e->call.arg_exprs); ++i) {
if (!eval_expr(ev, &e->call.arg_exprs[i], &args[i]))
return false;
}
diff --git a/identifiers.c b/identifiers.c
index b407e39..f1bd380 100644
--- a/identifiers.c
+++ b/identifiers.c
@@ -33,7 +33,7 @@ static Identifier ident_new(Identifiers *ids, Identifier parent, unsigned char i
memset(tree, 0, sizeof *tree); /* use zero value of IdentTree */
#ifdef NONZERO_NULL_PTRS
tree->parent = NULL;
- for (size_t i = 0; i < TREE_NCHILDREN; i++)
+ for (size_t i = 0; i < TREE_NCHILDREN; ++i)
tree->children[i] = NULL;
tree->decls = NULL;
#endif
@@ -170,7 +170,7 @@ static IdentDecl *ident_decl(Identifier i) {
static void ident_tree_free(IdentTree *id) {
if (!id) return;
arr_clear(&id->decls);
- for (int i = 0; i < TREE_NCHILDREN; i++)
+ for (int i = 0; i < TREE_NCHILDREN; ++i)
ident_tree_free(id->children[i]);
}
diff --git a/instance_table.c b/instance_table.c
index 731a020..5e7f917 100644
--- a/instance_table.c
+++ b/instance_table.c
@@ -41,7 +41,7 @@ static U64 f32_hash(F32 f) {
}
last = f;
}
- for (int i = 0; i < F32_MANT_DIG; i++) {
+ for (int i = 0; i < F32_MANT_DIG; ++i) {
f *= 10;
}
hash ^= (U64)exponent + (U64)F32_DIG * (U64)f;
@@ -67,7 +67,7 @@ static U64 f64_hash(F64 f) {
}
last = f;
}
- for (int i = 0; i < F64_MANT_DIG; i++) {
+ for (int i = 0; i < F64_MANT_DIG; ++i) {
f *= 10;
}
hash ^= (U64)exponent + (U64)F64_DIG * (U64)f;
@@ -151,7 +151,7 @@ static U64 val_ptr_hash(void *v, Type *t) {
U64 hash = 0;
Value *elems = *(Value **)v;
U32 x = 1;
- for (U64 i = 0; i < (U64)arr_len(t->tuple); i++) {
+ for (U64 i = 0; i < (U64)arr_len(t->tuple); ++i) {
hash += (U64)x * val_hash(elems[i], &t->tuple[i]);
x = rand_u32(x);
}
@@ -164,7 +164,7 @@ static U64 val_ptr_hash(void *v, Type *t) {
U32 x = 1;
U64 hash = 0;
U64 size = (U64)compiler_sizeof(t->arr.of);
- for (U64 i = 0; i < (U64)t->arr.n; i++) {
+ for (U64 i = 0; i < (U64)t->arr.n; ++i) {
hash += (U64)x * val_ptr_hash((char *)v + i * size, t->arr.of);
x = rand_u32(x);
}
@@ -175,7 +175,7 @@ static U64 val_ptr_hash(void *v, Type *t) {
U64 hash = 0;
Slice *s = v;
U64 size = (U64)compiler_sizeof(t->slice);
- for (U64 i = 0; i < (U64)s->n; i++) {
+ for (U64 i = 0; i < (U64)s->n; ++i) {
hash += (U64)x * val_ptr_hash((char *)s->data + i * size, t->slice);
x = rand_u32(x);
}
@@ -234,7 +234,7 @@ static bool val_ptr_eq(void *u, void *v, Type *t) {
case TYPE_TUPLE: {
Value *us = *(Value **)u;
Value *vs = *(Value **)v;
- for (size_t i = 0; i < arr_len(t->tuple); i++) {
+ for (size_t i = 0; i < arr_len(t->tuple); ++i) {
if (!val_eq(us[i], vs[i], &t->tuple[i]))
return false;
}
@@ -243,7 +243,7 @@ static bool val_ptr_eq(void *u, void *v, Type *t) {
case TYPE_ARR: {
U64 size = (U64)compiler_sizeof(t->arr.of);
char *uptr = u, *vptr = v;
- for (U64 i = 0; i < t->arr.n; i++) {
+ for (U64 i = 0; i < t->arr.n; ++i) {
if (!val_ptr_eq(uptr, vptr, t->arr.of))
return false;
uptr += size;
@@ -257,7 +257,7 @@ static bool val_ptr_eq(void *u, void *v, Type *t) {
Slice *s = v;
if (r->n != s->n) return false;
char *sptr = r->data, *tptr = s->data;
- for (U64 i = 0; i < (U64)s->n; i++) {
+ for (U64 i = 0; i < (U64)s->n; ++i) {
if (!val_ptr_eq(sptr, tptr, t->slice))
return false;
sptr += size;
@@ -294,7 +294,7 @@ static Instance *instance_table_adda(Allocator *a, HashTable *h, Value v, Type *
bool *new_occupied = allocr_calloc(a, (size_t)new_cap, sizeof *new_occupied);
Instance **old_data = h->data;
bool *old_occupied = h->occupied;
- for (U64 i = 0; i < h->cap; i++) {
+ for (U64 i = 0; i < h->cap; ++i) {
/* re-hash */
if (old_occupied[i]) {
U64 index = val_hash(old_data[i]->val, t) % new_cap;
diff --git a/main.c b/main.c
index 43f3927..8268ac7 100644
--- a/main.c
+++ b/main.c
@@ -19,7 +19,6 @@
/*
TODO:
-params with default values before params without them
check for duplicate params
inferred const params
packages
@@ -59,7 +58,7 @@ int main(int argc, char **argv) {
}
const char *out_filename = "out.c";
- for (int i = 2; i < argc-1; i++) {
+ for (int i = 2; i < argc-1; ++i) {
if (strcmp(argv[i], "-o") == 0)
out_filename = argv[i+1];
}
diff --git a/parse.c b/parse.c
index efd78d4..d9f73f5 100644
--- a/parse.c
+++ b/parse.c
@@ -194,7 +194,7 @@ static size_t type_to_str_(Type *t, char *buffer, size_t bufsize) {
Type *ret_type = t->fn.types;
Type *param_types = ret_type + 1;
size_t nparams = arr_len(t->fn.types) - 1;
- for (size_t i = 0; i < nparams; i++) {
+ for (size_t i = 0; i < nparams; ++i) {
if (i > 0)
written += str_copy(buffer + written, bufsize - written, ", ");
if (t->fn.constness) {
diff --git a/str.c b/str.c
index 2949564..b5e326d 100644
--- a/str.c
+++ b/str.c
@@ -15,7 +15,7 @@ size_t str_copy(char *dest, size_t destsz, const char *src) {
*dest = 0;
return 0;
}
- for (size_t i = 0; i < destsz-1; i++) {
+ for (size_t i = 0; i < destsz-1; ++i) {
*dest = *src;
if (!*src) {
*dest = 0;
diff --git a/test.toc b/test.toc
index b3161bb..9dd03c5 100644
--- a/test.toc
+++ b/test.toc
@@ -1,7 +1,7 @@
-f ::= fn(y:f64=19.2, x : f64) f64 {
+f ::= fn(y:f64=19.2, x :f64=3.4) f64 {
x + y
};
main ::= fn() {
- f(13, y = 23);
+ f(y = 13, 23);
};
diff --git a/tests.c b/tests.c
index e49eafe..d3805dd 100644
--- a/tests.c
+++ b/tests.c
@@ -10,18 +10,18 @@ static void allocr_test(void) {
for (int x = 1000; x <= 8000; x += 1000) {
int nfoos = x;
int *foos = allocr_malloc(&a, (size_t)nfoos * sizeof(int));
-
- for (int i = 0; i < nfoos; i++)
+ int i;
+ for (i = 0; i < nfoos; ++i)
foos[i] = i;
- for (int i = 0; i < nfoos; i++)
+ for (i = 0; i < nfoos; ++i)
assert(foos[i] == i);
int nbars = x;
int *bars = allocr_calloc(&a, (size_t)nbars, sizeof(int));
- for (int i = 0; i < nbars; i++)
+ for (i = 0; i < nbars; ++i)
assert(bars[i] == 0);
- for (int i = 0; i < nbars; i++)
+ for (i = 0; i < nbars; ++i)
bars[i] = i;
- for (int i = 0; i < nbars; i++)
+ for (i = 0; i < nbars; ++i)
assert(bars[i] == i);
}
allocr_free_all(&a);
diff --git a/tokenizer.c b/tokenizer.c
index 3992a75..6a3c136 100644
--- a/tokenizer.c
+++ b/tokenizer.c
@@ -114,9 +114,9 @@ static void fprint_token(FILE *out, Token *t) {
static inline void tokr_nextchar(Tokenizer *t) {
if (*(t->s) == '\n') {
- t->line++;
+ ++t->line;
}
- t->s++;
+ ++t->s;
}
static char tokr_esc_seq(Tokenizer *t) {
@@ -367,7 +367,7 @@ static bool tokenize_string(Tokenizer *t, char *str) {
exponent += *t->s - '0';
}
/* OPTIM: Slow for very large exponents (unlikely to happen) */
- for (int i = 0; i < exponent; i++) {
+ for (int i = 0; i < exponent; ++i) {
if (negative_exponent)
n->floatval /= 10;
else
diff --git a/types.c b/types.c
index 3627559..1ca20e0 100644
--- a/types.c
+++ b/types.c
@@ -60,7 +60,7 @@ static bool type_eq(Type *a, Type *b) {
if (arr_len(a->fn.types) != arr_len(b->fn.types)) return false;
Type *a_types = a->fn.types, *b_types = b->fn.types;
Constness *a_constness = a->fn.constness, *b_constness = b->fn.constness;
- for (size_t i = 0; i < arr_len(a->fn.types); i++) {
+ for (size_t i = 0; i < arr_len(a->fn.types); ++i) {
Constness const_a = CONSTNESS_NO, const_b = CONSTNESS_NO;
if (a_constness)
const_a = a_constness[i];
@@ -78,7 +78,7 @@ static bool type_eq(Type *a, Type *b) {
case TYPE_TUPLE: {
if (arr_len(a->tuple) != arr_len(b->tuple)) return false;
Type *a_types = a->tuple, *b_types = b->tuple;
- for (size_t i = 0; i < arr_len(a->tuple); i++) {
+ for (size_t i = 0; i < arr_len(a->tuple); ++i) {
if (!type_eq(&a_types[i], &b_types[i]))
return false;
}
@@ -263,12 +263,12 @@ static bool type_of_fn(Typer *tr, FnExpr *f, Location where, Type *t, U16 flags)
if (is_at_all_const) {
if (!t->fn.constness) {
has_constant_params = true;
- for (size_t i = 0; i < idx; i++) {
+ for (size_t i = 0; i < idx; ++i) {
*(Constness *)typer_arr_add(tr, &t->fn.constness) = CONSTNESS_NO;
}
}
}
- for (size_t i = 0; i < arr_len(param->idents); i++) {
+ for (size_t i = 0; i < arr_len(param->idents); ++i) {
Type *param_type = typer_arr_add(tr, &t->fn.types);
if (!generic) {
*param_type = param->type;
@@ -339,7 +339,7 @@ static bool type_of_fn(Typer *tr, FnExpr *f, Location where, Type *t, U16 flags)
tr->fn = prev_fn;
/* remove declarations from parameters we've already dealt with */
- for (size_t i = 0; i < param_idx; i++) {
+ for (size_t i = 0; i < param_idx; ++i) {
Declaration *p = &f->params[i];
if (p->flags & DECL_IS_CONST)
arr_foreach(p->idents, Identifier, ident)
@@ -1062,8 +1062,12 @@ static bool types_expr(Typer *tr, Expression *e) {
}
if (fn_decl) {
- /* TODO: make sure # of arguments isn't MORE than # of params */
- size_t p = 0;
+ if (nargs > nparams) {
+ err_print(e->where, "Expected at most %lu arguments to function, but got %lu.",
+ nparams, nargs);
+ return false;
+ }
+ int p = 0; /* counter for sequential parameters */
Declaration *last_param_without_default_value = NULL;
arr_foreach(fn_decl->params, Declaration, param) {
@@ -1076,9 +1080,10 @@ static bool types_expr(Typer *tr, Expression *e) {
arr_foreach(args, Argument, arg) {
bool named = arg->name != NULL;
+ int param_idx = -1;
if (named) {
/* named argument */
- long index = 0;
+ int index = 0;
bool found = false;
arr_foreach(fn_decl->params, Declaration, pa) {
arr_foreach(pa->idents, Identifier, id) {
@@ -1097,15 +1102,25 @@ static bool types_expr(Typer *tr, Expression *e) {
info_print(idecl_where(ident_decl(f->ident)), "Declaration is here.");
return false;
}
- params_set[index] = true;
- arg_exprs[index] = arg->val;
+ param_idx = index;
} else if ((param->flags & DECL_HAS_EXPR) && param < last_param_without_default_value) {
/* this param must be named; so this is referring to a later parameter */
--arg;
} else {
- params_set[p] = true;
- arg_exprs[p] = arg->val;
+ param_idx = p;
+ }
+
+ if (param_idx != -1) {
+ if (params_set[param_idx]) {
+ char *s = ident_to_str(param->idents[ident_idx]);
+ err_print(arg->where, "Argument #%lu (%s) set twice in function call.", param_idx+1, s);
+ free(s);
+ return false;
+ }
+ params_set[param_idx] = true;
+ arg_exprs[param_idx] = arg->val;
}
+
if (!named) {
/* sequential order of parameters */
++p;
@@ -1131,7 +1146,7 @@ static bool types_expr(Typer *tr, Expression *e) {
}
FnType *fn_type = &f->type.fn;
- for (size_t i = 0; i < nparams; i++) {
+ for (size_t i = 0; i < nparams; ++i) {
if (!params_set[i]) {
size_t index = 0;
assert(fn_decl); /* we can only miss an arg if we're using named/optional args */
@@ -1206,7 +1221,7 @@ static bool types_expr(Typer *tr, Expression *e) {
int semi_const_index = 0;
/* eval compile time arguments */
- for (i = 0; i < nparams; i++) {
+ for (i = 0; i < nparams; ++i) {
bool should_be_evald = arg_is_const(&arg_exprs[i], fn_type->constness[i]);
if (should_be_evald && params_set[i]) {
@@ -1833,7 +1848,7 @@ static bool types_decl(Typer *tr, Declaration *d) {
}
- for (size_t i = 0; i < arr_len(d->idents); i++) {
+ for (size_t i = 0; i < arr_len(d->idents); ++i) {
Type *t = d->type.kind == TYPE_TUPLE ? &d->type.tuple[i] : &d->type;
if (t->kind == TYPE_TYPE) {
if (!(d->flags & DECL_IS_CONST)) {