@@ -91,7 +91,8 @@ data TheState = TheState { freshCounter :: Integer
9191
9292type RetKontText = PP. Doc
9393
94- type W = RWS Bool ([LibAccess ], [Basics. AtomName ], [RetKontText ]) TheState
94+ type WData = ([LibAccess ], [Basics. AtomName ], [RetKontText ])
95+ type W = RWS Bool WData TheState
9596
9697
9798initState = TheState { freshCounter = 0
@@ -136,37 +137,48 @@ instance Identifier Raw.Assignable where
136137class ToJS a where
137138 toJS :: a -> W PP. Doc
138139
139- irProg2JSString :: CompileMode -> Bool -> StackProgram -> String
140- irProg2JSString compileMode debugMode ir =
141- let (fns, _, (_,_,konts)) = runRWS (toJS ir) debugMode initState
142- inner = vcat (fns: konts)
143- outer = vcat $
144- [ " function" <+> text " Top" <+> text " (rt) {"
145- , nest 2 inner
146- , text " }"
147- , " module.exports = Top"
140+ stack2PPDoc :: CompileMode -> Bool -> StackUnit -> (PP. Doc , WData )
141+
142+ stack2PPDoc compileMode debugMode (ProgramStackUnit sp) =
143+ let (fns, _, w@ (libs, atoms, konts)) = runRWS (toJS sp) debugMode initState
144+ inner = vcat $
145+ [ jsLoadLibs
146+ , addLibs libs
148147 ]
149- in PP. render $ case compileMode of Normal -> outer
150- Export -> inner
148+ ++ (fns: konts) ++
149+ [ ]
150+
151+ outer = (" function Top (rt)" <+> PP. lbrace)
152+ $$+ inner
153+ $$ PP. rbrace
154+ $$ PP. text " module.exports = Top"
155+
156+ ppDoc = case compileMode of CompileMode. Export -> inner
157+ CompileMode. Normal -> outer
158+ in (ppDoc, w)
151159
160+ stack2PPDoc _ debugMode su =
161+ let (inner, _, w@ (libs, _, konts)) = runRWS (toJS su) debugMode initState
162+ ppDoc = vcat $ [ addLibs libs ] ++ (inner: konts)
163+ in (ppDoc, w)
152164
153- stack2JSString :: StackUnit -> String
154- stack2JSString x =
155- let (inner, _, (libs, atoms, konts)) = runRWS (toJS x) False initState
156- in PP. render (addLibs libs $$ (vcat (inner: konts)))
157165
166+ stack2JSString :: CompileMode -> Bool -> StackUnit -> String
167+ stack2JSString compileMode debugMode su =
168+ let (ppDoc, _) = stack2PPDoc compileMode debugMode su
169+ in PP. render ppDoc
158170
159171
160- stack2JSON :: StackUnit -> ByteString
161- stack2JSON (ProgramStackUnit _) = error " needs to be ported"
162- stack2JSON x =
163- let (inner, _, (libs, atoms, konts)) = runRWS (toJS x) False initState
172+ stack2JSON :: CompileMode -> Bool -> StackUnit -> ByteString
173+ stack2JSON compileMode debugMode su =
174+ let (ppDoc, (libs, atoms, konts)) = stack2PPDoc compileMode debugMode su
175+ fname = case su of FunStackUnit (FunDef (HFN n) _ _ _ _) -> Just n
176+ AtomStackUnit _ -> Nothing
164177 in Aeson. encode $ JSOutput { libs = libs
165- , fname = case x of FunStackUnit (FunDef (HFN n)_ _ _ _) -> Just n
166- _ -> Nothing
167- , atoms = atoms
168- , code = PP. render (addLibs libs $$ (vcat (inner: konts)))
169- }
178+ , fname = fname
179+ , atoms = atoms
180+ , code = PP. render ppDoc
181+ }
170182
171183
172184instance ToJS StackUnit where
@@ -185,15 +197,8 @@ instance ToJS IR.VarAccess where
185197instance ToJS StackProgram where
186198 toJS (StackProgram atoms funs) = do
187199 jjA <- toJS atoms
188- (jjF, (libsF, atoms', _)) <- listen $ mapM toJS funs
189-
190- return $
191- vcat $ [ jsLoadLibs
192- , addLibs libsF
193- , jjA
194- ] ++ jjF
195-
196-
200+ jjF <- mapM toJS funs
201+ return $ vcat $ [jjA] ++ jjF
197202
198203
199204instance ToJS C. Atoms where
0 commit comments