Skip to content

Commit 6de1fe1

Browse files
committed
Passing all interpreter tests
1 parent 410ba30 commit 6de1fe1

File tree

4 files changed

+467
-33
lines changed

4 files changed

+467
-33
lines changed

enquanto.Test/InterpreterTests.cs

+20-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BabelFish.Interpreter;
12
using enquanto;
23
using enquanto.Model;
34
using NUnit.Framework;
@@ -9,7 +10,6 @@ namespace Tests
910
{
1011
public class IntrerpreterTests
1112
{
12-
/*
1313
private static BuildResult<Parser<EnquantoToken, AST>> Parser;
1414

1515

@@ -20,14 +20,13 @@ public BuildResult<Parser<EnquantoToken, AST>> BuildParser()
2020
var whileParser = new Parser();
2121
var builder = new ParserBuilder<EnquantoToken, AST>();
2222
Parser = builder.BuildParser(whileParser, ParserType.EBNF_LL_RECURSIVE_DESCENT, "statement");
23-
;
2423
}
2524

2625
return Parser;
2726
}
2827

2928

30-
public bool CheckIntVariable(InterpreterContext context, string variable, int value)
29+
public bool CheckIntVariable(InterpreterContext<EnquantoType> context, string variable, int value)
3130
{
3231
var ok = false;
3332
if (context.GetVariable(variable) != null)
@@ -67,7 +66,6 @@ public void TestBuildParser()
6766
{
6867
var buildResult = BuildParser();
6968
Assert.False(buildResult.IsError);
70-
var parser = buildResult.Result;
7169
}
7270

7371
[Test]
@@ -76,10 +74,9 @@ public void TestCounterProgram()
7674
var buildResult = BuildParser();
7775
Assert.False(buildResult.IsError);
7876
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 ))");
8078
Assert.False(result.IsError);
8179
Assert.NotNull(result.Result);
82-
;
8380
}
8481

8582
[Test]
@@ -88,11 +85,11 @@ public void TestCounterProgramExec()
8885
var buildResult = BuildParser();
8986
Assert.False(buildResult.IsError);
9087
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 ))");
9289
Assert.False(result.IsError);
9390
Assert.NotNull(result.Result);
9491
var interpreter = new Interpreter();
95-
var context = interpreter.Interprete(result.Result, true);
92+
var context = interpreter.Execute(result.Result, true);
9693
Assert.That(context.variables, Has.Exactly(1).Items);
9794
Assert.True(CheckIntVariable(context, "a", 10));
9895
}
@@ -104,11 +101,11 @@ public void TestFactorialProgramExec()
104101
(
105102
r:=1;
106103
i:=1;
107-
while i < 11 do
104+
enquanto i < 11 faça
108105
(
109106
r := r * i;
110-
print r;
111-
print i;
107+
imprima r;
108+
imprima i;
112109
i := i + 1 )
113110
)";
114111
var buildResult = BuildParser();
@@ -118,37 +115,32 @@ public void TestFactorialProgramExec()
118115
Assert.False(result.IsError);
119116
Assert.NotNull(result.Result);
120117
var interpreter = new Interpreter();
121-
var context = interpreter.Interprete(result.Result, true);
118+
var context = interpreter.Execute(result.Result, true);
122119
Assert.That(context.variables.Count, Is.EqualTo(2));
123120
Assert.True(CheckIntVariable(context, "i", 11));
124121
Assert.True(CheckIntVariable(context, "r", 3628800));
125-
126-
127-
;
128122
}
129123

130-
131124
[Test]
132125
public void TestFactorialProgramExecAsIL()
133126
{
134127
var program = @"
135128
(
136129
r:=1;
137130
i:=1;
138-
while i < 11 do
131+
enquanto i < 11 faça
139132
(
140133
r := r * i;
141-
print """".r;
142-
print """".i;
134+
imprima """".r;
135+
imprima """".i;
143136
i := i + 1 );
144-
return r
137+
retorne r
145138
)";
146139
var compiler = new Compiler();
147140
var func = compiler.CompileToFunction(program);
148141
Assert.NotNull(func);
149142
var f = func();
150143
Assert.That(f, Is.EqualTo(3628800));
151-
;
152144
}
153145

154146
[Test]
@@ -157,7 +149,7 @@ public void TestIfThenElse()
157149
var buildResult = BuildParser();
158150
Assert.False(buildResult.IsError);
159151
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\")");
161153
Assert.False(result.IsError);
162154
Assert.NotNull(result.Result);
163155

@@ -170,15 +162,14 @@ public void TestIfThenElse()
170162
Assert.True((cond as BoolConstant).Value);
171163
var s = si.ThenStmt;
172164

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;
175167
Assert.That(thenBlock.Count, Is.EqualTo(1));
176168
Assert.That(thenBlock.Get(0), Is.TypeOf<AssignStatement>());
177169
var thenAssign = thenBlock.Get(0) as AssignStatement;
178170
Assert.That(thenAssign.VariableName, Is.EqualTo("a"));
179171
Assert.That(thenAssign.Value, Is.TypeOf<StringConstant>());
180172
Assert.That((thenAssign.Value as StringConstant).Value, Is.EqualTo("hello"));
181-
;
182173

183174
Assert.That(si.ElseStmt, Is.TypeOf<SequenceStatement>());
184175
var elseBlock = si.ElseStmt as SequenceStatement;
@@ -196,7 +187,7 @@ public void TestInfiniteWhile()
196187
var buildResult = BuildParser();
197188
Assert.False(buildResult.IsError);
198189
var parser = buildResult.Result;
199-
var result = parser.Parse("while true do (skip)");
190+
var result = parser.Parse("enquanto verdadeiro faça (pule)");
200191
Assert.False(result.IsError);
201192
Assert.NotNull(result.Result);
202193

@@ -212,7 +203,6 @@ public void TestInfiniteWhile()
212203
var seqBlock = whil.BlockStmt as SequenceStatement;
213204
Assert.That(seqBlock.Count, Is.EqualTo(1));
214205
Assert.That(seqBlock.Get(0), Is.TypeOf<SkipStatement>());
215-
;
216206
}
217207

218208
[Test]
@@ -221,7 +211,7 @@ public void TestPrintBoolExpression()
221211
var buildResult = BuildParser();
222212
Assert.False(buildResult.IsError);
223213
var parser = buildResult.Result;
224-
var result = parser.Parse("print true and false");
214+
var result = parser.Parse("imprima verdadeiro e falso");
225215
Assert.False(result.IsError);
226216
Assert.NotNull(result.Result);
227217

@@ -244,7 +234,7 @@ public void TestSkip()
244234
var buildResult = BuildParser();
245235
Assert.False(buildResult.IsError);
246236
var parser = buildResult.Result;
247-
var result = parser.Parse("skip");
237+
var result = parser.Parse("pule");
248238
Assert.False(result.IsError);
249239
Assert.NotNull(result.Result);
250240

@@ -276,14 +266,13 @@ public void TestSkipAssignSequence()
276266
}
277267
}
278268

279-
280269
[Test]
281270
public void TestSkipSkipSequence()
282271
{
283272
var buildResult = BuildParser();
284273
Assert.False(buildResult.IsError);
285274
var parser = buildResult.Result;
286-
var result = parser.Parse("(skip; skip; skip)");
275+
var result = parser.Parse("(pule; pule; pule)");
287276
Assert.False(result.IsError);
288277
Assert.NotNull(result.Result);
289278
Assert.That(result.Result, Is.TypeOf<SequenceStatement>());
@@ -293,6 +282,5 @@ public void TestSkipSkipSequence()
293282
Assert.That(seq.Get(1), Is.TypeOf<SkipStatement>());
294283
Assert.That(seq.Get(2), Is.TypeOf<SkipStatement>());
295284
}
296-
*/
297285
}
298286
}

0 commit comments

Comments
 (0)