ref: 8eef2702d6533ca399758c38acedf0f086b93e13
parent: d73d54388b6c16e61ecceecb21a3df258f33c434
author: andrewc <andrew_chambers@trimble.com>
date: Wed Jan 20 10:13:42 EST 2016
fix decl for with no cond and add more tests
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -143,7 +143,7 @@
%type <node> bandexpr cmpexpr unionexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
%type <node> funclit seqlit tuplit name block stmt label use
%type <node> fnparam declbody declcore typedeclcore structent arrayelt structelt tuphead
-%type <node> ifstmt forstmt whilestmt matchstmt elifs optexprln optexpr
+%type <node> ifstmt forstmt whilestmt matchstmt elifs optexprln loopcond optexpr
%type <node> match
%type <node> castexpr
%type <ucon> unionelt
@@ -591,6 +591,10 @@
| /* empty */ {$$ = NULL;}
;
+loopcond : exprln {$$ = $1;}
+ | Tendln {$$ = mkboollit($1->loc, 1);}
+ ;
+
optexprln: exprln {$$ = $1;}
| Tendln {$$ = NULL;}
;
@@ -870,14 +874,11 @@
{$$ = mkexpr($1->loc, Ocontinue, NULL);}
;
-forstmt : Tfor optexprln optexprln optexprln block {
- if(!$3)
- $3 = mkboollit($1->loc, 1);
- $$ = mkloopstmt($1->loc, $2, $3, $4, $5);
- }
+forstmt : Tfor optexprln loopcond optexprln block
+ {$$ = mkloopstmt($1->loc, $2, $3, $4, $5);}
| Tfor expr Tin exprln block
{$$ = mkiterstmt($1->loc, $2, $4, $5);}
- | Tfor decl Tendln optexprln optexprln block {
+ | Tfor decl Tendln loopcond optexprln block {
//Node *init;
if ($2.nn != 1)
lfatal($1->loc, "only one declaration is allowed in for loop");
--- a/test/fornocond.myr
+++ /dev/null
@@ -1,14 +1,0 @@
-use std
-
-const main = {
- var i
-
- for i = 0; ; i++
- if i == 10
- break;
- ;;
- ;;
- if i == 10
- std.put("pass\n")
- ;;
-}
--- /dev/null
+++ b/test/foroptexpr.myr
@@ -1,0 +1,65 @@
+use std
+
+const main = {
+ var i
+
+ for i = 0; ; i++
+ if i == 10
+ break
+ ;;
+ ;;
+ if i != 10
+ std.exit(1)
+ ;;
+
+ i = 0
+ for var j = 0; ; j++
+ if j == 10
+ break
+ ;;
+ i++
+ ;;
+ if i != 10
+ std.exit(2)
+ ;;
+
+ i = 0
+ for ; ;
+ i++
+ if i == 10
+ break
+ ;;
+ ;;
+ if i != 10
+ std.exit(3)
+ ;;
+
+ i = 0
+ for ; i < 10;
+ i++
+ ;;
+ if i != 10
+ std.exit(4)
+ ;;
+
+ for i = 0 ; ;
+ if i == 10
+ break
+ ;;
+ i++
+ ;;
+ if i != 10
+ std.exit(5)
+ ;;
+
+ i = 0
+ for ; ; i++
+ if i == 10
+ break
+ ;;
+ ;;
+ if i != 10
+ std.exit(6)
+ ;;
+ std.put("pass\n")
+}
--- a/test/tests
+++ b/test/tests
@@ -149,4 +149,4 @@
F arityhigh
F badop
B voideffect P pass
-B fornocond P pass
+B foroptexpr P pass