Render unions
authorunc0rr
Sat, 14 Apr 2012 22:24:09 +0400
changeset 6886 4463ee51c9ec
parent 6885 53a87a4c7b35
child 6887 19d77932ea91
Render unions
hedgewars/GL.h
hedgewars/pas2c.h
tools/pas2c.hs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/GL.h	Sat Apr 14 22:24:09 2012 +0400
@@ -0,0 +1,2 @@
+#pragma once
+
--- a/hedgewars/pas2c.h	Fri Apr 13 01:41:11 2012 +0200
+++ b/hedgewars/pas2c.h	Sat Apr 14 22:24:09 2012 +0400
@@ -13,6 +13,7 @@
 typedef int LongWord;
 typedef int Byte;
 typedef int Integer;
+typedef long long int QWord;
 
 typedef float extended;
 typedef float real;
--- a/tools/pas2c.hs	Fri Apr 13 01:41:11 2012 +0200
+++ b/tools/pas2c.hs	Sat Apr 14 22:24:09 2012 +0400
@@ -367,7 +367,7 @@
 initExpr2C (InitReference i) = id2C IOLookup i
 initExpr2C (InitRecord fields) = do
     (fs :: [Doc]) <- mapM (\(Identifier a _, b) -> liftM (text "." <> text a <+> equals <+>) $ initExpr2C b) fields
-    return $ lbrace $+$ (nest 4 . vcat $ fs) $+$ rbrace
+    return $ lbrace $+$ (nest 4 . vcat . punctuate comma $ fs) $+$ rbrace
 initExpr2C (InitArray [value]) = initExpr2C value
 initExpr2C (InitArray values) = liftM (braces . vcat . punctuate comma) $ mapM initExpr2C values
 initExpr2C (InitRange (Range i)) = id2C IOLookup i
@@ -399,7 +399,17 @@
     type2C' (PointerTo t) = liftM (\t a -> t (text "*" <> a)) $ type2C t
     type2C' (RecordType tvs union) = do
         t <- withState' id $ mapM (tvar2C False) tvs
-        return $ \i -> text "struct" <+> lbrace $+$ (nest 4 . vcat . map (<> semi) . concat $ t) $+$ rbrace <+> i
+        u <- unions
+        return $ \i -> text "struct" <+> lbrace $+$ nest 4 ((vcat . map (<> semi) . concat $ t) $$ u) $+$ rbrace <+> i
+        where
+            unions = case union of
+                     Nothing -> return empty
+                     Just a -> do
+                         structs <- mapM struct2C a
+                         return $ text "union" $+$ braces (nest 4 $ vcat structs) <> semi
+            struct2C tvs = do
+                t <- withState' id $ mapM (tvar2C False) tvs
+                return $ text "struct" $+$ braces (nest 4 (vcat . map (<> semi) . concat $ t)) <> semi
     type2C' (RangeType r) = return (text "<<range type>>" <+>)
     type2C' (Sequence ids) = do
         is <- mapM (id2C IOInsert . setBaseType bt) ids