@@ -70,6 +70,11 @@ bool ast::isBinOp(const char &c)
7070 return c == ' +' || c == ' -' || c == ' *' || c == ' /' || c == ' ^' || c==' =' ;
7171}
7272
73+ bool ast::isBinOp (const string &c)
74+ {
75+ return c == " +" || c == " -" || c == " *" || c == " /" || c == " ^" || c==" =" ;
76+ }
77+
7378bool ast::isLetter (const char &c)
7479{
7580 return (c >= ' A' && c <=' Z' ) || (c >= ' a' && c <= ' z' );
@@ -81,7 +86,7 @@ bool ast::canpush(stack<string> &stackOp, const string& op)
8186 return BinOpPriority[op] > BinOpPriority[stackOp.top ()];
8287}
8388
84- BasicNode* ast::__ToAST (string &s, Scope* sc)
89+ BasicNode* ast::__toAST (string &s, Scope* sc)
8590{
8691 s += LowestPriority;
8792 stack<BasicNode*> stackAST;
@@ -129,7 +134,7 @@ BasicNode* ast::__ToAST(string &s, Scope* sc)
129134 j++;
130135 if (j < n && (s[j] == ' ,' || s[j] == ' )' ) && countleftParenthesis == 1 )
131136 {
132- node->addNode (__ToAST (temp = s.substr (i + 1 , j - i - 1 ), sc));
137+ node->addNode (__toAST (temp = s.substr (i + 1 , j - i - 1 ), sc));
133138 if (s[j]==' )' ) countleftParenthesis--;
134139 i=j;
135140 }
@@ -173,12 +178,12 @@ BasicNode* ast::__ToAST(string &s, Scope* sc)
173178 }
174179 if (s[j] == ' )' )
175180 {
176- stackAST.push (__ToAST (temp = s.substr (i + 1 , j - i - 1 ), sc));// 为了让参数1变为左值
181+ stackAST.push (__toAST (temp = s.substr (i + 1 , j - i - 1 ), sc));// 为了让参数1变为左值
177182 i = j + 1 ;// +1是要过掉‘)’
178183 }
179184 else // 读到了字符串尾还是没有右括号
180185 {
181- stackAST.push (__ToAST (temp = s.substr (i + 1 , j - i - 2 ), sc));
186+ stackAST.push (__toAST (temp = s.substr (i + 1 , j - i - 2 ), sc));
182187 i = j - 1 ;// 应该忽略掉字符串结尾的$,便于下面判断是否能进stackOp
183188 }
184189 }
@@ -216,7 +221,7 @@ BasicNode* ast::__ToAST(string &s, Scope* sc)
216221 return stackAST.top ();
217222}
218223
219- BasicNode* ast::ToAST (string s, Scope* sc){
224+ BasicNode* ast::toAST (string s, Scope* sc){
220225 if (!isInit){
221226 Init ();
222227 isInit = true ;
@@ -227,5 +232,94 @@ BasicNode* ast::ToAST(string s, Scope* sc){
227232 c--;
228233 }
229234 }
230- return __ToAST (s, sc);
235+ return __toAST (s, sc);
236+ }
237+
238+
239+
240+ void ast::__output (BasicNode* now, ostream &os, const string& FatherOP)
241+ {
242+ if (now->getType () == Num)
243+ {
244+ double nownum = ((NumNode*)now)->getNum ();
245+ if (nownum < 0 )
246+ os << ' (' << nownum << ' )' ;
247+ else
248+ os << nownum;
249+ return ;
250+ }
251+
252+ if (now->getType () == Fun)
253+ {
254+ FunNode* t = (FunNode*)now;
255+ string op = t->getEntity ()->NAME ;
256+ if (isBinOp (op))
257+ {
258+ if (ast::BinOpPriority[op] < ast::BinOpPriority[FatherOP])
259+ os << ' (' ;
260+ __output (t->sonNode [0 ], os, op);
261+ os << op;
262+ if (op == " -" && t->sonNode [1 ]->getType () == Fun && ast::BinOpPriority[op] == ast::BinOpPriority[((FunNode*)(t->sonNode [1 ]))->getEntity ()->NAME ])
263+ os << ' (' ;
264+ __output (t->sonNode [1 ], os, op);
265+ if (ast::BinOpPriority[op] < ast::BinOpPriority[FatherOP])
266+ os << ' )' ;
267+ if (op == " -" && t->sonNode [1 ]->getType () == Fun && ast::BinOpPriority[op] == ast::BinOpPriority[((FunNode*)(t->sonNode [1 ]))->getEntity ()->NAME ])
268+ os << ' )' ;
269+ return ;
270+ }
271+ os << op << ' (' ;
272+ int n = t->getEntity ()->getParnum ();
273+ for (int i = 0 ; i < n; i++)
274+ {
275+ __output (t->sonNode [i], os);
276+ if (i != n - 1 )
277+ os << " ," ;
278+ }
279+ os << ' )' ;
280+ return ;
281+ }
282+
283+ if (now->getType () == Var)
284+ {
285+ os << ((VarNode*)now)->NAME ;
286+ }
287+
288+ if (now->getType ()==Matrix)
289+ ((matrixNode*)now)->output (os);
290+
291+ if (now->getType ()==Vector)
292+ ((vectorNode*)now)->output (os);
293+
294+ if (now->getType ()==Null)
295+ return ;
296+ }
297+
298+ void ast::output (BasicNode *now, ostream &os)
299+ {
300+ __output (now, os);;
301+ }
302+
303+ void ast::outputASTstruct (BasicNode* now, int depth)// 方便输出AST结构(并没有找到别的好方法)
304+ {
305+ for (int i = 0 ; i < depth; i++)
306+ cout << ' ' ;
307+ if (now->getType () == Num)
308+ {
309+ cout << ((NumNode*)now)->getNum () << endl;
310+ return ;
311+ }
312+ if (now->getType () == Var)
313+ {
314+ cout << ((VarNode*)now)->NAME << endl;
315+ return ;
316+ }
317+ if (now->getType () == Fun)
318+ {
319+ FunNode* temp = (FunNode*)now;
320+ int n = temp->getEntity ()->getParnum ();
321+ cout << temp->getEntity ()->NAME << endl;
322+ for (int i = 0 ; i < n; i++)
323+ outputASTstruct (temp->sonNode [i], depth+1 );;
324+ }
231325}
0 commit comments