Skip to content

Commit

Permalink
handle for(var i= ...
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpilot committed May 31, 2010
1 parent fa95cf1 commit 8c38f61
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
27 changes: 14 additions & 13 deletions src/jscomp/jsgen.ml
Expand Up @@ -451,19 +451,17 @@ and comp_expr_st tail expr k =
match d with match d with
| Upto -> << $jv$ <= $ce2$ >>, << $jv$++ >> | Upto -> << $jv$ <= $ce2$ >>, << $jv$++ >>
| Downto -> << $jv$ >= $ce2$ >>, << $jv$-- >> in | Downto -> << $jv$ >= $ce2$ >>, << $jv$-- >> in
<:stmt< (* wrap loop body in a function / call so closures over loop var work *)
var $id:i$; Jfor (_loc,
$ (* wrap loop body in a function / call so closures over loop var work *) [ i, Some ce1 ],
Jfor (_loc, None,
Some << $id:i$ = $ce1$ >>, Some te,
Some te, Some ie,
Some ie, Jblock(_loc,
Jblock(_loc, Jexps (_loc,
Jexps (_loc, Jcall(_loc,
Jcall(_loc,
Jfun(_loc, None, [i], ce3), Jfun(_loc, None, [i], ce3),
jv)))) jv))))
$ >>


| Lwhile (e1, e2) -> | Lwhile (e1, e2) ->
Jwhile (_loc, comp_expr false e1, comp_expr_st false e2 keffect) Jwhile (_loc, comp_expr false e1, comp_expr_st false e2 keffect)
Expand Down Expand Up @@ -695,10 +693,12 @@ and inline_exp = function
| Lprim (Pccall { prim_name = "$inline_antiexp" }, [e]) -> comp_expr false e | Lprim (Pccall { prim_name = "$inline_antiexp" }, [e]) -> comp_expr false e
| _ -> raise (Failure "bad inline exp") | _ -> raise (Failure "bad inline exp")


and inline_variableDeclarationList seol = inline_list (inline_pair inline_string (inline_option inline_exp)) seol

and inline_stmt = function and inline_stmt = function
| Lconst (Const_block _) as cb -> inline_stmt (makeblock_of_const cb) | Lconst (Const_block _) as cb -> inline_stmt (makeblock_of_const cb)
| <:lam_astmt< Jvars ($_$, $seol$) >> -> | <: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$) >> -> | <:lam_astmt< Jfuns ($_$, $s$, $sl$, $stl$) >> ->
Jfuns (_loc, inline_string s, inline_list inline_string sl, inline_stmt 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) | <:lam_astmt< Jreturn ($_$, $eo$) >> -> Jreturn (_loc, inline_option inline_exp eo)
Expand All @@ -717,8 +717,9 @@ and inline_stmt = function
inline_stmt sl1, inline_stmt sl1,
inline_option (inline_pair inline_string inline_stmt) sslpo, inline_option (inline_pair inline_string inline_stmt) sslpo,
inline_stmt sl2) inline_stmt sl2)
| <:lam_astmt< Jfor ($_$, $eo1$, $eo2$, $eo3$, $s$) >> -> | <:lam_astmt< Jfor ($_$, $vars$, $eo1$, $eo2$, $eo3$, $s$) >> ->
Jfor (_loc, Jfor (_loc,
inline_variableDeclarationList vars,
inline_option inline_exp eo1, inline_option inline_exp eo1,
inline_option inline_exp eo2, inline_option inline_exp eo2,
inline_option inline_exp eo3, inline_option inline_exp eo3,
Expand Down
2 changes: 1 addition & 1 deletion src/jslib/jslib_ast.incl
Expand Up @@ -101,7 +101,7 @@ and stmt =
| Jthrow of loc * exp | Jthrow of loc * exp
| Jexps of loc * exp | Jexps of loc * exp
| Jtrycatch of loc * stmt * (string * stmt) option * stmt | 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 | Jdowhile of loc * stmt * exp
| Jwhile of loc * exp * stmt | Jwhile of loc * exp * stmt
| Jblock of loc * stmt | Jblock of loc * stmt
Expand Down
16 changes: 10 additions & 6 deletions src/jslib/jslib_parse.ml
Expand Up @@ -224,23 +224,27 @@ expression: [
] ]
]; ];


