From 647adc1d776acba2492b0b976fdf651d32d30935 Mon Sep 17 00:00:00 2001 From: mattias Date: Wed, 26 Mar 2008 11:24:46 +0000 Subject: [PATCH] codetools: h2p: implemented converting c hex numbers git-svn-id: trunk@14648 - --- components/codetools/h2pastool.pas | 21 ++++++++++ components/codetools/nonpascalcodetools.pas | 45 ++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/components/codetools/h2pastool.pas b/components/codetools/h2pastool.pas index 8554e4c126..726717d0a5 100644 --- a/components/codetools/h2pastool.pas +++ b/components/codetools/h2pastool.pas @@ -1410,6 +1410,7 @@ function TH2PasTool.MacroValueIsConstant(Node: TH2PDirectiveNode; var AtomStart: integer; p: Integer; + CurAtom: String; begin Result:=false; PasType:=''; @@ -1418,11 +1419,31 @@ begin repeat ReadRawNextCAtom(PasExpression,p,AtomStart); if AtomStart>length(PasExpression) then break; + DebugLn(['TH2PasTool.MacroValueIsConstant AAA1 ',copy(PasExpression,AtomStart,p-AtomStart)]); if IsIdentStartChar[PasExpression[AtomStart]] then begin exit; + end else if IsCHexNumber(PasExpression,AtomStart) then begin + // hex number + // replace 0x with $ + DebugLn(['TH2PasTool.MacroValueIsConstant AAA2']); + PasExpression:=copy(PasExpression,1,AtomStart-1) + +'$'+copy(PasExpression,AtomStart+2,length(PasExpression)); + dec(p); + end else if IsCDecimalNumber(PasExpression,AtomStart) then begin + // decimal number end else if PasExpression[AtomStart]='"' then begin PasExpression[AtomStart]:=''''; PasExpression[p-1]:=''''; + end else begin + CurAtom:=copy(PasExpression,AtomStart,p-AtomStart); + if (CurAtom='(') or (CurAtom=')') + or (CurAtom='+') or (CurAtom='-') then begin + // same in pascal + end else begin + DebugLn(['TH2PasTool.MacroValueIsConstant AAA3 ',CurAtom]); + // unknown + exit; + end; end; until false; Result:=true; diff --git a/components/codetools/nonpascalcodetools.pas b/components/codetools/nonpascalcodetools.pas index 93e133dfff..d0183388db 100644 --- a/components/codetools/nonpascalcodetools.pas +++ b/components/codetools/nonpascalcodetools.pas @@ -42,7 +42,9 @@ procedure ReadNextCAtom(const Source: string; var Position: integer; out AtomStart: integer); procedure ReadRawNextCAtom(const Source: string; var Position: integer; out AtomStart: integer); - +function IsCDecimalNumber(const Source: string; Position: integer): boolean; +function IsCHexNumber(const Source: string; Position: integer): boolean; + function ExtractCodeFromMakefile(const Source: string): string; function CConstantToInt64(const s: string; out i: int64): boolean; @@ -273,7 +275,18 @@ begin inc(Position); end; '0'..'9': // number - begin + if (Source[Position+1]='x') then begin + inc(Position); + // hex number + repeat + inc(Position); + until (Position>Len) or (not IsHexNumberChar[Source[Position]]); + end else if (Source[Position+1] in ['0'..'7']) then begin + // octal number + repeat + inc(Position); + until (Position>Len) or (not (Source[Position] in ['0'..'7'])); + end else begin inc(Position); // read numbers while (Position<=Len) and (Source[Position] in ['0'..'9']) do @@ -345,6 +358,34 @@ begin {$IFDEF RangeChecking}{$R+}{$UNDEF RangeChecking}{$ENDIF} end; +function IsCDecimalNumber(const Source: string; Position: integer): boolean; +var + l: Integer; +begin + {$IFOPT R+}{$DEFINE RangeChecking}{$ENDIF} + {$R-} + Result:=false; + l:=length(Source); + if (Position<1) or (Position>l) or (not IsNumberChar[Source[Position]]) + then exit; + // check octal and hex number + if (Source[Position]='0') and (Source[Position] in ['x','0'..'9']) + then exit; + // check float + inc(Position); + while (Position<=l) and (IsNumberChar[Source[Position]]) do + inc(Position); + if Source[Position]='.' then exit; + Result:=true; + {$IFDEF RangeChecking}{$R+}{$UNDEF RangeChecking}{$ENDIF} +end; + +function IsCHexNumber(const Source: string; Position: integer): boolean; +begin + Result:=(Position>=1) and (Position