Skip to content

Commit

Permalink
@TetzkatLipHoka: $IF Support
Browse files Browse the repository at this point in the history
  • Loading branch information
pult committed Nov 23, 2022
1 parent 2300851 commit 1f1ed85
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 17 deletions.
135 changes: 121 additions & 14 deletions Source/uPSPreProcessor.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ uPSPreProcessor.pas } // version: 2022.1123.1400
{ uPSPreProcessor.pas } // version: 2022.1123.1500
{----------------------------------------------------------------------------}
{ RemObjects Pascal Script }
{----------------------------------------------------------------------------}
Expand Down Expand Up @@ -175,10 +175,12 @@ TPSDefineStates = class
implementation

{+}
{$IFDEF DELPHI12UP}
uses
AnsiStrings;
{$ENDIF}
StrUtils
{$IFDEF DELPHI12UP}
,AnsiStrings
{$ENDIF}
;
{+.}

{$if (defined(DELPHI3UP) or defined(FPC))}
Expand All @@ -192,6 +194,8 @@ implementation
RPS_IncludeNotFoundFrom = ' used from ''%s''';
{+.}
RPS_DefineTooManyParameters = 'Too many parameters in ''%s'' at %d:%d';
RPS_DefineTooLessParameters = 'Too less parameters in ''%s'' at %d:%d';
RPS_DefineInvalidParameters = 'Invalid parameters in ''%s'' at %d:%d';
RPS_NoIfdefForEndif = 'No IFDEF for ENDIF in ''%s'' at %d:%d';
RPS_NoIfdefForElse = 'No IFDEF for ELSE in ''%s'' at %d:%d';
RPS_ElseTwice = 'Can''t use ELSE twice in ''%s'' at %d:%d';
Expand Down Expand Up @@ -578,15 +582,19 @@ procedure TPSPreProcessor.doAddStdPredefines;

