Make pas2c even more happier with uGears.c, allow assigning arrays in some cases
authorunc0rr
Sat, 12 May 2012 22:13:56 +0400
changeset 7066 12cc2bd84b0b
parent 7065 e80e0d3273c5
child 7067 f98ec3aecf4e
Make pas2c even more happier with uGears.c, allow assigning arrays in some cases
hedgewars/GSHandlers.inc
hedgewars/pas2cSystem.pas
hedgewars/uGearsUtils.pas
hedgewars/uVariables.pas
tools/PascalParser.hs
tools/pas2c.hs
--- a/hedgewars/GSHandlers.inc	Sat May 12 00:25:49 2012 +0400
+++ b/hedgewars/GSHandlers.inc	Sat May 12 22:13:56 2012 +0400
@@ -466,7 +466,7 @@
                     begin
                     Frame:= 2;
                     Tint:= $41B83ED0 - i * $10081000;
-                    Angle:= random * 360;
+                    Angle:= random(360);
                     dx:= 0.0000001;
                     dy:= 0;
                     if random(2) = 0 then
@@ -1090,7 +1090,7 @@
         begin
         cLaserSighting := true;
         HHGear^.Message := 0;
-        if (HHGear^.Angle - 32 >= 0) then
+        if (HHGear^.Angle >= 32) then
             dec(HHGear^.Angle,32)
         end;
 
@@ -1120,7 +1120,7 @@
                     inc(HHGear^.Angle)
                 end
     else
-        if (HHGear^.Angle - 1 >= 0) then
+        if (HHGear^.Angle >= 1) then
             dec(HHGear^.Angle);
 
     if (TurnTimeLeft > 0) then
@@ -2838,7 +2838,7 @@
         if sparkles <> nil then
             begin
             sparkles^.Tint:= ((random(210)+45) shl 24) or ((random(210)+45) shl 16) or ((random(210)+45) shl 8) or $FF;
-            sparkles^.Angle:= random * 360;
+            sparkles^.Angle:= random(360);
             end
         end;
 
@@ -2893,7 +2893,7 @@
                     with sparkles^ do
                         begin
                         Tint:= ((random(210)+45) shl 24) or ((random(210)+45) shl 16) or ((random(210)+45) shl 8) or $FF;
-                        Angle:= random * 360;
+                        Angle:= random(360);
                         dx:= 0.001 * (random(200));
                         dy:= 0.001 * (random(200));
                         if random(2) = 0 then
@@ -4559,7 +4559,7 @@
         doMakeExplosion(hwRound(Gear^.X), hwRound(Gear^.Y), 80 + r0, Gear^.Hedgehog, EXPLAutoSound);
         for r0:= 0 to 4 do
             AddVisualGear(hwRound(Gear^.X), hwRound(Gear^.Y), vgtNote);
-        Gear^.dY := odY * -1 + cGravity * 2;
+        Gear^.dY := cGravity * 2 - odY;
         Gear^.Pos := Gear^.Pos + 1;
         end
     else
--- a/hedgewars/pas2cSystem.pas	Sat May 12 00:25:49 2012 +0400
+++ b/hedgewars/pas2cSystem.pas	Sat May 12 22:13:56 2012 +0400
@@ -69,6 +69,7 @@
     Length, StrToInt : function : integer;
     SetLength, val : procedure;
     _pchar : function : PChar;
+    memcpy : procedure;
 
     assign, rewrite, reset, flush, BlockWrite, BlockRead, close : procedure;
     IOResult : integer;
@@ -143,4 +144,4 @@
     png_write_end : procedure;
 
     EnumToStr : function : string;
-function glGetString
+
--- a/hedgewars/uGearsUtils.pas	Sat May 12 00:25:49 2012 +0400
+++ b/hedgewars/uGearsUtils.pas	Sat May 12 22:13:56 2012 +0400
@@ -20,7 +20,7 @@
 
 unit uGearsUtils;
 interface
-uses uTypes, math;
+uses uTypes;
 
 procedure doMakeExplosion(X, Y, Radius: LongInt; AttackingHog: PHedgehog; Mask: Longword); inline;
 procedure doMakeExplosion(X, Y, Radius: LongInt; AttackingHog: PHedgehog; Mask: Longword; const Tint: LongWord); 
@@ -46,7 +46,7 @@
 uses uFloat, uSound, uCollisions, uUtils, uConsts, uVisualGears, uAIMisc,
     uVariables, uLandGraphics, uScript, uStats, uCaptions, uTeams, uStore,
     uLocale, uTextures, uRenderUtils, uRandom, SDLh, uDebug, uGears,
