Browse Source

compiler: allow 1 and 0 to be used as Booleans in preprocessor expressions

git-svn-id: trunk@25467 -
paul 12 years ago
parent
commit
cc37b19bb2
1 changed files with 22 additions and 9 deletions
  1. 22 9
      compiler/scanner.pas

+ 22 - 9
compiler/scanner.pas

@@ -825,6 +825,7 @@ type
     class function try_parse_real(s:string):texprvalue; static;
     class function try_parse_real(s:string):texprvalue; static;
     function evaluate(v:texprvalue;op:ttoken):texprvalue;
     function evaluate(v:texprvalue;op:ttoken):texprvalue;
     procedure error(expecteddef, place: string);
     procedure error(expecteddef, place: string);
+    function isBoolean: Boolean;
     function asBool: Boolean;
     function asBool: Boolean;
     function asInt: Integer;
     function asInt: Integer;
     function asStr: String;
     function asStr: String;
@@ -1035,7 +1036,7 @@ type
         end;
         end;
         _OP_NOT:
         _OP_NOT:
         begin
         begin
-          if is_boolean(def) then
+          if isBoolean then
             result:=texprvalue.create_bool(not asBool)
             result:=texprvalue.create_bool(not asBool)
           else
           else
             begin
             begin
@@ -1045,8 +1046,8 @@ type
         end;
         end;
         _OP_OR:
         _OP_OR:
         begin
         begin
-          if is_boolean(def) then
-            if is_boolean(v.def) then
+          if isBoolean then
+            if v.isBoolean then
               result:=texprvalue.create_bool(asBool or v.asBool)
               result:=texprvalue.create_bool(asBool or v.asBool)
             else
             else
               begin
               begin
@@ -1061,8 +1062,8 @@ type
         end;
         end;
         _OP_AND:
         _OP_AND:
         begin
         begin
-          if is_boolean(def) then
-            if is_boolean(v.def) then
+          if isBoolean then
+            if v.isBoolean then
               result:=texprvalue.create_bool(asBool and v.asBool)
               result:=texprvalue.create_bool(asBool and v.asBool)
             else
             else
               begin
               begin
@@ -1181,6 +1182,18 @@ type
               );
               );
     end;
     end;
 
 
+  function texprvalue.isBoolean: Boolean;
+    var
+      i: integer;
+    begin
+      result:=is_boolean(def);
+      if not result and is_integer(def) then
+        begin
+          i:=asInt;
+          result:=(i=0)or(i=1);
+        end;
+    end;
+
   function texprvalue.asBool: Boolean;
   function texprvalue.asBool: Boolean;
     begin
     begin
       result:=value.valueord<>0;
       result:=value.valueord<>0;
@@ -1881,7 +1894,7 @@ type
              begin
              begin
                hs1:=result;
                hs1:=result;
                preproc_consume(op);
                preproc_consume(op);
-               if (op=_OP_OR)and is_boolean(hs1.def) and hs1.asBool then
+               if (op=_OP_OR)and hs1.isBoolean and hs1.asBool then
                  begin
                  begin
                    { stop evaluation the rest of expression }
                    { stop evaluation the rest of expression }
                    result:=texprvalue.create_bool(true);
                    result:=texprvalue.create_bool(true);
@@ -1890,7 +1903,7 @@ type
                    else
                    else
                      hs2:=preproc_sub_expr(succ(pred_level),false);
                      hs2:=preproc_sub_expr(succ(pred_level),false);
                  end
                  end
-               else if (op=_OP_AND)and is_boolean(hs1.def) and not hs1.asBool then
+               else if (op=_OP_AND)and hs1.isBoolean and not hs1.asBool then
                  begin
                  begin
                    { stop evaluation the rest of expression }
                    { stop evaluation the rest of expression }
                    result:=texprvalue.create_bool(false);
                    result:=texprvalue.create_bool(false);
@@ -1930,7 +1943,7 @@ type
         hs: texprvalue;
         hs: texprvalue;
       begin
       begin
         hs:=preproc_comp_expr;
         hs:=preproc_comp_expr;
-        if is_boolean(hs.def) then
+        if hs.isBoolean then
           result:=hs.asBool
           result:=hs.asBool
         else
         else
           begin
           begin
@@ -2115,7 +2128,7 @@ type
                begin
                begin
                  {If we are absolutely shure it is boolean, translate
                  {If we are absolutely shure it is boolean, translate
                   to TRUE/FALSE to increase possibility to do future type check}
                   to TRUE/FALSE to increase possibility to do future type check}
-                 if is_boolean(exprvalue.def) then
+                 if exprvalue.isBoolean then
                    begin
                    begin
                      if exprvalue.asBool then
                      if exprvalue.asBool then
                        hs:='TRUE'
                        hs:='TRUE'