tools/pas2c.hs
author nemo
Sun, 06 Nov 2011 14:15:43 -0500
changeset 6306 553680d78546
parent 6277 627b5752733a
child 6307 25cfd9f4a567
permissions -rw-r--r--
tweak % again
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     1
module Pas2C where
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     2
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     3
import PascalParser
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     4
import Text.PrettyPrint.HughesPJ
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     5
import Data.Maybe
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
     6
import Data.Char
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     7
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     8
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
     9
pascal2C :: PascalUnit -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    10
pascal2C (Unit unitName interface implementation init fin) = implementation2C implementation
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    11
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    12
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    13
implementation2C :: Implementation -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    14
implementation2C (Implementation uses tvars) = typesAndVars2C tvars
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    15
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    16
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    17
typesAndVars2C :: TypesAndVars -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    18
typesAndVars2C (TypesAndVars ts) = vcat $ map tvar2C ts
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    19
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    20
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    21
tvar2C :: TypeVarDeclaration -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    22
tvar2C (FunctionDeclaration (Identifier name) (Identifier returnType) Nothing) = 
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    23
    text $ maybeVoid returnType ++ " " ++ name ++ "();"
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    24
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    25
    
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    26
tvar2C (FunctionDeclaration (Identifier name) (Identifier returnType) (Just phrase)) = 
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    27
    text (maybeVoid returnType ++ " " ++ name ++ "()") 
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    28
    $$
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    29
    phrase2C phrase
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    30
tvar2C _ = empty
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    31
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    32
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    33
phrase2C :: Phrase -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    34
phrase2C (Phrases p) = braces . nest 4 . vcat . map phrase2C $ p
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    35
phrase2C (ProcCall (Identifier name) params) = text name <> parens (hsep . punctuate (char ',') . map expr2C $ params) <> semi
6274
a3e1eb794249 Better 'else' part
unc0rr
parents: 6273
diff changeset
    36
phrase2C (IfThenElse (expr) phrase1 mphrase2) = text "if" <> parens (expr2C expr) $$ (braces . nest 4 . phrase2C) phrase1 $+$ elsePart
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    37
    where
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    38
    elsePart | isNothing mphrase2 = empty
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    39
             | otherwise = text "else" $$ (braces . nest 4 . phrase2C) (fromJust mphrase2)
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    40
phrase2C (Assignment ref expr) = ref2C ref <> text " = " <> expr2C expr <> semi
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    41
phrase2C (WhileCycle expr phrase) = text "while" <> parens (expr2C expr) $$ nest 4 (phrase2C phrase)
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    42
phrase2C (SwitchCase expr cases mphrase) = text "switch" <> parens (expr2C expr) <> text "of" $$ (nest 4 . vcat . map case2C) cases
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    43
    where
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    44
    case2C :: (Expression, Phrase) -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    45
    case2C (e, p) = text "case" <+> parens (expr2C e) <> char ':' <> nest 4 (phrase2C p $$ text "break;")
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    46
{-
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    47
        | RepeatCycle Expression Phrase
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    48
        | ForCycle
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    49
        | SwitchCase Expression [(Expression, Phrase)] (Maybe Phrase)
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    50
        | Assignment Identifier Expression
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    51
        -}
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    52
phrase2C _ = empty
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    53
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    54
ref2C :: Reference -> Doc
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    55
ref2C (ArrayElement (Identifier name) expr) = text name <> brackets (expr2C expr)
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    56
ref2C (SimpleReference (Identifier name)) = text name
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    57
ref2C (RecordField ref1 ref2) = ref2C ref1 <> text "." <> ref2C ref2
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    58
ref2C (Dereference ref) = parens $ text "*" <> ref2C ref
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    59
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    60
expr2C :: Expression -> Doc
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    61
expr2C (Expression s) = text s
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    62
expr2C (FunCall ref params) = ref2C ref <> parens (hsep . punctuate (char ',') . map expr2C $ params)
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    63
expr2C (BinOp op expr1 expr2) = (expr2C expr1) <+> op2C op <+> (expr2C expr2)
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    64
expr2C (NumberLiteral s) = text s
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    65
expr2C (HexNumber s) = text "0x" <> (text . map toLower $ s)
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    66
expr2C (StringLiteral s) = doubleQuotes $ text s 
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    67
expr2C (Address ref) = text "&" <> ref2C ref
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    68
expr2C (Reference ref) = ref2C ref
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    69
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    70
{-    
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    71
    | PrefixOp String Expression
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    72
    | PostfixOp String Expression
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    73
    | CharCode String
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    74
    -}            
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    75
expr2C _ = empty
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    76
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6274
diff changeset
    77
op2C "or" = text "|"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6274
diff changeset
    78
op2C "and" = text "&"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6274
diff changeset
    79
op2C "div" = text "/"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6274
diff changeset
    80
op2C "mod" = text "%"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6274
diff changeset
    81
op2C "<>" = text "!="
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    82
op2C "=" = text "=="
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6274
diff changeset
    83
op2C a = text a
6273
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    84
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    85
maybeVoid "" = "void"
13262c6e5027 Starting pas2C using library called 'pretty'
unc0rr
parents:
diff changeset
    86
maybeVoid a = a