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 [] ["run-indent"] (NoArg $ optRunIndent) "run indent on source before compilation (will full mode)"
, 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 [] ["lib-path"] (NoArg $ optPrintPath tockLibDir) "print lib path"
, 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, []) -> [p]
optImplicitModule :: String -> OptFunc
optImplicitModule s ps = return $ ps { csImplicitModules = csImplicitModules ps ++ [s] }
optCompilerFlags :: String -> OptFunc
optCompilerFlags flags ps = return $ ps { csCompilerFlags = flags ++ " " ++ csCompilerFlags ps}

View File

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

View File

@ -42,8 +42,8 @@ import StructureOccam
import Utils
-- | Preprocess a file and return its tokenised form ready for parsing.
preprocessFile :: Meta -> String -> PassM [Token]
preprocessFile m filename
preprocessFile :: Meta -> [String] -> String -> PassM [Token]
preprocessFile m implicitMods filename
= do (handle, realFilename) <- searchFile m filename
progress $ "Loading source file " ++ realFilename
origCS <- get
@ -54,7 +54,7 @@ preprocessFile m filename
modify (\cs -> cs { csCurrentFile = realFilename
, csUsedFiles = modFunc $ csUsedFiles cs })
s <- liftIO $ hGetContents handle
toks <- preprocessSource m realFilename s
toks <- preprocessSource m implicitMods realFilename s
modify (\cs -> cs { csCurrentFile = csCurrentFile origCS })
return toks
where
@ -62,13 +62,13 @@ preprocessFile m filename
drop9 = reverse . drop 9 . reverse
-- | Preprocesses source directly and returns its tokenised form ready for parsing.
preprocessSource :: Meta -> String -> String -> PassM [Token]
preprocessSource m realFilename s
preprocessSource :: Meta -> [String] -> String -> String -> PassM [Token]
preprocessSource m implicitMods realFilename s
= do toks <- runLexer realFilename $ removeASM s
veryDebug $ "{{{ lexer tokens"
veryDebug $ pshow toks
veryDebug $ "}}}"
toks' <- preprocessOccam toks
toks' <- preprocessOccam $ incImplicit ++ toks
veryDebug $ "{{{ preprocessed tokens"
veryDebug $ pshow toks'
veryDebug $ "}}}"
@ -78,6 +78,11 @@ preprocessSource m realFilename s
veryDebug $ "}}}"
expandIncludes toks''
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
-- 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
@ -119,7 +124,7 @@ preprocessSource m realFilename s
expandIncludes :: [Token] -> PassM [Token]
expandIncludes [] = return []
expandIncludes (Token m (IncludeFile filename) : Token _ EndOfLine : ts)
= do contents <- preprocessFile m filename
= do contents <- preprocessFile m [] filename
rest <- expandIncludes ts
return $ contents ++ rest
expandIncludes (Token m (IncludeFile _) : _)
@ -414,7 +419,8 @@ runPreprocParser m prod s
-- | Load and preprocess an occam program.
preprocessOccamProgram :: String -> PassM [Token]
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:
modify $ \cs -> cs { csCurrentFile = filename }
veryDebug $ "{{{ tokenised source"
@ -424,4 +430,4 @@ preprocessOccamProgram filename
-- | Preprocesses occam source direct from the given String
preprocessOccamSource :: String -> PassM [Token]
preprocessOccamSource source = preprocessSource emptyMeta "<unknown>" source
preprocessOccamSource source = preprocessSource emptyMeta [] "<unknown>" source