-    uGearsList;
+    uGearsList, Math;
 
 procedure doMakeExplosion(X, Y, Radius: LongInt; AttackingHog: PHedgehog; Mask: Longword); inline;
 begin
--- a/hedgewars/uVariables.pas	Sat May 12 00:25:49 2012 +0400
+++ b/hedgewars/uVariables.pas	Sat May 12 22:13:56 2012 +0400
@@ -202,7 +202,7 @@
 {$ENDIF}
 
 
-const
+var
     // these consts are here because they would cause circular dependencies in uConsts/uTypes
     cPathz: array[TPathType] of shortstring = (
         '',                              // ptNone
--- a/tools/PascalParser.hs	Sat May 12 00:25:49 2012 +0400
+++ b/tools/PascalParser.hs	Sat May 12 22:13:56 2012 +0400
@@ -563,14 +563,16 @@
         return (i ,e)
 
     table = [ 
-          [  Infix (char '*' >> return (InitBinOp "*")) AssocLeft
+          [
+             Prefix (char '-' >> return (InitPrefixOp "-"))
+          ]
+        , [  Infix (char '*' >> return (InitBinOp "*")) AssocLeft
            , Infix (char '/' >> return (InitBinOp "/")) AssocLeft
            , Infix (try (string "div") >> return (InitBinOp "div")) AssocLeft
            , Infix (try (string "mod") >> return (InitBinOp "mod")) AssocLeft
           ]
         , [  Infix (char '+' >> return (InitBinOp "+")) AssocLeft
            , Infix (char '-' >> return (InitBinOp "-")) AssocLeft
-           , Prefix (char '-' >> return (InitPrefixOp "-"))
           ]
         , [  Infix (try (string "<>") >> return (InitBinOp "<>")) AssocNone
            , Infix (try (string "<=") >> return (InitBinOp "<=")) AssocNone
--- a/tools/pas2c.hs	Sat May 12 00:25:49 2012 +0400
+++ b/tools/pas2c.hs	Sat May 12 22:13:56 2012 +0400
@@ -553,14 +553,18 @@
     type2C' (RangeType r) = return (text "int" <+>)
     type2C' (Sequence ids) = do
         is <- mapM (id2C IOInsert . setBaseType bt) ids
-        return (text "enum" <+> (braces . vcat . punctuate comma . map (\(a, b) -> a <+> equals <+> text "0x" <> text (showHex b "")) $ zip is [1..]) <+>)
+        return (text "enum" <+> (braces . vcat . punctuate comma . map (\(a, b) -> a <+> equals <+> text "0x" <> text (showHex b "")) $ zip is [0..]) <+>)
         where
             bt = BTEnum $ map (\(Identifier i _) -> map toLower i) ids
     type2C' (ArrayDecl Nothing t) = type2C (PointerTo t)
     type2C' (ArrayDecl (Just r) t) = do
         t' <- type2C t
+        lt <- gets lastType
+        ft <- case lt of
+                BTFunction {} -> type2C (PointerTo t)
+                _ -> return t'
         r' <- initExpr2C (InitRange r)
-        return $ \i -> t' i <> brackets r'
+        return $ \i -> ft i <> brackets r'
     type2C' (Set t) = return (text "<<set>>" <+>)
     type2C' (FunctionType returnType params) = do
         t <- type2C returnType
@@ -601,10 +605,22 @@
 phrase2C (Assignment ref expr) = do
     r <- ref2C ref
     t <- gets lastType
-    e <- case (t, expr) of
-         (BTFunction {}, (Reference r')) -> ref2C r'
-         _ -> expr2C expr
-    return $ r <+> text "=" <+> e <> semi
+    case (t, expr) of
+        (BTFunction {}, (Reference r')) -> do
+            e <- ref2C r'
+            return $ r <+> text "=" <+> e <> semi
+        (BTArray (Range _) _ _, _) -> phrase2C $ 
+            ProcCall (FunCall
+                [
+                Reference $ Address ref
+                , Reference $ Address $ RefExpression expr
+                , Reference $ FunCall [expr] (SimpleReference (Identifier "sizeof" BTUnknown))
+                ]
+                (SimpleReference (Identifier "memcpy" BTUnknown))
+                ) []
+        _ -> do
+            e <- expr2C expr
+            return $ r <+> text "=" <+> e <> semi
 phrase2C (WhileCycle expr phrase) = do
     e <- expr2C expr
     p <- phrase2C $ wrapPhrase phrase