Skip to content

Commit c3ee9d4

Browse files
committed
ArrNode改为字面量
1 parent 3ed7083 commit c3ee9d4

File tree

5 files changed

+55
-72
lines changed

5 files changed

+55
-72
lines changed

copynode.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ BasicNode* copyHelp::copyVal(BasicNode* node) //(值类型)拷贝
77
return new NumNode(*dynamic_cast<NumNode*>(node));
88
if(node->getType()==String)
99
return new StringNode(*dynamic_cast<StringNode*>(node));
10+
if(node->getType()==Arr)
11+
return new ArrNode(*dynamic_cast<ArrNode*>(node));
12+
if(node->getType()==Null)
13+
return new nullNode(*dynamic_cast<nullNode*>(node));
1014
//warn:支持更多具拷贝构造函数类型(目前都是字面量)后还需要在此处进行添加
1115
return nullptr; //如果进行参数检查了不会走到这一步
1216
}
@@ -17,8 +21,6 @@ BasicNode* copyHelp::copyNode(BasicNode* node) //拷贝单个子节点,warn:
1721
return copyHelp::copyVal(node);
1822
if(node->getType()==Var) //Var所有权在域,此处不进行复制,直接返回
1923
return node;
20-
if(node->getType()==Arr) //所有权在域,此处不进行复制,直接返回
21-
return node;
2224
if(node->getType()==Fun)
2325
return new FunNode(*dynamic_cast<FunNode*>(node));
2426
if(node->getType()==VarRef) //VarRef所有权在函数节点,此处不进行复制,直接返回
@@ -27,14 +29,12 @@ BasicNode* copyHelp::copyNode(BasicNode* node) //拷贝单个子节点,warn:
2729
return new IfNode(*dynamic_cast<IfNode*>(node));
2830
if(node->getType()==While)
2931
return new WhileNode(*dynamic_cast<WhileNode*>(node));
30-
if(node->getType()==Null)
31-
return new nullNode(*dynamic_cast<nullNode*>(node));
3232
throw string("The type is not regular son nodes to copy"); //Pro不作为常规子节点,不在此考虑
3333
}
3434

3535
BasicNode::BasicNode(const BasicNode &n)
3636
{
37-
this->retFlag=n.retFlag;
37+
this->retFlag=n.isRet();
3838
for(BasicNode* i:n.sonNode)
3939
this->sonNode.push_back(copyHelp::copyNode(i));
4040
}
@@ -51,3 +51,23 @@ WhileNode::WhileNode(const WhileNode &n):conditionalControlNode(n)
5151
this->condition=copyHelp::copyNode(n.condition);
5252
this->body=new ProNode(*(n.body));
5353
}
54+
55+
VarNode::VarNode(VarNode &n)
56+
{
57+
this->typeRestrictFlag=n.istypeRestrict();
58+
this->ownershipFlag=n.isOwnership();
59+
this->valtype=n.getValType();
60+
if(this->ownershipFlag) //有所有权拷贝,没有直接赋值
61+
this->val=copyHelp::copyVal(n.eval()); //此处按照默认的策略——只有字面量作为有所有权的值,所以直接调用拷贝字面量的方法进行拷贝
62+
else
63+
this->val=n.eval();
64+
}
65+
66+
ArrNode::ArrNode(const ArrNode &n):BasicNode(n)
67+
{
68+
this->typeRestrictFlag=n.istypeRestrict();
69+
this->valtype=n.getValType();
70+
this->len=n.getLen();
71+
for(VarNode* pn:n.allelm)
72+
this->allelm.push_back(new VarNode(*pn));
73+
}

nodetype.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
bool copyHelp::isLiteral(int type) //warn:是否为字面量,添加新的字面量要进行修改
44
{
5-
return (type==Num||type==String);
5+
return (type==Num||type==String||type==Arr||type==Null); //暂且先把Null安排上
66
}
77

88
bool copyHelp::isLiteral(BasicNode* node)
99
{
1010
return copyHelp::isLiteral(node->getType());
1111
}
1212

13-
bool isNotAssignable(BasicNode* val) //warn:是否不可赋值给变量,支持新的值类型要进行修改
13+
bool isNotAssignable(BasicNode* val) //warn:是否不可赋值给变量,支持新的节点类型要进行修改
1414
{
1515
return (val->getType()==Pro||val->getType()==Fun||val->getType()==If||val->getType()==While);
1616
//fix:目前暂不支持函数指针,因为函数实体的变量表示还没设计好
@@ -425,18 +425,18 @@ void ArrNode::clearArray()
425425
delete node;
426426
}
427427

