Skip to content

Commit 3ecdd7f

Browse files
committed
Clean up and simplify main interface for Stack2JS.hs
1 parent c2dbc95 commit 3ecdd7f

File tree

3 files changed

+57
-65
lines changed

3 files changed

+57
-65
lines changed

compiler/app/Main.hs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import qualified IR as CCIR
1414
import qualified IROpt
1515
-- import qualified RetRewrite as Rewrite
1616
import qualified CPSOpt as CPSOpt
17-
import qualified IR2JS
1817
import qualified IR2Raw
19-
-- import qualified Stack
2018
import qualified Raw2Stack
19+
import qualified Stack
2120
import qualified Stack2JS
2221
import qualified RawOpt
2322
-- import System.IO (isEOF)
@@ -77,6 +76,7 @@ process flags fname input = do
7776

7877
let verbose = Verbose `elem` flags
7978
noRawOpt = NoRawOpt `elem` flags
79+
debugJS = Debug `elem` flags
8080

8181
case ast of
8282
Left err -> do
@@ -161,7 +161,9 @@ process flags fname input = do
161161
when verbose $ writeFileD "out/out.stack" (show stack)
162162

163163
----- JAVASCRIPT -------------------------------------
164-
let stackjs = Stack2JS.irProg2JSString compileMode (Debug `elem` flags) stack
164+
let stackjs = Stack2JS.stack2JSString compileMode
165+
debugJS
166+
(Stack.ProgramStackUnit stack)
165167
writeFile outPath stackjs
166168

167169
case exports of
@@ -208,7 +210,7 @@ printHr = putStrLn (replicate hrWidth '-')
208210
--------------------------------------------------------------------------------
209211
----- DESERIALIZATION FOR INTERACTIVE MODES ------------------------------------
210212

211-
fromStdinIR putFormattedLn = do
213+
fromStdinIR putStrLn format = do
212214
eof <- isEOF
213215
if eof then exitSuccess else do
214216
input <- BS.getLine
@@ -220,19 +222,27 @@ fromStdinIR putFormattedLn = do
220222
case decode input of
221223
Right bs ->
222224
case CCIR.deserialize bs
223-
of Right x -> do putFormattedLn x
225+
of Right x -> do (putStrLn . format . ir2Stack) x
224226
Left s -> do putStrLn "ERROR in deserialization"
225227
debugOut $ "deserialization error" ++ s
226228
Left s -> do putStrLn "ERROR in B64 decoding"
227229
debugOut $ "decoding error" ++s
228230
putStrLn "" -- magic marker to be recognized by the JS runtime; 2018-03-04; aa
229231
hFlush stdout
230-
fromStdinIR putFormattedLn
232+
fromStdinIR putStrLn format
231233
-- AA: 2018-07-15: consider timestamping these entries
232234
where debugOut s = appendFile "/tmp/debug" (s ++ "\n")
233235

234-
fromStdinTextIR = fromStdinIR (putStrLn . IR2JS.irToJSString)
235-
fromStdinJsonIR = fromStdinIR (BSLazyChar8.putStrLn . IR2JS.irToJSON)
236+
ir2Stack = Raw2Stack.raw2Stack . RawOpt.rawopt . IR2Raw.ir2raw
237+
238+
fromStdinTextIR =
239+
let format = Stack2JS.stack2JSString CompileMode.Normal False
240+
in fromStdinIR putStrLn format
241+
242+
fromStdinJsonIR =
243+
let putStrLn = BSLazyChar8.putStrLn
244+
format = Stack2JS.stack2JSON CompileMode.Normal False
245+
in fromStdinIR putStrLn format
236246

237247
--------------------------------------------------------------------------------
238248
----- MAIN ---------------------------------------------------------------------

compiler/src/IR2JS.hs

Lines changed: 0 additions & 23 deletions
This file was deleted.

compiler/src/Stack2JS.hs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ data TheState = TheState { freshCounter :: Integer
9191

9292
type 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

9798
initState = TheState { freshCounter = 0
@@ -136,37 +137,48 @@ instance Identifier Raw.Assignable where
136137
class 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

172184
instance ToJS StackUnit where
@@ -185,15 +197,8 @@ instance ToJS IR.VarAccess where
185197
instance 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

199204
instance ToJS C.Atoms where

0 commit comments

Comments
 (0)