summary refs log tree commit diff
path: root/minic/minic.y
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/minic.y
parentb402c8f7d575130d08b4588f8212ad6c9ae73108 (diff)
downloadroux-7f7cf6a03254f0ab0ec58572adda1fc92b2506a6.tar.gz
add for loops
Diffstat (limited to 'minic/minic.y')
-rw-r--r--minic/minic.y36
1 files changed, 34 insertions, 2 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 },