-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.cpp
180 lines (167 loc) · 4.38 KB
/
script.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "script.h"
#include "excep.h"
void script::clear()
{
delete this->manager;
lineMap.clear();
this->manager=new nodeManager;
line* trueLine=new line("1",true);
manager->addLine(trueLine);
lineMap["1"]=trueLine;
line* falseLine=new line("0",false);
lineMap["0"]=falseLine;
manager->addLine(falseLine);
}
void script::runFile(string path)
{
string code=help::readTxt(path);
cout<<code<<endl;
cout<<"----- load:"<<path<<" -----"<<endl<<endl;
script::evalAll(code);
cout<<endl<<"-----end-----"<<endl;
}
void script::runFile(vector<string> com)
{
this->clear();
string path;
if(com.size()==3)
path=com[1]+":"+com[2];
else
path=com[1];
runFile(path);
}
void script::equExp(string sen)
{
vector<string> com=help::split(sen,"=");
vector<string> com2=help::split(com[1]," ");
//先根据com2创建门电路
gate *g;
//确认门类型,实例化对象
if(com2[0]=="and")
g=new andGate();
else if(com2[0]=="and4")
g=new and4Gate();
else if(com2[0]=="or")
g=new orGate();
else if(com2[0]=="or4")
g=new or4Gate();
else if(com2[0]=="not")
g=new notGate();
else if(com2[0]=="NA")
g=new NAGate();
else if(com2[0]=="NA4")
g=new NA4Gate();
else if(com2[0]=="NO")
g=new NOGate();
else if(com2[0]=="NOA")
g=new NOAGate();
else if(com2[0]=="xor")
g=new xorGate();
else if(com2[0]=="RS")
g=new RSTri();
else if(com2[0]=="RSC")
g=new RSCTri();
else if(com2[0]=="D")
g=new DTri();
else if(com2[0]=="JK")
g=new JKTri();
else if(com2[0]=="T")
g=new TTri();
else
throw string("Unknown gate type");
node *n=new node(g); //通过门对象初始化节点
manager->addNode(n);
for(uint i=1;i<com2.size();i++)
{
if(lineMap[com2[i]]==0) //目前这个输入线还没有被定义
{
line* newLine=new line(com2[i],nullptr); //先构造上对象本体,其它的等定义的时候再延迟构造
lineMap[com2[i]]=newLine;
manager->addLine(newLine);
n->addInputLine(newLine);
}
else
n->addInputLine(lineMap[com2[i]]); //设置节点输入
}
//创建该节点的输出导线
vector<string> com3=help::split(com[0],",");
for(uint i=0;i<com3.size();i++)
{
if(com3[i]=="_")
continue;
else
{
if(lineMap[com3[i]]==0) //目前这个输出线还没有被定义
{
line* newLine=new line(com3[i],n,i); //正常定义
lineMap[com3[i]]=newLine;
manager->addLine(newLine);
}
else
lineMap[com3[i]]->delayedConstruction(n,i); //延迟构造
}
}
}
void script::set(string name, string val)
{
if(lineMap.count(name)>0)
lineMap[name]->constVal=help::toint(val);
else
throw undefinedVariableExcep();
}
void script::colonExp(string sen)
{
vector<string> com=help::split(sen,":");
if(com[0]=="input")
{
line* newline=new line(com[1]);
lineMap[com[1]]=newline;
manager->addLine(newline);
manager->addInputLine(newline);
}
else if(com[0]=="output")
manager->addOutputLine(lineMap[com[1]]);
else if(com[0]=="set")
{
vector<string> com2=help::split(com[1]," ");
set(com2[0],com2[1]);
}
else if(com[0]=="load")
runFile(com);
else
throw string("Unexpected line markup");
}
void script::commandExp(string sen)
{
if(sen=="trueTable")
cout<<manager->trueTable();
else if(sen=="statTable")
cout<<manager->trueTable(0,true);
else if(sen=="gateNum")
cout<<manager->gateNum();
else if(sen=="run")
cout<<manager->run();
else if(sen=="stru")
manager->stru();
else if(sen=="clear")
this->clear();
else if(sen=="middleVar")
manager->middleVar();
else if(sen=="multiplexing")
cout<<manager->multiplexing();
else if(sen=="resetTri")
manager->resetTri();
else
throw string("Unknow command");
}
void script::eval(string sen)
{
if(sen=="")
return;
else if(sen.find(":")!=-1)
colonExp(sen);
else if(sen.find("=")!=-1)
equExp(sen);
else
commandExp(sen);
}