@@ -76,21 +76,21 @@ bindersToJs binders val = do
76binderToJs :: String -> [JS] -> Binder -> Gen [JS]
76binderToJs :: String -> [JS] -> Binder -> Gen [JS]
77binderToJs varName done NullBinder = return done
77binderToJs varName done NullBinder = return done
78binderToJs varName done (StringBinder str) =
78binderToJs varName done (StringBinder str) =
79− return [JSIfElse (JSBinary EqualTo (JSVar (Ident varName)) (JSStringLiteral str)) done Nothing]
79+ return [JSIfElse (JSBinary EqualTo (JSVar (Ident varName)) (JSStringLiteral str)) (JSBlock done) Nothing]
80binderToJs varName done (NumberBinder num) =
80binderToJs varName done (NumberBinder num) =
81− return [JSIfElse (JSBinary EqualTo (JSVar (Ident varName)) (JSNumericLiteral num)) done Nothing]
81+ return [JSIfElse (JSBinary EqualTo (JSVar (Ident varName)) (JSNumericLiteral num)) (JSBlock done) Nothing]
82binderToJs varName done (BooleanBinder True) =
82binderToJs varName done (BooleanBinder True) =
83− return [JSIfElse (JSVar (Ident varName)) done Nothing]
83+ return [JSIfElse (JSVar (Ident varName)) (JSBlock done) Nothing]
84binderToJs varName done (BooleanBinder False) =
84binderToJs varName done (BooleanBinder False) =
85− return [JSIfElse (JSUnary Not (JSVar (Ident varName))) done Nothing]
85+ return [JSIfElse (JSUnary Not (JSVar (Ident varName))) (JSBlock done) Nothing]
86binderToJs varName done (VarBinder ident) =
86binderToJs varName done (VarBinder ident) =
87 return (JSVariableIntroduction ident (JSVar (Ident varName)) : done)
87 return (JSVariableIntroduction ident (JSVar (Ident varName)) : done)
88binderToJs varName done (NullaryBinder ctor) =
88binderToJs varName done (NullaryBinder ctor) =
89− return [JSIfElse (JSBinary EqualTo (JSAccessor "ctor" (JSVar (Ident varName))) (JSStringLiteral ctor)) done Nothing]
89+ return [JSIfElse (JSBinary EqualTo (JSAccessor "ctor" (JSVar (Ident varName))) (JSStringLiteral ctor)) (JSBlock done) Nothing]
90binderToJs varName done (UnaryBinder ctor b) = do
90binderToJs varName done (UnaryBinder ctor b) = do
91 value <- fresh
91 value <- fresh
92 js <- binderToJs value done b
92 js <- binderToJs value done b
93− return [JSIfElse (JSBinary EqualTo (JSAccessor "ctor" (JSVar (Ident varName))) (JSStringLiteral ctor)) (JSVariableIntroduction (Ident value) (JSAccessor "value" (JSVar (Ident varName))) : js) Nothing]
93+ return [JSIfElse (JSBinary EqualTo (JSAccessor "ctor" (JSVar (Ident varName))) (JSStringLiteral ctor)) (JSBlock (JSVariableIntroduction (Ident value) (JSAccessor "value" (JSVar (Ident varName))) : js)) Nothing]
94binderToJs varName done (ObjectBinder bs) = go done bs
94binderToJs varName done (ObjectBinder bs) = go done bs
96 go :: [JS] -> [(String, Binder)] -> Gen [JS]
96 go :: [JS] -> [(String, Binder)] -> Gen [JS]
@@ -102,7 +102,7 @@ binderToJs varName done (ObjectBinder bs) = go done bs
102 return (JSVariableIntroduction (Ident propVar) (JSAccessor prop (JSVar (Ident varName))) : js)
102 return (JSVariableIntroduction (Ident propVar) (JSAccessor prop (JSVar (Ident varName))) : js)
103binderToJs varName done (ArrayBinder bs rest) = do
103binderToJs varName done (ArrayBinder bs rest) = do
104 js <- go done rest 0 bs
104 js <- go done rest 0 bs
105− return [JSIfElse (JSBinary cmp (JSAccessor "length" (JSVar (Ident varName))) (JSNumericLiteral (Left (fromIntegral $ length bs)))) js Nothing]
105+ return [JSIfElse (JSBinary cmp (JSAccessor "length" (JSVar (Ident varName))) (JSNumericLiteral (Left (fromIntegral $ length bs)))) (JSBlock js) Nothing]
107 cmp :: BinaryOperator
107 cmp :: BinaryOperator
108 cmp = maybe EqualTo (const GreaterThanOrEqualTo) rest
108 cmp = maybe EqualTo (const GreaterThanOrEqualTo) rest
@@ -122,18 +122,18 @@ binderToJs varName done (NamedBinder ident binder) = do
122 return (JSVariableIntroduction ident (JSVar (Ident varName)) : js)
122 return (JSVariableIntroduction ident (JSVar (Ident varName)) : js)
123binderToJs varName done (GuardedBinder cond binder) = binderToJs varName done' binder
123binderToJs varName done (GuardedBinder cond binder) = binderToJs varName done' binder
125− done' = [JSIfElse (valueToJs cond) done Nothing]
125+ done' = [JSIfElse (valueToJs cond) (JSBlock done) Nothing]
127statementToJs :: Statement -> JS
127statementToJs :: Statement -> JS
128statementToJs (VariableIntroduction ident value) = JSVariableIntroduction ident (valueToJs value)
128statementToJs (VariableIntroduction ident value) = JSVariableIntroduction ident (valueToJs value)
129statementToJs (Assignment target value) = JSAssignment target (valueToJs value)
129statementToJs (Assignment target value) = JSAssignment target (valueToJs value)
130−statementToJs (While cond sts) = JSWhile (valueToJs cond) (map statementToJs sts)
130+statementToJs (While cond sts) = JSWhile (valueToJs cond) (JSBlock (map statementToJs sts))
131−statementToJs (For ident start end sts) = JSFor ident (valueToJs start) (valueToJs end) (map statementToJs sts)
131+statementToJs (For ident start end sts) = JSFor ident (valueToJs start) (valueToJs end) (JSBlock (map statementToJs sts))
132statementToJs (ForEach ident arr sts) = JSApp (JSAccessor "forEach" (valueToJs arr)) [JSFunction Nothing [ident] (JSBlock (map statementToJs sts))]
132statementToJs (ForEach ident arr sts) = JSApp (JSAccessor "forEach" (valueToJs arr)) [JSFunction Nothing [ident] (JSBlock (map statementToJs sts))]
133statementToJs (If ifst) = ifToJs ifst
133statementToJs (If ifst) = ifToJs ifst
135 ifToJs :: IfStatement -> JS
135 ifToJs :: IfStatement -> JS
136− ifToJs (IfStatement cond thens elses) = JSIfElse (valueToJs cond) (map statementToJs thens) (fmap elseToJs elses)
136+ ifToJs (IfStatement cond thens elses) = JSIfElse (valueToJs cond) (JSBlock (map statementToJs thens)) (fmap elseToJs elses)
137 elseToJs :: ElseStatement -> JS
137 elseToJs :: ElseStatement -> JS
138 elseToJs (Else sts) = JSBlock (map statementToJs sts)
138 elseToJs (Else sts) = JSBlock (map statementToJs sts)
139 elseToJs (ElseIf ifst) = ifToJs ifst
139 elseToJs (ElseIf ifst) = ifToJs ifst