1 package visitor;
2
3 import calculator.Expression;
4 import calculator.operations.Operation;
5 import calculator.atoms.Complex;
6 import calculator.atoms.IntegerAtom;
7 import calculator.atoms.Rationnal;
8 import calculator.atoms.Real;
9 import calculator.functions.*;
10
11
12
13
14
15 public class Counter extends Visitor {
16
17 private int nbOps = 0;
18 private int nbNbs = 0;
19 private int maxDepth = 0;
20 private int currentDepth = 0;
21
22
23
24
25 public Counter() {
26 }
27
28 @Override
29 public void visit(Real r) {
30 nbNbs += 1;
31 maxDepth = Math.max(maxDepth, currentDepth);
32 }
33
34 @Override
35 public void visit(IntegerAtom i) {
36 nbNbs += 1;
37 maxDepth = Math.max(maxDepth, currentDepth);
38 }
39
40 @Override
41 public void visit(Complex c) {
42 nbNbs += 1;
43 maxDepth = Math.max(maxDepth, currentDepth);
44 }
45
46 @Override
47 public void visit(Rationnal q) {
48 nbNbs += 1;
49 maxDepth = Math.max(maxDepth, currentDepth);
50 }
51
52
53
54
55
56
57
58 @Override
59 public void visit(Operation o) {
60 nbOps++;
61 currentDepth++;
62 for (Expression a : o.getArgs()) {
63 a.accept(this);
64 }
65 currentDepth--;
66 }
67
68 @Override
69 public void visit(UnaryFunction o) {
70 nbOps++;
71 currentDepth++;
72 o.getArg().accept(this);
73 currentDepth--;
74 }
75
76 @Override
77 public void visit(BinaryFunction f) {
78 nbOps++;
79 currentDepth++;
80 f.getFirstArg().accept(this);
81 f.getSecondArg().accept(this);
82 currentDepth--;
83 }
84
85
86
87
88
89
90 public int getNbOps() {
91 return nbOps;
92 }
93
94
95
96
97
98
99 public int getNbNbs() {
100 return nbNbs;
101 }
102
103
104
105
106
107
108 public int getDepth() {
109 return maxDepth;
110 }
111
112 }