add interpreter path to module search path

This commit is contained in:
darkf 2014-02-12 00:36:30 -08:00
parent 32252b8d89
commit fe280dca78
5 changed files with 12 additions and 6 deletions

View File

@ -14,7 +14,9 @@ import Control.Monad.Trans.State (StateT, runStateT, evalStateT, get, put)
import System.IO (Handle, hPutStr, hGetLine, hClose, hIsEOF, hSetBuffering, import System.IO (Handle, hPutStr, hGetLine, hClose, hIsEOF, hSetBuffering,
hSetBinaryMode, openBinaryFile, IOMode(..), BufferMode(NoBuffering), stdout, stdin) hSetBinaryMode, openBinaryFile, IOMode(..), BufferMode(NoBuffering), stdout, stdin)
import System.Directory (doesFileExist) import System.Directory (doesFileExist)
import System.FilePath (FilePath, splitExtension, takeBaseName) import System.FilePath (takeDirectory)
import System.FilePath (FilePath, splitExtension, takeBaseName, (</>))
import System.Environment (getExecutablePath)
import AST import AST
import Parser (parseProgram) import Parser (parseProgram)
@ -233,11 +235,15 @@ _Import (StrV modname) = do
where where
findModule :: FilePath -> IO (FilePath, String) findModule :: FilePath -> IO (FilePath, String)
findModule modname = do findModule modname = do
let path = modname ++ ".lamb" execPath <- fmap takeDirectory getExecutablePath
findModuleIn [".", execPath </> "mods"] -- search paths for modules
where
findModuleIn [] = error $ "module " ++ modname ++ " couldn't be found"
findModuleIn (dir:xs) = do
let path = dir </> modname ++ ".lamb"
exists <- doesFileExist path exists <- doesFileExist path
if exists then if exists then return (path, takeBaseName path)
return (path, takeBaseName path) else findModuleIn xs
else error $ "module " ++ modname ++ " couldn't be found"
initialState = ([stdout, stdin], initialState = ([stdout, stdin],
[M.fromList [("id", FnV emptyEnv [(VarP "x", Var "x")]), [M.fromList [("id", FnV emptyEnv [(VarP "x", Var "x")]),