author  unC0Rr 
Wed, 14 Jan 2015 11:47:50 +0300  
changeset 10787  50a4cdeedb44 
parent 10763  161b9104b7b3 
child 10924  ed1b6914cac5 
permissions  rwrr 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

1 
module PascalParser ( 
10245  2 
pascalUnit, 
3 
mainResultInit 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

4 
) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

5 
where 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

6 

6412  7 
import Text.Parsec 
6272  8 
import Text.Parsec.Token 
6412  9 
import Text.Parsec.Expr 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

10 
import Control.Monad 
6399  11 
import Data.Maybe 
7067
f98ec3aecf4e
A solution to char vs string problem: mark singleletter strings with _S macro
unc0rr
parents:
7066
diff
changeset

12 
import Data.Char 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

13 

6412  14 
import PascalBasics 
6467  15 
import PascalUnitSyntaxTree 
7315  16 

10245  17 

18 
mainResultInit :: Phrase 

19 
mainResultInit = (\(Right a) > a) $ parse phrase "<builtin>" "main:= 0;" 

20 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

21 
knownTypes :: [String] 
6891
ab9843957664
Improve rendering of function types, ranges, and more
unc0rr
parents:
6875
diff
changeset

22 
knownTypes = ["shortstring", "ansistring", "char", "byte"] 
6357  23 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

24 
pascalUnit :: Parsec String u PascalUnit 
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

25 
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

26 
comments 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

27 
u < choice [program, unit, systemUnit, redoUnit] 
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

28 
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

29 
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

30 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

31 
iD :: Parsec String u Identifier 
6275  32 
iD = do 
9166  33 
i < identifier pas 
6275  34 
comments 
9166  35 
when (i == "not") $ unexpected "'not' used as an identifier" 
36 
return $ Identifier i BTUnknown 

7315  37 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

38 
unit :: Parsec String u PascalUnit 
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

39 
unit = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

40 
string' "unit" >> comments 
6275  41 
name < iD 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

42 
void $ 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

43 
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

44 
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

45 
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

46 
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

47 
return $ Unit name int impl Nothing Nothing 
6275  48 

7315  49 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

50 
reference :: Parsec String u Reference 
6275  51 
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

52 
where 
6275  53 
term = comments >> choice [ 
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

54 
parens pas (liftM RefExpression expression >>= postfixes) >>= postfixes 
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

55 
, try $ typeCast >>= postfixes 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

56 
, char' '@' >> liftM Address reference >>= postfixes 
8442  57 
, liftM SimpleReference iD >>= postfixes 
6275  58 
] <?> "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

59 

7315  60 
table = [ 
6275  61 
] 
7315  62 

6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

63 
postfixes r = many postfix >>= return . foldl (flip ($)) r 
6386
7d7703b26bda
Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents:
6357
diff
changeset

64 
postfix = choice [ 
7d7703b26bda
Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents:
6357
diff
changeset

65 
parens pas (option [] parameters) >>= return . FunCall 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

66 
, char' '^' >> return Dereference 
6386
7d7703b26bda
Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents:
6357
diff
changeset

67 
, (brackets pas) (commaSep1 pas $ expression) >>= return . ArrayElement 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

68 
, (char' '.' >> notFollowedBy (char' '.')) >> liftM (flip RecordField) reference 
6386
7d7703b26bda
Prefix followed by prefix issue solved. Also some slight improvements.
unc0rr
parents:
6357
diff
changeset

69 
] 
6316  70 

6450  71 
typeCast = do 
72 
t < choice $ map (\s > try $ caseInsensitiveString s >>= \i > notFollowedBy alphaNum >> return i) knownTypes 

6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

73 
e < parens pas expression 
6450  74 
comments 
6618  75 
return $ TypeCast (Identifier t BTUnknown) e 
7315  76 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

77 
varsDecl1, varsDecl :: Bool > Parsec String u [TypeVarDeclaration] 
7315  78 
varsDecl1 = varsParser sepEndBy1 
6290  79 
varsDecl = varsParser sepEndBy 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

80 

b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

81 
varsParser :: 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

