tools/PascalParser.hs
author unc0rr
Fri, 25 Nov 2011 18:36:12 +0300
changeset 6425 1ef4192aa80d
parent 6417 eae5900fd8a4
child 6426 2d44f6561e72
permissions -rw-r--r--
- Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas - Improve rendering of C code - Fix preprocessor issues, define "FPC" - Make pas2c convert unit along with its dependencies into corresponding .c files, so you just call it for hwengine.pas to convert the whole engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     1
module PascalParser where
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
     2
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
     3
import Text.Parsec
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     4
import Text.Parsec.Char
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     5
import Text.Parsec.Token
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     6
import Text.Parsec.Language
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
     7
import Text.Parsec.Expr
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     8
import Text.Parsec.Prim
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
     9
import Text.Parsec.Combinator
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
    10
import Text.Parsec.String
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    11
import Control.Monad
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
    12
import Data.Maybe
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    13
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
    14
import PascalBasics
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
    15
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    16
data PascalUnit =
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
    17
    Program Identifier Implementation Phrase
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    18
    | Unit Identifier Interface Implementation (Maybe Initialize) (Maybe Finalize)
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    19
    deriving Show
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    20
data Interface = Interface Uses TypesAndVars
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    21
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    22
data Implementation = Implementation Uses TypesAndVars
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    23
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    24
data Identifier = Identifier String
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    25
    deriving Show
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    26
data TypesAndVars = TypesAndVars [TypeVarDeclaration]
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    27
    deriving Show
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    28
data TypeVarDeclaration = TypeDeclaration Identifier TypeDecl
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    29
    | VarDeclaration Bool ([Identifier], TypeDecl) (Maybe InitExpression)
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
    30
    | FunctionDeclaration Identifier TypeDecl [TypeVarDeclaration] (Maybe (TypesAndVars, Phrase))
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    31
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    32
data TypeDecl = SimpleType Identifier
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    33
    | RangeType Range
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    34
    | Sequence [Identifier]
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
    35
    | ArrayDecl (Maybe Range) TypeDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
    36
    | RecordType [TypeVarDeclaration] (Maybe [[TypeVarDeclaration]])
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
    37
    | PointerTo TypeDecl
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
    38
    | String Integer
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
    39
    | Set TypeDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
    40
    | FunctionType TypeDecl [TypeVarDeclaration]
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    41
    | UnknownType
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    42
    deriving Show
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    43
data Range = Range Identifier
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    44
           | RangeFromTo InitExpression InitExpression
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    45
    deriving Show
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    46
data Initialize = Initialize String
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    47
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    48
data Finalize = Finalize String
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    49
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    50
data Uses = Uses [Identifier]
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    51
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    52
data Phrase = ProcCall Identifier [Expression]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    53
        | IfThenElse Expression Phrase (Maybe Phrase)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    54
        | WhileCycle Expression Phrase
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    55
        | RepeatCycle Expression [Phrase]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    56
        | ForCycle Identifier Expression Expression Phrase
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    57
        | WithBlock Reference Phrase
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    58
        | Phrases [Phrase]
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
    59
        | SwitchCase Expression [([Expression], Phrase)] (Maybe Phrase)
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    60
        | Assignment Reference Expression
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    61
    deriving Show
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    62
data Expression = Expression String
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    63
    | BuiltInFunCall [Expression] Reference
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    64
    | PrefixOp String Expression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    65
    | PostfixOp String Expression
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    66
    | BinOp String Expression Expression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    67
    | StringLiteral String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    68
    | CharCode String
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    69
    | NumberLiteral String
6357
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
    70
    | FloatLiteral String
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
    71
    | HexNumber String
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    72
    | Reference Reference
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
    73
    | Null
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    74
    deriving Show
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
    75
data Reference = ArrayElement [Expression] Reference
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
    76
    | FunCall [Expression] Reference
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
    77
    | SimpleReference Identifier
6315
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    78
    | Dereference Reference
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    79
    | RecordField Reference Reference
