@@ -38,12 +38,9 @@ import Data.List as List
3838import Data.Maybe (fromJust )
3939import System.FilePath
4040
41- -- import System.Console.Haskeline
42- -- import System.Process
41+ --------------------------------------------------------------------------------
42+ ----- COMPILER FLAGS -----------------------------------------------------------
4343
44-
45- -- compiler flags
46- --
4744data Flag
4845 = IRMode
4946 | JSONIRMode
@@ -67,13 +64,11 @@ options =
6764 , Option [' o' ] [" output" ] (ReqArg OutputFile " FILE" ) " output FILE"
6865 ]
6966
70- -- debugTokens (Right tks) =
71- -- mapM_ print tks
67+ --------------------------------------------------------------------------------
68+ ----- PIPELINE FROM FLAGS TO IR AND JS -----------------------------------------
7269
7370process :: [Flag ] -> Maybe String -> String -> IO ExitCode
7471process flags fname input = do
75- -- let tokens = parseTokens input
76- -- debugTokens tokens
7772 let ast = parseProg input
7873
7974 let compileMode =
@@ -85,49 +80,56 @@ process flags fname input = do
8580
8681 case ast of
8782 Left err -> do
88- -- putStrLn ("Tokens: " ++ show tokens)
8983 die $ " Parse Error:\n " ++ err
9084
9185 Right prog_parsed -> do
92- let prog_empty_imports =
93- case compileMode of
94- Normal -> addAmbientMethods prog_parsed
95- Export -> prog_parsed
96- prog <- processImports prog_empty_imports
97-
86+ let outPath = outFile flags (fromJust fname)
87+
88+ -- To print all tokens from the parser, uncomment the following line:
89+ -- debugTokens (Right tks) = mapM_ print tks
90+
91+ ------------------------------------------------------
92+ -- TROUPE (FRONTEND) ---------------------------------
93+ let prog_without_dependencies =
94+ case compileMode of
95+ Normal -> addAmbientMethods prog_parsed
96+ Export -> prog_parsed
97+
98+ prog <- (processImports) prog_without_dependencies
99+
98100 exports <- case compileMode of
99101 Normal -> return Nothing
100102 Export -> case runExcept (extractExports prog) of
101103 Right es -> return (Just (es))
102104 Left s -> die s
103-
104105
105106 when verbose $ do printSep " SYNTAX"
106107 putStrLn (showIndent 2 prog)
107-
108- --------------------------------------------------
108+ ------------------------------------------------------
109109 prog' <- case runExcept (C. trans compileMode (AF. visitProg prog)) of
110110 Right p -> return p
111111 Left s -> die s
112112 when verbose $ do printSep " PATTERN MATCH ELIMINATION"
113113 writeFileD " out/out.nopats" (showIndent 2 prog')
114- --------------------------------------------------
114+ ------------------------------------------------------
115115 let lowered = Core. lowerProg prog'
116116 when verbose $ do printSep " LOWERING FUNS AND LETS"
117117 writeFileD " out/out.lowered" (showIndent 2 lowered)
118- --------------------------------------------------
118+ ------------------------------------------------------
119119 let renamed = Core. renameProg lowered
120120 when verbose $ do printSep " α RENAMING"
121121 writeFileD " out/out.alpha" (showIndent 2 renamed)
122- --------------------------------------------------
122+ ------------------------------------------------------
123123 let cpsed = RetDFCPS. transProg renamed
124124 when verbose $ do printSep " CPSED"
125125 writeFileD " out/out.cps" (showIndent 2 cpsed)
126- --------------------------------------------------
127- let rwcps = CPSOpt. rewrite cpsed -- Rewrite.rewrite cpsed
126+ ------------------------------------------------------
127+ let rwcps = CPSOpt. rewrite cpsed
128128 when verbose $ do printSep " REWRITING CPS"
129129 writeFileD " out/out.cpsopt" (showIndent 2 rwcps)
130- --------------------------------------------------
130+
131+ ------------------------------------------------------
132+ ------ IR (BACKEND) ----------------------------------
131133 ir <- case runExcept (CC. closureConvert compileMode rwcps) of
132134 Right ir -> return ir
133135 Left s -> die $ " troupec: " ++ s
@@ -137,116 +139,87 @@ process flags fname input = do
137139 let iropt = IROpt. iropt ir
138140 when verbose $ writeFileD " out/out.iropt" (show iropt)
139141
140- --------------------------------------------------
141- let debugOut = elem Debug flags
142-
143-
144- ------ RAW -----------------------------------------
142+ ------ RAW -------------------------------------------
145143 let raw = IR2Raw. prog2raw iropt
146144 when verbose $ printSep " GENERATING RAW"
147145 when verbose $ writeFileD " out/out.rawout" (show raw)
148146
149- ----- RAW OPT --------------------------------------
150-
147+ ----- RAW OPT ----------------------------------------
151148 rawopt <- do
152- if noRawOpt
153- then return raw
154- else do
155- let opt = RawOpt. rawopt raw
156- when verbose $ printSep " OPTIMIZING RAW OPT"
157- when verbose $ writeFileD " out/out.rawopt" (show opt)
158- return opt
159-
160- ----- STACK ----------------------------------------
149+ if noRawOpt
150+ then return raw
151+ else do
152+ let opt = RawOpt. rawopt raw
153+ when verbose $ printSep " OPTIMIZING RAW OPT"
154+ when verbose $ writeFileD " out/out.rawopt" (show opt)
155+ return opt
156+
157+ ----- STACK ------------------------------------------
161158 let stack = Raw2Stack. rawProg2Stack rawopt
162159 when verbose $ printSep " GENERATING STACK"
163160 when verbose $ writeFileD " out/out.stack" (show stack)
164- let stackjs = Stack2JS. irProg2JSString compileMode debugOut stack
165- let jsFile = outFile flags (fromJust fname)
166- writeFile jsFile stackjs
167161
168- ----- MODULE ----------------------------------------
162+ ----- JAVASCRIPT -------------------------------------
163+ let stackjs = Stack2JS. irProg2JSString compileMode (Debug `elem` flags) stack
164+ writeFile outPath stackjs
165+
169166 case exports of
170167 Nothing -> return ()
171- Just es -> writeExports jsFile es
168+ Just es -> writeExports outPath es
172169
173170 ----- EPILOGUE --------------------------------------
174171 when verbose printHr
175172 exitSuccess
176173
177- writeExports jsF exports =
178- let exF' = if takeExtension jsF == " .js" then dropExtension jsF else jsF
179- in writeFileD (exF' ++ " .exports" ) (intercalate " \n " exports)
180-
181- defaultName f =
182- let ext = " .trp"
183- in concat [ takeDirectory f
184- , " /out/"
185- , if takeExtension f == ext then takeBaseName f else takeFileName f
186- ]
187-
188- isOutFlag (OutputFile _) = True
189- isOutFlag _ = False
190-
174+ -- TODO: 'where' for all helper functions below?
191175outFile :: [Flag ] -> String -> String
192- outFile flags fname | LibMode `elem` flags =
193- case List. find isOutFlag flags of
176+ outFile flags fname = case List. find isOutFlag flags of
194177 Just (OutputFile s) -> s
195- _ -> defaultName fname ++ " .js "
196- outFile flags _ =
197- case List. find isOutFlag flags of
198- Just (OutputFile s) -> s
199- _ -> " out/out.stack.js "
178+ _ -> if LibMode `elem` flags
179+ then defaultName fname ++ " .js "
180+ else " out/out.stack.js "
181+ where isOutFlag (OutputFile _) = True
182+ isOutFlag _ = False
200183
184+ defaultName f = concat [ takeDirectory f
185+ , " /out/"
186+ , if takeExtension f == " .trp" then takeBaseName f else takeFileName f
187+ ]
201188
202- -- AA: 2018-07-15: consider timestamping these entries
203- debugOut s =
204- appendFile " /tmp/debug " (s ++ " \n " )
189+ writeExports path exports =
190+ let path' = if takeExtension path == " .js " then dropExtension path else path
191+ in writeFileD (path' ++ " .exports " ) (intercalate " \n " exports )
205192
193+ --------------------------------------------------------------------------------
194+ ----- DESERIALIZATION FOR INTERACTIVE MODES ------------------------------------
206195
207- fromStdinIR = do
196+ fromStdin putFormattedLn = do
208197 eof <- isEOF
209198 if eof then exitSuccess else do
210199 input <- BS. getLine
211200 if BS. isPrefixOf " !ECHO " input
212201 then let response = BS. drop 6 input
213202 in do BS. putStrLn response
214- -- debugOut "echo"
215203 else
216204 case decode input of
217205 Right bs ->
218206 case CCIR. deserialize bs
219- of Right x -> do putStrLn (IR2JS. irToJSString x)
220- -- debugOut "deserialization OK"
221-
207+ of Right x -> do putFormattedLn x
222208 Left s -> do putStrLn " ERROR in deserialization"
223209 debugOut $ " deserialization error" ++ s
224210 Left s -> do putStrLn " ERROR in B64 decoding"
225211 debugOut $ " decoding error" ++ s
226212 putStrLn " " -- magic marker to be recognized by the JS runtime; 2018-03-04; aa
227213 hFlush stdout
228- fromStdinIR
214+ fromStdin putFormattedLn
215+ -- AA: 2018-07-15: consider timestamping these entries
216+ where debugOut s = appendFile " /tmp/debug" (s ++ " \n " )
229217
218+ fromStdinIR = fromStdin (putStrLn . IR2JS. irToJSString)
219+ fromStdinIRJson = fromStdin (BSLazyChar8. putStrLn . IR2JS. irToJSON)
230220
231- fromStdinIRJson = do
232- eof <- isEOF
233- if eof then exitSuccess else do
234- input <- BS. getLine
235- if BS. isPrefixOf " !ECHO " input
236- then let response = BS. drop 6 input
237- in BS. putStrLn response
238- else
239- case decode input of
240- Right bs ->
241- case CCIR. deserialize bs
242- of Right x -> BSLazyChar8. putStrLn (IR2JS. irToJSON x)
243- Left s -> do putStrLn " ERROR in deserialization"
244- debugOut $ " deserialization error" ++ s
245- Left s -> do putStrLn " ERROR in B64 decoding"
246- debugOut $ " decoding error" ++ s
247- putStrLn " " -- magic marker to be recognized by the JS runtime; 2018-03-04; aa
248- hFlush stdout
249- fromStdinIRJson
221+ --------------------------------------------------------------------------------
222+ ----- MAIN ---------------------------------------------------------------------
250223
251224main :: IO ExitCode
252225main = do
0 commit comments