Added a system for specifying implicitly-used modules on the command-line

An implicitly used module is equivalent to a #USE directive before the first line of the main file.  This, combined with changes in occbuild, are my current way of implementing the automatic use of the forall module in occam.
This commit is contained in:
Neil Brown 2009-04-10 19:26:22 +00:00
parent 09ea5fd610
commit e462da4a76
3 changed files with 21 additions and 9 deletions

View File

@ -73,6 +73,7 @@ optionsNoWarnings =
, Option [] ["external-link"] (ReqArg optCompilerLinkFlags "FLAGS") "link flags for C/C++ compiler" , Option [] ["external-link"] (ReqArg optCompilerLinkFlags "FLAGS") "link flags for C/C++ compiler"
, Option [] ["run-indent"] (NoArg $ optRunIndent) "run indent on source before compilation (will full mode)" , Option [] ["run-indent"] (NoArg $ optRunIndent) "run indent on source before compilation (will full mode)"
, Option [] ["frontend"] (ReqArg optFrontend "FRONTEND") "language frontend (options: occam, rain)" , Option [] ["frontend"] (ReqArg optFrontend "FRONTEND") "language frontend (options: occam, rain)"
, Option ['u'] ["implicit-module"] (ReqArg optImplicitModule "MODULE") "implicitly use this module"
, Option [] ["include-path"] (NoArg $ optPrintPath tockIncludeDir) "print include path" , Option [] ["include-path"] (NoArg $ optPrintPath tockIncludeDir) "print include path"
, Option [] ["lib-path"] (NoArg $ optPrintPath tockLibDir) "print lib path" , Option [] ["lib-path"] (NoArg $ optPrintPath tockLibDir) "print lib path"
, Option [] ["mode"] (ReqArg optMode "MODE") "select mode (options: flowgraph, lex, html, parse, compile, post-c, full)" , Option [] ["mode"] (ReqArg optMode "MODE") "select mode (options: flowgraph, lex, html, parse, compile, post-c, full)"
@ -136,6 +137,9 @@ optSearchPath s ps = return $ ps { csSearchPath = csSearchPath ps ++ splitOnColo
(p, _:more) -> p : splitOnColons more (p, _:more) -> p : splitOnColons more
(p, []) -> [p] (p, []) -> [p]
optImplicitModule :: String -> OptFunc
optImplicitModule s ps = return $ ps { csImplicitModules = csImplicitModules ps ++ [s] }
optCompilerFlags :: String -> OptFunc optCompilerFlags :: String -> OptFunc
optCompilerFlags flags ps = return $ ps { csCompilerFlags = flags ++ " " ++ csCompilerFlags ps} optCompilerFlags flags ps = return $ ps { csCompilerFlags = flags ++ " " ++ csCompilerFlags ps}

View File

@ -119,6 +119,7 @@ data CompState = CompState {
csClassicOccamMobility :: Bool, csClassicOccamMobility :: Bool,
csUnknownStackSize :: Integer, csUnknownStackSize :: Integer,
csSearchPath :: [String], csSearchPath :: [String],
csImplicitModules :: [String],
-- Set by preprocessor -- Set by preprocessor
csCurrentFile :: String, -- Also used by some later passes! csCurrentFile :: String, -- Also used by some later passes!
@ -181,6 +182,7 @@ emptyState = CompState {
csClassicOccamMobility = False, csClassicOccamMobility = False,
csUnknownStackSize = 512, csUnknownStackSize = 512,
csSearchPath = [".", tockIncludeDir], csSearchPath = [".", tockIncludeDir],
csImplicitModules = [],
csCurrentFile = "none", csCurrentFile = "none",
csUsedFiles = Set.empty, csUsedFiles = Set.empty,

View File

@ -42,8 +42,8 @@ import StructureOccam
import Utils import Utils
-- | Preprocess a file and return its tokenised form ready for parsing. -- | Preprocess a file and return its tokenised form ready for parsing.
preprocessFile :: Meta -> String -> PassM [Token] preprocessFile :: Meta -> [String] -> String -> PassM [Token]
preprocessFile m filename preprocessFile m implicitMods filename
= do (handle, realFilename) <- searchFile m filename = do (handle, realFilename) <- searchFile m filename
progress $ "Loading source file " ++ realFilename progress $ "Loading source file " ++ realFilename
origCS <- get origCS <- get
@ -54,7 +54,7 @@ preprocessFile m filename
modify (\cs -> cs { csCurrentFile = realFilename modify (\cs -> cs { csCurrentFile = realFilename
, csUsedFiles = modFunc $ csUsedFiles cs }) , csUsedFiles = modFunc $ csUsedFiles cs })
s <- liftIO $ hGetContents handle s <- liftIO $ hGetContents handle
toks <- preprocessSource m realFilename s toks <- preprocessSource m implicitMods realFilename s
modify (\cs -> cs { csCurrentFile = csCurrentFile origCS }) modify (\cs -> cs { csCurrentFile = csCurrentFile origCS })
return toks return toks
where where
@ -62,13 +62,13 @@ preprocessFile m filename
drop9 = reverse . drop 9 . reverse drop9 = reverse . drop 9 . reverse
-- | Preprocesses source directly and returns its tokenised form ready for parsing. -- | Preprocesses source directly and returns its tokenised form ready for parsing.
preprocessSource :: Meta -> String -> String -> PassM [Token] preprocessSource :: Meta -> [String] -> String -> String -> PassM [Token]
preprocessSource m realFilename s preprocessSource m implicitMods realFilename s
= do toks <- runLexer realFilename $ removeASM s = do toks <- runLexer realFilename $ removeASM s
veryDebug $ "{{{ lexer tokens" veryDebug $ "{{{ lexer tokens"
veryDebug $ pshow toks veryDebug $ pshow toks
veryDebug $ "}}}" veryDebug $ "}}}"
toks' <- preprocessOccam toks toks' <- preprocessOccam $ incImplicit ++ toks
veryDebug $ "{{{ preprocessed tokens" veryDebug $ "{{{ preprocessed tokens"
veryDebug $ pshow toks' veryDebug $ pshow toks'
veryDebug $ "}}}" veryDebug $ "}}}"
@ -78,6 +78,11 @@ preprocessSource m realFilename s
veryDebug $ "}}}" veryDebug $ "}}}"
expandIncludes toks'' expandIncludes toks''
where where
incImplicit = concat [[Token emptyMeta $ TokPreprocessor $ "#USE \"" ++ f ++ "\""
-- ,Token emptyMeta EndOfLine
]
| f <- implicitMods]
--ASM blocks tend to screw up the lexer. Tock is unlikely to support them --ASM blocks tend to screw up the lexer. Tock is unlikely to support them
-- any time soon, but at the same time lots of occam code does have ASM blocks, -- any time soon, but at the same time lots of occam code does have ASM blocks,
-- and even if they are inside a #IFDEF, it will still cause Tock to choke -- and even if they are inside a #IFDEF, it will still cause Tock to choke
@ -119,7 +124,7 @@ preprocessSource m realFilename s
expandIncludes :: [Token] -> PassM [Token] expandIncludes :: [Token] -> PassM [Token]
expandIncludes [] = return [] expandIncludes [] = return []
expandIncludes (Token m (IncludeFile filename) : Token _ EndOfLine : ts) expandIncludes (Token m (IncludeFile filename) : Token _ EndOfLine : ts)
= do contents <- preprocessFile m filename = do contents <- preprocessFile m [] filename
rest <- expandIncludes ts rest <- expandIncludes ts
return $ contents ++ rest return $ contents ++ rest
expandIncludes (Token m (IncludeFile _) : _) expandIncludes (Token m (IncludeFile _) : _)
@ -414,7 +419,8 @@ runPreprocParser m prod s
-- | Load and preprocess an occam program. -- | Load and preprocess an occam program.
preprocessOccamProgram :: String -> PassM [Token] preprocessOccamProgram :: String -> PassM [Token]
preprocessOccamProgram filename preprocessOccamProgram filename
= do toks <- preprocessFile emptyMeta filename = do mods <- getCompState >>* csImplicitModules
toks <- preprocessFile emptyMeta mods filename
-- Leave the main file name in the csCurrentFile slot: -- Leave the main file name in the csCurrentFile slot:
modify $ \cs -> cs { csCurrentFile = filename } modify $ \cs -> cs { csCurrentFile = filename }
veryDebug $ "{{{ tokenised source" veryDebug $ "{{{ tokenised source"
@ -424,4 +430,4 @@ preprocessOccamProgram filename
-- | Preprocesses occam source direct from the given String -- | Preprocesses occam source direct from the given String
preprocessOccamSource :: String -> PassM [Token] preprocessOccamSource :: String -> PassM [Token]
preprocessOccamSource source = preprocessSource emptyMeta "<unknown>" source preprocessOccamSource source = preprocessSource emptyMeta [] "<unknown>" source