tools/PascalUnitSyntaxTree.hs
author koda
Sat, 09 Mar 2013 00:57:09 +0100
changeset 8702 a28966180a29
parent 7513 39866eb9e4a6
permissions -rw-r--r--
have fpc work in the right directory instead of passing the full path of the main module (avoids having full paths in debug build backtraces for the first module only)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
     1
module PascalUnitSyntaxTree where
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
     2
6618
2d3232069c4b Propagate types on identifiers
unc0rr
parents: 6512
diff changeset
     3
import Data.Maybe
6626
a447993f2ad7 Further work on propagating types. Now it hopefully works fully, just need to annotate namespace with types first.
unc0rr
parents: 6618
diff changeset
     4
import Data.Char
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
     5
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
     6
data PascalUnit =
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
     7
    Program Identifier Implementation Phrase
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
     8
    | Unit Identifier Interface Implementation (Maybe Initialize) (Maybe Finalize)
6512
0df7f6697939 "System" unit to help converter
unc0rr
parents: 6489
diff changeset
     9
    | System [TypeVarDeclaration]
7429
fcf13e40d6b6 Changes to pas2c - unreviewed apart from cursory glance and compile test.
xymeng
parents: 7333
diff changeset
    10
    | Redo [TypeVarDeclaration]
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    11
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    12
data Interface = Interface Uses TypesAndVars
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    13
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    14
data Implementation = Implementation Uses TypesAndVars
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    15
    deriving Show
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
    16
data Identifier = Identifier String BaseType
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    17
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    18
data TypesAndVars = TypesAndVars [TypeVarDeclaration]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    19
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    20
data TypeVarDeclaration = TypeDeclaration Identifier TypeDecl
7317
3534a264b27a Prepare to handle passing by reference
unc0rr
parents: 7070
diff changeset
    21
    | VarDeclaration Bool Bool ([Identifier], TypeDecl) (Maybe InitExpression)
7513
39866eb9e4a6 Keep inlining
unc0rr
parents: 7429
diff changeset
    22
    | FunctionDeclaration Identifier Bool TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
39866eb9e4a6 Keep inlining
unc0rr
parents: 7429
diff changeset
    23
    | OperatorDeclaration String Identifier Bool TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    24
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    25
data TypeDecl = SimpleType Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    26
    | RangeType Range
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    27
    | Sequence [Identifier]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    28
    | ArrayDecl (Maybe Range) TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    29
    | RecordType [TypeVarDeclaration] (Maybe [[TypeVarDeclaration]])
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    30
    | PointerTo TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    31
    | String Integer
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    32
    | Set TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    33
    | FunctionType TypeDecl [TypeVarDeclaration]
7323
8490a4f439a5 Convert function with var parameters declarations into #define + function which accepts pointers
unc0rr
parents: 7317
diff changeset
    34
    | DeriveType InitExpression
6826
8fadeefdd352 Just some further work
unc0rr
parents: 6816
diff changeset
    35
    | VoidType
7323
8490a4f439a5 Convert function with var parameters declarations into #define + function which accepts pointers
unc0rr
parents: 7317
diff changeset
    36
    | VarParamType TypeDecl -- this is a hack
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    37
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    38
data Range = Range Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    39
           | RangeFromTo InitExpression InitExpression
