diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-12 23:02:24 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-12 23:02:24 -0400 |
commit | 9dc3dea276c2cc045a77dfbf2734fe85207c3452 (patch) | |
tree | b427c3bbc2cafcb2f5890774ee2b0932752e18c4 | |
parent | ec2f3f213e4abce5cef3131673d969ca9d3c534c (diff) |
fixed problems with varargs
-rw-r--r-- | test.toc | 4 | ||||
-rwxr-xr-x | tests/test.sh | 2 | ||||
-rw-r--r-- | tests/varargs.toc | 4 | ||||
-rw-r--r-- | tests/varargs_expected | 1 | ||||
-rw-r--r-- | types.c | 109 |
5 files changed, 60 insertions, 60 deletions
@@ -8,7 +8,7 @@ sum ::= fn(x: ..) int { total := 0; n := 0; for a, i := x { - total += a + i / i; + total += a + i - i + 1; n += 1; } total - n @@ -18,7 +18,7 @@ sumc ::= fn(x:: ..) int { total := 0; n := 0; for a, i := x { - total += a + i / i; + total += a + i - i + 1; n += 1; } total - n diff --git a/tests/test.sh b/tests/test.sh index ac195bc..5b2cb14 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,13 +1,13 @@ #!/bin/bash tests='bf -varargs arr arr2 arr3 foreign params nms +varargs misc' STARTPWD=$(pwd) diff --git a/tests/varargs.toc b/tests/varargs.toc index 7ffb758..8709a64 100644 --- a/tests/varargs.toc +++ b/tests/varargs.toc @@ -8,7 +8,7 @@ sum ::= fn(x: ..) int { total := 0; n := 0; for a, i := x { - total += a + i / i; + total += a + i - i + 1; n += 1; } total - n @@ -18,7 +18,7 @@ sumc ::= fn(x:: ..) int { total := 0; n := 0; for a, i := x { - total += a + i / i; + total += a + i - i + 1; n += 1; } total - n diff --git a/tests/varargs_expected b/tests/varargs_expected index 5436134..29c273c 100644 --- a/tests/varargs_expected +++ b/tests/varargs_expected @@ -6,4 +6,3 @@ 4 11111 11111 - @@ -2099,60 +2099,7 @@ static Status types_expr(Typer *tr, Expression *e) { ++i; } } - if (has_varargs) { - /* deal with varargs (put them at the end of arg_exprs) */ - int idx = order[nparams-1]; - assert(idx >= 0); - Expression *arg_out = &arg_exprs[(int)nparams-1]; - for (; idx < (int)nargs; ++idx) { - Expression *arg = &args[idx].val; - if (type_is_builtin(&arg->type, BUILTIN_VARARGS)) { - /* add each vararg separately */ - assert(arg->kind == EXPR_IDENT); - Identifier ident = arg->ident; - assert(ident->decl_kind == IDECL_DECL); - Declaration *decl = ident->decl; - VarArg *varargs_here = decl->val.varargs; - size_t nvarargs_here = arr_len(varargs_here); - /* not just += nvarargs-1 to handle nvarargs_here == 0 */ - narg_exprs += nvarargs_here; - --narg_exprs; - nvarargs += nvarargs_here; - --nvarargs; - - long arg_out_idx = arg_out - arg_exprs; /* save and restore arg_out to prevent realloc from causing problems */ - /* add more room (or if nvarargs_here == 0, remove room) for more varargs */ - arr_set_lena(&arg_exprs, narg_exprs, tr->allocr); - arg_out = arg_exprs + arg_out_idx; - for (i = 0; i < nvarargs_here; ++i) { - VarArg *vararg = &varargs_here[i]; - Expression *out = arg_out++; - /* construct varargs_here[i] */ - out->flags = EXPR_FOUND_TYPE; - out->type = *vararg->type; - out->where = arg->where; - out->kind = EXPR_BINARY_OP; - out->binary.op = BINARY_AT_INDEX; - Expression *lhs = out->binary.lhs = typer_malloc(tr, sizeof *out->binary.lhs); - lhs->kind = EXPR_IDENT; - lhs->flags = EXPR_FOUND_TYPE; - lhs->type = decl->type; - lhs->ident = ident; - lhs->where = arg->where; - Expression *rhs = out->binary.rhs = typer_malloc(tr, sizeof *out->binary.lhs); - rhs->kind = EXPR_VAL; - rhs->flags = EXPR_FOUND_TYPE; - rhs->type.kind = TYPE_BUILTIN; - rhs->type.builtin = BUILTIN_I64; - rhs->type.flags = TYPE_IS_RESOLVED; - rhs->val.i64 = (I64)i; - rhs->where = arg->where; - } - } else { - *arg_out++ = *arg; - } - } - } + } else { if (nargs != nparams) { err_print(e->where, "Expected %lu arguments to function call, but got %lu.", (unsigned long)nparams, (unsigned long)nargs); @@ -2165,6 +2112,60 @@ static Status types_expr(Typer *tr, Expression *e) { arg_exprs[p] = args[p].val; } } + if (has_varargs) { + /* deal with varargs (put them at the end of arg_exprs) */ + int idx = order ? order[nparams-1] : (I16)nparams-1; + assert(idx >= 0); + Expression *arg_out = &arg_exprs[(int)nparams-1]; + for (; idx < (int)nargs; ++idx) { + Expression *arg = &args[idx].val; + if (type_is_builtin(&arg->type, BUILTIN_VARARGS)) { + /* add each vararg separately */ + assert(arg->kind == EXPR_IDENT); + Identifier ident = arg->ident; + assert(ident->decl_kind == IDECL_DECL); + Declaration *decl = ident->decl; + VarArg *varargs_here = decl->val.varargs; + size_t nvarargs_here = arr_len(varargs_here); + /* not just += nvarargs-1 to handle nvarargs_here == 0 */ + narg_exprs += nvarargs_here; + --narg_exprs; + nvarargs += nvarargs_here; + --nvarargs; + + long arg_out_idx = arg_out - arg_exprs; /* save and restore arg_out to prevent realloc from causing problems */ + /* add more room (or if nvarargs_here == 0, remove room) for more varargs */ + arr_set_lena(&arg_exprs, narg_exprs, tr->allocr); + arg_out = arg_exprs + arg_out_idx; + for (size_t i = 0; i < nvarargs_here; ++i) { + VarArg *vararg = &varargs_here[i]; + Expression *out = arg_out++; + /* construct varargs_here[i] */ + out->flags = EXPR_FOUND_TYPE; + out->type = *vararg->type; + out->where = arg->where; + out->kind = EXPR_BINARY_OP; + out->binary.op = BINARY_AT_INDEX; + Expression *lhs = out->binary.lhs = typer_malloc(tr, sizeof *out->binary.lhs); + lhs->kind = EXPR_IDENT; + lhs->flags = EXPR_FOUND_TYPE; + lhs->type = decl->type; + lhs->ident = ident; + lhs->where = arg->where; + Expression *rhs = out->binary.rhs = typer_malloc(tr, sizeof *out->binary.lhs); + rhs->kind = EXPR_VAL; + rhs->flags = EXPR_FOUND_TYPE; + rhs->type.kind = TYPE_BUILTIN; + rhs->type.builtin = BUILTIN_I64; + rhs->type.flags = TYPE_IS_RESOLVED; + rhs->val.i64 = (I64)i; + rhs->where = arg->where; + } + } else { + *arg_out++ = *arg; + } + } + } FnType *fn_type = &f->type.fn; c->arg_exprs = arg_exprs; |