tools/pas2c.hs
changeset 6307 25cfd9f4a567
parent 6277 627b5752733a
child 6317 83b93a2d2741
equal deleted inserted replaced
6306:553680d78546 6307:25cfd9f4a567
    17 typesAndVars2C :: TypesAndVars -> Doc
    17 typesAndVars2C :: TypesAndVars -> Doc
    18 typesAndVars2C (TypesAndVars ts) = vcat $ map tvar2C ts
    18 typesAndVars2C (TypesAndVars ts) = vcat $ map tvar2C ts
    19 
    19 
    20 
    20 
    21 tvar2C :: TypeVarDeclaration -> Doc
    21 tvar2C :: TypeVarDeclaration -> Doc
    22 tvar2C (FunctionDeclaration (Identifier name) (Identifier returnType) Nothing) = 
    22 tvar2C (FunctionDeclaration (Identifier name) returnType Nothing) = 
    23     text $ maybeVoid returnType ++ " " ++ name ++ "();"
    23     type2C returnType <+> text (name ++ "();")
    24 
    24 
    25     
    25     
    26 tvar2C (FunctionDeclaration (Identifier name) (Identifier returnType) (Just phrase)) = 
    26 tvar2C (FunctionDeclaration (Identifier name) returnType (Just phrase)) = 
    27     text (maybeVoid returnType ++ " " ++ name ++ "()") 
    27     type2C returnType <+> text (name ++ "()") 
    28     $$
    28     $$
    29     phrase2C phrase
    29     phrase2C phrase
    30 tvar2C _ = empty
    30 tvar2C _ = empty
    31 
    31 
       
    32 type2C :: TypeDecl -> Doc
       
    33 type2C UnknownType = text "void"
       
    34 type2C _ = text "<<type>>"
    32 
    35 
    33 phrase2C :: Phrase -> Doc
    36 phrase2C :: Phrase -> Doc
    34 phrase2C (Phrases p) = braces . nest 4 . vcat . map phrase2C $ p
    37 phrase2C (Phrases p) = text "{" $+$ (nest 4 . vcat . map phrase2C $ p) $+$ text "}"
    35 phrase2C (ProcCall (Identifier name) params) = text name <> parens (hsep . punctuate (char ',') . map expr2C $ params) <> semi
    38 phrase2C (ProcCall (Identifier name) params) = text name <> parens (hsep . punctuate (char ',') . map expr2C $ params) <> semi
    36 phrase2C (IfThenElse (expr) phrase1 mphrase2) = text "if" <> parens (expr2C expr) $$ (braces . nest 4 . phrase2C) phrase1 $+$ elsePart
    39 phrase2C (IfThenElse (expr) phrase1 mphrase2) = text "if" <> parens (expr2C expr) $+$ (phrase2C . wrapPhrase) phrase1 $+$ elsePart
    37     where
    40     where
    38     elsePart | isNothing mphrase2 = empty
    41     elsePart | isNothing mphrase2 = empty
    39              | otherwise = text "else" $$ (braces . nest 4 . phrase2C) (fromJust mphrase2)
    42              | otherwise = text "else" $$ (phrase2C . wrapPhrase) (fromJust mphrase2)
    40 phrase2C (Assignment ref expr) = ref2C ref <> text " = " <> expr2C expr <> semi
    43 phrase2C (Assignment ref expr) = ref2C ref <> text " = " <> expr2C expr <> semi
    41 phrase2C (WhileCycle expr phrase) = text "while" <> parens (expr2C expr) $$ nest 4 (phrase2C phrase)
    44 phrase2C (WhileCycle expr phrase) = text "while" <> parens (expr2C expr) $$ (phrase2C $ wrapPhrase phrase)
    42 phrase2C (SwitchCase expr cases mphrase) = text "switch" <> parens (expr2C expr) <> text "of" $$ (nest 4 . vcat . map case2C) cases
    45 phrase2C (SwitchCase expr cases mphrase) = text "switch" <> parens (expr2C expr) <> text "of" $+$ (nest 4 . vcat . map case2C) cases
    43     where
    46     where
    44     case2C :: (Expression, Phrase) -> Doc
    47     case2C :: (Expression, Phrase) -> Doc
    45     case2C (e, p) = text "case" <+> parens (expr2C e) <> char ':' <> nest 4 (phrase2C p $$ text "break;")
    48     case2C (e, p) = text "case" <+> parens (expr2C e) <> char ':' <> nest 4 (phrase2C p $+$ text "break;")
    46 {-
    49 {-
    47         | RepeatCycle Expression Phrase
    50         | RepeatCycle Expression Phrase
    48         | ForCycle
    51         | ForCycle
    49         | SwitchCase Expression [(Expression, Phrase)] (Maybe Phrase)
       
    50         | Assignment Identifier Expression
       
    51         -}
    52         -}
    52 phrase2C _ = empty
    53 phrase2C _ = empty
    53 
    54 
    54 ref2C :: Reference -> Doc
    55 wrapPhrase p@(Phrases _) = p
    55 ref2C (ArrayElement (Identifier name) expr) = text name <> brackets (expr2C expr)
    56 wrapPhrase p = Phrases [p]
    56 ref2C (SimpleReference (Identifier name)) = text name
       
    57 ref2C (RecordField ref1 ref2) = ref2C ref1 <> text "." <> ref2C ref2
       
    58 ref2C (Dereference ref) = parens $ text "*" <> ref2C ref
       
    59 
    57 
    60 expr2C :: Expression -> Doc
    58 expr2C :: Expression -> Doc
    61 expr2C (Expression s) = text s
    59 expr2C (Expression s) = text s
    62 expr2C (FunCall ref params) = ref2C ref <> parens (hsep . punctuate (char ',') . map expr2C $ params)
    60 expr2C (FunCall ref params) = ref2C ref <> parens (hsep . punctuate (char ',') . map expr2C $ params)
    63 expr2C (BinOp op expr1 expr2) = (expr2C expr1) <+> op2C op <+> (expr2C expr2)
    61 expr2C (BinOp op expr1 expr2) = parens $ (expr2C expr1) <+> op2C op <+> (expr2C expr2)
    64 expr2C (NumberLiteral s) = text s
    62 expr2C (NumberLiteral s) = text s
    65 expr2C (HexNumber s) = text "0x" <> (text . map toLower $ s)
    63 expr2C (HexNumber s) = text "0x" <> (text . map toLower $ s)
    66 expr2C (StringLiteral s) = doubleQuotes $ text s 
    64 expr2C (StringLiteral s) = doubleQuotes $ text s 
    67 expr2C (Address ref) = text "&" <> ref2C ref
    65 expr2C (Address ref) = text "&" <> ref2C ref
    68 expr2C (Reference ref) = ref2C ref
    66 expr2C (Reference ref) = ref2C ref
    69 
    67 expr2C (PrefixOp op expr) = op2C op <+> expr2C expr
    70 {-    
    68     {-
    71     | PrefixOp String Expression
       
    72     | PostfixOp String Expression
    69     | PostfixOp String Expression
    73     | CharCode String
    70     | CharCode String
    74     -}            
    71     -}            
    75 expr2C _ = empty
    72 expr2C _ = empty
    76 
    73 
       
    74 
       
    75 ref2C :: Reference -> Doc
       
    76 ref2C (ArrayElement (Identifier name) expr) = text name <> brackets (expr2C expr)
       
    77 ref2C (SimpleReference (Identifier name)) = text name
       
    78 ref2C (RecordField (Dereference ref1) ref2) = ref2C ref1 <> text "->" <> ref2C ref2
       
    79 ref2C (RecordField ref1 ref2) = ref2C ref1 <> text "." <> ref2C ref2
       
    80 ref2C (Dereference ref) = parens $ text "*" <> ref2C ref
       
    81 
    77 op2C "or" = text "|"
    82 op2C "or" = text "|"
    78 op2C "and" = text "&"
    83 op2C "and" = text "&"
       
    84 op2C "not" = text "!"
       
    85 op2C "xor" = text "^"
    79 op2C "div" = text "/"
    86 op2C "div" = text "/"
    80 op2C "mod" = text "%"
    87 op2C "mod" = text "%"
       
    88 op2C "shl" = text "<<"
       
    89 op2C "shr" = text ">>"
    81 op2C "<>" = text "!="
    90 op2C "<>" = text "!="
    82 op2C "=" = text "=="
    91 op2C "=" = text "=="
    83 op2C a = text a
    92 op2C a = text a
    84 
    93 
    85 maybeVoid "" = "void"
    94 maybeVoid "" = "void"