Implement built-in functions Low() and High()
authorunc0rr
Thu, 10 May 2012 11:12:06 +0400
changeset 7036 d99934a827f0
parent 7035 823caba67738
child 7037 7edce323558f
Implement built-in functions Low() and High()
hedgewars/GSHandlers.inc
tools/pas2c.hs
--- a/hedgewars/GSHandlers.inc	Wed May 09 17:33:25 2012 -0400
+++ b/hedgewars/GSHandlers.inc	Thu May 10 11:12:06 2012 +0400
@@ -3164,7 +3164,7 @@
     hogs := GearsNear(Gear^.X, Gear^.Y, gtHedgehog, Gear^.Radius);
     if Length(hogs) > 0 then
         begin
-        for i:= 0 to High(hogs) do
+        for i:= 0 to Length(hogs) - 1 do
             begin
             if hogs[i] <> CurrentHedgehog^.Gear then
                 begin
@@ -5079,7 +5079,7 @@
     else 
         begin
         // now really resurrect the hogs with the hp saved in the graves
-        for i:= 0 to High(graves) do
+        for i:= 0 to Length(graves) - 1 do
             if graves[i]^.Health > 0 then
                 begin
                 resgear := AddGear(hwRound(graves[i]^.X), hwRound(graves[i]^.Y), gtHedgehog, gstWait, _0, _0, 0);
@@ -5116,7 +5116,7 @@
 
     if Length(graves) > 0 then
         begin
-        for i:= 0 to High(graves) do
+        for i:= 0 to Length(graves) - 1 do
             begin
             PHedgehog(graves[i]^.Hedgehog)^.Gear := nil;
             graves[i]^.Health := 0;
--- a/tools/pas2c.hs	Wed May 09 17:33:25 2012 -0400
+++ b/tools/pas2c.hs	Thu May 10 11:12:06 2012 +0400
@@ -685,6 +685,25 @@
 expr2C (HexCharCode a) = return $ quotes $ text "\\x" <> text (map toLower a)
 expr2C (SetExpression ids) = mapM (id2C IOLookup) ids >>= return . parens . hcat . punctuate (text " | ")
 
+expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "low" _))) = do
+    e' <- liftM (map toLower . render) $ expr2C e
+    lt <- gets lastType
+    case lt of
+         BTEnum a -> return $ int 0
+         BTInt -> case e' of
+                  "longint" -> return $ int (-2147483648)
+         BTArray {} -> return $ int 0
+         _ -> error $ "BuiltInFunCall 'low' from " ++ show e ++ "\ntype: " ++ show lt
+expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "high" _))) = do
+    e' <- liftM (map toLower . render) $ expr2C e
+    lt <- gets lastType
+    case lt of
+         BTEnum a -> return . int $ length a - 1
+         BTInt -> case e' of
+                  "longint" -> return $ int (2147483647)
+         BTString -> return $ int 255
+         BTArray (RangeFromTo _ n) _ _ -> initExpr2C n
+         _ -> error $ "BuiltInFunCall 'high' from " ++ show e ++ "\ntype: " ++ show lt
 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "ord" _))) = liftM parens $ expr2C e
 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "succ" _))) = liftM (<> text " + 1") $ expr2C e
 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "pred" _))) = liftM (<> text " - 1") $ expr2C e