variableDeclarationList: [[
LIST1
[ i = a_IDENT; e = OPT [ "="; e = expression LEVEL "AssignmentExpression" -> e ] -> (i, e) ]
SEP ","
]];

(* A.4 Statements *) (* A.4 Statements *)
statement: [[ statement: [[
(n, s) = antiquot_stmt -> Jstmt_Ant (_loc, mk_anti ~c:"stmt" n s) (n, s) = antiquot_stmt -> Jstmt_Ant (_loc, mk_anti ~c:"stmt" n s)
| ss = block -> Jblock (_loc, ss) | ss = block -> Jblock (_loc, ss)
| "var"; vars = | "var"; vars = variableDeclarationList; ";" -> Jvars (_loc, vars)
LIST1 [ i = a_IDENT;
e = OPT [ "="; e = expression LEVEL "AssignmentExpression" -> e ] -> (i, e) ]
SEP ",";
";" -> Jvars (_loc, vars)
| ";" -> Jstmt_nil (_loc) | ";" -> Jstmt_nil (_loc)
| test_lookahead_not_brace_function; e = expression; ";" -> Jexps (_loc, e) | 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; "else"; s2 = statement -> Jites(_loc, e, s1, Some s2)
| "if"; "("; e = expression; ")"; s1 = statement -> Jites(_loc, e, s1, None) | "if"; "("; e = expression; ")"; s1 = statement -> Jites(_loc, e, s1, None)
| "do"; s = statement; "while"; "("; e = expression; ")"; ";" -> Jdowhile (_loc, s, e) | "do"; s = statement; "while"; "("; e = expression; ")"; ";" -> Jdowhile (_loc, s, e)
| "while"; "("; e = expression; ")"; s = statement -> Jwhile (_loc, e, s) | "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 -> | "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) | "continue"; i = OPT a_IDENT; ";" -> Jcontinue (_loc, i)
| "break"; i = OPT a_IDENT; ";" -> Jbreak (_loc, i) | "break"; i = OPT a_IDENT; ";" -> Jbreak (_loc, i)
| "return"; e = OPT expression; ";" -> Jreturn (_loc, e) | "return"; e = OPT expression; ";" -> Jreturn (_loc, e)
Expand Down
37 changes: 20 additions & 17 deletions src/jslib/jslib_pp.ml
Expand Up @@ -414,24 +414,24 @@ and aexps ppf e =
| _ -> | _ ->
(expp pAssignment) 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 and stmt ppf = function
| Jvars (_, vars) -> | Jvars (_, vars) ->
begin fprintf ppf "%a;" variableDeclarationList vars
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


| Jfuns (_, i, is, ss) -> | Jfuns (_, i, is, ss) ->
fprintf ppf "@[<hv>function %s @[<hv 1>(%a)@]%a@]" i ids is block ss fprintf ppf "@[<hv>function %s @[<hv 1>(%a)@]%a@]" i ids is block ss
Expand Down Expand Up @@ -480,8 +480,11 @@ and stmt ppf = function
| Jtrycatch (_, ss, Some (ci, css), fss) -> | Jtrycatch (_, ss, Some (ci, css), fss) ->
fprintf ppf "@[<hv>try%a@ catch (%s)%a finally%a@]" block ss ci block css block 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 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) -> | Jdowhile (_, s, e) ->
fprintf ppf "@[<hv>do%a@ while (%a);@]" maybe_block s (expp p) e fprintf ppf "@[<hv>do%a@ while (%a);@]" maybe_block s (expp p) e
Expand Down

0 comments on commit 8c38f61

Please sign in to comment.