|
5 | 5 | namespace A { |
6 | 6 | int A::CompoundStm::MaxArgs() const { |
7 | 7 | // TODO: put your code here (lab1). |
| 8 | + int args1 = stm1->MaxArgs(); |
| 9 | + int args2 = stm2->MaxArgs(); |
| 10 | + return args1 > args2 ? args1 : args2; |
8 | 11 | } |
9 | 12 |
|
10 | 13 | Table *A::CompoundStm::Interp(Table *t) const { |
11 | 14 | // TODO: put your code here (lab1). |
| 15 | + return stm2 -> Interp(stm1 -> Interp(t)); |
12 | 16 | } |
13 | 17 |
|
14 | 18 | int A::AssignStm::MaxArgs() const { |
15 | 19 | // TODO: put your code here (lab1). |
| 20 | + return exp->MaxArgs(); |
16 | 21 | } |
17 | 22 |
|
18 | 23 | Table *A::AssignStm::Interp(Table *t) const { |
19 | 24 | // TODO: put your code here (lab1). |
| 25 | + IntAndTable *tmp = exp -> Interp(t); |
| 26 | + t = tmp -> t; |
| 27 | + return t -> Update(id, tmp -> i); |
20 | 28 | } |
21 | 29 |
|
22 | 30 | int A::PrintStm::MaxArgs() const { |
23 | 31 | // TODO: put your code here (lab1). |
| 32 | + int exps_num = exps->NumExps(); |
| 33 | + int exps_max = exps->MaxArgs(); |
| 34 | + return exps_num > exps_max ? exps_num : exps_max; |
24 | 35 | } |
25 | 36 |
|
26 | 37 | Table *A::PrintStm::Interp(Table *t) const { |
27 | 38 | // TODO: put your code here (lab1). |
| 39 | + return exps->Interp(t)->t; |
28 | 40 | } |
29 | 41 |
|
| 42 | +int A::IdExp::MaxArgs() const { |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +IntAndTable *A::IdExp::Interp(Table *t) const { |
| 47 | + return new IntAndTable(t -> Lookup(id), t); |
| 48 | +} |
| 49 | + |
| 50 | +int A::NumExp::MaxArgs() const { |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +IntAndTable *A::NumExp::Interp(Table *t) const { |
| 55 | + return new IntAndTable(num, t); |
| 56 | +} |
| 57 | + |
| 58 | +int A::OpExp::MaxArgs() const { |
| 59 | + int left_num = left->MaxArgs(); |
| 60 | + int right_num = right->MaxArgs(); |
| 61 | + return left_num > right_num ? left_num : right_num; |
| 62 | +} |
| 63 | + |
| 64 | +IntAndTable *A::OpExp::Interp(Table *t) const { |
| 65 | + IntAndTable *tmp = left->Interp(t); |
| 66 | + int left_result = tmp->i; |
| 67 | + tmp = right->Interp(tmp->t); |
| 68 | + int right_result = tmp->i; |
| 69 | + switch (oper){ |
| 70 | + case PLUS: |
| 71 | + return new IntAndTable(left_result + right_result, tmp->t); |
| 72 | + break; |
| 73 | + case MINUS: |
| 74 | + return new IntAndTable(left_result - right_result, tmp->t); |
| 75 | + break; |
| 76 | + case TIMES: |
| 77 | + return new IntAndTable(left_result * right_result, tmp->t); |
| 78 | + break; |
| 79 | + case DIV: |
| 80 | + return new IntAndTable(left_result / right_result, tmp->t); |
| 81 | + break; |
| 82 | + default: |
| 83 | + break; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +int A::EseqExp::MaxArgs() const { |
| 88 | + int stm_num = stm->MaxArgs(); |
| 89 | + int exp_num = exp->MaxArgs(); |
| 90 | + return stm_num > exp_num ? stm_num : exp_num; |
| 91 | +} |
| 92 | + |
| 93 | +IntAndTable *A::EseqExp::Interp(Table *t) const { |
| 94 | + t = stm->Interp(t); |
| 95 | + return exp->Interp(t); |
| 96 | +} |
| 97 | + |
| 98 | +int A::PairExpList::MaxArgs() const { |
| 99 | + int exp_num = exp->MaxArgs(); |
| 100 | + int tail_num = tail->MaxArgs(); |
| 101 | + return exp_num > tail_num ? exp_num : tail_num; |
| 102 | +} |
| 103 | + |
| 104 | +int A::PairExpList::NumExps() const { |
| 105 | + return 1 + tail->NumExps(); |
| 106 | +} |
| 107 | + |
| 108 | +IntAndTable *A::PairExpList::Interp(Table *t) const { |
| 109 | + IntAndTable *tmp = exp->Interp(t); |
| 110 | + std::cout << tmp->i << ' '; |
| 111 | + return tail->Interp(tmp->t); |
| 112 | +} |
| 113 | + |
| 114 | +int A::LastExpList::MaxArgs() const { |
| 115 | + return exp->MaxArgs(); |
| 116 | +} |
| 117 | + |
| 118 | +int A::LastExpList::NumExps() const { |
| 119 | + return 1; |
| 120 | +} |
| 121 | + |
| 122 | +IntAndTable *A::LastExpList::Interp(Table *t) const { |
| 123 | + IntAndTable *tmp = exp->Interp(t); |
| 124 | + std::cout << tmp->i << '\n'; |
| 125 | + return tmp; |
| 126 | +} |
30 | 127 |
|
31 | 128 | int Table::Lookup(const std::string &key) const { |
32 | 129 | if (id == key) { |
|
0 commit comments