mirror of
https://github.com/byteworksinc/ORCA-C.git
synced 2025-03-06 21:30:27 +00:00
Don't inappropriately re-expand a macro's name when it appears within the macro.
This should implement the C standard rules about making macro name tokens ineligible for replacement, except that it does not currently handle cases of nested replacements (such as a cycle of mutually-referential macros). This fixes #12. There are still a couple other bugs with macro expansion in obscure cases, but I'll consider them separate issues.
This commit is contained in:
parent
bfb929f4a7
commit
5152790b00
22
Scanner.pas
22
Scanner.pas
@ -248,6 +248,7 @@ var
|
|||||||
tokenList: tokenListRecordPtr; {token putback buffer}
|
tokenList: tokenListRecordPtr; {token putback buffer}
|
||||||
tokenStart: ptr; {pointer to the first char in the token}
|
tokenStart: ptr; {pointer to the first char in the token}
|
||||||
tokenEnd: ptr; {pointer to the first char past the token}
|
tokenEnd: ptr; {pointer to the first char past the token}
|
||||||
|
tokenExpandEnabled: boolean; {can token be macro expanded? (only for ident)}
|
||||||
versionStrL: longStringPtr; {macro version string}
|
versionStrL: longStringPtr; {macro version string}
|
||||||
workString: pstring; {for building strings and identifiers}
|
workString: pstring; {for building strings and identifiers}
|
||||||
|
|
||||||
@ -1300,6 +1301,7 @@ if macro^.parameters >= 0 then begin {find the values of the parameters}
|
|||||||
tPtr^.token := token;
|
tPtr^.token := token;
|
||||||
tPtr^.tokenStart := tokenStart;
|
tPtr^.tokenStart := tokenStart;
|
||||||
tPtr^.tokenEnd := tokenEnd;
|
tPtr^.tokenEnd := tokenEnd;
|
||||||
|
tPtr^.expandEnabled := tokenExpandEnabled;
|
||||||
if token.kind = lparench then
|
if token.kind = lparench then
|
||||||
parenCount := parenCount+1
|
parenCount := parenCount+1
|
||||||
else if token.kind = rparench then
|
else if token.kind = rparench then
|
||||||
@ -1442,10 +1444,20 @@ else begin
|
|||||||
if lastPtr <> nil then
|
if lastPtr <> nil then
|
||||||
if lastPtr^.token.kind = poundpoundop then
|
if lastPtr^.token.kind = poundpoundop then
|
||||||
inhibit := true;
|
inhibit := true;
|
||||||
|
if not tcPtr^.expandEnabled then
|
||||||
|
inhibit := true;
|
||||||
|
if tcPtr = pptr^.tokens then
|
||||||
|
if (mPtr <> nil) and (mPtr^.parameters > 0) then
|
||||||
|
inhibit := true;
|
||||||
if (mPtr <> nil) and (not inhibit) then
|
if (mPtr <> nil) and (not inhibit) then
|
||||||
Expand(mPtr)
|
Expand(mPtr)
|
||||||
else
|
else begin
|
||||||
PutBackToken(tcPtr^.token, true);
|
expandEnabled := tcPtr^.expandEnabled;
|
||||||
|
if expandEnabled then
|
||||||
|
if tcPtr^.token.name^ = macro^.name^ then
|
||||||
|
expandEnabled := false;
|
||||||
|
PutBackToken(tcPtr^.token, expandEnabled);
|
||||||
|
end; {else}
|
||||||
end {if}
|
end {if}
|
||||||
else
|
else
|
||||||
PutBackToken(tcPtr^.token, true);
|
PutBackToken(tcPtr^.token, true);
|
||||||
@ -2089,6 +2101,7 @@ var
|
|||||||
tPtr^.token := token;
|
tPtr^.token := token;
|
||||||
tPtr^.tokenStart := tokenStart;
|
tPtr^.tokenStart := tokenStart;
|
||||||
tPtr^.tokenEnd := tokenEnd;
|
tPtr^.tokenEnd := tokenEnd;
|
||||||
|
tPtr^.expandEnabled := true;
|
||||||
slen := ord(ord4(chPtr) - ord4(tokenStart));
|
slen := ord(ord4(chPtr) - ord4(tokenStart));
|
||||||
sptr := pointer(GMalloc(slen+2));
|
sptr := pointer(GMalloc(slen+2));
|
||||||
sptr^.length := slen;
|
sptr^.length := slen;
|
||||||
@ -3772,6 +3785,7 @@ if tokenList <> nil then begin {get a token put back by a macro}
|
|||||||
tPtr := tokenList;
|
tPtr := tokenList;
|
||||||
tokenList := tPtr^.next;
|
tokenList := tPtr^.next;
|
||||||
expandEnabled := tPtr^.expandEnabled;
|
expandEnabled := tPtr^.expandEnabled;
|
||||||
|
tokenExpandEnabled := expandEnabled;
|
||||||
token := tPtr^.token;
|
token := tPtr^.token;
|
||||||
tokenStart := tPtr^.tokenStart;
|
tokenStart := tPtr^.tokenStart;
|
||||||
tokenEnd := tPtr^.tokenEnd;
|
tokenEnd := tPtr^.tokenEnd;
|
||||||
@ -3825,6 +3839,7 @@ if tokenList <> nil then begin {get a token put back by a macro}
|
|||||||
Merge(tToken, tPtr^.token);
|
Merge(tToken, tPtr^.token);
|
||||||
tokenList := tPtr^.next;
|
tokenList := tPtr^.next;
|
||||||
token := tToken;
|
token := tToken;
|
||||||
|
tokenExpandEnabled := true;
|
||||||
dispose(tPtr);
|
dispose(tPtr);
|
||||||
goto 4;
|
goto 4;
|
||||||
end; {if}
|
end; {if}
|
||||||
@ -4112,6 +4127,7 @@ case charKinds[ord(ch)] of
|
|||||||
token.kind := ident;
|
token.kind := ident;
|
||||||
token.class := identifier;
|
token.class := identifier;
|
||||||
token.name := @workString;
|
token.name := @workString;
|
||||||
|
tokenExpandEnabled := true;
|
||||||
i := 0;
|
i := 0;
|
||||||
while charKinds[ord(ch)] in [letter,digit] do begin
|
while charKinds[ord(ch)] in [letter,digit] do begin
|
||||||
i := i+1;
|
i := i+1;
|
||||||
@ -4150,7 +4166,7 @@ if token.kind = stringconst then {handle adjacent strings}
|
|||||||
done := false;
|
done := false;
|
||||||
end {if}
|
end {if}
|
||||||
else begin
|
else begin
|
||||||
PutBackToken(token, true);
|
PutBackToken(token, tokenExpandEnabled);
|
||||||
done := true;
|
done := true;
|
||||||
end; {else}
|
end; {else}
|
||||||
token := tToken;
|
token := tToken;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user