-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.php
48 lines (40 loc) · 1.18 KB
/
test3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
require 'ast/Exp.php';
require 'ast/Program.php';
require 'Eval.php';
/**
a := 1;
num := 100;
i := 0;
sum := 0;
while (i < num)
{
sum := sum + a;
a := a + 1;
i := i + 1;
}
print sum
*/
$a = new ExpVar('a');
$assignStmt1 = new ProgramAssign($a, new ExpNum(1)); // a := 1
$num = new ExpVar('num');
$assignStmt2 = new ProgramAssign($num, new ExpNum(100)); // num := 100
$i = new ExpVar('i');
$assignStmt3 = new ProgramAssign($i, new ExpNum(0)); // i := 0
$sum = new ExpVar('sum');
$assignStmt4 = new ProgramAssign($sum, new ExpNum(0)); // sum := 0
$pred = new ExpLT($i, $num); // i < num
$body1 = new ProgramAssign($sum, new ExpPlus($sum, $a)); // sum := sum + a
$body2 = new ProgramAssign($a, new ExpPlus($a, new ExpNum(1))); // a := a + 1
$body3 = new ProgramAssign($i, new ExpPlus($i, new ExpNum(1))); // i := i + 1
$whileStmt = new ProgramWhile($pred, new ProgramStmts([$body1, $body2, $body3])); // while ...
$printStmt = new ProgramPrint($sum); // print sum
$stmts = [
$assignStmt1,
$assignStmt2,
$assignStmt3,
$assignStmt4,
$whileStmt,
$printStmt
];
evalProgram(new ProgramStmts($stmts));