tools/PascalUnitSyntaxTree.hs
author unc0rr
Mon, 30 Apr 2012 23:35:40 +0400
changeset 6967 1224c6fb36c3
parent 6895 31def088a870
child 7019 333afe233886
permissions -rw-r--r--
Support recurrent function calls. The code is kinda hackish and ugly, but I really spent a few hours thinking on a good solution.
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]
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    10
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    11
data Interface = Interface Uses TypesAndVars
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    12
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    13
data Implementation = Implementation Uses TypesAndVars
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    14
    deriving Show
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
    15
data Identifier = Identifier String BaseType
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    16
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    17
data TypesAndVars = TypesAndVars [TypeVarDeclaration]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    18
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    19
data TypeVarDeclaration = TypeDeclaration Identifier TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    20
    | VarDeclaration Bool ([Identifier], TypeDecl) (Maybe InitExpression)
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    21
    | FunctionDeclaration Identifier TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    22
    | OperatorDeclaration String Identifier TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    23
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    24
data TypeDecl = SimpleType Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    25
    | RangeType Range
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    26
    | Sequence [Identifier]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    27
    | ArrayDecl (Maybe Range) TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    28
    | RecordType [TypeVarDeclaration] (Maybe [[TypeVarDeclaration]])
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    29
    | PointerTo TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    30
    | String Integer
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    31
    | Set TypeDecl
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    32
    | FunctionType TypeDecl [TypeVarDeclaration]
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
    33
    | DeriveType InitExpression 
6826
8fadeefdd352 Just some further work
unc0rr
parents: 6816
diff changeset
    34
    | VoidType
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    35
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    36
data Range = Range Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    37
           | RangeFromTo InitExpression InitExpression
6893
69cc0166be8d - Track array size to use for High function
unc0rr
parents: 6843
diff changeset
    38
           | RangeInfinite
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    39
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    40
data Initialize = Initialize String
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 Finalize = Finalize 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 Uses = Uses [Identifier]
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 Phrase = ProcCall Reference [Expression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    47
        | IfThenElse Expression Phrase (Maybe Phrase)
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    48
        | WhileCycle Expression Phrase
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    49
        | RepeatCycle Expression [Phrase]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    50
        | ForCycle Identifier Expression Expression Phrase
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    51
        | WithBlock Reference Phrase
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    52
        | Phrases [Phrase]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    53
        | SwitchCase Expression [([InitExpression], Phrase)] (Maybe [Phrase])
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    54
        | Assignment Reference Expression
6895
31def088a870 Many small improvements to pas2c
unc0rr
parents: 6893
diff changeset
    55
        | BuiltInFunctionCall [Expression] Reference
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    56
        | NOP
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    57
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    58
data Expression = Expression String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    59
    | BuiltInFunCall [Expression] Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    60
    | PrefixOp String Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    61
    | PostfixOp String Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    62
    | BinOp String Expression Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    63
    | StringLiteral String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    64
    | CharCode String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    65
    | HexCharCode String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    66
    | NumberLiteral String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    67
    | FloatLiteral String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    68
    | HexNumber String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    69
    | Reference Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    70
    | SetExpression [Identifier]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    71
    | Null
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    72
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    73
data Reference = ArrayElement [Expression] Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    74
    | FunCall [Expression] Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    75
    | TypeCast Identifier Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    76
    | SimpleReference Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    77
    | Dereference Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    78
    | RecordField Reference Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    79
    | Address Reference
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    80
    | RefExpression Expression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    81
    deriving Show
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    82
data InitExpression = InitBinOp String InitExpression InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    83
    | InitPrefixOp String InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    84
    | InitReference Identifier
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    85
    | InitArray [InitExpression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    86
    | InitRecord [(Identifier, InitExpression)]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    87
    | InitFloat String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    88
    | InitNumber String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    89
    | InitHexNumber String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    90
    | InitString String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    91
    | InitChar String
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    92
    | BuiltInFunction String [InitExpression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    93
    | InitSet [InitExpression]
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    94
    | InitAddress InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    95
    | InitNull
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    96
    | InitRange Range
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    97
    | InitTypeCast Identifier InitExpression
090269e528df - Improve parsing of prefix operators
unc0rr
parents:
diff changeset
    98
    deriving Show
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
    99
6618
2d3232069c4b Propagate types on identifiers
unc0rr
parents: 6512
diff changeset
   100
data BaseType = BTUnknown
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   101
    | BTChar
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   102
    | BTString
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   103
    | BTInt
6635
c2fa29fe2a58 Some progress, still can't find the source of bad behavior
unc0rr
parents: 6626
diff changeset
   104
    | BTBool
6649
7f78e8a6db69 Fix a bug with type declaration trying to resolve type being declared
unc0rr
parents: 6635
diff changeset
   105
    | BTFloat
6827
a0e152e68337 Dig into namespaces even more
unc0rr
parents: 6826
diff changeset
   106
    | BTRecord [(String, BaseType)]
6893
69cc0166be8d - Track array size to use for High function
unc0rr
parents: 6843
diff changeset
   107
    | BTArray Range BaseType BaseType
6826
8fadeefdd352 Just some further work
unc0rr
parents: 6816
diff changeset
   108
    | BTFunction BaseType
6967
1224c6fb36c3 Support recurrent function calls. The code is kinda hackish and ugly, but I really spent a few hours thinking on a good solution.
unc0rr
parents: 6895
diff changeset
   109
    | BTFunctionReturn String BaseType
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   110
    | BTPointerTo BaseType
6827
a0e152e68337 Dig into namespaces even more
unc0rr
parents: 6826
diff changeset
   111
    | BTUnresolved String
6653
d45b6dbd2ad6 Move a bit further
unc0rr
parents: 6649
diff changeset
   112
    | BTSet BaseType
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   113
    | BTEnum [String]
6618
2d3232069c4b Propagate types on identifiers
unc0rr
parents: 6512
diff changeset
   114
    | BTVoid
6816
572571ea945e Fix wrong type returned from id2C
unc0rr
parents: 6653
diff changeset
   115
    | BTUnit
6489
e1f0058cfedd Add base type tags to identifiers
unc0rr
parents: 6467
diff changeset
   116
    deriving Show