tools/pas2c.hs
changeset 6277 627b5752733a
parent 6275 f1b4f37dba22
child 6307 25cfd9f4a567
--- a/tools/pas2c.hs	Fri Nov 04 14:10:27 2011 +0300
+++ b/tools/pas2c.hs	Sat Nov 05 09:37:17 2011 +0300
@@ -3,6 +3,7 @@
 import PascalParser
 import Text.PrettyPrint.HughesPJ
 import Data.Maybe
+import Data.Char
 
 
 pascal2C :: PascalUnit -> Doc
@@ -36,7 +37,7 @@
     where
     elsePart | isNothing mphrase2 = empty
              | otherwise = text "else" $$ (braces . nest 4 . phrase2C) (fromJust mphrase2)
-phrase2C (Assignment (Identifier name) expr) = text name <> text " = " <> expr2C expr <> semi
+phrase2C (Assignment ref expr) = ref2C ref <> text " = " <> expr2C expr <> semi
 phrase2C (WhileCycle expr phrase) = text "while" <> parens (expr2C expr) $$ nest 4 (phrase2C phrase)
 phrase2C (SwitchCase expr cases mphrase) = text "switch" <> parens (expr2C expr) <> text "of" $$ (nest 4 . vcat . map case2C) cases
     where
@@ -50,14 +51,26 @@
         -}
 phrase2C _ = empty
 
+ref2C :: Reference -> Doc
+ref2C (ArrayElement (Identifier name) expr) = text name <> brackets (expr2C expr)
+ref2C (SimpleReference (Identifier name)) = text name
+ref2C (RecordField ref1 ref2) = ref2C ref1 <> text "." <> ref2C ref2
+ref2C (Dereference ref) = parens $ text "*" <> ref2C ref
 
 expr2C :: Expression -> Doc
 expr2C (Expression s) = text s
-expr2C (FunCall (Identifier name) params) = text name <> parens (hsep . punctuate (char ',') . map expr2C $ params)
+expr2C (FunCall ref params) = ref2C ref <> parens (hsep . punctuate (char ',') . map expr2C $ params)
 expr2C (BinOp op expr1 expr2) = (expr2C expr1) <+> op2C op <+> (expr2C expr2)
-{-    | FunCall Identifier [Expression]
+expr2C (NumberLiteral s) = text s
+expr2C (HexNumber s) = text "0x" <> (text . map toLower $ s)
+expr2C (StringLiteral s) = doubleQuotes $ text s 
+expr2C (Address ref) = text "&" <> ref2C ref
+expr2C (Reference ref) = ref2C ref
+
+{-    
     | PrefixOp String Expression
-    | BinOp String Expression Expression
+    | PostfixOp String Expression
+    | CharCode String
     -}            
 expr2C _ = empty
 
@@ -66,6 +79,7 @@
 op2C "div" = text "/"
 op2C "mod" = text "%"
 op2C "<>" = text "!="
+op2C "=" = text "=="
 op2C a = text a
 
 maybeVoid "" = "void"