@@ -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
4785bool ast::isBinOp (const char &c)
4886{
49- return c == ' +' || c == ' -' || c == ' *' || c == ' /' || c == ' ^' ;
87+ return c == ' +' || c == ' -' || c == ' *' || c == ' /' || c == ' ^' || c== ' = ' ;
5088}
5189
5290bool 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 }
0 commit comments