82 
(Parsec String u TypeVarDeclaration 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

83 
> Parsec String u String 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

84 
> Parsec 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

85 
String u [TypeVarDeclaration]) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

86 
> Bool 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

87 
> Parsec 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

88 
String u [TypeVarDeclaration] 
6277  89 
varsParser m endsWithSemi = do 
6290  90 
vs < m (aVarDecl endsWithSemi) (semi pas) 
91 
return vs 

92 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

93 
aVarDecl :: Bool > Parsec String u TypeVarDeclaration 
6290  94 
aVarDecl endsWithSemi = do 
10353  95 
isVar < liftM (\i > i == Just "var"  i == Just "out") $ 
7317  96 
if not endsWithSemi then 
97 
optionMaybe $ choice [ 

98 
try $ string "var" 

99 
, try $ string "const" 

100 
, try $ string "out" 

101 
] 

102 
else 

103 
return Nothing 

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

104 
comments 
6290  105 
ids < do 
106 
i < (commaSep1 pas) $ (try iD <?> "variable declaration") 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

107 
char' ':' 
6290  108 
return i 
109 
comments 

110 
t < typeDecl <?> "variable type declaration" 

111 
comments 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

112 
initialization < option Nothing $ do 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

113 
char' '=' 
6275  114 
comments 
6355  115 
e < initExpression 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

116 
comments 
6290  117 
return (Just e) 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

118 
return $ VarDeclaration isVar False (ids, t) initialization 
6275  119 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

120 
constsDecl :: Parsec String u [TypeVarDeclaration] 
6275  121 
constsDecl = do 
6317
83b93a2d2741
Improve parsing of complex references like "a^[b[c], d]"
unc0rr
parents:
6316
diff
changeset

122 
vs < many1 (try (aConstDecl >>= \i > semi pas >> return i) >>= \i > comments >> return i) 
6275  123 
comments 
6277  124 
return vs 
6275  125 
where 
126 
aConstDecl = do 

127 
comments 

6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

128 
i < iD 
6444  129 
t < optionMaybe $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

130 
char' ':' 
6275  131 
comments 
132 
t < typeDecl 

6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

133 
comments 
6444  134 
return t 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

135 
char' '=' 
6275  136 
comments 
6355  137 
e < initExpression 
6275  138 
comments 
7317  139 
return $ VarDeclaration False (isNothing t) ([i], fromMaybe (DeriveType e) t) (Just e) 
7315  140 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

141 
typeDecl :: Parsec String u 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

