@@ -39,12 +39,9 @@ import Data.List as List
3939import Data.Maybe (fromJust )
4040import System.FilePath
4141
42- -- import System.Console.Haskeline
43- -- import System.Process
42+ --------------------------------------------------------------------------------
43+ ----- COMPILER FLAGS -----------------------------------------------------------
4444
45-
46- -- compiler flags
47- --
4845data Flag
4946 = IRMode
5047 | JSONIRMode
@@ -68,13 +65,11 @@ options =
6865 , Option [' o' ] [" output" ] (ReqArg OutputFile " FILE" ) " output FILE"
6966 ]
7067
71- -- debugTokens (Right tks) =
72- -- mapM_ print tks
68+ --------------------------------------------------------------------------------
69+ ----- PIPELINE FROM FLAGS TO IR AND JS -----------------------------------------
7370
7471process :: [Flag ] -> Maybe String -> String -> IO ExitCode
7572process flags fname input = do
76- -- let tokens = parseTokens input
77- -- debugTokens tokens
7873 let ast = parseProg input
7974
8075 let compileMode =
@@ -86,49 +81,56 @@ process flags fname input = do
8681
8782 case ast of
8883 Left err -> do
89- -- putStrLn ("Tokens: " ++ show tokens)
9084 die $ " Parse Error:\n " ++ err
9185
9286 Right prog_parsed -> do
93- let prog_empty_imports =
94- case compileMode of
95- Normal -> addAmbientMethods prog_parsed
96- Export -> prog_parsed
97- prog <- processImports prog_empty_imports
98-
87+ let outPath = outFile flags (fromJust fname)
88+
89+ -- To print all tokens from the parser, uncomment the following line:
90+ -- debugTokens (Right tks) = mapM_ print tks
91+
92+ ------------------------------------------------------
93+ -- TROUPE (FRONTEND) ---------------------------------
94+ let prog_without_dependencies =
95+ case compileMode of
96+ Normal -> addAmbientMethods prog_parsed
97+ Export -> prog_parsed
98+
99+ prog <- (processImports) prog_without_dependencies
100+
99101 exports <- case compileMode of
100102 Normal -> return Nothing
101103 Export -> case runExcept (extractExports prog) of
102104 Right es -> return (Just (es))
103105 Left s -> die s
104-
105106
106107 when verbose $ do printSep " SYNTAX"
107108 putStrLn (showIndent 2 prog)
108-
109- --------------------------------------------------
109+ ------------------------------------------------------
110110 prog' <- case runExcept (C. trans compileMode (AF. visitProg prog)) of
111111 Right p -> return p
112112 Left s -> die s
113113 when verbose $ do printSep " PATTERN MATCH ELIMINATION"
114114 writeFileD " out/out.nopats" (showIndent 2 prog')
115- --------------------------------------------------
115+ ------------------------------------------------------
116116 let lowered = Core. lowerProg prog'
117117 when verbose $ do printSep " LOWERING FUNS AND LETS"
118118 writeFileD " out/out.lowered" (showIndent 2 lowered)
119- --------------------------------------------------
119+ ------------------------------------------------------
120120 let renamed = Core. renameProg lowered
121121 when verbose $ do printSep " α RENAMING"
122122 writeFileD " out/out.alpha" (showIndent 2 renamed)
123- --------------------------------------------------
123+ ------------------------------------------------------
124124 let cpsed = RetDFCPS. transProg renamed
125125 when verbose $ do printSep " CPSED"
126126 writeFileD " out/out.cps" (showIndent 2 cpsed)
127- --------------------------------------------------
128- let rwcps = CPSOpt. rewrite cpsed -- Rewrite.rewrite cpsed
127+ ------------------------------------------------------
128+ let rwcps = CPSOpt. rewrite cpsed
129129 when verbose $ do printSep " REWRITING CPS"
130130 writeFileD " out/out.cpsopt" (showIndent 2 rwcps)
131- --------------------------------------------------
131+
132+ ------------------------------------------------------
133+ ------ IR (BACKEND) ----------------------------------
132134 ir <- case runExcept (CC. closureConvert compileMode rwcps) of
133135 Right ir -> return ir
134136 Left s -> die $ " troupec: " ++ s
@@ -138,116 +140,87 @@ process flags fname input = do
138140 let iropt = IROpt. iropt ir
139141 when verbose $ writeFileD " out/out.iropt" (show iropt)
140142
141- --------------------------------------------------
142- let debugOut = elem Debug flags
143-
144-
145- ------ RAW -----------------------------------------
143+ ------ RAW -------------------------------------------
146144 let raw = IR2Raw. prog2raw iropt
147145 when verbose $ printSep " GENERATING RAW"
148146 when verbose $ writeFileD " out/out.rawout" (show raw)
149147
150- ----- RAW OPT --------------------------------------
151-
148+ ----- RAW OPT ----------------------------------------
152149 rawopt <- do
153- if noRawOpt
154- then return raw
155- else do
156- let opt = RawOpt. rawopt raw
157- when verbose $ printSep " OPTIMIZING RAW OPT"
158- when verbose $ writeFileD " out/out.rawopt" (show opt)
159- return opt
160-
161- ----- STACK ----------------------------------------
150+ if noRawOpt
151+ then return raw
152+ else do
153+ let opt = RawOpt. rawopt raw
154+ when verbose $ printSep " OPTIMIZING RAW OPT"
155+ when verbose $ writeFileD " out/out.rawopt" (show opt)
156+ return opt
157+
158+ ----- STACK ------------------------------------------
162159 let stack = Raw2Stack. rawProg2Stack rawopt
163160 when verbose $ printSep " GENERATING STACK"
164161 when verbose $ writeFileD " out/out.stack" (show stack)
165- let stackjs = Stack2JS. irProg2JSString compileMode debugOut stack
166- let jsFile = outFile flags (fromJust fname)
167- writeFile jsFile stackjs
168162
169- ----- MODULE ----------------------------------------
163+ ----- JAVASCRIPT -------------------------------------
164+ let stackjs = Stack2JS. irProg2JSString compileMode (Debug `elem` flags) stack
165+ writeFile outPath stackjs
166+
170167 case exports of
171168 Nothing -> return ()
172- Just es -> writeExports jsFile es
169+ Just es -> writeExports outPath es
173170
174171 ----- EPILOGUE --------------------------------------
175172 when verbose printHr
176173 exitSuccess
177174
178- writeExports jsF exports =
179- let exF' = if takeExtension jsF == " .js" then dropExtension jsF else jsF
180- in writeFileD (exF' ++ " .exports" ) (intercalate " \n " exports)
181-
182- defaultName f =
183- let ext = " .trp"
184- in concat [ takeDirectory f
185- , " /out/"
186- , if takeExtension f == ext then takeBaseName f else takeFileName f
187- ]
188-
189- isOutFlag (OutputFile _) = True
190- isOutFlag _ = False
191-
175+ -- TODO: 'where' for all helper functions below?
192176outFile :: [Flag ] -> String -> String
193- outFile flags fname | LibMode `elem` flags =
194- case List. find isOutFlag flags of
177+ outFile flags fname = case List. find isOutFlag flags of
195178 Just (OutputFile s) -> s
196- _ -> defaultName fname ++ " .js "
197- outFile flags _ =
198- case List. find isOutFlag flags of
199- Just (OutputFile s) -> s
200- _ -> " out/out.stack.js "
179+ _ -> if LibMode `elem` flags
180+ then defaultName fname ++ " .js "
181+ else " out/out.stack.js "
182+ where isOutFlag (OutputFile _) = True
183+ isOutFlag _ = False
201184
185+ defaultName f = concat [ takeDirectory f
186+ , " /out/"
187+ , if takeExtension f == " .trp" then takeBaseName f else takeFileName f
188+ ]
202189
203- -- AA: 2018-07-15: consider timestamping these entries
204- debugOut s =
205- appendFile " /tmp/debug " (s ++ " \n " )
190+ writeExports path exports =
191+ let path' = if takeExtension path == " .js " then dropExtension path else path
192+ in writeFileD (path' ++ " .exports " ) (intercalate " \n " exports )
206193
194+ --------------------------------------------------------------------------------
195+ ----- DESERIALIZATION FOR INTERACTIVE MODES ------------------------------------
207196
208- fromStdinIR = do
197+ fromStdin putFormattedLn = do
209198 eof <- isEOF
210199 if eof then exitSuccess else do
211200 input <- BS. getLine
212201 if BS. isPrefixOf " !ECHO " input
213202 then let response = BS. drop 6 input
214203 in do BSChar8. putStrLn response
215- -- debugOut "echo"
216204 else
217205 case decode input of
218206 Right bs ->
219207 case CCIR. deserialize bs
220- of Right x -> do putStrLn (IR2JS. irToJSString x)
221- -- debugOut "deserialization OK"
222-
208+ of Right x -> do putFormattedLn x
223209 Left s -> do putStrLn " ERROR in deserialization"
224210 debugOut $ " deserialization error" ++ s
225211 Left s -> do putStrLn " ERROR in B64 decoding"
226212 debugOut $ " decoding error" ++ s
227213 putStrLn " " -- magic marker to be recognized by the JS runtime; 2018-03-04; aa
228214 hFlush stdout
229- fromStdinIR
215+ fromStdin putFormattedLn
216+ -- AA: 2018-07-15: consider timestamping these entries
217+ where debugOut s = appendFile " /tmp/debug" (s ++ " \n " )
230218
219+ fromStdinIR = fromStdin (putStrLn . IR2JS. irToJSString)
220+ fromStdinIRJson = fromStdin (BSLazyChar8. putStrLn . IR2JS. irToJSON)
231221
232- fromStdinIRJson = do
233- eof <- isEOF
234- if eof then exitSuccess else do
235- input <- BS. getLine
236- if BS. isPrefixOf " !ECHO " input
237- then let response = BS. drop 6 input
238- in BSChar8. putStrLn response
239- else
240- case decode input of
241- Right bs ->
242- case CCIR. deserialize bs
243- of Right x -> BSLazyChar8. putStrLn (IR2JS. irToJSON x)
244- Left s -> do putStrLn " ERROR in deserialization"
245- debugOut $ " deserialization error" ++ s
246- Left s -> do putStrLn " ERROR in B64 decoding"
247- debugOut $ " decoding error" ++ s
248- putStrLn " " -- magic marker to be recognized by the JS runtime; 2018-03-04; aa
249- hFlush stdout
250- fromStdinIRJson
222+ --------------------------------------------------------------------------------
223+ ----- MAIN ---------------------------------------------------------------------
251224
252225main :: IO ExitCode
253226main = do
0 commit comments