Skip to content

Commit 5cf2d2b

Browse files
committed
支持矩阵运算、赋值
1 parent d5e63e7 commit 5cf2d2b

File tree

13 files changed

+586
-45
lines changed

13 files changed

+586
-45
lines changed

SCMath.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ HEADERS += \
2121
output.h \
2222
excep.h \
2323
marco.h \
24-
scmath.h
24+
scmath.h \
25+
matrix.hpp

SCMath.pro.user

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.5.0, 2019-02-19T08:48:50. -->
3+
<!-- Written by QtCreator 4.2.1, 2019-03-16T16:42:03. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
7-
<value type="QByteArray">{8c0ba939-0b4d-4cf6-98ab-ff3200f6974d}</value>
7+
<value type="QByteArray">{5cdfc2d9-746f-4959-a869-d810886911bf}</value>
88
</data>
99
<data>
1010
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -59,14 +59,14 @@
5959
<data>
6060
<variable>ProjectExplorer.Project.Target.0</variable>
6161
<valuemap type="QVariantMap">
62-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.9.4 MinGW 32bit</value>
63-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.9.4 MinGW 32bit</value>
64-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.594.win32_mingw53_kit</value>
62+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.8.0 MinGW 32bit</value>
63+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.8.0 MinGW 32bit</value>
64+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.58.win32_mingw53_kit</value>
6565
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
6666
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
6767
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
6868
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
69-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/mycodes/build-SCMath-Desktop_Qt_5_9_4_MinGW_32bit-Debug</value>
69+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/gitreg/build-SCMath-Desktop_Qt_5_8_0_MinGW_32bit-Debug</value>
7070
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
7171
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
7272
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -120,7 +120,7 @@
120120
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
121121
</valuemap>
122122
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
123-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/mycodes/build-SCMath-Desktop_Qt_5_9_4_MinGW_32bit-Release</value>
123+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/gitreg/build-SCMath-Desktop_Qt_5_8_0_MinGW_32bit-Release</value>
124124
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
125125
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
126126
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -174,7 +174,7 @@
174174
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
175175
</valuemap>
176176
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
177-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/mycodes/build-SCMath-Desktop_Qt_5_9_4_MinGW_32bit-Profile</value>
177+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/gitreg/build-SCMath-Desktop_Qt_5_8_0_MinGW_32bit-Profile</value>
178178
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
179179
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
180180
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -286,13 +286,13 @@
286286
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
287287
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">SCMath</value>
288288
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
289-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:F:/mycodes/SCMath/SCMath.pro</value>
289+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/gitreg/SCMath/SCMath.pro</value>
290290
<value type="bool" key="QmakeProjectManager.QmakeRunConfiguration.UseLibrarySearchPath">true</value>
291291
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
292292
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">SCMath.pro</value>
293293
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
294294
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
295-
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">F:/mycodes/build-SCMath-Desktop_Qt_5_9_4_MinGW_32bit-Debug</value>
295+
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">D:/gitreg/build-SCMath-Desktop_Qt_5_8_0_MinGW_32bit-Debug</value>
296296
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
297297
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
298298
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>

