Compare commits

..

No commits in common. "92c4c5c991dcf007bca4a2db0c81555c5b3817bd" and "bb75b78a36cb58328b1be41ead155fdb7f4d7c1f" have entirely different histories.

2 changed files with 26 additions and 37 deletions

2
ir.c
View File

@ -43,7 +43,7 @@ void irtoks_init_short(IRToks *v) {
void irtoks_term(IRToks *v) { void irtoks_term(IRToks *v) {
for (size_t i = 0; i < v->len; i++) { for (size_t i = 0; i < v->len; i++) {
if (v->toks[i].instr == IRCallInternal && v->toks[i].CallI.args) if (v->toks[i].instr == IRCallInternal)
free(v->toks[i].CallI.args); free(v->toks[i].CallI.args);
} }
free(v->toks); free(v->toks);

19
parse.c
View File

@ -242,15 +242,8 @@ static ExprRet expr(IRToks *out_ir, TokList *toks, Map *funcs, Scope *parent_sc,
bool eval_func_in_place = !func.side_effects; bool eval_func_in_place = !func.side_effects;
size_t args_len = 0; size_t args_len = 0;
IRParam *args = NULL;
if (t->next->tok.kind == TokOp && t->next->tok.Op == OpRParen) {
/* no args */
toklist_del(toks, t->next, t->next); /* delete right parenthesis */
} else {
/* go through the arguments, evaluate them and put them into the args array */
size_t args_cap = 16; size_t args_cap = 16;
args = xmalloc(sizeof(IRParam) * args_cap); IRParam *args = xmalloc(sizeof(IRParam) * args_cap);
for (;;) { for (;;) {
if (args_len+1 > args_cap) if (args_len+1 > args_cap)
args = xrealloc(args, (args_cap *= 2)); args = xrealloc(args, (args_cap *= 2));
@ -261,10 +254,10 @@ static ExprRet expr(IRToks *out_ir, TokList *toks, Map *funcs, Scope *parent_sc,
eval_func_in_place = false; eval_func_in_place = false;
if (t->next->tok.kind == TokOp) { if (t->next->tok.kind == TokOp) {
if (t->next->tok.Op == OpComma) { if (t->next->tok.Op == OpComma) {
toklist_del(toks, t->next, t->next); /* delete right parenthesis */ toklist_del(toks, t->next, t->next);
continue; continue;
} else if (t->next->tok.Op == OpRParen) { } else if (t->next->tok.Op == OpRParen) {
toklist_del(toks, t->next, t->next); /* delete right parenthesis */ toklist_del(toks, t->next, t->next);
break; break;
} }
} }
@ -273,7 +266,6 @@ static ExprRet expr(IRToks *out_ir, TokList *toks, Map *funcs, Scope *parent_sc,
free(args); free(args);
return (ExprRet){0}; return (ExprRet){0};
} }
}
t = func_ident; t = func_ident;
toklist_del(toks, t->next, t->next); /* delete left parenthesis */ toklist_del(toks, t->next, t->next); /* delete left parenthesis */
@ -282,23 +274,20 @@ static ExprRet expr(IRToks *out_ir, TokList *toks, Map *funcs, Scope *parent_sc,
mark_err(&func_ident->tok); mark_err(&func_ident->tok);
const char *plural = func.n_args == 1 ? "" : "s"; const char *plural = func.n_args == 1 ? "" : "s";
set_err("Function %s() takes %zu argument%s but got %zu", func.name, func.n_args, plural, args_len); set_err("Function %s() takes %zu argument%s but got %zu", func.name, func.n_args, plural, args_len);
if (args)
free(args); free(args);
return (ExprRet){0}; return (ExprRet){0};
} }
if (eval_func_in_place) { if (eval_func_in_place) {
/* evaluate the function in place */ /* evaluate the function in place */
Value *arg_vals = args_len ? xmalloc(sizeof(Value) * args_len) : NULL; Value *arg_vals = xmalloc(sizeof(Value) * args_len);
for (size_t i = 0; i < args_len; i++) for (size_t i = 0; i < args_len; i++)
arg_vals[i] = args[i].Literal; arg_vals[i] = args[i].Literal;
func_ident->tok = (Tok) { func_ident->tok = (Tok) {
.kind = TokVal, .kind = TokVal,
.Val = func.func(arg_vals), .Val = func.func(arg_vals),
}; };
if (arg_vals)
free(arg_vals); free(arg_vals);
if (args)
free(args); free(args);
} else { } else {
bool is_last_operation = t == start && t->next->tok.kind == TokOp && op_prec[t->next->tok.Op] == PREC_DELIM; bool is_last_operation = t == start && t->next->tok.kind == TokOp && op_prec[t->next->tok.Op] == PREC_DELIM;