procedure TPSPreProcessor.IntPreProcess(Level: Integer; const OrgFileName, AFileName: TbtString;
Dest: TStream);
const // TetzkatLipHoka: implement "$IF Defined" https://github.com/remobjects/pascalscript/pull/265
sDEFINED = 'DEFINED(';
sNOT = 'NOT';
sAND = 'AND';
sOR = 'OR';
sANDNOT = 'ANDNOT';
sORNOT = 'ORNOT';
sCompilerVersion = 'COMPILERVERSION';
var
Parser: TPSPascalPreProcessorParser;
FileName, dta: TbtString;
item: TPSLineInfo;
s, name: TbtString;
current, i: Longint;
ds: TPSDefineState;
AppContinue: Boolean;
ADoWrite: Boolean;
dta: TbtString; item: TPSLineInfo; FileName, s, ts, name: TbtString;
current, i, j: Longint; ds: TPSDefineState;
OK, AppContinue, ADoWrite: Boolean;
begin
FileName := AFileName;
if Level > MaxLevel then
Expand Down Expand Up @@ -677,7 +685,7 @@ procedure TPSPreProcessor.IntPreProcess(Level: Integer; const OrgFileName, AFile
ADoWrite := (FCurrentDefines.IndexOf(UpperCase(string(s))) < 0) and FDefineState.DoWrite;
FDefineState.Add.DoWrite := ADoWrite;
{+.}
end else if (Name = 'ENDIF') then begin
end else if (Name = 'ENDIF') or (Name = 'IFEND') then begin
//- jgv remove - borland use it (sysutils.pas)
//- if s <> '' then raise EPSPreProcessor.CreateFmt(RPS_DefineTooManyParameters, [string(FileName), Parser.Row, Parser.Col]);
if FDefineState.Count = 0 then
Expand All @@ -694,8 +702,107 @@ procedure TPSPreProcessor.IntPreProcess(Level: Integer; const OrgFileName, AFile
ds.FInElse := True;
//JeromeWelsh - nesting fix
ds.DoWrite := not ds.DoWrite and FDefineState.DoPrevWrite;
end
else begin
end else if (Name = 'IF') then begin
OK := PosT(' ', S) <= 0;
if OK then
raise EPSPreProcessor.CreateFmt(RPS_DefineTooLessParameters, [FileName, Parser.Row, Parser.Col]);
S := Trim(S);
S := UpperCase(S);
S := StringReplaceT(S, #32#32, #32, [rfReplaceAll]);
S := StringReplaceT(S, ' (', '(', [rfReplaceAll]);
S := StringReplaceT(S, '( ', '(', [rfReplaceAll]);
S := StringReplaceT(S, ' )', ')', [rfReplaceAll]);
S := StringReplaceT(S, ') ', ')', [rfReplaceAll]);
OK := (Copy(S, 1, Length(sDEFINED))=sDEFINED)
or (Copy(S, 1, Length(sNOT) + Length(sDEFINED)) = sNOT + sDEFINED)
or (Copy(S, 1, Length(sNOT) + Length(sDEFINED) + 1 ) = sNOT + ' ' + sDEFINED)
;
if OK then begin // MS
S := StringReplaceT(S, ' NOT', 'NOT', [rfReplaceAll]);
S := StringReplaceT(S, 'NOT ', 'NOT', [rfReplaceAll]);
S := StringReplaceT(S, ' AND', 'AND', [rfReplaceAll]);
S := StringReplaceT(S, 'AND ', 'AND', [rfReplaceAll]);
S := StringReplaceT(S, ' OR', 'OR', [rfReplaceAll]);
S := StringReplaceT(S, 'OR ', 'OR', [rfReplaceAll]);
ADoWrite := FDefineState.DoWrite;
ts := S;
if (Copy(ts, 1, Length(sNOT)) = sNOT) then begin
j := 2;
ts := Copy(ts, Length(sNOT)+1, Length(ts)-Length(sNOT));
end else
j := 0; // AND
while (ts <> '') do begin
i := PosExT(')', ts, Length(sDEFINED)+1);
if (i = 0) then begin
raise EPSPreProcessor.CreateFmt(RPS_DefineInvalidParameters, [FileName, Parser.Row, Parser.Col]);
Break;
end;
case j of
0: // AND
ADoWrite := ADoWrite and
(FCurrentDefines.IndexOf(string(Copy(ts, Length(sDefined)+1, i-Length(sDefined)-1))) >= 0);
1: // OR
ADoWrite := ADoWrite or
(FCurrentDefines.IndexOf(string(Copy(ts, Length(sDefined)+1, i-Length(sDefined)-1))) >= 0);
2: // (AND) NOT
ADoWrite := ADoWrite and
(FCurrentDefines.IndexOf(string(Copy(ts, Length(sDefined)+1, i-Length(sDefined)-1))) < 0);
3: // OR NOT
ADoWrite := ADoWrite or
(FCurrentDefines.IndexOf(string(Copy(ts, Length(sDefined)+1, i-Length(sDefined)-1))) < 0);
else
ADoWrite := ADoWrite or
(FCurrentDefines.IndexOf(string(Copy(ts, Length(sDefined)+1, i-Length(sDefined)-1))) >= 0);
end; //case j
ts := Copy(ts, i+1, Length( ts )-i);
if (Copy(ts, 1, Length(sANDNOT)) = sANDNOT ) then begin
j := 2;
ts := Copy(ts, Length(sANDNOT)+1, Length(ts)-Length(sANDNOT));
end else if (Copy(ts, 1, Length(sORNOT)) = sORNOT) then begin
j := 3;
ts := Copy(ts, Length(sORNOT)+1, Length(ts)-Length(sORNOT));
end else if (Copy(ts, 1, Length(sAND)) = sAND) then begin
j := 0;
ts := Copy(ts, Length(sAND)+1, Length(ts)-Length(sAND));
end else if (Copy(ts, 1, Length(sOR)) = sOR) then begin
j := 1;
ts := Copy(ts, Length(sOR)+1, Length(ts)-Length(sOR));
end;
end; // "while (ts <> '')"
FDefineState.Add.DoWrite := ADoWrite;
end else if (Copy(S, 1, Length(sCompilerVersion)) = sCompilerVersion) then begin
S := StringReplaceT(S, #32, '', [rfReplaceAll]);
if (Copy(S, 16, 2) = '>=') then
FDefineState.Add.DoWrite := ( StrToIntDef(Copy(S, 18, Length(S)-17), -1 ) >= CompilerVersion )
else if (Copy(S, 16, 2) = '<=') then
FDefineState.Add.DoWrite := ( StrToIntDef(Copy(S, 18, Length(S)-17), High( Integer ) ) <= CompilerVersion )
else if (Copy(S, 16, 1) = '<') then
FDefineState.Add.DoWrite := ( StrToIntDef(Copy(S, 17, Length(S)-16), High( Integer ) ) < CompilerVersion )
else if (Copy(S, 16, 1) = '>') then
FDefineState.Add.DoWrite := ( StrToIntDef(Copy(S, 17, Length(S)-16), -1 ) > CompilerVersion )
else if (Copy(S, 16, 1) = '=') then
FDefineState.Add.DoWrite := ( StrToIntDef(Copy(S, 17, Length(S)-16), -1 ) = CompilerVersion )
else
raise EPSPreProcessor.CreateFmt(RPS_DefineInvalidParameters, [FileName, Parser.Row, Parser.Col]);
end else begin
//-- jgv: 20050710 custom application error process
if @OnProcessUnknowDirective <> nil then begin
OnProcessUnknowDirective(Self, Parser, FDefineState.DoWrite, name, {+}TbtString(s){+.}, AppContinue);
end;
if AppContinue then
//-- jgv.
raise EPSPreProcessor.CreateFmt(RPS_UnknownCompilerDirective, [string(FileName), Parser.Row, Parser.Col]);
end;
// Compatibility Dummys
end else if (Name = 'UNSAFE_TYPE') or (Name = 'UNSAFE_CODE') or (Name = 'UNSAFE_CAST') OR (Name = 'SYMBOL_PLATFORM')
or (Name = 'GARBAGE') or (Name = 'WARN') or (Name = 'RANGECHECKS')
or (Name = 'WEAKPACKAGEUNIT') or (Name = 'EXTERNALSYM') or (Name = 'NODEFINE')
then begin
SetLength(s, Length(Parser.Token));
for i := Length(s) downto 1 do begin
s[i] := #32; // space
end;
end else begin
//-- jgv: 20050710 custom application error process
if @OnProcessUnknowDirective <> nil then begin
OnProcessUnknowDirective(Self, Parser, FDefineState.DoWrite, name, {+}TbtString(s){+.}, AppContinue);
Expand Down
6 changes: 3 additions & 3 deletions Source/uPSUtils.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ uPSUtils.pas } // version: 2022.1123.1400
{ uPSUtils.pas } // version: 2022.1123.1500
{----------------------------------------------------------------------------}
{ RemObjects Pascal Script }
{----------------------------------------------------------------------------}
Expand All @@ -18,15 +18,15 @@ interface

{+}
const
uPSVersion = 202211231400; // format: yyyymmddhhnn
uPSVersion = 202211231500; // format: yyyymmddhhnn
// yyyymmddhhnn
{$EXTERNALSYM uPSVersion}
(*
// Sample for checking library version:
// <sample>
uses ... uPSUtils ...
{$warn comparison_true off}
{$if (not declared(uPSVersion)) or (uPSVersion < 202211231400)}
{$if (not declared(uPSVersion)) or (uPSVersion < 202211231500)}
//{$warn message_directive on}{$MESSAGE WARN 'Need update RemObjects Pascal Script Library'}
{$MESSAGE FATAL 'Need update RemObjects Pascal Script Library'}
{$ifend}{$warnings on}
Expand Down

0 comments on commit 1f1ed85

Please sign in to comment.