-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
111 lines (101 loc) · 3.28 KB
/
example.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
include_once 'RussellInterpreter/Interpreter.php';
include_once 'RussellInterpreter/Lexer.php';
include_once 'RussellInterpreter/ParserTree.php';
$code = <<<EOD
variable(result, rand(0, 10))
variable(x, rand(0, result))
assign(summation,
sum(
sum(
sum(2, 3),
sum(4, 5)
), 5
)
)
assign(subtraction, subtraction(10, 100))
assign(division, division(10, 100))
assign(multiplication, multiplication(2, 2, 2))
assign(array, array(
summation,
subtraction,
division,
multiplication
))
cycle(not_equal(i, 10),
increment(i)
)
until(equal(i, 1),
decrement(i)
)
var(rt, if(true, 'yes', 'no'))
var(rf, if(false, 'yes', 'no'))
var(he, plural(1, 'парень', 'парня', 'парней'))
var(she, plural(2, 'девука', 'девуки', 'девушек'))
var(it, plural(5, 'солнце', 'солнца', 'солнц'))
until(
and(greater(a, b), less(d, 100)),
assign(a, random(2,11)),
assign(b, random(2,11)),
assign(d, multiplication(a,b)),
assign(e, multiplication(sum(a,b), 2))
)
assign(z, sum(a, 2))
assign(m, sum(a, 4))
assign(n, sum(a, 1))
EOD;
$lexer = new RussellInterpreter\Lexer(array(
'parser_tree' => new RussellInterpreter\ParserTree(),
));
$parserTree = $lexer->code($code);
$extensions = array(
'Assignment' => array('var', 'variable', 'assignment', 'assign'),
'Summation' => array('sum', 'summation'),
'Subtraction' => array('minus', 'subtraction'),
'Division' => array('div', 'division'),
'Multiplication' => array('multi', 'multiplication'),
'Random' => array('rand', 'random'),
'Arr' => array('arr', 'array'),
'Cycle' => array('cycle', 'while'),
'Until' => array('until'),
'True' => array('isTrue', 'true'),
'False' => array('isFalse', 'false'),
'Increment' => array('increment'),
'Decrement' => array('decrement'),
'Equal' => array('equal'),
'NotEqual' => array('notequal', 'not_equal'),
'ConditionalOperator' => array('if'),
'Conjunction' => array('disjunction', 'and'),
'Disjunction' => array('disjunction', 'or'),
'Negation' => array('negation', 'not'),
'Plural' => array('plural'),
'Concatenation' => array('concatenation', 'concat'),
'Greater' => array('greater', 'gt'),
'Greaterorequal' => array('greater_or_equal', 'gteq'),
'Less' => array('less', 'ls'),
'Lessorequal' => array('less_or_queal', 'lseq'),
'Modulo' => array('modulo', 'mod'),
'RandomArgument' => array('random_argument', 'rand_arg'),
'SquareRoot' => array('square_root', 'sqrt'),
'Exponential' => array('exponential', 'pow'),
);
foreach ($extensions as $file => $synonyms) {
include_once "RussellInterpreter/Extension/{$file}.php";
$class = "RussellInterpreter\\Extension\\{$file}";
$extension = new $class();
$extensions[$file] = array(
'object' => $extension,
'synonyms' => $synonyms,
);
}
$interpreter = new RussellInterpreter\Interpreter(array(
'extensions' => $extensions,
));
$isComplete = $interpreter->execute($parserTree);
echo ($isComplete ? 'COMPLETE!' : 'NOT COMPLETE, NO FUTURE') . PHP_EOL;
$variables = $interpreter->getVariables();
echo 'variable:' . PHP_EOL;
var_dump($variables);
echo 'errors:' . PHP_EOL;
$errors = $interpreter->getErrors();
var_dump($errors);