瀏覽代碼

* use val() for decoding integers

peter 20 年之前
父節點
當前提交
79c20ebf7d
共有 8 個文件被更改,包括 93 次插入166 次删除
  1. 5 42
      compiler/cutils.pas
  2. 9 6
      compiler/i386/ra386int.pas
  3. 6 3
      compiler/options.pas
  4. 5 2
      compiler/pexpr.pas
  5. 9 6
      compiler/raatt.pas
  6. 39 93
      compiler/rautils.pas
  7. 7 4
      compiler/scandir.pas
  8. 13 10
      compiler/scanner.pas

+ 5 - 42
compiler/cutils.pas

@@ -77,7 +77,6 @@ interface
     function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
     function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
     function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
     function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
     function DStr(l:longint):string;
     function DStr(l:longint):string;
-    procedure valint(S : string;var V : longint;var code : integer);
     {# Returns true if the string s is a number }
     {# Returns true if the string s is a number }
     function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
     function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
     {# Returns true if value is a power of 2, the actual
     {# Returns true if value is a power of 2, the actual
@@ -604,45 +603,6 @@ uses
      end;
      end;
 
 
 
 
-    procedure valint(S : string;var V : longint;var code : integer);
-    {
-      val() with support for octal, which is not supported under tp7
-    }
-{$ifndef FPC}
-      var
-        vs : longint;
-        c  : byte;
-      begin
-        if s[1]='%' then
-          begin
-             vs:=0;
-             longint(v):=0;
-             for c:=2 to length(s) do
-               begin
-                  if s[c]='0' then
-                    vs:=vs shl 1
-                  else
-                  if s[c]='1' then
-                    vs:=vs shl 1+1
-                  else
-                    begin
-                      code:=c;
-                      exit;
-                    end;
-               end;
-             code:=0;
-             longint(v):=vs;
-          end
-        else
-         system.val(S,V,code);
-      end;
-{$else not FPC}
-      begin
-         system.val(S,V,code);
-      end;
-{$endif not FPC}
-
-
     function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
     function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
     {
     {
       is string a correct number ?
       is string a correct number ?
@@ -651,7 +611,7 @@ uses
          w : integer;
          w : integer;
          l : longint;
          l : longint;
       begin
       begin
-         valint(s,l,w);
+         val(s,l,w);
          is_number:=(w=0);
          is_number:=(w=0);
       end;
       end;
 
 
@@ -1264,7 +1224,10 @@ initialization
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.47  2004-11-15 23:35:31  peter
+  Revision 1.48  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.47  2004/11/15 23:35:31  peter
     * tparaitem removed, use tparavarsym instead
     * tparaitem removed, use tparavarsym instead
     * parameter order is now calculated from paranr value in tparavarsym
     * parameter order is now calculated from paranr value in tparavarsym
 
 

+ 9 - 6
compiler/i386/ra386int.pas

@@ -515,7 +515,7 @@ Unit Ra386int;
                     actasmpattern:=actasmpattern + c;
                     actasmpattern:=actasmpattern + c;
                     c:=current_scanner.asmgetchar;
                     c:=current_scanner.asmgetchar;
                   end;
                   end;
-                 actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
+                 actasmpattern:=tostr(ParseVal(actasmpattern,16));
                  actasmtoken:=AS_INTNUM;
                  actasmtoken:=AS_INTNUM;
                  exit;
                  exit;
                end;
                end;
@@ -615,7 +615,7 @@ Unit Ra386int;
                   Begin
                   Begin
                     { Delete the last binary specifier }
                     { Delete the last binary specifier }
                     delete(actasmpattern,length(actasmpattern),1);
                     delete(actasmpattern,length(actasmpattern),1);
-                    actasmpattern:=tostr(ValBinary(actasmpattern));
+                    actasmpattern:=tostr(ParseVal(actasmpattern,2));
                     actasmtoken:=AS_INTNUM;
                     actasmtoken:=AS_INTNUM;
                     exit;
                     exit;
                   end
                   end
@@ -624,21 +624,21 @@ Unit Ra386int;
                     case c of
                     case c of
                       'O' :
                       'O' :
                         Begin
                         Begin
-                          actasmpattern:=tostr(ValOctal(actasmpattern));
+                          actasmpattern:=tostr(ParseVal(actasmpattern,8));
                           actasmtoken:=AS_INTNUM;
                           actasmtoken:=AS_INTNUM;
                           c:=current_scanner.asmgetchar;
                           c:=current_scanner.asmgetchar;
                           exit;
                           exit;
                         end;
                         end;
                       'H' :
                       'H' :
                         Begin
                         Begin
-                          actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
+                          actasmpattern:=tostr(ParseVal(actasmpattern,16));
                           actasmtoken:=AS_INTNUM;
                           actasmtoken:=AS_INTNUM;
                           c:=current_scanner.asmgetchar;
                           c:=current_scanner.asmgetchar;
                           exit;
                           exit;
                         end;
                         end;
                       else { must be an integer number }
                       else { must be an integer number }
                         begin
                         begin
-                          actasmpattern:=tostr(ValDecimal(actasmpattern));
+                          actasmpattern:=tostr(ParseVal(actasmpattern,10));
                           actasmtoken:=AS_INTNUM;
                           actasmtoken:=AS_INTNUM;
                           exit;
                           exit;
                         end;
                         end;
@@ -2036,7 +2036,10 @@ begin
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.85  2005-01-19 22:19:41  peter
+  Revision 1.86  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.85  2005/01/19 22:19:41  peter
     * unit mapping rewrite
     * unit mapping rewrite
     * new derefmap added
     * new derefmap added
 
 

+ 6 - 3
compiler/options.pas

@@ -591,7 +591,7 @@ begin
                         error:=0;
                         error:=0;
                         if l>0 then
                         if l>0 then
                          begin
                          begin
-                           valint(copy(dllversion,l+1,255),minor,error);
+                           val(copy(dllversion,l+1,255),minor,error);
                            if (error=0) and
                            if (error=0) and
                               (minor>=0) and (minor<=$ffff) then
                               (minor>=0) and (minor<=$ffff) then
                              dllminor:=minor
                              dllminor:=minor
@@ -603,7 +603,7 @@ begin
                           l:=256;
                           l:=256;
                         dllmajor:=1;
                         dllmajor:=1;
                         if error=0 then
                         if error=0 then
-                          valint(copy(dllversion,1,l-1),major,error);
+                          val(copy(dllversion,1,l-1),major,error);
                         if (error=0) and (major>=0) and (major<=$ffff) then
                         if (error=0) and (major>=0) and (major<=$ffff) then
                           dllmajor:=major
                           dllmajor:=major
                         else
                         else
@@ -2089,7 +2089,10 @@ finalization
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.161  2005-01-09 20:24:43  olle
+  Revision 1.162  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.161  2005/01/09 20:24:43  olle
     * rework of macro subsystem
     * rework of macro subsystem
     + exportable macros for mode macpas
     + exportable macros for mode macpas
 
 

+ 5 - 2
compiler/pexpr.pas

@@ -2017,7 +2017,7 @@ implementation
                else
                else
                  begin
                  begin
                    { then longint }
                    { then longint }
-                   valint(pattern,l,code);
+                   val(pattern,l,code);
                    if code = 0 then
                    if code = 0 then
                      begin
                      begin
                        consume(_INTCONST);
                        consume(_INTCONST);
@@ -2527,7 +2527,10 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.178  2005-01-04 16:38:07  peter
+  Revision 1.179  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.178  2005/01/04 16:38:07  peter
     * don't allow properties in C style operators
     * don't allow properties in C style operators
 
 
   Revision 1.177  2004/12/26 16:22:01  peter
   Revision 1.177  2004/12/26 16:22:01  peter

+ 9 - 6
compiler/raatt.pas

@@ -400,7 +400,7 @@ unit raatt;
                     c:=current_scanner.asmgetchar;
                     c:=current_scanner.asmgetchar;
                   end;
                   end;
                  actasmpattern[0]:=chr(len);
                  actasmpattern[0]:=chr(len);
-                 actasmpattern:=tostr(ValDecimal(actasmpattern));
+                 actasmpattern:=tostr(ParseVal(actasmpattern,10));
                  actasmtoken:=AS_INTNUM;
                  actasmtoken:=AS_INTNUM;
                  exit;
                  exit;
                end;
                end;
@@ -417,7 +417,7 @@ unit raatt;
                           actasmpattern:=actasmpattern + c;
                           actasmpattern:=actasmpattern + c;
                           c:=current_scanner.asmgetchar;
                           c:=current_scanner.asmgetchar;
                         end;
                         end;
-                       actasmpattern:=tostr(ValBinary(actasmpattern));
+                       actasmpattern:=tostr(ParseVal(actasmpattern,2));
                        actasmtoken:=AS_INTNUM;
                        actasmtoken:=AS_INTNUM;
                        exit;
                        exit;
                      end;
                      end;
@@ -478,7 +478,7 @@ unit raatt;
                           actasmpattern:=actasmpattern + c;
                           actasmpattern:=actasmpattern + c;
                           c:=current_scanner.asmgetchar;
                           c:=current_scanner.asmgetchar;
                         end;
                         end;
-                       actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
+                       actasmpattern:=tostr(ParseVal(actasmpattern,16));
                        actasmtoken:=AS_INTNUM;
                        actasmtoken:=AS_INTNUM;
                        exit;
                        exit;
                      end;
                      end;
@@ -490,13 +490,13 @@ unit raatt;
                           actasmpattern:=actasmpattern + c;
                           actasmpattern:=actasmpattern + c;
                           c:=current_scanner.asmgetchar;
                           c:=current_scanner.asmgetchar;
                         end;
                         end;
-                       actasmpattern:=tostr(ValOctal(actasmpattern));
+                       actasmpattern:=tostr(ParseVal(actasmpattern,8));
                        actasmtoken:=AS_INTNUM;
                        actasmtoken:=AS_INTNUM;
                        exit;
                        exit;
                      end;
                      end;
                    else { octal number zero value...}
                    else { octal number zero value...}
                      Begin
                      Begin
-                       actasmpattern:=tostr(ValOctal(actasmpattern));
+                       actasmpattern:=tostr(ParseVal(actasmpattern,8));
                        actasmtoken:=AS_INTNUM;
                        actasmtoken:=AS_INTNUM;
                        exit;
                        exit;
                      end;
                      end;
@@ -1523,7 +1523,10 @@ end.
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.17  2005-01-19 22:19:41  peter
+  Revision 1.18  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.17  2005/01/19 22:19:41  peter
     * unit mapping rewrite
     * unit mapping rewrite
     * new derefmap added
     * new derefmap added
 
 

+ 39 - 93
compiler/rautils.pas

@@ -175,10 +175,7 @@ type
   {                     String routines                                 }
   {                     String routines                                 }
   {---------------------------------------------------------------------}
   {---------------------------------------------------------------------}
 
 
-Function ValDecimal(const S:String):aint;
-Function ValOctal(const S:String):aint;
-Function ValBinary(const S:String):aint;
-Function ValHexaDecimal(const S:String):aint;
+Function ParseVal(const S:String;base:byte):aint;
 Function PadZero(Var s: String; n: byte): Boolean;
 Function PadZero(Var s: String; n: byte): Boolean;
 Function EscapeToPascal(const s:string): string;
 Function EscapeToPascal(const s:string): string;
 
 
@@ -543,14 +540,14 @@ Begin
              temp:=temp+s[i+1];
              temp:=temp+s[i+1];
              temp:=temp+s[i+2];
              temp:=temp+s[i+2];
              inc(i,2);
              inc(i,2);
-             c:=chr(ValOctal(temp));
+             c:=chr(ParseVal(temp,8));
            end;
            end;
          'x':
          'x':
            Begin
            Begin
              temp:=s[i+1];
              temp:=s[i+1];
              temp:=temp+s[i+2];
              temp:=temp+s[i+2];
              inc(i,2);
              inc(i,2);
-             c:=chr(ValHexaDecimal(temp));
+             c:=chr(ParseVal(temp,16));
            end;
            end;
          else
          else
            Begin
            Begin
@@ -569,101 +566,47 @@ Begin
 end;
 end;
 
 
 
 
-Function ValDecimal(const S:String):aint;
+Function ParseVal(const S:String;base:byte):aint;
 { Converts a decimal string to aint }
 { Converts a decimal string to aint }
 var
 var
-  vs : aint;
-  c : longint;
+  code : integer;
+  errmsg : word;
+  prefix : string[2];
 Begin
 Begin
-  vs:=0;
-  for c:=1 to length(s) do
-   begin
-     vs:=vs*10;
-     if s[c] in ['0'..'9'] then
-      inc(vs,ord(s[c])-ord('0'))
-     else
+  case base of
+    2 :
       begin
       begin
-        Message1(asmr_e_error_converting_decimal,s);
-        ValDecimal:=0;
-        exit;
+        errmsg:=asmr_e_error_converting_binary;
+        prefix:='%';
       end;
       end;
-   end;
-  ValDecimal:=vs;
-end;
-
-
-Function ValOctal(const S:String):aint;
-{ Converts an octal string to aint }
-var
-  vs : aint;
-  c : longint;
-Begin
-  vs:=0;
-  for c:=1 to length(s) do
-   begin
-     vs:=vs shl 3;
-     if s[c] in ['0'..'7'] then
-      inc(vs,ord(s[c])-ord('0'))
-     else
+    8 :
       begin
       begin
-        Message1(asmr_e_error_converting_octal,s);
-        ValOctal:=0;
-        exit;
+        errmsg:=asmr_e_error_converting_octal;
+        prefix:='&';
       end;
       end;
-   end;
-  ValOctal:=vs;
-end;
-
-
-Function ValBinary(const S:String):aint;
-{ Converts a binary string to aint }
-var
-  vs : aint;
-  c : longint;
-Begin
-  vs:=0;
-  for c:=1 to length(s) do
-   begin
-     vs:=vs shl 1;
-     if s[c] in ['0'..'1'] then
-      inc(vs,ord(s[c])-ord('0'))
-     else
+    10 :
       begin
       begin
-        Message1(asmr_e_error_converting_binary,s);
-        ValBinary:=0;
-        exit;
+        errmsg:=asmr_e_error_converting_decimal;
+        prefix:='';
       end;
       end;
-   end;
-  ValBinary:=vs;
-end;
-
-
-Function ValHexadecimal(const S:String):aint;
-{ Converts a binary string to aint }
-var
-  vs : aint;
-  c : longint;
-Begin
-  vs:=0;
-  for c:=1 to length(s) do
-   begin
-     vs:=vs shl 4;
-     case s[c] of
-       '0'..'9' :
-         inc(vs,ord(s[c])-ord('0'));
-       'A'..'F' :
-         inc(vs,ord(s[c])-ord('A')+10);
-       'a'..'f' :
-         inc(vs,ord(s[c])-ord('a')+10);
-       else
-         begin
-           Message1(asmr_e_error_converting_hexadecimal,s);
-           ValHexadecimal:=0;
-           exit;
-         end;
-     end;
-   end;
-  ValHexadecimal:=vs;
+    16 :
+      begin
+        errmsg:=asmr_e_error_converting_hexadecimal;
+        prefix:='$';
+      end;
+    else
+      internalerror(200501202);
+  end;
+  val(prefix+s,result,code);
+  if code<>0 then
+    begin
+      val(prefix+s,aword(result),code);
+      if code<>0 then
+        begin
+          Message1(errmsg,s);
+          result:=0;
+        end;
+    end;
 end;
 end;
 
 
 
 
@@ -1626,7 +1569,10 @@ end;
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.101  2005-01-19 22:19:41  peter
+  Revision 1.102  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.101  2005/01/19 22:19:41  peter
     * unit mapping rewrite
     * unit mapping rewrite
     * new derefmap added
     * new derefmap added
 
 

+ 7 - 4
compiler/scandir.pas

@@ -958,7 +958,7 @@ implementation
             major:=0;
             major:=0;
             minor:=0;
             minor:=0;
             revision:=0;
             revision:=0;
-            valint(pattern,major,error);
+            val(pattern,major,error);
             if (error<>0) or (major > high(word)) or (major < 0) then
             if (error<>0) or (major > high(word)) or (major < 0) then
               begin
               begin
                 Message1(scan_w_wrong_version_ignored,pattern);
                 Message1(scan_w_wrong_version_ignored,pattern);
@@ -968,7 +968,7 @@ implementation
               begin
               begin
                 current_scanner.readchar;
                 current_scanner.readchar;
                 current_scanner.readnumber;
                 current_scanner.readnumber;
-                valint(pattern,minor,error);
+                val(pattern,minor,error);
                 if (error<>0) or (minor > high(word)) or (minor < 0) then
                 if (error<>0) or (minor > high(word)) or (minor < 0) then
                   begin
                   begin
                     Message1(scan_w_wrong_version_ignored,tostr(major)+'.'+pattern);
                     Message1(scan_w_wrong_version_ignored,tostr(major)+'.'+pattern);
@@ -979,7 +979,7 @@ implementation
                   begin
                   begin
                      current_scanner.readchar;
                      current_scanner.readchar;
                      current_scanner.readnumber;
                      current_scanner.readnumber;
-                     valint(pattern,revision,error);
+                     val(pattern,revision,error);
                      if (error<>0) or (revision > high(word)) or (revision < 0) then
                      if (error<>0) or (revision > high(word)) or (revision < 0) then
                        begin
                        begin
                           Message1(scan_w_wrong_version_ignored,tostr(revision)+'.'+pattern);
                           Message1(scan_w_wrong_version_ignored,tostr(revision)+'.'+pattern);
@@ -1183,7 +1183,10 @@ begin
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.52  2005-01-18 15:44:43  peter
+  Revision 1.53  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.52  2005/01/18 15:44:43  peter
     * ignore more delphi directives
     * ignore more delphi directives
 
 
   Revision 1.51  2005/01/09 20:24:43  olle
   Revision 1.51  2005/01/09 20:24:43  olle

+ 13 - 10
compiler/scanner.pas

@@ -607,7 +607,7 @@ implementation
                   begin
                   begin
                     preproc_consume(_ID);
                     preproc_consume(_ID);
                     hs:=read_factor();
                     hs:=read_factor();
-                    valint(hs,l,w);
+                    val(hs,l,w);
                     if l<>0 then
                     if l<>0 then
                       read_factor:='0'
                       read_factor:='0'
                     else
                     else
@@ -656,8 +656,8 @@ implementation
               break;
               break;
             preproc_consume(_ID);
             preproc_consume(_ID);
             hs2:=read_factor;
             hs2:=read_factor;
-            valint(hs1,l1,w);
-            valint(hs2,l2,w);
+            val(hs1,l1,w);
+            val(hs2,l2,w);
             if (l1<>0) and (l2<>0) then
             if (l1<>0) and (l2<>0) then
               hs1:='1'
               hs1:='1'
             else
             else
@@ -681,8 +681,8 @@ implementation
               break;
               break;
             preproc_consume(_ID);
             preproc_consume(_ID);
             hs2:=read_term;
             hs2:=read_term;
-            valint(hs1,l1,w);
-            valint(hs2,l2,w);
+            val(hs1,l1,w);
+            val(hs2,l2,w);
             if (l1<>0) or (l2<>0) then
             if (l1<>0) or (l2<>0) then
               hs1:='1'
               hs1:='1'
             else
             else
@@ -710,8 +710,8 @@ implementation
            hs2:=read_simple_expr;
            hs2:=read_simple_expr;
            if is_number(hs1) and is_number(hs2) then
            if is_number(hs1) and is_number(hs2) then
              begin
              begin
-                valint(hs1,l1,w);
-                valint(hs2,l2,w);
+                val(hs1,l1,w);
+                val(hs2,l2,w);
                 case t of
                 case t of
                    _EQUAL : b:=l1=l2;
                    _EQUAL : b:=l1=l2;
                  _UNEQUAL : b:=l1<>l2;
                  _UNEQUAL : b:=l1<>l2;
@@ -1876,7 +1876,7 @@ implementation
         w : integer;
         w : integer;
       begin
       begin
         readnumber;
         readnumber;
-        valint(pattern,l,w);
+        val(pattern,l,w);
         readval:=l;
         readval:=l;
       end;
       end;
 
 
@@ -2803,7 +2803,7 @@ implementation
                                  readchar;
                                  readchar;
                                end;
                                end;
                            end;
                            end;
-                         valint(asciinr,m,code);
+                         val(asciinr,m,code);
                          if (asciinr='') or (code<>0) then
                          if (asciinr='') or (code<>0) then
                            Message(scan_e_illegal_char_const)
                            Message(scan_e_illegal_char_const)
                          else if (m<0) or (m>255) or (length(asciinr)>3) then
                          else if (m<0) or (m>255) or (length(asciinr)>3) then
@@ -3259,7 +3259,10 @@ exit_label:
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.98  2005-01-09 20:24:43  olle
+  Revision 1.99  2005-01-20 17:05:53  peter
+    * use val() for decoding integers
+
+  Revision 1.98  2005/01/09 20:24:43  olle
     * rework of macro subsystem
     * rework of macro subsystem
     + exportable macros for mode macpas
     + exportable macros for mode macpas