@@ -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,49 @@ 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 indent inner
146- , text " }"
147- , " module.exports = Top"
140+ -- TODO: Merge into `stack2PPDoc` which then splits in two: `stack2Text` and `stack2JSON`.
141+ stack2PPDoc :: CompileMode -> Bool -> StackUnit -> (PP. Doc , WData )
142+
143+ stack2PPDoc compileMode debugMode (ProgramStackUnit sp) =
144+ let (fns, _, w@ (libs, atoms, konts)) = runRWS (toJS sp) debugMode initState
145+ inner = vcat $
146+ [ jsLoadLibs
147+ , addLibs libs
148148 ]
149- in PP. render $ case compileMode of Normal -> outer
150- Export -> inner
149+ ++ (fns: konts) ++
150+ [ ]
151+
152+ outer = (" function Top (rt)" <+> PP. lbrace)
153+ $$+ inner
154+ $$ PP. rbrace
155+ $$ PP. text " module.exports = Top"
156+
157+ ppDoc = case compileMode of CompileMode. Export -> inner
158+ CompileMode. Normal -> outer
159+ in (ppDoc, w)
151160
161+ stack2PPDoc _ debugMode su =
162+ let (inner, _, w@ (libs, _, konts)) = runRWS (toJS su) debugMode initState
163+ ppDoc = vcat $ [ addLibs libs ] ++ (inner: konts)
164+ in (ppDoc, w)
152165
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)))
157166
167+ stack2JSString :: CompileMode -> Bool -> StackUnit -> String
168+ stack2JSString compileMode debugMode su =
169+ let (ppDoc, _) = stack2PPDoc compileMode debugMode su
170+ in PP. render ppDoc
158171
159172
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
173+ stack2JSON :: CompileMode -> Bool -> StackUnit -> ByteString
174+ stack2JSON compileMode debugMode su =
175+ let (ppDoc, (libs, atoms, konts)) = stack2PPDoc compileMode debugMode su
176+ fname = case su of FunStackUnit (FunDef (HFN n) _ _ _ _) -> Just n
177+ AtomStackUnit _ -> Nothing
164178 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- }
179+ , fname = fname
180+ , atoms = atoms
181+ , code = PP. render ppDoc
182+ }
170183
171184
172185instance ToJS StackUnit where
@@ -185,15 +198,8 @@ instance ToJS IR.VarAccess where
185198instance ToJS StackProgram where
186199 toJS (StackProgram atoms funs) = do
187200 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-
201+ jjF <- mapM toJS funs
202+ return $ vcat $ [jjA] ++ jjF
197203
198204
199205instance ToJS C. Atoms where
0 commit comments