428-
ArrNode::ArrNode(int valtype, int size)
428+
ArrNode::ArrNode(int valtype, int len)
429429
{
430430
this->valtype=valtype;
431-
this->size=size;
431+
this->len=len;
432432
if(valtype!=-1) //开启严格求值
433433
this->typeRestrictFlag=true;
434434
}
435435

436436
void ArrNode::arrSizeCheck()
437437
{
438438
if(!this->isVLA()) //限制参数个数
439-
if(this->allelm.size()+1>this->size)
439+
if(this->allelm.size()+1>this->len)
440440
throw string("Array is too long");
441441
}
442442

@@ -463,13 +463,6 @@ VarNode* ArrNode::addElm(VarNode *var)
463463
return var;
464464
}
465465

466-
int ArrNode::getValType()
467-
{
468-
if(!this->typeRestrictFlag)
469-
throw string("non-typeRestrict array no same valtype");
470-
return this->valtype;
471-
}
472-
473466
void ArrNode::delElm(unsigned int sub)
474467
{
475468
vector<VarNode*>::iterator it=this->allelm.begin()+sub;

nodetype.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BasicNode
2020

2121
//fix:这个ret的实现方法可能不太对劲
2222
void setRet() {this->retFlag=true;} //不可eval节点设置ret无效
23-
bool isRet() {return this->retFlag;}
23+
bool isRet() const {return this->retFlag;}
2424
vector<BasicNode*> sonNode;
2525
};
2626
typedef function<bool(vector<BasicNode*>&sonNode)>canBE; //检测函数基础求值参数表是否合法
@@ -45,9 +45,9 @@ class NumNode : public BasicNode
4545
virtual void addNode(BasicNode*) {throw addSonExcep(Num);}
4646
virtual BasicNode* eval() {return this;}
4747
NumNode(double num) {this->num=num;}
48-
NumNode(const NumNode& n):BasicNode(n) {this->num=n.num;}
48+
NumNode(const NumNode& n):BasicNode(n) {this->num=n.getNum();}
4949

50-
double getNum() {return this->num;}
50+
double getNum() const {return this->num;}
5151
};
5252

5353

@@ -60,9 +60,9 @@ class StringNode : public BasicNode
6060
virtual void addNode(BasicNode*) {throw addSonExcep(String);}
6161
virtual BasicNode* eval() {return this;}
6262
StringNode(string str) {this->str=str;}
63-
StringNode(const StringNode& n):BasicNode(n) {this->str=n.str;}
63+
StringNode(const StringNode& n):BasicNode(n) {this->str=n.getStr();}
6464

65-
string getStr() {return this->str;}
65+
string getStr() const {return this->str;}
6666
};
6767

6868

@@ -80,11 +80,12 @@ class VarNode : public BasicNode
8080
virtual BasicNode* eval();
8181
virtual ~VarNode();
8282
VarNode(int valtype=-1);
83+
VarNode(VarNode &n);
8384

84-
bool isEmpty() {return (this->val==nullptr);}
85-
bool istypeRestrict() {return this->typeRestrictFlag;}
86-
int getValType() {return this->valtype;}
87-
bool isOwnership() {return this->ownershipFlag;}
85+
bool isEmpty() const {return (this->val==nullptr);}
86+
bool istypeRestrict() const {return this->typeRestrictFlag;}
87+
int getValType() const {return this->valtype;}
88+
bool isOwnership() const {return this->ownershipFlag;}
8889
void setVal(BasicNode* val); //直接对值进行赋值,用这个传进来意味着转移所有权到本类(一般赋值为字面量用)
8990
void setBorrowVal(BasicNode* val); //直接对值进行赋值,用这个不转移所有权(一般赋值为变量指针用)
9091
void setVarVal(VarNode* node); //传递变量的值到this的值,即需要进行一次解包
@@ -96,37 +97,37 @@ class VarNode : public BasicNode
9697
};
9798
typedef VarNode Variable; //内存实体是Variable,其指针才作为节点(不像某些节点一样是遇到一个就new一次),参考函数实体和函数节点的思想
9899

