1: <?php
2:
3: namespace BN\Compiler\Grammar;
4:
5: use BN\Compiler\Parser\Operator\OperatorOrder;
6: use BN\Compiler\Postfix\Operator\CallbackEvaluator;
7: use BN\Compiler\Postfix\Operands\FixedCount;
8: use BN\Compiler\Postfix\Operands\AtLeastN;
9:
10: class OperatorBuilder
11: {
12: private $precedence;
13: private $isLeftAssociative;
14: private $operands;
15: private $evaluator;
16:
17: public function precedence($precedence)
18: {
19: $this->precedence = $precedence;
20: return $this;
21: }
22:
23: public function leftAssociative()
24: {
25: $this->isLeftAssociative = true;
26: return $this;
27: }
28:
29: public function rightAssociative()
30: {
31: $this->isLeftAssociative = false;
32: return $this;
33: }
34:
35: public function unary($callback)
36: {
37: return $this->operator(1, $callback);
38: }
39:
40: public function binary($callback)
41: {
42: return $this->operator(2, $callback);
43: }
44:
45: public function operator($fixedOperandsCount, $callback)
46: {
47: $this->operands = new FixedCount($fixedOperandsCount);
48: $this->evaluator = new CallbackEvaluator($callback);
49: return $this;
50: }
51:
52: public function aggregate($callback)
53: {
54: $this->operands = new AtLeastN(1);
55: $this->evaluator = $callback;
56: return $this;
57: }
58:
59: public function buildOrder()
60: {
61: return new OperatorOrder($this->precedence, $this->isLeftAssociative);
62: }
63:
64: public function buildEvaluator()
65: {
66: return $this->evaluator;
67: }
68:
69: public function buildOperands()
70: {
71: return $this->operands;
72: }
73: }
74: