Error on import cycles
This commit is contained in:
parent
63504ba939
commit
ae971ec968
@ -10,6 +10,7 @@ import Control.Monad (foldM)
|
||||
import System.IO
|
||||
|
||||
import qualified Data.Map as Map
|
||||
import qualified Data.Set as Set
|
||||
|
||||
evaluateFileResult :: FilePath -> IO T
|
||||
evaluateFileResult filePath = do
|
||||
@ -61,7 +62,13 @@ mainAlias moduleName env =
|
||||
Nothing -> env
|
||||
|
||||
preprocessFile :: FilePath -> IO [TricuAST]
|
||||
preprocessFile filePath = do
|
||||
preprocessFile filePath = preprocessFile' Set.empty filePath
|
||||
|
||||
preprocessFile' :: Set.Set FilePath -> FilePath -> IO [TricuAST]
|
||||
preprocessFile' inProgress filePath
|
||||
| filePath `Set.member` inProgress =
|
||||
errorWithoutStackTrace $ "Encountered cyclic import: " ++ filePath
|
||||
| otherwise = do
|
||||
contents <- readFile filePath
|
||||
let tokens = lexTricu contents
|
||||
case parseProgram tokens of
|
||||
@ -69,10 +76,10 @@ preprocessFile filePath = do
|
||||
Right asts -> do
|
||||
let (moduleName, restAST) = extractModule asts
|
||||
let (imports, nonImports) = partition isImport restAST
|
||||
importedASTs <- concat <$> mapM (processImport moduleName) imports
|
||||
let newInProgress = Set.insert filePath inProgress
|
||||
importedASTs <- concat <$> mapM (processImport newInProgress) imports
|
||||
let namespacedAST = namespaceDefinitions moduleName nonImports
|
||||
let fullyNamespacedImports = map (namespaceBody moduleName) importedASTs
|
||||
pure $ fullyNamespacedImports ++ namespacedAST
|
||||
pure $ importedASTs ++ namespacedAST
|
||||
where
|
||||
extractModule :: [TricuAST] -> (String, [TricuAST])
|
||||
extractModule ((SModule name) : xs) = (name, xs)
|
||||
@ -82,11 +89,11 @@ preprocessFile filePath = do
|
||||
isImport (SImport _ _) = True
|
||||
isImport _ = False
|
||||
|
||||
processImport :: String -> TricuAST -> IO [TricuAST]
|
||||
processImport _ (SImport filePath moduleName) = do
|
||||
importedAST <- preprocessFile filePath
|
||||
processImport :: Set.Set FilePath -> TricuAST -> IO [TricuAST]
|
||||
processImport inProgress (SImport filePath moduleName) = do
|
||||
importedAST <- preprocessFile' inProgress filePath
|
||||
pure $ namespaceDefinitions moduleName importedAST
|
||||
processImport _ _ = error "Unexpected non-import AST node in processImport"
|
||||
processImport _ _ = error "Unexpected non-import in processImport"
|
||||
|
||||
namespaceDefinitions :: String -> [TricuAST] -> [TricuAST]
|
||||
namespaceDefinitions moduleName = map (namespaceDefinition moduleName)
|
||||
|
5
test/cycle-1.tri
Normal file
5
test/cycle-1.tri
Normal file
@ -0,0 +1,5 @@
|
||||
!module Cycle
|
||||
|
||||
!import "test/cycle-2.tri" Cycle2
|
||||
|
||||
cycle1 = t Cycle2.cycle2
|
5
test/cycle-2.tri
Normal file
5
test/cycle-2.tri
Normal file
@ -0,0 +1,5 @@
|
||||
!module Cycle2
|
||||
|
||||
!import "test/cycle-1.tri" Cycle1
|
||||
|
||||
cycle2 = t Cycle1.cycle1
|
Loading…
x
Reference in New Issue
Block a user