ast.cpp

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,57 @@ static void ast::Init()
1919
Function* cos = new Function(BuiltinFunc::hasOneSonNode, BuiltinFunc::cos,1);
2020
Function* log = new Function(BuiltinFunc::hasTwoSonNodes, BuiltinFunc::log, 2);
2121
Function* ln = new Function(BuiltinFunc::hasOneSonNode, BuiltinFunc::ln, 1);
22+
23+
Function* assignment = new Function(BuiltinFunc::assignmentCheck, BuiltinFunc::assignment, 2);
24+
25+
Function* vecDot = new Function(BuiltinFunc::twoVec, BuiltinFunc::vecDot, 2);
26+
Function* matDot = new Function(BuiltinFunc::twoMat, BuiltinFunc::matDot, 2);
27+
Function* matAdd = new Function(BuiltinFunc::twoMat, BuiltinFunc::matAdd, 2);
28+
Function* vecAdd = new Function(BuiltinFunc::twoVec, BuiltinFunc::vecAdd, 2);
29+
Function* vecSub = new Function(BuiltinFunc::twoVec, BuiltinFunc::vecSub, 2);
30+
Function* matSub = new Function(BuiltinFunc::twoMat, BuiltinFunc::matSub, 2);
31+
Function* vecMul = new Function(BuiltinFunc::vecNum, BuiltinFunc::vecMul, 2);
32+
Function* matMul = new Function(BuiltinFunc::matNum, BuiltinFunc::matMul, 2);
33+
Function* getRVector = new Function(BuiltinFunc::matNum, BuiltinFunc::getRVector, 2);
34+
Function* getCVector = new Function(BuiltinFunc::matNum, BuiltinFunc::getCVector, 2);
35+
Function* setCVector = new Function(BuiltinFunc::pmatVecNum, BuiltinFunc::setCVector, 3);
36+
Function* setRVector = new Function(BuiltinFunc::pmatVecNum, BuiltinFunc::setRVector, 3);
37+
Function* det = new Function(BuiltinFunc::oneMat, BuiltinFunc::det, 1);
38+
Function* linerSolve = new Function(BuiltinFunc::twoMat, BuiltinFunc::linerSolve, 2);
2239
//将这些函数置入函数域
2340
record::globalScope.addFunction("+",add);
2441
record::globalScope.addFunction("-",sub);
2542
record::globalScope.addFunction("*",mul);
2643
record::globalScope.addFunction("/",div);
2744
record::globalScope.addFunction("^",pow);
45+
record::globalScope.addFunction("=",assignment);
46+
//fix:目前=是通过局部求值实现的,即在被赋值变量原先没有值的情况下,局部求值模式下eval结果仍然为
47+
//变量本身,这样传进=函数之后左边儿子就是变量本身,可以正确赋值。一旦变量已经被赋值,那进入=前
48+
//就会被eval为它的值,值不能赋值给值,这就错了。因此现在这个写法不好。正确的做法应该是在parse阶段
49+
//创建每个变量的指针(二级varnode),然后将这个指针作为=的左边儿子
2850
record::globalScope.addFunction("sin",sin);
2951
record::globalScope.addFunction("cos",cos);
3052
record::globalScope.addFunction("log", log);
3153
record::globalScope.addFunction("ln", ln);
54+
55+
record::globalScope.addFunction("linerSolve", linerSolve);
56+
record::globalScope.addFunction("det", det);
57+
record::globalScope.addFunction("setRVector", setRVector);
58+
record::globalScope.addFunction("setCVector", setCVector);
59+
record::globalScope.addFunction("getCVector", getCVector);
60+
record::globalScope.addFunction("getRVector", getRVector);
61+
record::globalScope.addFunction("matMul", matMul);
62+
record::globalScope.addFunction("vecMul", vecMul);
63+
record::globalScope.addFunction("matSub", matSub);
64+
record::globalScope.addFunction("vecSub", vecSub);
65+
record::globalScope.addFunction("vecAdd", vecAdd);
66+
record::globalScope.addFunction("matAdd", matAdd);
67+
record::globalScope.addFunction("matDot", matDot);
68+
record::globalScope.addFunction("vecDot", vecDot);
3269
//Function* entity=runtime::globalScope.functionList["+"]; //在parse阶段,可以这样从函数域中找到函数名对应的函数实体
3370
//FunNode* testNode=new FunNode(entity); //然后这样通过函数实体创建相应的函数节点
3471
BinOpPriority["$"] =0;
72+
BinOpPriority["="] =1;
3573
BinOpPriority["+"] =10;
3674
BinOpPriority["-"] =10;
3775
BinOpPriority["*"] =20;
@@ -46,7 +84,7 @@ bool ast::isNum(const char &c)
4684

4785
bool ast::isBinOp(const char &c)
4886
{
49-
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
87+
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c=='=';
5088
}
5189

