summary refs log tree commit diff
path: root/minic
diff options
context:
space:
mode:
authorQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2015-10-12 21:46:27 -0400
committerQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2015-10-12 21:46:27 -0400
commit7f7cf6a03254f0ab0ec58572adda1fc92b2506a6 (patch)
treee674b657458bca3d7cef080048174258102f3946 /minic
parentb402c8f7d575130d08b4588f8212ad6c9ae73108 (diff)
downloadroux-7f7cf6a03254f0ab0ec58572adda1fc92b2506a6.tar.gz
add for loops
Diffstat (limited to 'minic')
-rw-r--r--minic/minic.y36
-rw-r--r--minic/test/queen.c24
2 files changed, 40 insertions, 20 deletions
diff --git a/minic/minic.y b/minic/minic.y
index 1564a1f..4087e9d 100644
--- a/minic/minic.y
+++ b/minic/minic.y
@@ -565,6 +565,31 @@ param(char *v, unsigned ctyp, Node *pl)
 	return n;
 }
 
+Stmt *
+mkfor(Node *ini, Node *tst, Node *inc, Stmt *s)
+{
+	Stmt *s1, *s2;
+
+	if (ini)
+		s1 = mkstmt(Expr, ini, 0, 0);
+	else
+		s1 = 0;
+	if (inc) {
+		s2 = mkstmt(Expr, inc, 0, 0);
+		s2 = mkstmt(Seq, s, s2, 0);
+	} else
+		s2 = s;
+	if (!tst) {
+		tst = mknode('N', 0, 0);
+		tst->u.n = 1;
+	}
+	s2 = mkstmt(While, tst, s2, 0);
+	if (s1)
+		return mkstmt(Seq, s1, s2, 0);
+	else
+		return s2;
+}
+
 %}
 
 %union {
@@ -579,7 +604,7 @@ param(char *v, unsigned ctyp, Node *pl)
 %token PP MM LE GE SIZEOF
 
 %token TINT TLNG
-%token IF ELSE WHILE BREAK RETURN
+%token IF ELSE WHILE FOR BREAK RETURN
 
 %right '='
 %left '&'
@@ -590,7 +615,7 @@ param(char *v, unsigned ctyp, Node *pl)
 
 %type <u> type
 %type <s> stmt stmts
-%type <n> expr pref post arg0 arg1 par0 par1
+%type <n> expr exp0 pref post arg0 arg1 par0 par1
 
 %%
 
@@ -686,6 +711,8 @@ stmt: ';'                            { $$ = 0; }
     | WHILE '(' expr ')' stmt        { $$ = mkstmt(While, $3, $5, 0); }
     | IF '(' expr ')' stmt ELSE stmt { $$ = mkstmt(If, $3, $5, $7); }
     | IF '(' expr ')' stmt           { $$ = mkstmt(If, $3, $5, 0); }
+    | FOR '(' exp0 ';' exp0 ';' exp0 ')' stmt
+                                     { $$ = mkfor($3, $5, $7, $9); }
     ;
 
 stmts: stmts stmt { $$ = mkstmt(Seq, $1, $2, 0); }
@@ -708,6 +735,10 @@ expr: pref
     | expr '&' expr     { $$ = mknode('&', $1, $3); }
     ;
 
+exp0: expr
+    |                   { $$ = 0; }
+    ;
+
 pref: post
     | '-' pref          { $$ = mkneg($2); }
     | '*' pref          { $$ = mknode('@', $2, 0); }
@@ -745,6 +776,7 @@ yylex()
 		{ "long", TLNG },
 		{ "if", IF },
 		{ "else", ELSE },
+		{ "for", FOR },
 		{ "while", WHILE },
 		{ "return", RETURN },
 		{ "break", BREAK },
diff --git a/minic/test/queen.c b/minic/test/queen.c
index c998e2a..86058d2 100644
--- a/minic/test/queen.c
+++ b/minic/test/queen.c
@@ -8,18 +8,13 @@ print() {
 	int x;
 	int y;
 
-	y = 0;
-	while (y < 8) {
-		x = 0;
-		while (x < 8) {
+	for (y=0; y<8; y++) {
+		for (x=0; x<8; x++)
 			if (t[x + 8*y])
 				printf(" Q");
 			else
 				printf(" .");
-			x++;
-		}
 		printf("\n");
-		y++;
 	}
 	printf("\n");
 }
@@ -29,8 +24,7 @@ chk(int x, int y) {
 	int r;
 
 	r = 0;
-	i = 0;
-	while (i < 8) {
+	for (i=0; i<8; i++) {
 		r = r + t[x + 8*i];
 		r = r + t[i + 8*y];
 		if (x+i < 8 & y+i < 8)
@@ -41,11 +35,8 @@ chk(int x, int y) {
 			r = r + t[x-i + 8*(y+i)];
 		if (x-i >= 0 & y-i >= 0)
 			r = r + t[x-i + 8*(y-i)];
-		if (r)
-			return 1;
-		i++;
 	}
-	return 0;
+	return r;
 }
 
 go(int n, int x, int y) {
@@ -54,17 +45,14 @@ go(int n, int x, int y) {
 		N++;
 		return 0;
 	}
-	while (y < 8) {
-		while (x < 8) {
+	for (; y<8; y++) {
+		for (; x<8; x++)
 			if (chk(x, y) == 0) {
 				t[x + 8*y]++;
 				go(n+1, x, y);
 				t[x + 8*y]--;
 			}
-			x++;
-		}
 		x = 0;
-		y++;
 	}
 }