Permalink
Browse files

switch handled better

  • Loading branch information...
1 parent afb834d commit 923c0465eff585dbc60880d4feffb13d9eab3b06 Arjun Guha committed Mar 13, 2009
Showing with 12 additions and 2 deletions.
  1. +3 −1 src/WebBits/JavaScript/Simplify.hs
  2. +9 −1 src/WebBits/JavaScript/ToCore.hs
@@ -335,10 +335,12 @@ purifyStmt labels stmt = case stmt of
return (BlockStmt p [BlockStmt p ess,IfStmt p e' s1' s2'])
SwitchStmt p e cases -> do
(eStmts,e') <- purifyExpr e
+ (s1,id) <- needVar e'
l <- newStmtLabel
cases' <- mapM (purifyCase ((l,ImplicitSwitchLabel):labels)) cases
return $ BlockStmt p [BlockStmt p eStmts,
- LabelledStmt p l $ SwitchStmt p e' cases']
+ s1,
+ LabelledStmt p l $ SwitchStmt p id cases']
WhileStmt p e s -> do
(ess,e') <- purifyExpr e
l <- newStmtLabel
@@ -91,13 +91,21 @@ stmt (ReturnStmt p maybeE) = Core.ReturnStmt p (liftM expr maybeE)
stmt (LabelledStmt p id s) = Core.LabelledStmt p (unId id) (stmt s)
stmt (BreakStmt p (Just id)) = Core.BreakStmt p (unId id)
stmt (ContinueStmt p (Just id)) = Core.ContinueStmt p (unId id)
+stmt (SwitchStmt p (VarRef _ v) cases) =
+ Core.SwitchStmt p (unId v) (map case_ cases) where
+ case_ (CaseClause _ e [s]) = (lit e,stmt s)
+ case_ c = error $ "case_: cannot translate to ANF syntax:\n" ++ show c
stmt s = error $ "cannot translate this statement to core syntax:\n" ++
(render $ pp s) ++ "\n" ++ show s
field (PropString _ s,e) = (Left s,expr e)
field (PropNum _ n,e) = (Right n,expr e)
field (PropId _ (Id _ s),e) = (Left s,expr e)
+lit e = unLit (expr e) where
+ unLit (Core.Lit l) = l
+ unLit e = error $ "unLit: not a literal " ++ show e
+
expr (StringLit p s) = Core.Lit (Core.StringLit p s)
expr (RegexpLit p s b0 b1) = Core.Lit (Core.RegexpLit p s b0 b1)
expr (NumLit p x) = Core.Lit (Core.NumLit p x)
@@ -116,4 +124,4 @@ expr (ThisRef p) = Core.This p
expr (DotRef p e id) = Core.DotRef p (expr e) (unId id)
expr (BracketRef p e1 e2) = Core.BracketRef p (expr e1) (expr e2)
expr e = error $ "cannot translate this expression to core syntax:\n" ++
- (render $ pp e)
+ (show e)

0 comments on commit 923c046

Please sign in to comment.