142 
typeDecl = choice [ 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

143 
char' '^' >> typeDecl >>= return . PointerTo 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

144 
, try (string' "shortstring") >> return String 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

145 
, try (string' "string") >> optionMaybe (brackets pas $ integer pas) >> return String 
10120  146 
, try (string' "ansistring") >> optionMaybe (brackets pas $ integer pas) >> return AString 
6290  147 
, arrayDecl 
6277  148 
, recordDecl 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

149 
, setDecl 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

150 
, functionType 
6355  151 
, sequenceDecl >>= return . Sequence 
6489  152 
, try iD >>= return . SimpleType 
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

153 
, 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

154 
] <?> "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

155 
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

156 
arrayDecl = do 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

157 
try $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

158 
optional $ (try $ string' "packed") >> comments 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

159 
string' "array" 
6270
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

160 
comments 
6426  161 
r < option [] $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

162 
char' '[' 
6426  163 
r < commaSep pas rangeDecl 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

164 
char' ']' 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

165 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

166 
return r 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

167 
string' "of" 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

168 
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

169 
t < typeDecl 
6426  170 
if null r then 
171 
return $ ArrayDecl Nothing t 

172 
else 

8442  173 
return $ foldr (\a b > ArrayDecl (Just a) b) (ArrayDecl (Just $ head r) t) (tail r) 
6277  174 
recordDecl = do 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

175 
try $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

176 
optional $ (try $ string' "packed") >> comments 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

177 
string' "record" 
6277  178 
comments 
179 
vs < varsDecl True 

6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

180 
union < optionMaybe $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

181 
string' "case" 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

182 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

183 
void $ iD 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

184 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

185 
string' "of" 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

186 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

187 
many unionCase 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

188 
string' "end" 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

189 
return $ RecordType vs union 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

190 
setDecl = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

191 
try $ string' "set" >> void space 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

192 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

193 
string' "of" 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

194 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

195 
liftM Set typeDecl 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

196 
unionCase = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

197 
void $ try $ commaSep pas $ (void $ iD) <> (void $ integer pas) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

198 
char' ':' 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

199 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

200 
u < parens pas $ varsDecl True 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

201 
char' ';' 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

202 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

203 
return u 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

204 
sequenceDecl = (parens pas) $ (commaSep pas) (iD >>= \i > optional (spaces >> char' '=' >> spaces >> integer pas) >> return i) 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

205 
functionType = do 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

206 
fp < try (string "function") <> try (string "procedure") 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

207 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

208 
vs < option [] $ parens pas $ varsDecl False 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

209 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

210 
ret < if (fp == "function") then do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

211 
char' ':' 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

212 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

213 
ret < typeDecl 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

214 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

215 
return ret 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

216 
else 
6826  217 
return VoidType 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

218 
optional $ try $ char' ';' >> comments >> string' "cdecl" 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

219 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

220 
return $ FunctionType ret vs 
6270
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

221 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

222 
typesDecl :: Parsec String u [TypeVarDeclaration] 
6277  223 
typesDecl = many (aTypeDecl >>= \t > comments >> return t) 
224 
where 

225 
aTypeDecl = do 

226 
i < try $ do 

227 
i < iD <?> "type declaration" 

228 
comments 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

229 
char' '=' 
6277  230 
return i 
231 
comments 

232 
t < typeDecl 

233 
comments 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

234 
void $ semi pas 
6277  235 
comments 
236 
return $ TypeDeclaration i t 

6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

237 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

238 
rangeDecl :: Parsec String u 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

239 
rangeDecl = choice [ 
6277  240 
try $ rangeft 
241 
, 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

242 
] <?> "range declaration" 
6277  243 
where 
244 
rangeft = do 

6388  245 
e1 < initExpression 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

246 
string' ".." 
6388  247 
e2 < initExpression 
7315  248 
return $ RangeFromTo e1 e2 
249 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

250 
typeVarDeclaration :: Bool > Parsec String u [TypeVarDeclaration] 
6277  251 
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

252 
varSection, 
6275  253 
constSection, 
6277  254 
typeSection, 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

255 
funcDecl, 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

256 
operatorDecl 
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 
] 
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

258 
where 
8020  259 

260 
fixInit v = concat $ map (\x > case x of 

261 
VarDeclaration a b (ids, t) c > 

262 
let typeId = (Identifier ((\(Identifier i _) > i) (head ids) ++ "_tt") BTUnknown) in 

263 
let res = [TypeDeclaration typeId t, VarDeclaration a b (ids, (SimpleType typeId)) c] in 

264 
case t of 

265 
RecordType _ _ > res  create a separated type declaration 

266 
ArrayDecl _ _ > res 

267 
_ > [x] 

268 
_ > error ("checkInit:\n" ++ (show v))) v 

269 

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

270 
varSection = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

271 
try $ string' "var" 
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

272 
comments 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

273 
v < varsDecl1 True <?> "variable declaration" 
6272  274 
comments 
8020  275 
return $ fixInit v 
6275  276 

277 
constSection = do 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

278 
try $ string' "const" 
6275  279 
comments 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

280 
c < constsDecl <?> "const declaration" 
6275  281 
comments 
8020  282 
return $ fixInit c 
6277  283 

284 
typeSection = do 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

285 
try $ string' "type" 
6277  286 
comments 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

287 
t < typesDecl <?> "type declaration" 
6277  288 
comments 
289 
return t 

7315  290 

6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

291 
operatorDecl = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

292 
try $ string' "operator" 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

293 
comments 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

294 
i < manyTill anyChar space 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

295 
comments 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

296 
vs < parens pas $ varsDecl False 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

297 
comments 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

298 
rid < iD 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

299 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

300 
char' ':' 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

301 
comments 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

302 
ret < typeDecl 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

303 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

304 
 return ret 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

305 
 ^^^^^^^^^^ wth was this??? 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

306 
char' ';' 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

307 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

308 
forward < liftM isJust $ optionMaybe (try (string' "forward;") >> comments) 
7513  309 
inline < liftM (any (== "inline;")) $ many functionDecorator 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

310 
b < if isImpl && (not forward) then 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

311 
liftM Just functionBody 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

312 
else 
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

313 
return Nothing 
7513  314 
return $ [OperatorDeclaration i rid inline ret vs b] 
6443
23364a5fcc86
 Help parser by dropping that insane formatting syntax in str function
unc0rr
parents:
6426
diff
changeset

315 

7315  316 

6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

317 
funcDecl = do 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

318 
fp < try (string "function") <> try (string "procedure") 
6270
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

319 
comments 
6275  320 
i < iD 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

321 
vs < option [] $ parens pas $ varsDecl False 
6270
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

322 
comments 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

323 
ret < if (fp == "function") then do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

324 
char' ':' 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

325 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

326 
ret < typeDecl 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

327 
comments 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

328 
return ret 
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

329 
else 
6836  330 
return VoidType 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

331 
char' ';' 
6399  332 
comments 
6425
1ef4192aa80d
 Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6417
diff
changeset

333 
forward < liftM isJust $ optionMaybe (try (string "forward;") >> comments) 
8020  334 
decorators < many functionDecorator 
335 
let inline = any (== "inline;") decorators 

336 
overload = any (== "overload;") decorators 

10129
cd2a64a1f4aa
 Pas2C: make use of 'external' function decorator
unc0rr
parents:
10120
diff
changeset

337 
external = any (== "external;") decorators 
cd2a64a1f4aa
 Pas2C: make use of 'external' function decorator
unc0rr
parents:
10120
diff
changeset

338 
 TODO: don't mangle external functions names (and remove fpcrtl.h defines hacks) 
cd2a64a1f4aa
 Pas2C: make use of 'external' function decorator
unc0rr
parents:
10120
diff
changeset

339 
b < if isImpl && (not forward) && (not external) 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

340 
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

341 
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

342 
return Nothing 
10129
cd2a64a1f4aa
 Pas2C: make use of 'external' function decorator
unc0rr
parents:
10120
diff
changeset

343 
return $ [FunctionDeclaration i inline overload external ret vs b] 
7315  344 

7513  345 
functionDecorator = do 
346 
d < choice [ 

347 
try $ string "inline;" 

348 
, try $ caseInsensitiveString "cdecl;" 

349 
, try $ string "overload;" 

350 
, try $ string "export;" 

351 
, try $ string "varargs;" 

10129
cd2a64a1f4aa
 Pas2C: make use of 'external' function decorator
unc0rr
parents:
10120
diff
changeset

352 
, try (string' "external") >> comments >> iD >> comments >> 
cd2a64a1f4aa
 Pas2C: make use of 'external' function decorator
unc0rr
parents:
10120
diff
changeset

353 
optional (string' "name" >> comments >> stringLiteral pas) >> string' ";" >> return "external;" 
7513  354 
] 
355 
comments 

356 
return d 

7315  357 

358 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

359 
program :: Parsec String u PascalUnit 
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

360 
program = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

361 
string' "program" 
6275  362 
comments 
363 
name < iD 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

364 
(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

365 
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

366 
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

367 
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

368 
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

369 
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

370 
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

371 
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

372 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

373 
char' '.' 
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

374 
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

375 
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

376 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

377 
interface :: Parsec String u Interface 
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

378 
interface = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

379 
string' "interface" 
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

380 
comments 
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

381 
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

382 
comments 
6277  383 
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

384 
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

385 
return $ Interface u (TypesAndVars tv) 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

386 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

387 
implementation :: Parsec String u Implementation 
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

388 
implementation = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

389 
string' "implementation" 
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

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 
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

392 
comments 
6277  393 
tv < typeVarDeclaration True 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

394 
string' "end." 
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

395 
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

396 
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

397 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

398 
expression :: Parsec String u Expression 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

399 
expression = do 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

400 
buildExpressionParser table term <?> "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

401 
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

402 
term = comments >> choice [ 
6618  403 
builtInFunction expression >>= \(n, e) > return $ BuiltInFunCall e (SimpleReference (Identifier n BTUnknown)) 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

404 
, try (parens pas $ expression >>= \e > notFollowedBy (comments >> char' '.') >> return e) 
6450  405 
, brackets pas (commaSep pas iD) >>= return . SetExpression 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

406 
, try $ integer pas >>= \i > notFollowedBy (char' '.') >> (return . NumberLiteral . show) i 
6452  407 
, float pas >>= return . FloatLiteral . show 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

408 
, try $ integer pas >>= return . NumberLiteral . show 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

409 
, try (string' "_S" >> stringLiteral pas) >>= return . StringLiteral 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

410 
, try (string' "_P" >> stringLiteral pas) >>= return . PCharLiteral 
7067
f98ec3aecf4e
A solution to char vs string problem: mark singleletter strings with _S macro
unc0rr
parents:
7066
diff
changeset

411 
, stringLiteral pas >>= return . strOrChar 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

412 
, try (string' "#$") >> many hexDigit >>= \c > comments >> return (HexCharCode c) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

413 
, char' '#' >> many digit >>= \c > comments >> return (CharCode c) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

414 
, char' '$' >> many hexDigit >>= \h > comments >> return (HexNumber h) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

415 
, char' '' >> expression >>= return . PrefixOp "" 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

416 
, char' '' >> reference >>= return . PrefixOp "" . Reference 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

417 
, (try $ string' "not" >> notFollowedBy comments) >> unexpected "'not'" 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

418 
, try $ string' "nil" >> return Null 
6275  419 
, 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

420 
] <?> "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

421 

7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

422 
table = [ 
8020  423 
[ Prefix (reservedOp pas "not">> return (PrefixOp "not")) 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

424 
, Prefix (try (char' '') >> return (PrefixOp ""))] 
8020  425 
, 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

426 
[ Infix (char' '*' >> return (BinOp "*")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

427 
, Infix (char' '/' >> return (BinOp "/")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

428 
, Infix (try (string' "div") >> return (BinOp "div")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

429 
, Infix (try (string' "mod") >> return (BinOp "mod")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

430 
, Infix (try (string' "in") >> return (BinOp "in")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

431 
, Infix (try $ string' "and" >> return (BinOp "and")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

432 
, Infix (try $ string' "shl" >> return (BinOp "shl")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

433 
, Infix (try $ string' "shr" >> return (BinOp "shr")) AssocLeft 
6275  434 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

435 
, [ Infix (char' '+' >> return (BinOp "+")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

436 
, Infix (char' '' >> return (BinOp "")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

437 
, Infix (try $ string' "or" >> return (BinOp "or")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

438 
, Infix (try $ string' "xor" >> return (BinOp "xor")) AssocLeft 
6275  439 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

440 
, [ Infix (try (string' "<>") >> return (BinOp "<>")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

441 
, Infix (try (string' "<=") >> return (BinOp "<=")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

442 
, Infix (try (string' ">=") >> return (BinOp ">=")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

443 
, Infix (char' '<' >> return (BinOp "<")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

444 
, Infix (char' '>' >> return (BinOp ">")) AssocNone 
6275  445 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

446 
{, [ Infix (try $ string' "shl" >> return (BinOp "shl")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

447 
, Infix (try $ string' "shr" >> return (BinOp "shr")) AssocNone 
7043
7c080e5ac8d0
Some work to make more units compile after conversion to c
unc0rr
parents:
6897
diff
changeset

448 
] 
8442  449 
, [ 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

450 
Infix (try $ string' "or" >> return (BinOp "or")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

451 
, Infix (try $ string' "xor" >> return (BinOp "xor")) AssocLeft 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

452 
]} 
7069  453 
, [ 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

454 
Infix (char' '=' >> return (BinOp "=")) AssocNone 
7069  455 
] 
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

456 
] 
7067
f98ec3aecf4e
A solution to char vs string problem: mark singleletter strings with _S macro
unc0rr
parents:
7066
diff
changeset

457 
strOrChar [a] = CharCode . show . ord $ a 
7315  458 
strOrChar a = StringLiteral a 
459 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

460 
phrasesBlock :: Parsec String u 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

461 
phrasesBlock = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

462 
try $ string' "begin" 
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

463 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

464 
p < manyTill phrase (try $ string' "end" >> notFollowedBy alphaNum) 
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

465 
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

466 
return $ Phrases p 
7315  467 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

468 
phrase :: Parsec String u 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

469 
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

470 
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

471 
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

472 
, 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

473 
, whileCycle 
6275  474 
, 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

475 
, switchCase 
6275  476 
, withBlock 
477 
, forCycle 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

478 
, (try $ reference >>= \r > string' ":=" >> return r) >>= \r > comments >> expression >>= return . Assignment r 
6895  479 
, builtInFunction expression >>= \(n, e) > return $ BuiltInFunctionCall e (SimpleReference (Identifier n BTUnknown)) 
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

480 
, procCall 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

481 
, char' ';' >> comments >> return NOP 
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

482 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

483 
optional $ 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

484 
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

485 
return o 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

486 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

487 
ifBlock :: Parsec String u 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

488 
ifBlock = do 
7642
91fce82f9c6f
fix parsing of 'if': identifiers can also contain underscores
sheepluva
parents:
7641
diff
changeset

489 
try $ string "if" >> notFollowedBy (alphaNum <> 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

490 
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

491 
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

492 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

493 
string' "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

494 
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

495 
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

496 
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

497 
o2 < optionMaybe $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

498 
try $ string' "else" >> void space 
6270
0a99f73dd8dd
Improve pascal parser, now it is able to successfully parse uGame.pas (though it eats all comments). Many things are still missing. Well, it's just a matter of time to implement the rest. All basic work is already done anyway.
unc0rr
parents:
4353
diff
changeset

499 
comments 
6450  500 
o < option NOP 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

501 
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

502 
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

503 
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

504 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

505 
whileCycle :: Parsec String u 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

506 
whileCycle = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

507 
try $ string' "while" 
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 
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

509 
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

510 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

511 
string' "do" 
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 
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

513 
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

514 
return $ WhileCycle e o 
4353
671d66ba3af6
Dumb parser of pascal, and a program which lists unit dependencies
unC0Rr
parents:
diff
changeset

515 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

516 
withBlock :: Parsec String u Phrase 
6275  517 
withBlock = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

518 
try $ string' "with" >> void space 
6275  519 
comments 
6387  520 
rs < (commaSep1 pas) reference 
6275  521 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

522 
string' "do" 
6275  523 
comments 
524 
o < phrase 

6387  525 
return $ foldr WithBlock o rs 
7315  526 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

527 
repeatCycle :: Parsec String u Phrase 
6275  528 
repeatCycle = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

529 
try $ string' "repeat" >> void space 
6275  530 
comments 
531 
o < many phrase 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

532 
string' "until" 
6275  533 
comments 
534 
e < expression 

535 
comments 

536 
return $ RepeatCycle e o 

537 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

538 
forCycle :: Parsec String u Phrase 
6275  539 
forCycle = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

540 
try $ string' "for" >> void space 
6275  541 
comments 
542 
i < iD 

543 
comments 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

544 
string' ":=" 
6275  545 
comments 
546 
e1 < expression 

547 
comments 

7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

548 
up < liftM (== Just "to") $ 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

549 
optionMaybe $ choice [ 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

550 
try $ string "to" 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

551 
, try $ string "downto" 
8442  552 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

553 
choice [string' "to", string' "downto"] 
6275  554 
comments 
555 
e2 < expression 

556 
comments 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

557 
string' "do" 
6275  558 
comments 
559 
p < phrase 

560 
comments 

7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

561 
return $ ForCycle i e1 e2 p up 
7315  562 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

563 
switchCase :: Parsec String u 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

564 
switchCase = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

565 
try $ string' "case" 
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

566 
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

567 
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

568 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

569 
string' "of" 
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

570 
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

571 
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

572 
o2 < optionMaybe $ do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

573 
try $ string' "else" >> notFollowedBy alphaNum 
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

574 
comments 
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

575 
o < many 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

576 
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

577 
return o 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

578 
string' "end" 
6399  579 
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

580 
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

581 
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

582 
aCase = do 
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

583 
e < (commaSep pas) $ (liftM InitRange rangeDecl <> 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

584 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

585 
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

586 
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

587 
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

588 
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

589 
return (e, p) 
7315  590 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

591 
procCall :: Parsec String u 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

592 
procCall = do 
6450  593 
r < 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

594 
p < option [] $ (parens pas) parameters 
6450  595 
return $ ProcCall r 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

596 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

597 
parameters :: Parsec String u [Expression] 
6275  598 
parameters = (commaSep pas) expression <?> "parameters" 
7315  599 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

600 
functionBody :: Parsec String u (TypesAndVars, 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

601 
functionBody = do 
6399  602 
tv < typeVarDeclaration True 
603 
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

604 
p < phrasesBlock 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

605 
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

606 
comments 
6399  607 
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

608 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

609 
uses :: Parsec String u Uses 
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

610 
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

611 
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

612 
u = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

613 
string' "uses" 
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

614 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

615 
ulist < (iD >>= \i > comments >> return i) `sepBy1` (char' ',' >> comments) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

616 
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

617 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

618 
return ulist 
6355  619 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

620 
initExpression :: Parsec String u InitExpression 
6355  621 
initExpression = buildExpressionParser table term <?> "initialization expression" 
622 
where 

623 
term = comments >> choice [ 

8442  624 
liftM (uncurry BuiltInFunction) $ builtInFunction initExpression 
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

625 
, try $ brackets pas (commaSep pas $ initExpression) >>= return . InitSet 
9954  626 
, try $ parens pas (commaSep pas $ initExpression) >>= \ia > when ((notRecord $ head ia) && (null $ tail ia)) mzero >> return (InitArray ia) 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

627 
, try $ parens pas (sepEndBy recField (char' ';' >> comments)) >>= return . InitRecord 
7690
6ef121a80cb0
Treat init expression in parens as just expression instead of array initializer when there's only one entry in expressions list
unc0rr
parents:
7642
diff
changeset

628 
, parens pas initExpression 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

629 
, try $ integer pas >>= \i > notFollowedBy (char' '.') >> (return . InitNumber . show) i 
6355  630 
, try $ float pas >>= return . InitFloat . show 
6388  631 
, try $ integer pas >>= return . InitNumber . show 
10763  632 
, try (string' "_S" >> stringLiteral pas) >>= return . InitString 
633 
, try (string' "_P" >> stringLiteral pas) >>= return . InitPChar 

6355  634 
, stringLiteral pas >>= return . InitString 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

635 
, char' '#' >> many digit >>= \c > comments >> return (InitChar c) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

636 
, char' '$' >> many hexDigit >>= \h > comments >> return (InitHexNumber h) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

637 
, char' '@' >> initExpression >>= \c > comments >> return (InitAddress c) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

638 
, try $ string' "nil" >> return InitNull 
10131
4b4a043111f4
 pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10129
diff
changeset

639 
, try itypeCast 
6355  640 
, iD >>= return . InitReference 
641 
] 

7315  642 

9954  643 
notRecord (InitRecord _) = False 
644 
notRecord _ = True 

645 

6355  646 
recField = do 
647 
i < iD 

648 
spaces 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

649 
char' ':' 
6355  650 
spaces 
651 
e < initExpression 

652 
spaces 

653 
return (i ,e) 

654 

7315  655 
table = [ 
7066
12cc2bd84b0b
Make pas2c even more happier with uGears.c, allow assigning arrays in some cases
unc0rr
parents:
7043
diff
changeset

656 
[ 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

657 
Prefix (char' '' >> return (InitPrefixOp "")) 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

658 
,Prefix (try (string' "not") >> return (InitPrefixOp "not")) 
7066
12cc2bd84b0b
Make pas2c even more happier with uGears.c, allow assigning arrays in some cases
unc0rr
parents:
7043
diff
changeset

659 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

660 
, [ Infix (char' '*' >> return (InitBinOp "*")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

661 
, Infix (char' '/' >> return (InitBinOp "/")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

662 
, Infix (try (string' "div") >> return (InitBinOp "div")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

663 
, Infix (try (string' "mod") >> return (InitBinOp "mod")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

664 
, Infix (try $ string' "and" >> return (InitBinOp "and")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

665 
, Infix (try $ string' "shl" >> return (InitBinOp "shl")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

666 
, Infix (try $ string' "shr" >> return (InitBinOp "shr")) AssocNone 
6355  667 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

668 
, [ Infix (char' '+' >> return (InitBinOp "+")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

669 
, Infix (char' '' >> return (InitBinOp "")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

670 
, Infix (try $ string' "or" >> return (InitBinOp "or")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

671 
, Infix (try $ string' "xor" >> return (InitBinOp "xor")) AssocLeft 
6355  672 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

673 
, [ Infix (try (string' "<>") >> return (InitBinOp "<>")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

674 
, Infix (try (string' "<=") >> return (InitBinOp "<=")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

675 
, Infix (try (string' ">=") >> return (InitBinOp ">=")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

676 
, Infix (char' '<' >> return (InitBinOp "<")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

677 
, Infix (char' '>' >> return (InitBinOp ">")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

678 
, Infix (char' '=' >> return (InitBinOp "=")) AssocNone 
6355  679 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

680 
{, [ Infix (try $ string' "and" >> return (InitBinOp "and")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

681 
, Infix (try $ string' "or" >> return (InitBinOp "or")) AssocLeft 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

682 
, Infix (try $ string' "xor" >> return (InitBinOp "xor")) AssocLeft 
6355  683 
] 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

684 
, [ Infix (try $ string' "shl" >> return (InitBinOp "shl")) AssocNone 
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

685 
, Infix (try $ string' "shr" >> return (InitBinOp "shr")) AssocNone 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

686 
]} 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

687 
, [Prefix (try (string' "not") >> return (InitPrefixOp "not"))] 
6355  688 
] 
6388  689 

6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

690 
itypeCast = do 
10131
4b4a043111f4
 pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10129
diff
changeset

691 
t < choice $ map (\s > try $ caseInsensitiveString s >>= \i > notFollowedBy alphaNum >> return i) knownTypes 
4b4a043111f4
 pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10129
diff
changeset

692 
t < iD 
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

693 
i < parens pas initExpression 
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6452
diff
changeset

694 
comments 
10131
4b4a043111f4
 pas2c recognizes typecasts in initialization expressions
unc0rr
parents:
10129
diff
changeset

695 
return $ InitTypeCast t i 
7315  696 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

697 
builtInFunction :: Parsec String u a > Parsec String u (String, [a]) 
6388  698 
builtInFunction e = do 
699 
name < choice $ map (\s > try $ caseInsensitiveString s >>= \i > notFollowedBy alphaNum >> return i) builtin 

700 
spaces 

6897  701 
exprs < option [] $ parens pas $ option [] $ commaSep1 pas $ e 
6388  702 
spaces 
703 
return (name, exprs) 

6512  704 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

705 
systemUnit :: Parsec String u PascalUnit 
6512  706 
systemUnit = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

707 
string' "system;" 
6512  708 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

709 
string' "type" 
6512  710 
comments 
711 
t < typesDecl 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

712 
string' "var" 
6512  713 
v < varsDecl True 
714 
return $ System (t ++ v) 

7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

715 

10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

716 
redoUnit :: Parsec String u PascalUnit 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

717 
redoUnit = do 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

718 
string' "redo;" 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

719 
comments 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

720 
string' "type" 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

721 
comments 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

722 
t < typesDecl 
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10111
diff
changeset

723 
string' "var" 
7429
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

724 
v < varsDecl True 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

725 
return $ Redo (t ++ v) 
fcf13e40d6b6
Changes to pas2c  unreviewed apart from cursory glance and compile test.
xymeng
parents:
7317
diff
changeset

726 