Rework string init macro
authorunc0rr
Mon, 30 Apr 2012 17:43:21 +0400
changeset 6965 5718ec36900c
parent 6964 6dde80ae7049
child 6966 eda4f63bec41
Rework string init macro
hedgewars/pas2c.h
tools/pas2c.hs
--- a/hedgewars/pas2c.h	Mon Apr 30 16:55:02 2012 +0400
+++ b/hedgewars/pas2c.h	Mon Apr 30 17:43:21 2012 +0400
@@ -63,7 +63,7 @@
 string255 copy(string255 a, int s, int l);
 string255 delete(string255 a, int s, int l);
 
-#define STRCONSTDECL(a, b) const string255 a = {.len = sizeof(b), .str = b}
+#define STRINIT(a) {.len = sizeof(a), .str = a}
 
 typedef int file;
 extern int FileMode;
--- a/tools/pas2c.hs	Mon Apr 30 16:55:02 2012 +0400
+++ b/tools/pas2c.hs	Mon Apr 30 17:43:21 2012 +0400
@@ -66,8 +66,11 @@
 escapeChar '"' s = "\\\"" ++ s
 escapeChar a s = a : s
 
+strInit :: String -> Doc
+strInit a = text "STRINIT" <> parens (doubleQuotes (text $ escapeStr a))
+
 renderStringConsts :: State RenderState Doc
-renderStringConsts = liftM (vcat . map (\(a, b) -> text "STRCONSTDECL" <> parens (text a <> comma <+> doubleQuotes (text $ escapeStr b)) <> semi)) 
+renderStringConsts = liftM (vcat . map (\(a, b) -> text "const string255" <+> (text a) <+> text "=" <+> strInit b <> semi)) 
     $ gets stringConsts
     
 docToLower :: Doc -> Doc
@@ -183,8 +186,12 @@
     
     
 interface2C :: Interface -> State RenderState Doc
-interface2C (Interface uses tvars) = liftM2 ($+$) (uses2C uses) (typesAndVars2C True tvars)
-
+interface2C (Interface uses tvars) = do
+    u <- uses2C uses
+    tv <- typesAndVars2C True tvars
+    r <- renderStringConsts
+    return (u $+$ r $+$ tv)
+    
 implementation2C :: Implementation -> State RenderState Doc
 implementation2C (Implementation uses tvars) = do
     u <- uses2C uses
@@ -408,7 +415,7 @@
 initExpr2C (InitFloat s) = return $ text s
 initExpr2C (InitHexNumber s) = return $ text "0x" <> (text . map toLower $ s)
 initExpr2C (InitString [a]) = return . quotes $ text [a]
-initExpr2C (InitString s) = return $ braces $ text ".s = " <> doubleQuotes (text s)
+initExpr2C (InitString s) = return $ strInit s
 initExpr2C (InitChar a) = return $ quotes $ text "\\x" <> text (showHex (read a) "")
 initExpr2C (InitReference i) = id2C IOLookup i
 initExpr2C (InitRecord fields) = do