@@ -317,9 +317,61 @@ jsSubst var new (JSCond conds) =
317jsSubst var new (JSAssign lhs rhs) =
317jsSubst var new (JSAssign lhs rhs) =
318 JSAssign (jsSubst var new lhs) (jsSubst var new rhs)
318 JSAssign (jsSubst var new lhs) (jsSubst var new rhs)
320+jsSubst var new (JSAlloc val (Just js)) =
321+ JSAlloc val (Just (jsSubst var new js))
320jsSubst _ _ js = js
323jsSubst _ _ js = js
326+removeAllocations :: JS -> JS
327+removeAllocations (JSSeq body) =
328+ JSSeq $ removeHelper (map removeAllocations body)
330+ removeHelper :: [JS] -> [JS]
331+ removeHelper [js] = [js]
332+ removeHelper ((JSAlloc name (Just val@(JSIdent _))):js) =
333+ map (jsSubst name val) (removeHelper js)
334+ removeHelper (j:js) = j : removeHelper js
336+removeAllocations (JSFunction args body) =
337+ JSFunction args $ removeAllocations body
339+removeAllocations (JSReturn ret) =
340+ JSReturn $ removeAllocations ret
342+removeAllocations (JSApp fun args) =
343+ JSApp (removeAllocations fun) (map removeAllocations args)
345+removeAllocations (JSNew con args) =
346+ JSNew con $ map removeAllocations args
348+removeAllocations (JSOp op lhs rhs) =
349+ JSOp op (removeAllocations lhs) (removeAllocations rhs)
351+removeAllocations (JSProj obj field) =
352+ JSProj (removeAllocations obj) field
354+removeAllocations (JSArray vals) =
355+ JSArray $ map removeAllocations vals
357+removeAllocations (JSAssign lhs rhs) =
358+ JSAssign (removeAllocations lhs) (removeAllocations rhs)
360+removeAllocations (JSAlloc name (Just val)) =
361+ JSAlloc name $ Just (removeAllocations val)
363+removeAllocations (JSIndex val idx) =
364+ JSIndex (removeAllocations val) (removeAllocations idx)
366+removeAllocations (JSCond conds) =
367+ JSCond $ map (removeAllocations *** removeAllocations) conds
369+removeAllocations (JSTernary c t f) =
370+ JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
372+removeAllocations js = js
323isJSConstant :: JS -> Bool
375isJSConstant :: JS -> Bool
324isJSConstant js
376isJSConstant js
325 | JSString _ <- js = True
377 | JSString _ <- js = True
@@ -749,7 +801,7 @@ codegenJavaScript target definitions filename outputType = do
750 functions :: [String]
802 functions :: [String]
751 functions =
803 functions =
752− map compileJS $ initConstructors (map reduceConstants ((reduceJS . removeIDs) $ map (optimizeJS . translateDeclaration) def))
804+ map (compileJS . removeAllocations) $ initConstructors (map reduceConstants ((reduceJS . removeIDs) $ map (optimizeJS . translateDeclaration) def))
754 mainLoop :: String
806 mainLoop :: String
755 mainLoop = compileJS $
807 mainLoop = compileJS $