Express.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-10
  5. * @Description 表达式计算
  6. */
  7. using MECF.Framework.UI.Core.DxfScript;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace MECF.Framework.UI.Core.DxfScript
  15. {
  16. public partial class Express
  17. {
  18. public ExpressNode Root { get; set; }
  19. private Dictionary<string, Variable> Variables = new Dictionary<string, Variable>();
  20. public Express()
  21. {
  22. Root = null;
  23. }
  24. private void Trace(string s)
  25. {
  26. string a = s;
  27. }
  28. public ExpressNode AddChild(ExpressNode curr, ExpressNode node)
  29. {
  30. if (curr == null)
  31. {
  32. if (Root != null )
  33. {
  34. throw new Exception("Add tree child error:try to add the fist node,but root is not null");
  35. }
  36. else
  37. {
  38. Root = node;
  39. return Root;
  40. }
  41. }
  42. if (curr.FirstChild == null)
  43. {
  44. //maintain father-child realtionship
  45. curr.FirstChild = node;
  46. curr.LastChild = node;
  47. node.Father = curr;
  48. }
  49. else
  50. {
  51. //maintain brothers realtionship
  52. curr.LastChild.NextBrother = node;
  53. node.PrevBrother = curr.LastChild;
  54. //maintain father-child realtionship
  55. curr.LastChild = node;
  56. node.Father = curr;
  57. }
  58. return node;
  59. }
  60. public ExpressNode AddFather(ExpressNode curr, ExpressNode node)
  61. {
  62. if (curr == null)
  63. {
  64. throw new Exception("It's invalid to add father to a null node");
  65. }
  66. if (curr.Father == null)
  67. {
  68. if (curr != Root)
  69. {
  70. throw new Exception("Try to add father and be root,but current node is not root");
  71. }
  72. //maintain father-child realtionship
  73. curr.Father = node;
  74. node.FirstChild = curr;
  75. node.LastChild = curr;
  76. Root = node;
  77. }
  78. else
  79. {
  80. //take over curr's brothers relationship
  81. if (curr.PrevBrother != null)
  82. {
  83. node.PrevBrother = curr.PrevBrother;
  84. curr.PrevBrother.NextBrother = node;
  85. }
  86. curr.PrevBrother = null;
  87. if (curr.NextBrother != null)
  88. {
  89. node.NextBrother = curr.NextBrother;
  90. curr.NextBrother.PrevBrother = node;
  91. }
  92. curr.NextBrother = null;
  93. //take over curr's father relationship
  94. node.Father = curr.Father;
  95. if (node.Father.FirstChild == curr)
  96. {
  97. node.Father.FirstChild = node;
  98. }
  99. if (node.Father.LastChild == curr)
  100. {
  101. node.Father.LastChild = node;
  102. }
  103. //maintain curr's child realtionship
  104. node.FirstChild = curr;
  105. node.LastChild = curr;
  106. curr.Father = node;
  107. }
  108. return node;
  109. }
  110. public void Create(StringReader reader)
  111. {
  112. CreateMain(reader);
  113. ComputeNode(Root, true);
  114. }
  115. public Variable GetTopVariable()
  116. {
  117. return Root.Var;
  118. }
  119. public Variable Compute()
  120. {
  121. ComputeNode(Root);
  122. return Root.Var ;
  123. }
  124. public void ComputeNode(ExpressNode curr,bool OnlyCheckDataType = false)
  125. {
  126. if (curr == null)
  127. {
  128. return ;
  129. }
  130. if (curr.NodeType == KeywordType.KW_VARIABLE || curr.NodeType == KeywordType.KW_CONST_TRUE || curr.NodeType == KeywordType.KW_CONST_FALSE ||
  131. curr.NodeType == KeywordType.KW_CONST_INTEGER || curr.NodeType == KeywordType.KW_CONST_DECIMAL || curr.NodeType == KeywordType.KW_CONST_STRING)
  132. {
  133. //变量或常量
  134. if (curr.Var.IsNone && !OnlyCheckDataType)
  135. {
  136. throw new Exception("Computing varialbe:" + curr.Var.Name + " is none");
  137. }
  138. else
  139. {
  140. return;
  141. }
  142. }
  143. else if (curr.NodeType == KeywordType.KW_OPERATOR_NEGATIVE)
  144. {
  145. //单目运算符-
  146. if (curr.FirstChild == null)
  147. {
  148. throw new Exception("Negtive operator error:No data");
  149. }
  150. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  151. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  152. curr.Var.Nigtive(OnlyCheckDataType);
  153. return;
  154. }
  155. else if (curr.NodeType == KeywordType.KW_OPERATOR_NOT)
  156. {
  157. if (curr.FirstChild == null)
  158. {
  159. throw new Exception("Not operator error:No data");
  160. }
  161. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  162. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  163. curr.Var.Not(OnlyCheckDataType);
  164. return;
  165. }
  166. else if (curr.NodeType == KeywordType.KW_OPERATOR_BIT_NOT)
  167. {
  168. throw new Exception("BitNot operator not implemented");
  169. if (curr.FirstChild == null)
  170. {
  171. throw new Exception("BitNot operator error:No data");
  172. }
  173. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  174. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  175. //curr.Var.BitNot(OnlyCheckDataType);
  176. }
  177. else if (curr.NodeType == KeywordType.KW_OPERATOR_PLUS)
  178. {
  179. //双目运算符+(多目)
  180. if (curr.FirstChild == null)
  181. {
  182. throw new Exception("Plus operator error:No data");
  183. }
  184. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  185. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  186. ExpressNode child = curr.FirstChild.NextBrother;
  187. if (child == null)
  188. {
  189. throw new Exception("Plus operator error:No second data");
  190. }
  191. while (child != null)
  192. {
  193. ComputeNode(child, OnlyCheckDataType);
  194. curr.Var.Plus(child.Var, OnlyCheckDataType);
  195. child = child.NextBrother;
  196. }
  197. return ;
  198. }
  199. else if (curr.NodeType == KeywordType.KW_OPERATOR_MINUS)
  200. {
  201. //双目运算符-
  202. if (curr.FirstChild == null)
  203. {
  204. throw new Exception("Minus operator error:No data");
  205. }
  206. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  207. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  208. ExpressNode child = curr.FirstChild.NextBrother;
  209. if (child == null)
  210. {
  211. throw new Exception("Minus otperator error:No second data");
  212. }
  213. ComputeNode(child, OnlyCheckDataType);
  214. curr.Var.Minus(child.Var, OnlyCheckDataType);
  215. return ;
  216. }
  217. else if (curr.NodeType == KeywordType.KW_OPERATOR_MULTIPLE)
  218. {
  219. //双目运算符*(多目)
  220. if (curr.FirstChild == null)
  221. {
  222. throw new Exception("Multiple operator error:No data");
  223. }
  224. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  225. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  226. ExpressNode child = curr.FirstChild.NextBrother;
  227. if (child == null)
  228. {
  229. throw new Exception("Multiple operator error:No second data");
  230. }
  231. while (child != null)
  232. {
  233. ComputeNode(child, OnlyCheckDataType);
  234. curr.Var.Multiple(child.Var, OnlyCheckDataType);
  235. child = child.NextBrother;
  236. }
  237. return ;
  238. }
  239. else if (curr.NodeType == KeywordType.KW_OPERATOR_DEVIDE)
  240. {
  241. //双目运算符/
  242. if (curr.FirstChild == null)
  243. {
  244. throw new Exception("Multiple operator error:No data");
  245. }
  246. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  247. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  248. ExpressNode child = curr.FirstChild.NextBrother;
  249. if (child == null)
  250. {
  251. throw new Exception("Devide operator error:No second data");
  252. }
  253. ComputeNode(child, OnlyCheckDataType);
  254. curr.Var.Devide(child.Var, OnlyCheckDataType);
  255. return ;
  256. }
  257. else if (curr.NodeType == KeywordType.KW_OPERATOR_EQUAL)
  258. {
  259. //双目运算符==
  260. if (curr.FirstChild == null)
  261. {
  262. throw new Exception("Equal operator error:No data");
  263. }
  264. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  265. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  266. ExpressNode child = curr.FirstChild.NextBrother;
  267. if (child == null)
  268. {
  269. throw new Exception("Equal operator error:No second data");
  270. ;
  271. }
  272. ComputeNode(child, OnlyCheckDataType);
  273. curr.Var.Equal(child.Var, OnlyCheckDataType);
  274. return ;
  275. }
  276. else if (curr.NodeType == KeywordType.KW_OPERATOR_NOT_EQUAL)
  277. {
  278. //双目运算符!=
  279. if (curr.FirstChild == null)
  280. {
  281. throw new Exception("NotEqual operator error:No data");
  282. }
  283. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  284. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  285. ExpressNode child = curr.FirstChild.NextBrother;
  286. if (child == null)
  287. {
  288. throw new Exception("NotEqual operator error:No second data");
  289. }
  290. ComputeNode(child, OnlyCheckDataType);
  291. curr.Var.NotEqual(child.Var, OnlyCheckDataType);
  292. return ;
  293. }
  294. else if (curr.NodeType == KeywordType.KW_OPERATOR_GT)
  295. {
  296. //双目运算符>
  297. if (curr.FirstChild == null)
  298. {
  299. throw new Exception("Gt operator error:No data");
  300. }
  301. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  302. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  303. ExpressNode child = curr.FirstChild.NextBrother;
  304. if (child == null)
  305. {
  306. throw new Exception("Gt operator error:No second data");
  307. }
  308. ComputeNode(child, OnlyCheckDataType);
  309. curr.Var.Gt(child.Var, OnlyCheckDataType);
  310. return ;
  311. }
  312. else if (curr.NodeType == KeywordType.KW_OPERATOR_GE)
  313. {
  314. //双目运算符>=
  315. if (curr.FirstChild == null)
  316. {
  317. throw new Exception("Ge operator error:No data");
  318. ;
  319. }
  320. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  321. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  322. ExpressNode child = curr.FirstChild.NextBrother;
  323. if (child == null)
  324. {
  325. throw new Exception("Ge operator error:No second data");
  326. }
  327. ComputeNode(child, OnlyCheckDataType);
  328. curr.Var.Ge(child.Var, OnlyCheckDataType);
  329. return ;
  330. }
  331. else if (curr.NodeType == KeywordType.KW_OPERATOR_LT)
  332. {
  333. //双目运算符<
  334. if (curr.FirstChild == null)
  335. {
  336. throw new Exception("Lt operator error:No data");
  337. }
  338. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  339. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  340. ExpressNode child = curr.FirstChild.NextBrother;
  341. if (child == null)
  342. {
  343. throw new Exception("Lt operator error:No second data");
  344. }
  345. ComputeNode(child, OnlyCheckDataType);
  346. curr.Var.Lt(child.Var, OnlyCheckDataType);
  347. return ;
  348. }
  349. else if (curr.NodeType == KeywordType.KW_OPERATOR_LE)
  350. {
  351. //双目运算符<=
  352. if (curr.FirstChild == null)
  353. {
  354. throw new Exception("Le operator error:No data");
  355. }
  356. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  357. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  358. ExpressNode child = curr.FirstChild.NextBrother;
  359. if (child == null)
  360. {
  361. throw new Exception("Le operator error:No second data");
  362. }
  363. ComputeNode(child, OnlyCheckDataType);
  364. curr.Var.Le(child.Var, OnlyCheckDataType);
  365. return ;
  366. }
  367. else if (curr.NodeType == KeywordType.KW_OPERATOR_AND)
  368. {
  369. //双目运算符&&
  370. if (curr.FirstChild == null)
  371. {
  372. throw new Exception("And operator error:No data");
  373. }
  374. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  375. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  376. ExpressNode child = curr.FirstChild.NextBrother;
  377. if (child == null)
  378. {
  379. throw new Exception("And operator error:No second data");
  380. }
  381. while (child != null)
  382. {
  383. ComputeNode(child, OnlyCheckDataType);
  384. curr.Var.And(child.Var, OnlyCheckDataType);
  385. child = child.NextBrother;
  386. }
  387. return ;
  388. }
  389. else if (curr.NodeType == KeywordType.KW_OPERATOR_OR)
  390. {
  391. //双目运算符||
  392. if (curr.FirstChild == null)
  393. {
  394. throw new Exception("Or operator error:No data");
  395. }
  396. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  397. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  398. ExpressNode child = curr.FirstChild.NextBrother;
  399. if (child == null)
  400. {
  401. throw new Exception("Or operator error:No second data");
  402. }
  403. while (child != null)
  404. {
  405. ComputeNode(child, OnlyCheckDataType);
  406. curr.Var.Or(child.Var, OnlyCheckDataType);
  407. child = child.NextBrother;
  408. }
  409. return ;
  410. }
  411. else if (curr.NodeType == KeywordType.KW_OPERATOR_BIT_OR)
  412. {
  413. //双目运算符|
  414. throw new Exception("BitOr operator error:Not support");
  415. }
  416. else if (curr.NodeType == KeywordType.KW_OPERATOR_BIT_AND)
  417. {
  418. //双目运算符&
  419. throw new Exception("BitAnd operator error:Not support");
  420. }
  421. else if (curr.NodeType == KeywordType.KW_OPERATOR_MOVE_LEFT)
  422. {
  423. //双目运算符<<
  424. throw new Exception("Move left operator error:Not support");
  425. }
  426. else if (curr.NodeType == KeywordType.KW_OPERATOR_MOVE_RIGHT)
  427. {
  428. //双目运算符>>
  429. throw new Exception("Move right operator error:Not support");
  430. }
  431. else if (curr.NodeType == KeywordType.KW_ROUND_LEFT)
  432. {
  433. //左小括号
  434. if (curr.FirstChild == null)
  435. {
  436. throw new Exception("Move operator error:No data");
  437. }
  438. if (!curr.Closed)
  439. {
  440. throw new Exception("() not matched");
  441. }
  442. ComputeNode(curr.FirstChild, OnlyCheckDataType);
  443. curr.Var.CopyFrom(curr.FirstChild.Var, OnlyCheckDataType);
  444. return ;
  445. }
  446. else if (curr.NodeType == KeywordType.KW_FUNCTION)
  447. {
  448. //函数
  449. if (!curr.Closed)
  450. {
  451. throw new Exception("() not matched");
  452. }
  453. UserFunction function = curr.UserFunc;
  454. function(curr, OnlyCheckDataType);
  455. return;
  456. }
  457. else
  458. {
  459. throw new Exception("Undefined operator:"+ curr.NodeType);
  460. }
  461. }
  462. private void CreateMain(StringReader reader)
  463. {
  464. ExpressNode curr = Root;
  465. KeywordType kw;
  466. while ((kw = reader.Next()) != KeywordType.KW_EOF && kw != KeywordType.KW_SEP)
  467. {
  468. Trace("kw="+kw+","+reader.Data());
  469. if (kw == KeywordType.KW_CONST_INTEGER || kw == KeywordType.KW_CONST_DECIMAL ||
  470. kw == KeywordType.KW_CONST_TRUE || kw == KeywordType.KW_CONST_FALSE ||
  471. kw == KeywordType.KW_CONST_STRING)
  472. {
  473. Variable var;
  474. if (kw == KeywordType.KW_CONST_INTEGER)
  475. {
  476. var = new Variable(null, VariableDataType.DOUBLE);
  477. var.DoubleValue=(int.Parse(reader.Data()));
  478. }
  479. else if (kw == KeywordType.KW_CONST_DECIMAL)
  480. {
  481. var = new Variable(null, VariableDataType.DOUBLE);
  482. var.DoubleValue =(double.Parse(reader.Data()));
  483. }
  484. else if (kw == KeywordType.KW_CONST_TRUE)
  485. {
  486. var = new Variable(null, VariableDataType.BOOL);
  487. var.BoolValue=true;
  488. }
  489. else if (kw == KeywordType.KW_CONST_FALSE)
  490. {
  491. var = new Variable(null, VariableDataType.BOOL);
  492. var.BoolValue= false;
  493. }
  494. else
  495. {
  496. var = new Variable(null, VariableDataType.STRING);
  497. var.StringValue=reader.Data();
  498. }
  499. ExpressNode expressNode = new ExpressNode(var, null, KeywordType.KW_VARIABLE);
  500. if (curr == null)
  501. {
  502. curr = AddChild(curr, expressNode);
  503. }
  504. else
  505. {
  506. if (curr.NodeType == KeywordType.KW_VARIABLE || curr.Closed)
  507. {
  508. throw new Exception("Syntax error:Expected operator but const found in line:"+ reader.__line__);
  509. }
  510. curr = AddChild(curr, expressNode);
  511. }
  512. }
  513. else if (kw == KeywordType.KW_VARIABLE)
  514. {
  515. string varName = reader.Data();
  516. Variable var = null;
  517. if (Variables.ContainsKey(varName))
  518. {
  519. var = Variables[varName];
  520. }
  521. else
  522. {
  523. VariableDataType varType = ScriptVariables.GetVariableDataTypeByName(varName);
  524. var = new Variable(varName, varType);
  525. Variables.Add(varName, var);
  526. }
  527. ExpressNode expressNode = new ExpressNode(var, null, KeywordType.KW_VARIABLE);
  528. if (curr == null)
  529. {
  530. curr = AddChild(curr, expressNode);
  531. }
  532. else
  533. {
  534. if (curr.NodeType == KeywordType.KW_VARIABLE || curr.Closed)
  535. {
  536. throw new Exception("Syntax error:Expected operator but variable found:"+ reader.__line__);
  537. }
  538. curr = AddChild(curr, expressNode);
  539. }
  540. }
  541. else if (kw == KeywordType.KW_ROUND_LEFT)
  542. {
  543. Variable var = new Variable(null, VariableDataType.DOUBLE);
  544. ExpressNode expressNode = new ExpressNode(var, null, KeywordType.KW_ROUND_LEFT);
  545. if (curr == null)
  546. {
  547. curr = AddChild(curr, expressNode);
  548. }
  549. else
  550. {
  551. if (curr.NodeType == KeywordType.KW_VARIABLE || curr.Closed)
  552. {
  553. throw new Exception("Syntax error:Expected operator but '(' found:"+ reader.__line__);
  554. }
  555. curr = AddChild(curr, expressNode);
  556. }
  557. }
  558. else if (kw == KeywordType.KW_FUNCTION)
  559. {
  560. string funName = reader.Data();
  561. UserFunction function = GetFunAddr(funName);
  562. KeywordType kw2 = reader.Next();
  563. if (kw2 != KeywordType.KW_ROUND_LEFT)
  564. {
  565. throw new Exception("Syntax error:Expected '(' :"+ reader.__line__);
  566. }
  567. //g_log.info("funName=%s,addr=%d",funName,function);
  568. Variable var = new Variable(null, VariableDataType.DOUBLE);
  569. ExpressNode expressNode = new ExpressNode(var, function, KeywordType.KW_FUNCTION);
  570. if (curr == null)
  571. {
  572. curr = AddChild(curr, expressNode);
  573. }
  574. else
  575. {
  576. if (curr.NodeType == KeywordType.KW_VARIABLE || curr.Closed)
  577. {
  578. throw new Exception("Syntax error:Expected operator but function found:"+reader.__line__);
  579. }
  580. curr = AddChild(curr, expressNode);
  581. }
  582. }
  583. else if (kw == KeywordType.KW_ROUND_RIGHT)
  584. {
  585. while (curr != null)
  586. {
  587. if ((curr.NodeType == KeywordType.KW_FUNCTION || curr.NodeType == KeywordType.KW_ROUND_LEFT) && !curr.Closed)
  588. {
  589. break;
  590. }
  591. curr = curr.Father;
  592. }
  593. if (curr == null)
  594. {
  595. reader.rollback(KeywordType.KW_ROUND_RIGHT);
  596. return;
  597. }
  598. curr.Closed = true;
  599. }
  600. else if (kw == KeywordType.KW_OPERATOR_NEGATIVE || kw == KeywordType.KW_OPERATOR_NOT ||
  601. kw == KeywordType.KW_OPERATOR_BIT_NOT)
  602. {
  603. //single data operator
  604. Variable var = new Variable(null, VariableDataType.DOUBLE);
  605. ExpressNode expressNode = new ExpressNode(var, null, kw);
  606. if (curr == null)
  607. {
  608. curr = AddChild(curr, expressNode);
  609. }
  610. else
  611. {
  612. if (curr.NodeType == KeywordType.KW_VARIABLE || curr.Closed)
  613. {
  614. throw new Exception("Syntax error:Unexpected variable found:"+reader.__line__);
  615. }
  616. curr = AddChild(curr, expressNode);
  617. }
  618. }
  619. else if (kw == KeywordType.KW_OPERATOR_PLUS || kw == KeywordType.KW_OPERATOR_MINUS ||
  620. kw == KeywordType.KW_OPERATOR_MULTIPLE || kw == KeywordType.KW_OPERATOR_DEVIDE ||
  621. kw == KeywordType.KW_OPERATOR_AND || kw == KeywordType.KW_OPERATOR_BIT_AND ||
  622. kw == KeywordType.KW_OPERATOR_OR || kw == KeywordType.KW_OPERATOR_BIT_OR ||
  623. kw == KeywordType.KW_OPERATOR_GT || kw == KeywordType.KW_OPERATOR_GE ||
  624. kw == KeywordType.KW_OPERATOR_LT || kw == KeywordType.KW_OPERATOR_LE ||
  625. kw == KeywordType.KW_OPERATOR_EQUAL || kw == KeywordType.KW_OPERATOR_NOT_EQUAL ||
  626. kw == KeywordType.KW_OPERATOR_MOVE_LEFT || kw == KeywordType.KW_OPERATOR_MOVE_RIGHT || kw == KeywordType.KW_OPERATOR_COMMA)
  627. {
  628. //two data operator
  629. Variable var = new Variable(null, VariableDataType.DOUBLE);
  630. ExpressNode expressNode = new ExpressNode(var, null, kw);
  631. if (curr == null)
  632. {
  633. throw new Exception("Syntax error:Can not begin with a operator:"+reader.__line__);
  634. }
  635. int compare = -2;
  636. while (curr != null)
  637. {
  638. compare = -2;
  639. if (curr.Father == null)
  640. {
  641. break;
  642. }
  643. else if ((compare = curr.Father.ComparePrior(kw)) <= 0)
  644. {
  645. if (compare < -1)
  646. {
  647. throw new Exception("Syntax error:Unmatched operator:"+reader.__line__);
  648. }
  649. break;
  650. }
  651. curr = curr.Father;
  652. }
  653. if (curr.Father == null || compare == -1)
  654. {
  655. curr = AddFather(curr, expressNode);
  656. }
  657. else
  658. {
  659. //优先级相等
  660. curr = curr.Father;
  661. }
  662. }
  663. else
  664. {
  665. reader.rollback(kw);
  666. return ;
  667. }
  668. }
  669. //throw new Exception("Syntax error:Found unexpected EOF:"+ reader.__line__);
  670. }
  671. }
  672. }