Skip to content

Commit 252e234

Browse files
finish lab1
1 parent 2956c03 commit 252e234

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

.info

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
STUDENT_NAME=Xinpeng Wei
2+
STUDENT_ID=519021910888

src/straightline/slp.cc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,125 @@
55
namespace A {
66
int A::CompoundStm::MaxArgs() const {
77
// TODO: put your code here (lab1).
8+
int args1 = stm1->MaxArgs();
9+
int args2 = stm2->MaxArgs();
10+
return args1 > args2 ? args1 : args2;
811
}
912

1013
Table *A::CompoundStm::Interp(Table *t) const {
1114
// TODO: put your code here (lab1).
15+
return stm2 -> Interp(stm1 -> Interp(t));
1216
}
1317

1418
int A::AssignStm::MaxArgs() const {
1519
// TODO: put your code here (lab1).
20+
return exp->MaxArgs();
1621
}
1722

1823
Table *A::AssignStm::Interp(Table *t) const {
1924
// TODO: put your code here (lab1).
25+
IntAndTable *tmp = exp -> Interp(t);
26+
t = tmp -> t;
27+
return t -> Update(id, tmp -> i);
2028
}
2129

2230
int A::PrintStm::MaxArgs() const {
2331
// 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;
2435
}
2536

2637
Table *A::PrintStm::Interp(Table *t) const {
2738
// TODO: put your code here (lab1).
39+
return exps->Interp(t)->t;
2840
}
2941

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+
}
30127

31128
int Table::Lookup(const std::string &key) const {
32129
if (id == key) {

src/straightline/slp.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ class Exp {
5959
// TODO: you'll have to add some definitions here (lab1).
6060
// Hints: You may add interfaces like `int MaxArgs()`,
6161
// and ` IntAndTable *Interp(Table *)`
62+
public:
63+
virtual int MaxArgs() const = 0;
64+
virtual IntAndTable *Interp(Table *) const = 0;
6265
};
6366

6467
class IdExp : public Exp {
6568
public:
6669
explicit IdExp(std::string id) : id(std::move(id)) {}
6770
// TODO: you'll have to add some definitions here (lab1).
68-
71+
int MaxArgs() const override;
72+
IntAndTable *Interp(Table *t) const override;
73+
6974
private:
7075
std::string id;
7176
};
@@ -74,7 +79,8 @@ class NumExp : public Exp {
7479
public:
7580
explicit NumExp(int num) : num(num) {}
7681
// TODO: you'll have to add some definitions here.
77-
82+
int MaxArgs() const override;
83+
IntAndTable *Interp(Table *t) const override;
7884
private:
7985
int num;
8086
};
@@ -83,6 +89,8 @@ class OpExp : public Exp {
8389
public:
8490
OpExp(Exp *left, BinOp oper, Exp *right)
8591
: left(left), oper(oper), right(right) {}
92+
int MaxArgs() const override;
93+
IntAndTable *Interp(Table *t) const override;
8694

8795
private:
8896
Exp *left;
@@ -93,8 +101,10 @@ class OpExp : public Exp {
93101
class EseqExp : public Exp {
94102
public:
95103
EseqExp(Stm *stm, Exp *exp) : stm(stm), exp(exp) {}
104+
int MaxArgs() const override;
105+
IntAndTable *Interp(Table *t) const override;
96106

97-
private:
107+
private:
98108
Stm *stm;
99109
Exp *exp;
100110
};
@@ -104,12 +114,19 @@ class ExpList {
104114
// TODO: you'll have to add some definitions here (lab1).
105115
// Hints: You may add interfaces like `int MaxArgs()`, `int NumExps()`,
106116
// and ` IntAndTable *Interp(Table *)`
117+
virtual int MaxArgs() const = 0;
118+
virtual int NumExps() const = 0;
119+
virtual IntAndTable *Interp(Table *) const = 0;
107120
};
108121

109122
class PairExpList : public ExpList {
110123
public:
111124
PairExpList(Exp *exp, ExpList *tail) : exp(exp), tail(tail) {}
112125
// TODO: you'll have to add some definitions here (lab1).
126+
int MaxArgs() const override;
127+
int NumExps() const override;
128+
IntAndTable *Interp(Table *t) const override;
129+
113130
private:
114131
Exp *exp;
115132
ExpList *tail;
@@ -119,6 +136,9 @@ class LastExpList : public ExpList {
119136
public:
120137
LastExpList(Exp *exp) : exp(exp) {}
121138
// TODO: you'll have to add some definitions here (lab1).
139+
int MaxArgs() const override;
140+
int NumExps() const override;
141+
IntAndTable *Interp(Table *t) const override;
122142
private:
123143
Exp *exp;
124144
};

0 commit comments

Comments
 (0)