# HG changeset patch # User unc0rr # Date 1336633926 -14400 # Node ID d99934a827f042256e648b92acc776329662c412 # Parent 823caba6773876a61c733d5b8b28cfbc4b28dfac Implement built-in functions Low() and High() diff -r 823caba67738 -r d99934a827f0 hedgewars/GSHandlers.inc --- 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; diff -r 823caba67738 -r d99934a827f0 tools/pas2c.hs --- 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