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