1f7a7a330c59 Rearrange token types
unc0rr
parents: 6310
diff changeset
    80
    | Address Reference
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
    81
    deriving Show
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    82
data InitExpression = InitBinOp String InitExpression InitExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    83
    | InitPrefixOp String InitExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    84
    | InitReference Identifier
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    85
    | InitArray [InitExpression]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    86
    | InitRecord [(Identifier, InitExpression)]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    87
    | InitFloat String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    88
    | InitNumber String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    89
    | InitHexNumber String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    90
    | InitString String
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    91
    | InitChar String
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
    92
    | BuiltInFunction String [InitExpression]
6391
bd5851ab3157 - Parse sets initialization
unc0rr
parents: 6388
diff changeset
    93
    | InitSet [Identifier]
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    94
    | InitNull
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
    95
    deriving Show
6357
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
    96
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    97
pascalUnit = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    98
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
    99
    u <- choice [program, unit]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   100
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   101
    return u
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   102
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   103
iD = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   104
    i <- liftM Identifier (identifier pas)
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   105
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   106
    return i
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   107
        
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   108
unit = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   109
    string "unit" >> comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   110
    name <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   111
    semi pas
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   112
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   113
    int <- interface
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   114
    impl <- implementation
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   115
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   116
    return $ Unit name int impl Nothing Nothing
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   117
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   118
    
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   119
reference = buildExpressionParser table term <?> "reference"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   120
    where
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   121
    term = comments >> choice [
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   122
        parens pas (reference >>= postfixes) >>= postfixes
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   123
        , char '@' >> reference >>= postfixes >>= return . Address
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   124
        , liftM SimpleReference iD >>= postfixes 
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   125
        ] <?> "simple reference"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   126
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   127
    table = [ 
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   128
            [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   129
        ]
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   130
    
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   131
    postfixes r = many postfix >>= return . foldl (flip ($)) r
6386
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   132
    postfix = choice [
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   133
            parens pas (option [] parameters) >>= return . FunCall
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   134
          , char '^' >> return Dereference
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   135
          , (brackets pas) (commaSep1 pas $ expression) >>= return . ArrayElement
7d7703b26bda Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents: 6357
diff changeset
   136
        ]
6316
ac23ba018ed2 Fix inifinite loops
unc0rr
parents: 6315
diff changeset
   137
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   138
    
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   139
varsDecl1 = varsParser sepEndBy1    
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   140
varsDecl = varsParser sepEndBy
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   141
varsParser m endsWithSemi = do
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   142
    vs <- m (aVarDecl endsWithSemi) (semi pas)
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   143
    return vs
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   144
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   145
aVarDecl endsWithSemi = do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   146
    unless endsWithSemi $
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   147
        optional $ choice [
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   148
            try $ string "var"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   149
            , try $ string "const"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   150
            , try $ string "out"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   151
            ]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   152
    comments
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   153
    ids <- do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   154
        i <- (commaSep1 pas) $ (try iD <?> "variable declaration")
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   155
        char ':'
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   156
        return i
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   157
    comments
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   158
    t <- typeDecl <?> "variable type declaration"
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   159
    comments
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   160
    init <- option Nothing $ do
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   161
        char '='
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   162
        comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   163
        e <- initExpression
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   164
        comments
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   165
        return (Just e)
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   166
    return $ VarDeclaration False (ids, t) init
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   167
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   168
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   169
constsDecl = do
6317
83b93a2d2741 Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents: 6316
diff changeset
   170
    vs <- many1 (try (aConstDecl >>= \i -> semi pas >> return i) >>= \i -> comments >> return i)
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   171
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   172
    return vs
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   173
    where
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   174
    aConstDecl = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   175
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   176
        i <- iD <?> "const declaration"
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   177
        optional $ do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   178
            char ':'
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   179
            comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   180
            t <- typeDecl
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   181
            comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   182
            return ()
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   183
        char '='
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   184
        comments
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   185
        e <- initExpression
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   186
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   187
        return $ VarDeclaration False ([i], UnknownType) (Just e)
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   188
        
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   189
typeDecl = choice [
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   190
    char '^' >> typeDecl >>= return . PointerTo
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   191
    , try (string "shortstring") >> return (String 255)
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   192
    , try (string "string") >> optionMaybe (brackets pas $ integer pas) >>= return . String . fromMaybe 255
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   193
    , arrayDecl
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   194
    , recordDecl
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   195
    , setDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   196
    , functionType
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   197
    , sequenceDecl >>= return . Sequence
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   198
    , try (identifier pas) >>= return . SimpleType . Identifier
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   199
    , rangeDecl >>= return . RangeType
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   200
    ] <?> "type declaration"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   201
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   202
    arrayDecl = do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   203
        try $ do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   204
            optional $ (try $ string "packed") >> comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   205
            string "array"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   206
        comments
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   207
        r <- optionMaybe $ do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   208
            char '['
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   209
            r <- rangeDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   210
            char ']'
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   211
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   212
            return r
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   213
        string "of"
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   214
        comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   215
        t <- typeDecl
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   216
        return $ ArrayDecl r t
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   217
    recordDecl = do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   218
        try $ do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   219
            optional $ (try $ string "packed") >> comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   220
            string "record"
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   221
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   222
        vs <- varsDecl True
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   223
        union <- optionMaybe $ do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   224
            string "case"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   225
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   226
            iD
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   227
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   228
            string "of"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   229
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   230
            many unionCase
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   231
        string "end"
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   232
        return $ RecordType vs union
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   233
    setDecl = do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   234
        try $ string "set" >> space
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   235
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   236
        string "of"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   237
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   238
        liftM Set typeDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   239
    unionCase = do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   240
        try $ commaSep pas $ (iD >> return ()) <|> (integer pas >> return ())
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   241
        char ':'
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   242
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   243
        u <- parens pas $ varsDecl True
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   244
        char ';'
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   245
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   246
        return u
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   247
    sequenceDecl = (parens pas) $ (commaSep pas) (iD >>= \i -> optional (spaces >> char '=' >> spaces >> integer pas) >> return i)
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   248
    functionType = do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   249
        fp <- try (string "function") <|> try (string "procedure")
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   250
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   251
        vs <- option [] $ parens pas $ varsDecl False
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   252
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   253
        ret <- if (fp == "function") then do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   254
            char ':'
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   255
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   256
            ret <- typeDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   257
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   258
            return ret
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   259
            else
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   260
            return UnknownType
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   261
        optional $ try $ char ';' >> comments >> string "cdecl"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   262
        comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   263
        return $ FunctionType ret vs
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   264
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   265
typesDecl = many (aTypeDecl >>= \t -> comments >> return t)
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   266
    where
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   267
    aTypeDecl = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   268
        i <- try $ do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   269
            i <- iD <?> "type declaration"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   270
            comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   271
            char '='
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   272
            return i
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   273
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   274
        t <- typeDecl
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   275
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   276
        semi pas
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   277
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   278
        return $ TypeDeclaration i t
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   279
        
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   280
rangeDecl = choice [
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   281
    try $ rangeft
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   282
    , iD >>= return . Range
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   283
    ] <?> "range declaration"
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   284
    where
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   285
    rangeft = do
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   286
    e1 <- initExpression
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   287
    string ".."
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   288
    e2 <- initExpression
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   289
    return $ RangeFromTo e1 e2
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   290
    
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   291
typeVarDeclaration isImpl = (liftM concat . many . choice) [
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   292
    varSection,
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   293
    constSection,
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   294
    typeSection,
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   295
    funcDecl
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   296
    ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   297
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   298
    varSection = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   299
        try $ string "var"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   300
        comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   301
        v <- varsDecl1 True
6272
a93cb9ca9fda - Update to compile with parsec 3.*
unc0rr
parents: 6270
diff changeset
   302
        comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   303
        return v
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   304
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   305
    constSection = do
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   306
        try $ string "const"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   307
        comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   308
        c <- constsDecl
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   309
        comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   310
        return c
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   311
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   312
    typeSection = do
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   313
        try $ string "type"
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   314
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   315
        t <- typesDecl
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   316
        comments
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   317
        return t
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   318
        
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   319
    funcDecl = do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   320
        fp <- try (string "function") <|> try (string "procedure")
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   321
        comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   322
        i <- iD
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   323
        vs <- option [] $ parens pas $ varsDecl False
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   324
        comments
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   325
        ret <- if (fp == "function") then do
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   326
            char ':'
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   327
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   328
            ret <- typeDecl
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   329
            comments
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   330
            return ret
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   331
            else
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   332
            return UnknownType
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   333
        char ';'
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   334
        comments
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   335
        forward <- liftM isJust $ optionMaybe (try (string "forward;") >> comments)
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   336
        many functionDecorator
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   337
        b <- if isImpl && (not forward) then
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   338
                liftM Just functionBody
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   339
                else
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   340
                return Nothing
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   341
        return $ [FunctionDeclaration i ret vs b]
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   342
    functionDecorator = choice [
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   343
        try $ string "inline;"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   344
        , try $ string "cdecl;"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   345
        , try (string "external") >> comments >> iD >> optional (string "name" >> comments >> stringLiteral pas)>> string ";"
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   346
        ] >> comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   347
program = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   348
    string "program"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   349
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   350
    name <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   351
    (char ';')
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   352
    comments
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   353
    comments
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   354
    u <- uses
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   355
    comments
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   356
    tv <- typeVarDeclaration True
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   357
    comments
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   358
    p <- phrase
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   359
    comments
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   360
    char '.'
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   361
    comments
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   362
    return $ Program name (Implementation u (TypesAndVars tv)) p
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   363
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   364
interface = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   365
    string "interface"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   366
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   367
    u <- uses
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   368
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   369
    tv <- typeVarDeclaration False
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   370
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   371
    return $ Interface u (TypesAndVars tv)
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   372
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   373
implementation = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   374
    string "implementation"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   375
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   376
    u <- uses
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   377
    comments
6277
627b5752733a A try to improve parser move (has regressions)
unc0rr
parents: 6275
diff changeset
   378
    tv <- typeVarDeclaration True
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   379
    string "end."
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   380
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   381
    return $ Implementation u (TypesAndVars tv)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   382
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   383
expression = buildExpressionParser table term <?> "expression"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   384
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   385
    term = comments >> choice [
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   386
        builtInFunction expression >>= \(n, e) -> return $ BuiltInFunCall e (SimpleReference (Identifier n))
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   387
        , parens pas $ expression 
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
   388
        , try $ natural pas >>= \i -> notFollowedBy (char '.') >> (return . NumberLiteral . show) i
6357
52cb4807a78c Something
unc0rr
parents: 6355
diff changeset
   389
        , try $ float pas >>= return . FloatLiteral . show
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
   390
        , try $ natural pas >>= return . NumberLiteral . show
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   391
        , stringLiteral pas >>= return . StringLiteral
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   392
        , char '#' >> many digit >>= \c -> comments >> return (CharCode c)
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   393
        , char '$' >> many hexDigit >>=  \h -> comments >> return (HexNumber h)
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents: 6399
diff changeset
   394
        , char '-' >> expression >>= return . PrefixOp "-"
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   395
        , try $ string "nil" >> return Null
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   396
        , reference >>= return . Reference
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   397
        ] <?> "simple expression"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   398
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   399
    table = [ 
6290
c6245ed6cbc0 Some improvements to the parser
unc0rr
parents: 6277
diff changeset
   400
          [  Infix (char '*' >> return (BinOp "*")) AssocLeft
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   401
           , Infix (char '/' >> return (BinOp "/")) AssocLeft
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   402
           , Infix (try (string "div") >> return (BinOp "div")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   403
           , Infix (try (string "mod") >> return (BinOp "mod")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   404
          ]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   405
        , [  Infix (char '+' >> return (BinOp "+")) AssocLeft
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   406
           , Infix (char '-' >> return (BinOp "-")) AssocLeft
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   407
          ]
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   408
        , [Prefix (try (string "not") >> return (PrefixOp "not"))]
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   409
        , [  Infix (try (string "<>") >> return (BinOp "<>")) AssocNone
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   410
           , Infix (try (string "<=") >> return (BinOp "<=")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   411
           , Infix (try (string ">=") >> return (BinOp ">=")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   412
           , Infix (char '<' >> return (BinOp "<")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   413
           , Infix (char '>' >> return (BinOp ">")) AssocNone
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   414
           , Infix (char '=' >> return (BinOp "=")) AssocNone
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   415
          ]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   416
        , [  Infix (try $ string "and" >> return (BinOp "and")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   417
           , Infix (try $ string "or" >> return (BinOp "or")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   418
           , Infix (try $ string "xor" >> return (BinOp "xor")) AssocLeft
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   419
          ]
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   420
        , [  Infix (try $ string "shl" >> return (BinOp "shl")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   421
           , Infix (try $ string "shr" >> return (BinOp "shr")) AssocNone
6307
25cfd9f4a567 Even more improvements to the parser and converter
unc0rr
parents: 6290
diff changeset
   422
          ]
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   423
        ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   424
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   425
phrasesBlock = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   426
    try $ string "begin"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   427
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   428
    p <- manyTill phrase (try $ string "end")
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   429
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   430
    return $ Phrases p
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   431
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   432
phrase = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   433
    o <- choice [
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   434
        phrasesBlock
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   435
        , ifBlock
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   436
        , whileCycle
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   437
        , repeatCycle
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   438
        , switchCase
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   439
        , withBlock
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   440
        , forCycle
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   441
        , (try $ reference >>= \r -> string ":=" >> return r) >>= \r -> expression >>= return . Assignment r
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   442
        , procCall
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   443
        ]
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   444
    optional $ char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   445
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   446
    return o
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   447
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   448
ifBlock = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   449
    try $ string "if"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   450
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   451
    e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   452
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   453
    string "then"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   454
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   455
    o1 <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   456
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   457
    o2 <- optionMaybe $ do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   458
        try $ string "else" >> space
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   459
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   460
        o <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   461
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   462
        return o
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   463
    return $ IfThenElse e o1 o2
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   464
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   465
whileCycle = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   466
    try $ string "while"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   467
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   468
    e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   469
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   470
    string "do"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   471
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   472
    o <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   473
    return $ WhileCycle e o
4353
671d66ba3af6 Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff changeset
   474
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   475
withBlock = do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   476
    try $ string "with" >> space
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   477
    comments
6387
3dcb839b5904 Less code
unc0rr
parents: 6386
diff changeset
   478
    rs <- (commaSep1 pas) reference
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   479
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   480
    string "do"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   481
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   482
    o <- phrase
6387
3dcb839b5904 Less code
unc0rr
parents: 6386
diff changeset
   483
    return $ foldr WithBlock o rs
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   484
    
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   485
repeatCycle = do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   486
    try $ string "repeat" >> space
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   487
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   488
    o <- many phrase
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   489
    string "until"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   490
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   491
    e <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   492
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   493
    return $ RepeatCycle e o
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   494
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   495
forCycle = do
6425
1ef4192aa80d - Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents: 6417
diff changeset
   496
    try $ string "for" >> space
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   497
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   498
    i <- iD
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   499
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   500
    string ":="
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   501
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   502
    e1 <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   503
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   504
    choice [string "to", string "downto"]
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   505
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   506
    e2 <- expression
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   507
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   508
    string "do"
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   509
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   510
    p <- phrase
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   511
    comments
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   512
    return $ ForCycle i e1 e2 p
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   513
    
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   514
switchCase = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   515
    try $ string "case"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   516
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   517
    e <- expression
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   518
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   519
    string "of"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   520
    comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   521
    cs <- many1 aCase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   522
    o2 <- optionMaybe $ do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   523
        try $ string "else"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   524
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   525
        o <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   526
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   527
        return o
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   528
    string "end"
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   529
    comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   530
    return $ SwitchCase e cs o2
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   531
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   532
    aCase = do
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   533
        e <- (commaSep pas) expression
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   534
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   535
        char ':'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   536
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   537
        p <- phrase
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   538
        comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   539
        return (e, p)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   540
    
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   541
procCall = do
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   542
    i <- iD
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   543
    p <- option [] $ (parens pas) parameters
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   544
    return $ ProcCall i p
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   545
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   546
parameters = (commaSep pas) expression <?> "parameters"
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   547
        
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   548
functionBody = do
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   549
    tv <- typeVarDeclaration True
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   550
    comments
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   551
    p <- phrasesBlock
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   552
    char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   553
    comments
6399
a904c735979c Improve parser and converter
unc0rr
parents: 6397
diff changeset
   554
    return (TypesAndVars tv, p)
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   555
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   556
uses = liftM Uses (option [] u)
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   557
    where
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   558
        u = do
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   559
            string "uses"
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   560
            comments
6275
f1b4f37dba22 Many improvements to the parser
unc0rr
parents: 6272
diff changeset
   561
            u <- (iD >>= \i -> comments >> return i) `sepBy1` (char ',' >> comments)
6270
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   562
            char ';'
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   563
            comments
0a99f73dd8dd Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents: 4353
diff changeset
   564
            return u
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   565
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   566
initExpression = buildExpressionParser table term <?> "initialization expression"
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   567
    where
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   568
    term = comments >> choice [
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   569
        liftM (uncurry BuiltInFunction) $ builtInFunction initExpression 
6391
bd5851ab3157 - Parse sets initialization
unc0rr
parents: 6388
diff changeset
   570
        , try $ brackets pas (commaSep pas $ iD) >>= return . InitSet
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   571
        , try $ parens pas (commaSep pas $ initExpression) >>= return . InitArray
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   572
        , parens pas (semiSep pas $ recField) >>= return . InitRecord
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   573
        , try $ integer pas >>= \i -> notFollowedBy (char '.') >> (return . InitNumber . show) i
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   574
        , try $ float pas >>= return . InitFloat . show
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   575
        , try $ integer pas >>= return . InitNumber . show
6355
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   576
        , stringLiteral pas >>= return . InitString
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   577
        , char '#' >> many digit >>= return . InitChar
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   578
        , char '$' >> many hexDigit >>= return . InitHexNumber
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   579
        , try $ string "nil" >> return InitNull
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   580
        , iD >>= return . InitReference
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   581
        ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   582
        
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   583
    recField = do
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   584
        i <- iD
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   585
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   586
        char ':'
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   587
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   588
        e <- initExpression
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   589
        spaces
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   590
        return (i ,e)
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   591
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   592
    table = [ 
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   593
          [  Infix (char '*' >> return (InitBinOp "*")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   594
           , Infix (char '/' >> return (InitBinOp "/")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   595
           , Infix (try (string "div") >> return (InitBinOp "div")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   596
           , Infix (try (string "mod") >> return (InitBinOp "mod")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   597
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   598
        , [  Infix (char '+' >> return (InitBinOp "+")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   599
           , Infix (char '-' >> return (InitBinOp "-")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   600
           , Prefix (char '-' >> return (InitPrefixOp "-"))
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   601
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   602
        , [  Infix (try (string "<>") >> return (InitBinOp "<>")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   603
           , Infix (try (string "<=") >> return (InitBinOp "<=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   604
           , Infix (try (string ">=") >> return (InitBinOp ">=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   605
           , Infix (char '<' >> return (InitBinOp "<")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   606
           , Infix (char '>' >> return (InitBinOp ">")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   607
           , Infix (char '=' >> return (InitBinOp "=")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   608
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   609
        , [  Infix (try $ string "and" >> return (InitBinOp "and")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   610
           , Infix (try $ string "or" >> return (InitBinOp "or")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   611
           , Infix (try $ string "xor" >> return (InitBinOp "xor")) AssocLeft
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   612
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   613
        , [  Infix (try $ string "shl" >> return (InitBinOp "and")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   614
           , Infix (try $ string "shr" >> return (InitBinOp "or")) AssocNone
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   615
          ]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   616
        , [Prefix (try (string "not") >> return (InitPrefixOp "not"))]
734fed7aefd3 Introduce initialization expressions
unc0rr
parents: 6317
diff changeset
   617
        ]
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   618
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   619
builtInFunction e = do
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   620
    name <- choice $ map (\s -> try $ caseInsensitiveString s >>= \i -> notFollowedBy alphaNum >> return i) builtin
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   621
    spaces
6397
6eb58ae8b510 Oops, function call has parens
unc0rr
parents: 6391
diff changeset
   622
    exprs <- parens pas $ commaSep1 pas $ e
6388
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   623
    spaces
14718b2685a3 Recognize some built-in functions
unc0rr
parents: 6387
diff changeset
   624
    return (name, exprs)
6417
eae5900fd8a4 Improve parser a bit, preparation to parsing whole program at once and compiling it into single C file
unc0rr
parents: 6414
diff changeset
   625