1: <?php
2:
3: namespace Demo;
4:
5: use \BN\Compiler\EvaluatorResponder;
6: use BN\Compiler\Postfix\Operands\OperandsSummary;
7:
8: class Presenter implements EvaluatorResponder
9: {
10: private $model;
11: private $statementsCount;
12:
13: public function __construct(Model $model)
14: {
15: $this->model = $model;
16: $this->statementsCount = 0;
17: }
18:
19: public function nextStatement($statement)
20: {
21: $this->statementsCount++;
22: }
23:
24: public function unknownToken($token)
25: {
26: $this->addError("Unknown lexeme <strong>{$token}</strong>");
27: }
28:
29: public function unknownOperator($operator)
30: {
31: $this->addError("Undefined operator <strong>{$operator}</strong>");
32: }
33:
34: public function mismatchedBrackets($mismatchedBracket)
35: {
36: $this->addError("Mismatched bracket <strong>{$mismatchedBracket}</strong>");
37: }
38:
39: public function invalidOperands($operatorSymbol, OperandsSummary $operands)
40: {
41: $this->addError(
42: "Operator <strong>{$operatorSymbol}</strong> must have <strong>{$operands->expectedCount}</strong>, " .
43: "but had <strong>{$operands->countOperands()}</strong> operands"
44: );
45: }
46:
47: public function missingOperator($expectedOperandsCount)
48: {
49: $this->addError("Missing operator with <strong>{$expectedOperandsCount}</strong> operands");
50: }
51:
52: public function undefinedVariable($variableName)
53: {
54: $this->addError("Undefined variable <strong>{$variableName}</strong>");
55: }
56:
57: public function exception(\Exception $e)
58: {
59: $class = get_class($e);
60: $this->addError(
61: "<strong>{$class}</strong> in file <strong>{$e->getFile()}</strong> " .
62: "on line <strong>{$e->getLine()}</strong>"
63: );
64: }
65:
66: private function addError($errorMessage)
67: {
68: $this->model->error .= "<em>{$this->statementsCount}:</em> {$errorMessage}\n";
69: }
70:
71: public function result($result)
72: {
73: $this->model->result = $result;
74: }
75:
76: public function variables(\stdClass $variables)
77: {
78: foreach (get_object_vars($variables) as $name => $value) {
79: $this->model->variables[] = "\${$name} = {$value}";
80: }
81: }
82: }
83: