Commit 2885e21c by Zachary Snow

fix handling of preproc conditionals within macros

- preproc reads identifiers unconditionally
- drop leading whitespace for default macro args
- very minor preproc cleanup
parent c59334ce
...@@ -244,11 +244,18 @@ takeIdentifierFollow firstPass = do ...@@ -244,11 +244,18 @@ takeIdentifierFollow firstPass = do
where where
process :: (PPS ()) -> PPS String process :: (PPS ()) -> PPS String
process action = do process action = do
outputFollow <- getOutput -- save the state
outputOrig <- getOutput
condStackOrig <- getCondStack
-- process this chunk of the identifier
setOutput [] setOutput []
setCondStack []
() <- action () <- action
outputIdent <- getOutput outputIdent <- getOutput
setOutput outputFollow -- restore the previous state
setOutput outputOrig
setCondStack condStackOrig
-- move on to the next chunk
let ident = reverse $ map fst outputIdent let ident = reverse $ map fst outputIdent
identFollow <- takeIdentifierFollow False identFollow <- takeIdentifierFollow False
return $ ident ++ identFollow return $ ident ++ identFollow
...@@ -372,11 +379,11 @@ takeMacroDefinition = do ...@@ -372,11 +379,11 @@ takeMacroDefinition = do
else if null rest then else if null rest then
return (name, Nothing) return (name, Nothing)
else do else do
let trimmed = dropWhile isWhitespaceChar rest let leadCh : after = dropWhile isWhitespaceChar rest
let leadCh = head trimmed let value = dropWhile isWhitespaceChar after
if leadCh /= '=' if leadCh /= '='
then lexicalError $ "bad char after arg name: " ++ (show leadCh) then lexicalError $ "bad char after arg name: " ++ (show leadCh)
else return (name, Just $ tail trimmed) else return (name, Just value)
-- commas and right parens are forbidden outside matched pairs of: (), [], {}, -- commas and right parens are forbidden outside matched pairs of: (), [], {},
-- "", except to delimit arguments or end the list of arguments; see 22.5.1 -- "", except to delimit arguments or end the list of arguments; see 22.5.1
...@@ -811,7 +818,6 @@ handleDirective macrosOnly = do ...@@ -811,7 +818,6 @@ handleDirective macrosOnly = do
setBuffer (body, pos) setBuffer (body, pos)
preprocessInput preprocessInput
"" <- getInput "" <- getInput
setMacroStack $ error $ show $ (zip names args) : macroStack
-- return to the rest of the input -- return to the rest of the input
setMacroStack macroStack setMacroStack macroStack
setBuffer bufFollow setBuffer bufFollow
...@@ -830,7 +836,7 @@ lineLookahead :: PPS () ...@@ -830,7 +836,7 @@ lineLookahead :: PPS ()
lineLookahead = do lineLookahead = do
line <- takeUntilNewline line <- takeUntilNewline
-- save the state -- save the state
outputOrig <- gets ppOutput outputOrig <- getOutput
condStackOrig <- getCondStack condStackOrig <- getCondStack
inputOrig <- getInput inputOrig <- getInput
-- process the line -- process the line
...@@ -850,7 +856,7 @@ lineLookahead = do ...@@ -850,7 +856,7 @@ lineLookahead = do
preprocessString :: String -> PPS String preprocessString :: String -> PPS String
preprocessString str = do preprocessString str = do
-- save the state -- save the state
outputOrig <- gets ppOutput outputOrig <- getOutput
condStackOrig <- getCondStack condStackOrig <- getCondStack
bufferOrig <- getBuffer bufferOrig <- getBuffer
-- process the line -- process the line
......
`define FELSE `else
`define MACRO_A(flag, check1 = ifdef, check2 = else) \
initial $display( \
`check1 flag \
`"flag is check1'd`" \
`check2 \
`"flag is not check1'd`" \
`endif \
);
`define A
module top;
`MACRO_A(A)
`MACRO_A(B)
`MACRO_A(A, ifndef)
`MACRO_A(B, ifndef)
`MACRO_A(A, ifdef, FELSE)
`MACRO_A(B, ifdef, FELSE)
`MACRO_A(A, ifndef, FELSE)
`MACRO_A(B, ifndef, FELSE)
endmodule
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment