summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-03-16 18:49:37 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-03-16 18:49:37 -0400
commit47ffc86ede157409dd04f2cc6e569efd47240a4e (patch)
tree0d319b7f4aa147e3a857c8c96153ebc0de78af46
parentb7b3e0868b9eef1018f87bd1c81c0112abaf94f4 (diff)
removed for/while ret exprs
-rw-r--r--main.c1
-rw-r--r--test.toc28
-rw-r--r--types.c20
3 files changed, 10 insertions, 39 deletions
diff --git a/main.c b/main.c
index f4db063..8b01d85 100644
--- a/main.c
+++ b/main.c
@@ -8,7 +8,6 @@
/*
TODO:
-don't allow while/for ret exprs, e.g. for 0..10 { 3; 5 }
make #sizeof always take a Type
sizeof (not #sizeof)
- make new(s) and del functions!
diff --git a/test.toc b/test.toc
index 9c9d467..03f7ae6 100644
--- a/test.toc
+++ b/test.toc
@@ -1,31 +1,5 @@
#include "std/io.toc", io;
-f ::= fn() int {
- total := 0;
- for i := 1..10 {
- if i % 2 == 0 { continue; }
- total += i;
- if i == 7 { break; }
- }
- i := 0;
- while {
- i += 1;
- total += i;
- if i == 10 {
- break;
- }
- }
- while i < 100 {
- i += 1;
- if i == 100 {
- return total;
- }
- }
- 0
-}
-
main ::= fn() {
- x ::= f();
- io.puti(x);
- io.puti(f());
+ for i := 0..10 { 3; };
} \ No newline at end of file
diff --git a/types.c b/types.c
index eca871c..0ec2a26 100644
--- a/types.c
+++ b/types.c
@@ -1726,13 +1726,13 @@ static Status types_expr(Typer *tr, Expression *e) {
arr_remove_lasta(&tr->in_exprs, tr->allocr);
in_header = false;
if (!types_block(tr, &fo->body)) goto for_fail;
-
if (fo->body.ret_expr) {
- *t = fo->body.ret_expr->type;
- } else {
- t->kind = TYPE_VOID;
- t->flags |= TYPE_IS_RESOLVED;
+ err_print(fo->body.ret_expr->where, "for loops can't return values -- you're missing a semicolon (;)");
+ goto for_fail;
}
+
+ t->kind = TYPE_VOID;
+
typer_block_exit(tr);
break;
for_fail:
@@ -1882,14 +1882,12 @@ static Status types_expr(Typer *tr, Expression *e) {
if (!types_block(tr, &w->body))
ret = false;
if (!ret) return false;
- if (w->cond != NULL && w->body.ret_expr != NULL) {
- err_print(e->where, "A finite loop can't have a return expression (for an infinite loop, use while { ... }).");
+
+ if (w->body.ret_expr) {
+ err_print(w->body.ret_expr->where, "while loops can't return values -- you're missing a semicolon (;)");
return false;
}
- if (w->body.ret_expr)
- *t = w->body.ret_expr->type;
- else
- t->kind = TYPE_VOID;
+ t->kind = TYPE_VOID;
} break;
case EXPR_CALL: {
CallExpr *c = &e->call;