1
+ using BabelFish . Interpreter ;
1
2
using enquanto ;
2
3
using enquanto . Model ;
3
4
using NUnit . Framework ;
@@ -9,7 +10,6 @@ namespace Tests
9
10
{
10
11
public class IntrerpreterTests
11
12
{
12
- /*
13
13
private static BuildResult < Parser < EnquantoToken , AST > > Parser ;
14
14
15
15
@@ -20,14 +20,13 @@ public BuildResult<Parser<EnquantoToken, AST>> BuildParser()
20
20
var whileParser = new Parser ( ) ;
21
21
var builder = new ParserBuilder < EnquantoToken , AST > ( ) ;
22
22
Parser = builder . BuildParser ( whileParser , ParserType . EBNF_LL_RECURSIVE_DESCENT , "statement" ) ;
23
- ;
24
23
}
25
24
26
25
return Parser ;
27
26
}
28
27
29
28
30
- public bool CheckIntVariable(InterpreterContext context, string variable, int value)
29
+ public bool CheckIntVariable ( InterpreterContext < EnquantoType > context , string variable , int value )
31
30
{
32
31
var ok = false ;
33
32
if ( context . GetVariable ( variable ) != null )
@@ -67,7 +66,6 @@ public void TestBuildParser()
67
66
{
68
67
var buildResult = BuildParser ( ) ;
69
68
Assert . False ( buildResult . IsError ) ;
70
- var parser = buildResult.Result;
71
69
}
72
70
73
71
[ Test ]
@@ -76,10 +74,9 @@ public void TestCounterProgram()
76
74
var buildResult = BuildParser ( ) ;
77
75
Assert . False ( buildResult . IsError ) ;
78
76
var parser = buildResult . Result ;
79
- var result = parser.Parse("(a:=0; while a < 10 do (print a; a := a +1 ))");
77
+ var result = parser . Parse ( "(a:=0; enquanto a < 10 faça (imprima a; a := a +1 ))" ) ;
80
78
Assert . False ( result . IsError ) ;
81
79
Assert . NotNull ( result . Result ) ;
82
- ;
83
80
}
84
81
85
82
[ Test ]
@@ -88,11 +85,11 @@ public void TestCounterProgramExec()
88
85
var buildResult = BuildParser ( ) ;
89
86
Assert . False ( buildResult . IsError ) ;
90
87
var parser = buildResult . Result ;
91
- var result = parser.Parse("(a:=0; while a < 10 do (print a; a := a +1 ))");
88
+ var result = parser . Parse ( "(a:=0; enquanto a < 10 faça (imprima a; a := a +1 ))" ) ;
92
89
Assert . False ( result . IsError ) ;
93
90
Assert . NotNull ( result . Result ) ;
94
91
var interpreter = new Interpreter ( ) ;
95
- var context = interpreter.Interprete (result.Result, true);
92
+ var context = interpreter . Execute ( result . Result , true ) ;
96
93
Assert . That ( context . variables , Has . Exactly ( 1 ) . Items ) ;
97
94
Assert . True ( CheckIntVariable ( context , "a" , 10 ) ) ;
98
95
}
@@ -104,11 +101,11 @@ public void TestFactorialProgramExec()
104
101
(
105
102
r:=1;
106
103
i:=1;
107
- while i < 11 do
104
+ enquanto i < 11 faça
108
105
(
109
106
r := r * i;
110
- print r;
111
- print i;
107
+ imprima r;
108
+ imprima i;
112
109
i := i + 1 )
113
110
)" ;
114
111
var buildResult = BuildParser ( ) ;
@@ -118,37 +115,32 @@ public void TestFactorialProgramExec()
118
115
Assert . False ( result . IsError ) ;
119
116
Assert . NotNull ( result . Result ) ;
120
117
var interpreter = new Interpreter ( ) ;
121
- var context = interpreter.Interprete (result.Result, true);
118
+ var context = interpreter . Execute ( result . Result , true ) ;
122
119
Assert . That ( context . variables . Count , Is . EqualTo ( 2 ) ) ;
123
120
Assert . True ( CheckIntVariable ( context , "i" , 11 ) ) ;
124
121
Assert . True ( CheckIntVariable ( context , "r" , 3628800 ) ) ;
125
-
126
-
127
- ;
128
122
}
129
123
130
-
131
124
[ Test ]
132
125
public void TestFactorialProgramExecAsIL ( )
133
126
{
134
127
var program = @"
135
128
(
136
129
r:=1;
137
130
i:=1;
138
- while i < 11 do
131
+ enquanto i < 11 faça
139
132
(
140
133
r := r * i;
141
- print """".r;
142
- print """".i;
134
+ imprima """".r;
135
+ imprima """".i;
143
136
i := i + 1 );
144
- return r
137
+ retorne r
145
138
)" ;
146
139
var compiler = new Compiler ( ) ;
147
140
var func = compiler . CompileToFunction ( program ) ;
148
141
Assert . NotNull ( func ) ;
149
142
var f = func ( ) ;
150
143
Assert . That ( f , Is . EqualTo ( 3628800 ) ) ;
151
- ;
152
144
}
153
145
154
146
[ Test ]
@@ -157,7 +149,7 @@ public void TestIfThenElse()
157
149
var buildResult = BuildParser ( ) ;
158
150
Assert . False ( buildResult . IsError ) ;
159
151
var parser = buildResult . Result ;
160
- var result = parser.Parse("if true then (a := \"hello\") else (b := \"world\")");
152
+ var result = parser . Parse ( "se verdadeiro então (a := \" hello\" ) senão (b := \" world\" )" ) ;
161
153
Assert . False ( result . IsError ) ;
162
154
Assert . NotNull ( result . Result ) ;
163
155
@@ -170,15 +162,14 @@ public void TestIfThenElse()
170
162
Assert . True ( ( cond as BoolConstant ) . Value ) ;
171
163
var s = si . ThenStmt ;
172
164
173
- Assert.That(si.ThenStmt , Is.TypeOf<SequenceStatement>());
174
- var thenBlock = si.ThenStmt as SequenceStatement;
165
+ Assert . That ( s , Is . TypeOf < SequenceStatement > ( ) ) ;
166
+ var thenBlock = s as SequenceStatement ;
175
167
Assert . That ( thenBlock . Count , Is . EqualTo ( 1 ) ) ;
176
168
Assert . That ( thenBlock . Get ( 0 ) , Is . TypeOf < AssignStatement > ( ) ) ;
177
169
var thenAssign = thenBlock . Get ( 0 ) as AssignStatement ;
178
170
Assert . That ( thenAssign . VariableName , Is . EqualTo ( "a" ) ) ;
179
171
Assert . That ( thenAssign . Value , Is . TypeOf < StringConstant > ( ) ) ;
180
172
Assert . That ( ( thenAssign . Value as StringConstant ) . Value , Is . EqualTo ( "hello" ) ) ;
181
- ;
182
173
183
174
Assert . That ( si . ElseStmt , Is . TypeOf < SequenceStatement > ( ) ) ;
184
175
var elseBlock = si . ElseStmt as SequenceStatement ;
@@ -196,7 +187,7 @@ public void TestInfiniteWhile()
196
187
var buildResult = BuildParser ( ) ;
197
188
Assert . False ( buildResult . IsError ) ;
198
189
var parser = buildResult . Result ;
199
- var result = parser.Parse("while true do (skip )");
190
+ var result = parser . Parse ( "enquanto verdadeiro faça (pule )" ) ;
200
191
Assert . False ( result . IsError ) ;
201
192
Assert . NotNull ( result . Result ) ;
202
193
@@ -212,7 +203,6 @@ public void TestInfiniteWhile()
212
203
var seqBlock = whil . BlockStmt as SequenceStatement ;
213
204
Assert . That ( seqBlock . Count , Is . EqualTo ( 1 ) ) ;
214
205
Assert . That ( seqBlock . Get ( 0 ) , Is . TypeOf < SkipStatement > ( ) ) ;
215
- ;
216
206
}
217
207
218
208
[ Test ]
@@ -221,7 +211,7 @@ public void TestPrintBoolExpression()
221
211
var buildResult = BuildParser ( ) ;
222
212
Assert . False ( buildResult . IsError ) ;
223
213
var parser = buildResult . Result ;
224
- var result = parser.Parse("print true and false ");
214
+ var result = parser . Parse ( "imprima verdadeiro e falso " ) ;
225
215
Assert . False ( result . IsError ) ;
226
216
Assert . NotNull ( result . Result ) ;
227
217
@@ -244,7 +234,7 @@ public void TestSkip()
244
234
var buildResult = BuildParser ( ) ;
245
235
Assert . False ( buildResult . IsError ) ;
246
236
var parser = buildResult . Result ;
247
- var result = parser.Parse("skip ");
237
+ var result = parser . Parse ( "pule " ) ;
248
238
Assert . False ( result . IsError ) ;
249
239
Assert . NotNull ( result . Result ) ;
250
240
@@ -276,14 +266,13 @@ public void TestSkipAssignSequence()
276
266
}
277
267
}
278
268
279
-
280
269
[ Test ]
281
270
public void TestSkipSkipSequence ( )
282
271
{
283
272
var buildResult = BuildParser ( ) ;
284
273
Assert . False ( buildResult . IsError ) ;
285
274
var parser = buildResult . Result ;
286
- var result = parser.Parse("(skip; skip; skip )");
275
+ var result = parser . Parse ( "(pule; pule; pule )" ) ;
287
276
Assert . False ( result . IsError ) ;
288
277
Assert . NotNull ( result . Result ) ;
289
278
Assert . That ( result . Result , Is . TypeOf < SequenceStatement > ( ) ) ;
@@ -293,6 +282,5 @@ public void TestSkipSkipSequence()
293
282
Assert . That ( seq . Get ( 1 ) , Is . TypeOf < SkipStatement > ( ) ) ;
294
283
Assert . That ( seq . Get ( 2 ) , Is . TypeOf < SkipStatement > ( ) ) ;
295
284
}
296
- */
297
285
}
298
286
}
0 commit comments