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 System.IO
|
||||||
|
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
|
import qualified Data.Set as Set
|
||||||
|
|
||||||
evaluateFileResult :: FilePath -> IO T
|
evaluateFileResult :: FilePath -> IO T
|
||||||
evaluateFileResult filePath = do
|
evaluateFileResult filePath = do
|
||||||
@ -61,18 +62,24 @@ mainAlias moduleName env =
|
|||||||
Nothing -> env
|
Nothing -> env
|
||||||
|
|
||||||
preprocessFile :: FilePath -> IO [TricuAST]
|
preprocessFile :: FilePath -> IO [TricuAST]
|
||||||
preprocessFile filePath = do
|
preprocessFile filePath = preprocessFile' Set.empty filePath
|
||||||
contents <- readFile filePath
|
|
||||||
let tokens = lexTricu contents
|
preprocessFile' :: Set.Set FilePath -> FilePath -> IO [TricuAST]
|
||||||
case parseProgram tokens of
|
preprocessFile' inProgress filePath
|
||||||
Left err -> errorWithoutStackTrace (handleParseError err)
|
| filePath `Set.member` inProgress =
|
||||||
Right asts -> do
|
errorWithoutStackTrace $ "Encountered cyclic import: " ++ filePath
|
||||||
let (moduleName, restAST) = extractModule asts
|
| otherwise = do
|
||||||
let (imports, nonImports) = partition isImport restAST
|
contents <- readFile filePath
|
||||||
importedASTs <- concat <$> mapM (processImport moduleName) imports
|
let tokens = lexTricu contents
|
||||||
let namespacedAST = namespaceDefinitions moduleName nonImports
|
case parseProgram tokens of
|
||||||
let fullyNamespacedImports = map (namespaceBody moduleName) importedASTs
|
Left err -> errorWithoutStackTrace (handleParseError err)
|
||||||
pure $ fullyNamespacedImports ++ namespacedAST
|
Right asts -> do
|
||||||
|
let (moduleName, restAST) = extractModule asts
|
||||||
|
let (imports, nonImports) = partition isImport restAST
|
||||||
|
let newInProgress = Set.insert filePath inProgress
|
||||||
|
importedASTs <- concat <$> mapM (processImport newInProgress) imports
|
||||||
|
let namespacedAST = namespaceDefinitions moduleName nonImports
|
||||||
|
pure $ importedASTs ++ namespacedAST
|
||||||
where
|
where
|
||||||
extractModule :: [TricuAST] -> (String, [TricuAST])
|
extractModule :: [TricuAST] -> (String, [TricuAST])
|
||||||
extractModule ((SModule name) : xs) = (name, xs)
|
extractModule ((SModule name) : xs) = (name, xs)
|
||||||
@ -82,11 +89,11 @@ preprocessFile filePath = do
|
|||||||
isImport (SImport _ _) = True
|
isImport (SImport _ _) = True
|
||||||
isImport _ = False
|
isImport _ = False
|
||||||
|
|
||||||
processImport :: String -> TricuAST -> IO [TricuAST]
|
processImport :: Set.Set FilePath -> TricuAST -> IO [TricuAST]
|
||||||
processImport _ (SImport filePath moduleName) = do
|
processImport inProgress (SImport filePath moduleName) = do
|
||||||
importedAST <- preprocessFile filePath
|
importedAST <- preprocessFile' inProgress filePath
|
||||||
pure $ namespaceDefinitions moduleName importedAST
|
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 :: String -> [TricuAST] -> [TricuAST]
|
||||||
namespaceDefinitions moduleName = map (namespaceDefinition moduleName)
|
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