99-
class ArrNode : public BasicNode
100+
class ArrNode : public BasicNode //注意,该节点是作为字面量存在的,传递时应当转移所有权
100101
{
101102
protected:
102103
bool typeRestrictFlag=false;
103-
vector<VarNode*>allelm;
104-
int valtype; //每个变量值的类型
105-
int size; //最大长度,-1为不限
104+
int valtype; //fix:应当记录交叉类型,所以这里和设计原则有些冲突,集成TSystem前要改好
105+
//可选的方案:将typeRestrictFlag改为【严格同一类型】,同一类型用现在这个valtype,正常的typeRestrictFlag检查交叉类型
106+
int len; //最大长度,-1为不限
106107

107108
void arrSizeCheck();
109+
void clearArray(); //这个暂时调整成私有
108110
public:
109111
virtual int getType() {return Arr;}
110112
virtual void addNode(BasicNode*) {throw addSonExcep(Arr);}
111113
virtual BasicNode* eval() {return this;}
112114
virtual ~ArrNode() {this->clearArray();}
113-
ArrNode(int valtype=-1,int size=-1);
115+
ArrNode(int valtype=-1,int len=-1);
116+
ArrNode(const ArrNode& n);
114117

115118
VarNode* addElm(int valtype=-1);
116119
VarNode* addElm(VarNode* var); //注意,该函数会移交所传递变量的所有权
117-
VarNode* getElm(unsigned int sub) {return this->allelm.at(sub);}
118-
unsigned int getLen() {return this->allelm.size();}
119120
void delElm(unsigned int sub);
120-
bool istypeRestrict() {return this->typeRestrictFlag;}
121-
int getValType();
122-
void clearArray();
123-
bool isVLA() {return this->size==-1;}
121+
VarNode* getElm(unsigned int sub) {return this->allelm.at(sub);} //和访问直接访问allelm没啥区别,为了和楼上对称先留着
122+
unsigned int getNowLen() const {return this->allelm.size();}
123+
bool istypeRestrict() const {return this->typeRestrictFlag;}
124+
int getValType() const {return this->valtype;} //调用前自行检查istypeRestrict
125+
bool isVLA() const {return this->len==-1;}
126+
int getLen() const {return this->len;} //最大长度
124127

125-
#ifdef READABLEGEN
126-
string NAME;
127-
#endif
128+
vector<VarNode*>allelm;
128129
};
129-
typedef ArrNode Array; //同上
130+
//typedef ArrNode Array; //同上
130131

131132
class VarRefNode : public BasicNode
132133
{

scope.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,6 @@ void Scope::addVariable(string name, Variable *var)
2828
this->variableList[name]=var;
2929
}
3030

31-
void Scope::addArray(string name)
32-
{
33-
Array* var=new Array();
34-
#ifdef READABLEGEN
35-
var->NAME=name;
36-
#endif
37-
this->arrayList[name]=var;
38-
}
39-
40-
void Scope::addArray(string name, Array *var)
41-
{
42-
#ifdef READABLEGEN
43-
var->NAME=name;
44-
#endif
45-
this->arrayList[name]=var;
46-
}
47-
4831
void Scope::addFunction(string name, Function *fun)
4932
{
5033
#ifdef READABLEGEN
@@ -82,12 +65,3 @@ void Scope::deleteFunction(Function *fun)
8265
this->functionList.erase(p.first);
8366
}
8467
}
85-
86-
void Scope::deleteArray(Array *arr)
87-
{
88-
for(auto p:this->arrayList)
89-
{
90-
if(p.second==arr)
91-
this->arrayList.erase(p.first);
92-
}
93-
}

scope.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,20 @@ class Scope //作用是记录不同语句块内函数及变量名到实体的映
1010
~Scope();
1111
map<string,Variable*> variableList;
1212
map<string,Function*> functionList;
13-
map<string,Array*> arrayList;
1413
vector<Scope*> sonScope;
1514
Scope* fatherScope; //因为可能顺着作用域向上层找变量,所以需要上级节点的指针
1615
Scope(Scope* fatherScope=nullptr):fatherScope(fatherScope){}
1716

1817
void addVariable(string name);
1918
void addVariable(string name,Variable* var);
20-
void addArray(string name);
21-
void addArray(string name,Array* var);
2219
void addFunction(string name,Function* fun);
2320
bool haveVariable(string name) {return this->variableList[name]!=0;}
2421
bool haveFunction(string name) {return this->functionList[name]!=0;}
25-
bool haveArray(string name) {return this->arrayList[name]!=0;}
2622
//直接删除实体,在已经将所有实体引用在树中清空时使用(目前还没看到有什么用)
2723
//void deleteVariable(string name);
2824
//void deleteFunction(string name);
2925
//删除实体指针在域中的存储,在已经将所有实体引用在树中清空时使用且已经delete时使用
3026
void deleteVariable(Variable* var);
3127
void deleteFunction(Function* fun);
32-
void deleteArray(Array* arr);
3328
void settopASTNode(BasicNode* topnode) {this->topASTNode=topnode;}
3429
};

0 commit comments

Comments
 (0)