Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

handle for(var i= ...

  • Loading branch information...
commit 8c38f618565424b8c7b3a95a45299af51da7cccd 1 parent fa95cf1
Jake Donham authored
View
27 src/jscomp/jsgen.ml
@@ -451,19 +451,17 @@ and comp_expr_st tail expr k =
match d with
| Upto -> << $jv$ <= $ce2$ >>, << $jv$++ >>
| Downto -> << $jv$ >= $ce2$ >>, << $jv$-- >> in
- <:stmt<
- var $id:i$;
- $ (* wrap loop body in a function / call so closures over loop var work *)
- Jfor (_loc,
- Some << $id:i$ = $ce1$ >>,
- Some te,
- Some ie,
- Jblock(_loc,
- Jexps (_loc,
- Jcall(_loc,
+ (* wrap loop body in a function / call so closures over loop var work *)
+ Jfor (_loc,
+ [ i, Some ce1 ],
+ None,
+ Some te,
+ Some ie,
+ Jblock(_loc,
+ Jexps (_loc,
+ Jcall(_loc,
Jfun(_loc, None, [i], ce3),
jv))))
- $ >>
| Lwhile (e1, e2) ->
Jwhile (_loc, comp_expr false e1, comp_expr_st false e2 keffect)
@@ -695,10 +693,12 @@ and inline_exp = function
| Lprim (Pccall { prim_name = "$inline_antiexp" }, [e]) -> comp_expr false e
| _ -> raise (Failure "bad inline exp")
+and inline_variableDeclarationList seol = inline_list (inline_pair inline_string (inline_option inline_exp)) seol
+
and inline_stmt = function
| Lconst (Const_block _) as cb -> inline_stmt (makeblock_of_const cb)
| <:lam_astmt< Jvars ($_$, $seol$) >> ->
- Jvars (_loc, inline_list (inline_pair inline_string (inline_option inline_exp)) seol)
+ Jvars (_loc, inline_variableDeclarationList seol)
| <:lam_astmt< Jfuns ($_$, $s$, $sl$, $stl$) >> ->
Jfuns (_loc, inline_string s, inline_list inline_string sl, inline_stmt stl)
| <:lam_astmt< Jreturn ($_$, $eo$) >> -> Jreturn (_loc, inline_option inline_exp eo)
@@ -717,8 +717,9 @@ and inline_stmt = function
inline_stmt sl1,
inline_option (inline_pair inline_string inline_stmt) sslpo,
inline_stmt sl2)
- | <:lam_astmt< Jfor ($_$, $eo1$, $eo2$, $eo3$, $s$) >> ->
+ | <:lam_astmt< Jfor ($_$, $vars$, $eo1$, $eo2$, $eo3$, $s$) >> ->
Jfor (_loc,
+ inline_variableDeclarationList vars,
inline_option inline_exp eo1,
inline_option inline_exp eo2,
inline_option inline_exp eo3,
View
2  src/jslib/jslib_ast.incl
@@ -101,7 +101,7 @@ and stmt =
| Jthrow of loc * exp
| Jexps of loc * exp
| Jtrycatch of loc * stmt * (string * stmt) option * stmt
- | Jfor of loc * exp option * exp option * exp option * stmt
+ | Jfor of loc * (string * exp option) list * exp option * exp option * exp option * stmt
| Jdowhile of loc * stmt * exp
| Jwhile of loc * exp * stmt
| Jblock of loc * stmt
View
16 src/jslib/jslib_parse.ml
@@ -224,23 +224,27 @@ expression: [
]
];
+variableDeclarationList: [[
+ LIST1
+ [ i = a_IDENT; e = OPT [ "="; e = expression LEVEL "AssignmentExpression" -> e ] -> (i, e) ]
+ SEP ","
+]];
+
(* A.4 Statements *)
statement: [[
(n, s) = antiquot_stmt -> Jstmt_Ant (_loc, mk_anti ~c:"stmt" n s)
| ss = block -> Jblock (_loc, ss)
-| "var"; vars =
- LIST1 [ i = a_IDENT;
- e = OPT [ "="; e = expression LEVEL "AssignmentExpression" -> e ] -> (i, e) ]
- SEP ",";
- ";" -> Jvars (_loc, vars)
+| "var"; vars = variableDeclarationList; ";" -> Jvars (_loc, vars)
| ";" -> Jstmt_nil (_loc)
| test_lookahead_not_brace_function; e = expression; ";" -> Jexps (_loc, e)
| "if"; "("; e = expression; ")"; s1 = statement; "else"; s2 = statement -> Jites(_loc, e, s1, Some s2)
| "if"; "("; e = expression; ")"; s1 = statement -> Jites(_loc, e, s1, None)
| "do"; s = statement; "while"; "("; e = expression; ")"; ";" -> Jdowhile (_loc, s, e)
| "while"; "("; e = expression; ")"; s = statement -> Jwhile (_loc, e, s)
+| "for"; "("; "var"; vars = variableDeclarationList; ";"; e2 = OPT expression; ";"; e3 = OPT expression; ")"; s = statement ->
+ Jfor (_loc, vars, None, e2, e3, s)
| "for"; "("; e1 = OPT expression; ";"; e2 = OPT expression; ";"; e3 = OPT expression; ")"; s = statement ->
- Jfor (_loc, e1, e2, e3, s)
+ Jfor (_loc, [], e1, e2, e3, s)
| "continue"; i = OPT a_IDENT; ";" -> Jcontinue (_loc, i)
| "break"; i = OPT a_IDENT; ";" -> Jbreak (_loc, i)
| "return"; e = OPT expression; ";" -> Jreturn (_loc, e)
View
37 src/jslib/jslib_pp.ml
@@ -414,24 +414,24 @@ and aexps ppf e =
| _ ->
(expp pAssignment) ppf e
+and variableDeclarationList ppf = function
+ | [ (i, None) ] -> fprintf ppf "@[<hv 2>var %s@]" i
+ | [ (i, Some e) ] -> fprintf ppf "@[<hv 2>var %s =@ %a@]" i (expp pAssignment) e
+ | vars ->
+ let fvars ppf vars =
+ let comma = ref false in
+ List.iter
+ (fun (i, e) ->
+ if !comma then fprintf ppf ",@ " else comma := true;
+ match e with
+ | Some e -> fprintf ppf "%s =@;<1 2>%a" i (expp pAssignment) e
+ | None -> fprintf ppf "%s" i)
+ vars in
+ fprintf ppf "@[<hv 2>var@ %a@]" fvars vars
+
and stmt ppf = function
| Jvars (_, vars) ->
- begin
- match vars with
- | [ (i, None) ] -> fprintf ppf "@[<hv 2>var %s;@]" i
- | [ (i, Some e) ] -> fprintf ppf "@[<hv 2>var %s =@ %a;@]" i (expp pAssignment) e
- | _ ->
- let fvars ppf vars =
- let comma = ref false in
- List.iter
- (fun (i, e) ->
- if !comma then fprintf ppf ",@ " else comma := true;
- match e with
- | Some e -> fprintf ppf "%s =@;<1 2>%a" i (expp pAssignment) e
- | None -> fprintf ppf "%s" i)
- vars in
- fprintf ppf "@[<hv 2>var@ %a;@]" fvars vars
- end
+ fprintf ppf "%a;" variableDeclarationList vars
| Jfuns (_, i, is, ss) ->
fprintf ppf "@[<hv>function %s @[<hv 1>(%a)@]%a@]" i ids is block ss
@@ -480,8 +480,11 @@ and stmt ppf = function
| Jtrycatch (_, ss, Some (ci, css), fss) ->
fprintf ppf "@[<hv>try%a@ catch (%s)%a finally%a@]" block ss ci block css block fss
- | Jfor (_, e1, e2, e3, s) ->
+ | Jfor (_, [], e1, e2, e3, s) ->
fprintf ppf "@[<hv>for @[<hv 1>(%a;@ %a;@ %a)@]%a@]" (opt (expp p)) e1 (opt (expp p)) e2 (opt (expp p)) e3 maybe_block s
+ | Jfor (_, vars, None, e2, e3, s) ->
+ fprintf ppf "@[<hv>for @[<hv 1>(%a;@ %a;@ %a)@]%a@]" variableDeclarationList vars (opt (expp p)) e2 (opt (expp p)) e3 maybe_block s
+ | Jfor _ -> assert false
| Jdowhile (_, s, e) ->
fprintf ppf "@[<hv>do%a@ while (%a);@]" maybe_block s (expp p) e
Please sign in to comment.
Something went wrong with that request. Please try again.