5290
bool ast::isLetter(const char &c)
@@ -96,9 +134,10 @@ BasicNode* ast::__ToAST(string &s)
96134
{
97135
while(j < n && isLetter(s[j]))
98136
j++;
99-
if(j < n && s[j] == '(')//函数
137+
if(j < n && s[j] == '(') //函数
100138
{
101-
FunNode* node = new FunNode(record::globalScope.functionList[s.substr(i, j - i)]);
139+
string name=s.substr(i, j - i);
140+
FunNode* node = new FunNode(record::globalScope.functionList[name]);
102141
//此时s[j] == '('
103142
while(s[j] != ')' && s[j] != LowestPriority)
104143
{
@@ -119,14 +158,19 @@ BasicNode* ast::__ToAST(string &s)
119158
i = j + 1;
120159
}
121160

122-
else//变量
161+
else //变量
123162
{
124-
record::globalScope.addVariable(s.substr(i, j - i));
125-
stackAST.push(record::globalScope.variableList[s.substr(i, j - i)]);
163+
string name=s.substr(i, j - i);
164+
if(record::globalScope.haveVariable(name)==false) //先前没有这变量
165+
{
166+
Variable* v=record::globalScope.addVariable(name);
167+
Variable* pv=record::globalScope.addVariable("ptr"+name);
168+
pv->setBorrowVal(v);
169+
}
170+
stackAST.push(record::globalScope.variableList[name]);
126171
i = j;
127172
}
128173
}
129-
130174
else if(j < n && s[j] == '(')//含括号的表达式
131175
{
132176
i = j;
@@ -183,7 +227,7 @@ BasicNode* ast::ToAST(string s){
183227
isInit = true;
184228
}
185229
for(string::iterator c = s.begin(); c != s.end(); c++){
186-
if(*c == '\x20' || * c == '\n' || *c == 't'){
230+
if(*c == '\x20' || * c == '\n' || *c == '\t'){
187231
s.erase(c);
188232
c--;
189233
}

copynode.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "nodetype.h"
2+
#include "matrix.hpp"
23

34
BasicNode* copyHelp::copyVal(BasicNode* node) //(值类型)拷贝
45
{
@@ -7,10 +8,14 @@ BasicNode* copyHelp::copyVal(BasicNode* node) //(值类型)拷贝
78
return new NumNode(*dynamic_cast<NumNode*>(node));
89
if(node->getType()==String)
910
return new StringNode(*dynamic_cast<StringNode*>(node));
11+
if(node->getType()==Matrix)
12+
return new matrixNode(*dynamic_cast<matrixNode*>(node));
13+
if(node->getType()==Vector)
14+
return new vectorNode(*dynamic_cast<vectorNode*>(node));
1015
if(node->getType()==Arr)
1116
return new ArrNode(*dynamic_cast<ArrNode*>(node));
1217
if(node->getType()==Null)
13-
return new nullNode(*dynamic_cast<nullNode*>(node));
18+
return new nullNode();
1419
//warn:支持更多具拷贝构造函数类型(目前都是字面量)后还需要在此处进行添加
1520
return nullptr; //如果进行参数检查了不会走到这一步
1621
}
@@ -32,6 +37,18 @@ BasicNode* copyHelp::copyNode(BasicNode* node) //拷贝单个子节点,warn:
3237
throw string("The type is not regular son nodes to copy"); //Pro不作为常规子节点,不在此考虑
3338
}
3439

40+
void copyHelp::delTree(BasicNode *n)
41+
{
42+
for(BasicNode* node:n->sonNode)
43+
{
44+
if(node->getType()!=Var) //fix:这里没有VarRef,是否错误?
45+
{
46+
delete node;
47+
node=nullptr;
48+
}
49+
}
50+
}
51+
3552
BasicNode::BasicNode(const BasicNode &n)
3653
{
3754
this->retFlag=n.isRet();
@@ -52,7 +69,7 @@ WhileNode::WhileNode(const WhileNode &n):conditionalControlNode(n)
5269
this->body=new ProNode(*(n.body));
5370
}
5471

55-
VarNode::VarNode(VarNode &n)
72+
VarNode::VarNode(VarNode &n):BasicNode(n)
5673
{
5774
this->typeRestrictFlag=n.istypeRestrict();
5875
this->ownershipFlag=n.isOwnership();

0 commit comments

Comments
 (0)