6893
69cc0166be8d - Track array size to use for High function
unc0rr
parents: 6843
diff changeset
    40
           | RangeInfinite
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    41
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    42
data Initialize = Initialize String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    43
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    44
data Finalize = Finalize String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    45
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    46
data Uses = Uses [Identifier]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    47
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    48
data Phrase = ProcCall Reference [Expression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    49
        | IfThenElse Expression Phrase (Maybe Phrase)
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    50
        | WhileCycle Expression Phrase
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    51
        | RepeatCycle Expression [Phrase]
7429
fcf13e40d6b6 Changes to pas2c - unreviewed apart from cursory glance and compile test.
xymeng
parents: 7333
diff changeset
    52
        | ForCycle Identifier Expression Expression Phrase Bool -- The last Boolean indicates wether it's up or down counting
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    53
        | WithBlock Reference Phrase
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    54
        | Phrases [Phrase]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    55
        | SwitchCase Expression [([InitExpression], Phrase)] (Maybe [Phrase])
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    56
        | Assignment Reference Expression
6895
31def088a870 Many small improvements to pas2c
unc0rr
parents: 6893
diff changeset
    57
        | BuiltInFunctionCall [Expression] Reference
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    58
        | NOP
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    59
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    60
data Expression = Expression String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    61
    | BuiltInFunCall [Expression] Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    62
    | PrefixOp String Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    63
    | PostfixOp String Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    64
    | BinOp String Expression Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    65
    | StringLiteral String
7070
8d4189609e90 oops, wrong type
unc0rr
parents: 7042
diff changeset
    66
    | PCharLiteral String
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    67
    | CharCode String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    68
    | HexCharCode String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    69
    | NumberLiteral String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    70
    | FloatLiteral String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    71
    | HexNumber String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    72
    | Reference Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    73
    | SetExpression [Identifier]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    74
    | Null
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    75
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    76
data Reference = ArrayElement [Expression] Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    77
    | FunCall [Expression] Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    78
    | TypeCast Identifier Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    79
    | SimpleReference Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    80
    | Dereference Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    81
    | RecordField Reference Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    82
    | Address Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    83
    | RefExpression Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    84
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    85
data InitExpression = InitBinOp String InitExpression InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    86
    | InitPrefixOp String InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    87
    | InitReference Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    88
    | InitArray [InitExpression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    89
    | InitRecord [(Identifier, InitExpression)]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    90
    | InitFloat String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    91
    | InitNumber String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    92
    | InitHexNumber String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    93
    | InitString String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    94
    | InitChar String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    95
    | BuiltInFunction String [InitExpression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    96
    | InitSet [InitExpression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    97
    | InitAddress InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    98
    | InitNull
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    99
    | InitRange Range
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
   100
    | InitTypeCast Identifier InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
   101
    deriving Show
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   102
6618
2d3232069c4b Propagate types on identifiers
unc0rr
parents: 6512
diff changeset
   103
data BaseType = BTUnknown
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   104
    | BTChar
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   105
    | BTString
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   106
    | BTInt
6635
c2fa29fe2a58 Some progress, still can't find the source of bad behavior
unc0rr
parents: 6626
diff changeset
   107
    | BTBool
6649
7f78e8a6db69 Fix a bug with type declaration trying to resolve type being declared
unc0rr
parents: 6635
diff changeset
   108
    | BTFloat
7042
de20086a6bcc Support overloaded operators on (hwFloat op hwFloat) calls
unc0rr
parents: 7032
diff changeset
   109
    | BTRecord String [(String, BaseType)]
6893
69cc0166be8d - Track array size to use for High function
unc0rr
parents: 6843
diff changeset
   110
    | BTArray Range BaseType BaseType
7333
520a16a14747 Properly convert taking address of function with var parameters
unc0rr
parents: 7323
diff changeset
   111
    | BTFunction Bool Int BaseType
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   112
    | BTPointerTo BaseType
6827
a0e152e68337 Dig into namespaces even more
unc0rr
parents: 6826
diff changeset
   113
    | BTUnresolved String
6653
d45b6dbd2ad6 Move a bit further
unc0rr
parents: 6649
diff changeset
   114
    | BTSet BaseType
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   115
    | BTEnum [String]
6618
2d3232069c4b Propagate types on identifiers
unc0rr
parents: 6512
diff changeset
   116
    | BTVoid
6816
572571ea945e Fix wrong type returned from id2C
unc0rr
parents: 6653
diff changeset
   117
    | BTUnit
7323
8490a4f439a5 Convert function with var parameters declarations into #define + function which accepts pointers
unc0rr
parents: 7317
diff changeset
   118
    | BTVarParam BaseType
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   119
    deriving Show