@@ -22,10 +22,10 @@ import System.Process (CreateProcess (std_out), StdStream (CreatePipe),
2222 createProcess , shell )
2323import Telomare
2424import Telomare.Optimizer (optimize )
25- import Telomare.Parser (AnnotatedUPT , parseOneExprOrTopLevelDefs , parsePrelude )
25+ import Telomare.Parser (AnnotatedUPT , parseModule , parseOneExprOrTopLevelDefs )
2626import Telomare.Possible (AbortExpr , abortExprToTerm4 , evalA , sizeTerm ,
2727 term3ToUnsizedExpr )
28- import Telomare.Resolver (parseMain , process )
28+ import Telomare.Resolver (main2Term3 , process , resolveAllImports )
2929import Telomare.RunTime (pureEval , rEval )
3030import Telomare.TypeChecker (TypeCheckError (.. ), typeCheck )
3131import Text.Megaparsec (errorBundlePretty , runParser )
@@ -207,24 +207,45 @@ funWrap eval fun inp =
207207 Pair disp newState -> (g2s disp, Just newState)
208208 z -> (" runtime error, dumped:\n " <> show z, Nothing )
209209
210- runMainCore :: String -> String -> (IExpr -> IO a ) -> IO a
211- runMainCore preludeString s e =
212- let prelude :: [(String , AnnotatedUPT )]
213- prelude =
214- case parsePrelude preludeString of
215- Right p -> p
216- Left pe -> error pe
210+ runMainCore :: [(String , String )] -> String -> (IExpr -> IO a ) -> IO a
211+ runMainCore modulesStrings s e =
212+ let parsedModules :: [(String , Either String [Either AnnotatedUPT (String , AnnotatedUPT )])]
213+ parsedModules = (fmap . fmap ) parseModule modulesStrings
214+ parsedModulesErrors :: [(String , Either String [Either AnnotatedUPT (String , AnnotatedUPT )])]
215+ parsedModulesErrors = filter (\ (moduleStr, parsed) -> case parsed of
216+ Left _ -> True
217+ Right _ -> False )
218+ parsedModules
219+ flattenLeft = \ case
220+ Left a -> a
221+ Right a -> error " flattenLeft error: got a Right when it should be Left"
222+ flattenRight = \ case
223+ Right a -> a
224+ Left a -> error " flattenRight error: got a Left when it should be Right"
225+ modules :: [(String , [Either AnnotatedUPT (String , AnnotatedUPT )])]
226+ modules =
227+ case parsedModulesErrors of
228+ [] -> (fmap . fmap ) flattenRight parsedModules
229+ errors -> let moduleWithError :: [(String , String )]
230+ moduleWithError = (fmap . fmap ) flattenLeft parsedModulesErrors
231+ joinModuleError :: (String , String ) -> String
232+ joinModuleError (moduleName, errorStr) = " Error in module " <> moduleName <> " :\n " <> errorStr
233+ in error . unlines $ joinModuleError <$> moduleWithError
234+
217235 in
218- case compileMain <$> parseMain prelude s of
236+ case compileMain <$> main2Term3 modules s of
219237 Left e -> error $ concat [" failed to parse " , s, " " , e]
220238 Right (Right g) -> e g
221239 Right z -> error $ " compilation failed somehow, with result " <> show z
222240
223- runMain_ :: String -> String -> IO String
224- runMain_ preludeString s = runMainCore preludeString s evalLoop_
241+ runMain_ :: [(String , String )] -> String -> IO String
242+ runMain_ modulesStrings s = runMainCore modulesStrings s evalLoop_
243+
244+ runMain :: [(String , String )] -> String -> IO ()
245+ runMain modulesStrings s = runMainCore modulesStrings s evalLoop
225246
226- runMain :: String -> String -> IO ()
227- runMain preludeString s = runMainCore preludeString s evalLoop
247+ runMainWithInput :: [ String ] -> [( String , String )] -> String -> IO String
248+ runMainWithInput inputList modulesStrings s = runMainCore modulesStrings s (evalLoopWithInput inputList)
228249
229250schemeEval :: IExpr -> IO ()
230251schemeEval iexpr = do
@@ -270,19 +291,6 @@ evalLoopWithInput inputList iexpr = evalLoopCore iexpr printAcc "" inputList
270291 then pure out
271292 else pure (acc <> " \n " <> out)
272293
273- runMainWithInput :: [String ] -> String -> String -> IO String
274- runMainWithInput inputList preludeString s =
275- let prelude :: [(String , AnnotatedUPT )]
276- prelude =
277- case parsePrelude preludeString of
278- Right p -> p
279- Left pe -> error pe
280- in
281- case compileMain <$> parseMain prelude s of
282- Left e -> pure $ concat [" failed to parse " , s, " " , e]
283- Right (Right g) -> evalLoopWithInput inputList g
284- Right z -> pure $ " compilation failed somehow, with result " <> show z
285-
286294-- | Same as `evalLoop`, but keeping what was displayed.
287295evalLoop_ :: IExpr -> IO String
288296evalLoop_ iexpr = evalLoopCore iexpr printAcc " " []
@@ -302,10 +310,14 @@ calculateRecursionLimits t3 =
302310 Left a -> Left . StaticCheckError . convertAbortMessage $ a
303311 Right t -> pure t
304312
305- eval2IExpr :: [(String , AnnotatedUPT )] -> String -> Either String IExpr
306- eval2IExpr prelude str = bimap errorBundlePretty (\ x -> DummyLoc :< LetUPF prelude x) (runParser (parseOneExprOrTopLevelDefs prelude) " " str)
307- >>= process
308- >>= first show . compileUnitTest
313+ eval2IExpr :: [(String , [Either AnnotatedUPT (String , AnnotatedUPT )])] -> String -> Either String IExpr
314+ eval2IExpr extraModuleBindings str =
315+ first errorBundlePretty (runParser (parseOneExprOrTopLevelDefs resolved) " " str)
316+ >>= process
317+ >>= first show . compileUnitTest
318+ where
319+ aux = (\ str -> Left (DummyLoc :< ImportQualifiedUPF str str)) . fst <$> extraModuleBindings
320+ resolved = resolveAllImports extraModuleBindings aux
309321
310322tagIExprWithEval :: IExpr -> Cofree IExprF (Int , IExpr )
311323tagIExprWithEval iexpr = evalState (para alg iexpr) 0 where
0 commit comments