summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--eval.c29
-rw-r--r--test.toc26
2 files changed, 18 insertions, 37 deletions
diff --git a/eval.c b/eval.c
index bafa6fc..d916ef4 100644
--- a/eval.c
+++ b/eval.c
@@ -1731,19 +1731,24 @@ static Status eval_block(Evaluator *ev, Block *b, Value *v) {
break;
}
}
- if (!ev->returning && b->ret_expr) {
- Value r;
- if (!eval_expr(ev, b->ret_expr, &r)) {
- success = false;
- goto ret;
- }
- if (!type_is_builtin(&b->ret_expr->type, BUILTIN_TYPE)) {
- /* make a copy so that r's data isn't freed when we exit the block */
- copy_val(NULL, v, r, &b->ret_expr->type);
- if (b->ret_expr->kind == EXPR_TUPLE)
- free(r.tuple);
+ if (b->ret_expr) {
+ if (ev->returning) {
+ /* return 0 from this block */
+ *v = val_zero(&b->ret_expr->type);
} else {
- *v = r;
+ Value r;
+ if (!eval_expr(ev, b->ret_expr, &r)) {
+ success = false;
+ goto ret;
+ }
+ if (!type_is_builtin(&b->ret_expr->type, BUILTIN_TYPE)) {
+ /* make a copy so that r's data isn't freed when we exit the block */
+ copy_val(NULL, v, r, &b->ret_expr->type);
+ if (b->ret_expr->kind == EXPR_TUPLE)
+ free(r.tuple);
+ } else {
+ *v = r;
+ }
}
}
eval_exit_stmts(b->stmts, last_reached);
diff --git a/test.toc b/test.toc
index 790a115..f953cfe 100644
--- a/test.toc
+++ b/test.toc
@@ -1,27 +1,3 @@
-#include "std/mem.toc", mem;
-#include "std/io.toc", io;
-
-calculation ::= fn() int {
- total := 0;
- i := mem.new(int);
- *i = 3;
- ns := mem.news(int, 10);
- for n, i := &ns {
- if i % 2 == 0 {
- *n = i;
- }
- }
- for n := ns {
- total += n;
- }
- total += *i;
- mem.del(i);
- mem.dels(ns);
- total
-}
-
main ::= fn() {
- io.puti(calculation());
- x ::= calculation();
- io.puti(x);
+ x ::= {return; 3}
}