Move definition closer to usage location
[tiger.ml.git] / tiger / src / lib / tiger / tiger_parser.mly
index 2cd530d..fee8888 100644 (file)
@@ -167,6 +167,21 @@ exp:
       "unit[]"
     }
 
+rec_field_assignments:
+  | ID EQ exp
+    {
+      let id = $1 in
+      let exp = $3 in
+      sprintf "%S = %s" id exp
+    }
+  | ID EQ exp COMMA rec_field_assignments
+    {
+      let id = $1 in
+      let exp = $3 in
+      let rec_field_assignments = $5 in
+      sprintf "%S = %s, %s" id exp rec_field_assignments
+    }
+
 exps:
   | exp
     {
@@ -188,51 +203,7 @@ decs:
     }
 
 dec:
-  | tydec  {$1}
-  | vardec {$1}
-  | fundec {$1}
-
-fundec:
-  | FUNCTION ID LPAREN RPAREN EQ exp
-    {
-      let id       = $2 in
-      let exp      = $6 in
-      sprintf "fundec[%s, arguments[], exp[%s]]" id exp
-    }
-  | FUNCTION ID LPAREN tyfields RPAREN EQ exp
-    {
-      let id       = $2 in
-      let tyfields = $4 in
-      let exp      = $7 in
-      sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
-    }
-  | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
-    {
-      let id       = $2 in
-      let tyfields = $4 in
-      let type_id  = $7 in
-      let exp      = $9 in
-      sprintf
-        "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
-        id tyfields type_id exp
-    }
-
-vardec:
-  | VAR ID ASSIGN exp
-    {
-      let id = $2 in
-      let exp = $4 in
-      sprintf "vardec[%s, exp[%s]]" id exp
-    }
-  | VAR ID COLON ID ASSIGN exp
-    {
-      let id = $2 in
-      let type_id = $4 in
-      let exp = $6 in
-      sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
-    }
-
-tydec:
+  /* Tydec */
   | TYPE ID EQ ID
     {
       let type_id_new = $2 in
@@ -257,38 +228,60 @@ tydec:
       sprintf "tydec_array[%s, elements_of_type[%s]]" type_id element_type_id
     }
 
-tyfields:
-/*| epsilon */
-  | tyfield
-    {$1}
-  | tyfield COMMA tyfields
+  /* Vardec */
+  | VAR ID ASSIGN exp
     {
-      let tyfield = $1 in
-      let tyfields = $3 in
-      sprintf "%s, %s" tyfield tyfields
+      let id = $2 in
+      let exp = $4 in
+      sprintf "vardec[%s, exp[%s]]" id exp
+    }
+  | VAR ID COLON ID ASSIGN exp
+    {
+      let id = $2 in
+      let type_id = $4 in
+      let exp = $6 in
+      sprintf "vardec[%s, type_id[%s], exp[%s]]" id type_id exp
     }
 
-tyfield:
-  | ID COLON ID
+  /* Fundec */
+  | FUNCTION ID LPAREN RPAREN EQ exp
     {
-      let id = $1 in
-      let type_id = $3 in
-      sprintf "tyfield[%s, %s]" id type_id
+      let id       = $2 in
+      let exp      = $6 in
+      sprintf "fundec[%s, arguments[], exp[%s]]" id exp
+    }
+  | FUNCTION ID LPAREN tyfields RPAREN EQ exp
+    {
+      let id       = $2 in
+      let tyfields = $4 in
+      let exp      = $7 in
+      sprintf "fundec[%s, arguments[%s], exp[%s]]" id tyfields exp
+    }
+  | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp
+    {
+      let id       = $2 in
+      let tyfields = $4 in
+      let type_id  = $7 in
+      let exp      = $9 in
+      sprintf
+        "fundec[%s, tyfields[%s], type_id[%s], exp[%s]]"
+        id tyfields type_id exp
     }
 
-rec_field_assignments:
-  | ID EQ exp
+tyfields:
+  | ID COLON ID
     {
-      let id = $1 in
-      let exp = $3 in
-      sprintf "%S = %s" id exp
+      let id_1 = $1 in
+      let id_2 = $3 in
+      sprintf "%s : %s" id_1 id_2
     }
-  | ID EQ exp COMMA rec_field_assignments
+  | ID COLON ID COMMA tyfields
     {
-      let id = $1 in
-      let exp = $3 in
-      let rec_field_assignments = $5 in
-      sprintf "%S = %s, %s" id exp rec_field_assignments
+      let id_1 = $1 in
+      let id_2 = $3 in
+      let tyfield = sprintf "%s : %s" id_1 id_2 in
+      let tyfields = $5 in
+      sprintf "%s, %s" tyfield tyfields
     }
 
 fun_args:
This page took 0.033154 seconds and 4 git commands to generate.