@@ -14,7 +14,14 @@ public partial class AlgorithmEngine
1414 private Dictionary < string , Func < List < Operand > , Operand > > funcDict = new Dictionary < string , Func < List < Operand > , Operand > > ( ) ;
1515 private bool _useExcelIndex = true ;
1616 private int excelIndex = 1 ;
17+ /// <summary>
18+ /// 使用EXCEL索引
19+ /// </summary>
1720 public bool UseExcelIndex { get { return _useExcelIndex ; } set { _useExcelIndex = value ; excelIndex = value ? 1 : 0 ; } }
21+ /// <summary>
22+ /// 最后一个错误
23+ /// </summary>
24+ public string LastError { get ; private set ; }
1825
1926 public AlgorithmEngine ( )
2027 {
@@ -47,22 +54,63 @@ protected void AddFunction(string funName, Func<List<Operand>, Operand> function
4754
4855 public bool Parse ( string exp )
4956 {
50- m_tokens . Clear ( ) ; //清空语法单元堆栈
5157 if ( string . IsNullOrEmpty ( exp ) || exp . Trim ( ) == "" || ! this . isMatching ( exp ) ) {
58+ LastError = "exp无效" ;
5259 return false ;
5360 }
54- m_tokens = parse ( exp ) ;
61+ string error ;
62+ m_tokens = parse ( exp , out error ) ;
5563 return true ;
5664 }
65+ public bool Parse ( string exp , out string error )
66+ {
67+ if ( string . IsNullOrEmpty ( exp ) || exp . Trim ( ) == "" || ! this . isMatching ( exp ) ) {
68+ error = "exp无效" ;
69+ LastError = "exp无效" ;
70+ return false ;
71+ }
72+ m_tokens = parse ( exp , out error ) ;
73+ return true ;
74+ }
75+
5776 public bool Parse ( string name , string exp )
5877 {
5978 if ( string . IsNullOrEmpty ( exp ) || exp . Trim ( ) == "" || ! this . isMatching ( exp ) ) {
79+ LastError = "exp无效" ;
80+ return false ;
81+ }
82+ string error ;
83+ tokenDict [ name ] = parse ( exp , out error ) ;
84+ LastError = error ;
85+ return true ;
86+ }
87+ public bool Parse ( string name , string exp , out string error )
88+ {
89+ if ( string . IsNullOrEmpty ( exp ) || exp . Trim ( ) == "" || ! this . isMatching ( exp ) ) {
90+ error = "exp无效" ;
91+ LastError = "exp无效" ;
6092 return false ;
6193 }
62- tokenDict [ name ] = parse ( exp ) ;
94+ tokenDict [ name ] = parse ( exp , out error ) ;
95+ LastError = error ;
6396 return true ;
6497 }
6598
99+
100+ private Stack < object > parse ( string exp , out string error )
101+ {
102+ error = null ;
103+ var tmp = parse ( exp ) ;
104+ var names = GetParameterNames ( tmp ) ;
105+ foreach ( var item in names ) {
106+ if ( GetParameter ( new Operand ( item ) ) == null ) {
107+ error = $ "参数{ item } 无效!";
108+ return null ;
109+ }
110+ }
111+ return tmp ;
112+ }
113+
66114 private Stack < object > parse ( string exp )
67115 {
68116 Stack < object > operands = new Stack < object > ( ) ; //操作数堆栈
@@ -350,9 +398,13 @@ private List<string> splitText(string exp)
350398 #region GetParameterNames
351399
352400 public List < string > GetParameterNames ( )
401+ {
402+ return GetParameterNames ( m_tokens ) ;
403+ }
404+ private List < string > GetParameterNames ( Stack < object > tokens )
353405 {
354406 List < string > list = new List < string > ( ) ;
355- foreach ( var item in m_tokens ) {
407+ foreach ( var item in tokens ) {
356408 var curOpd = item as Operand ;
357409 if ( curOpd != null ) {
358410 if ( curOpd . Type == OperandType . PARAMETER ) {
@@ -364,6 +416,7 @@ public List<string> GetParameterNames()
364416 return list ;
365417 }
366418
419+
367420 #endregion
368421
369422 #region Evaluate
@@ -491,6 +544,7 @@ private object evaluate(Stack<object> tokens)
491544 if ( opds . Count == 1 ) {
492545 var outopd = opds . Pop ( ) ;
493546 if ( outopd . Type == OperandType . ERROR ) {
547+ LastError = outopd . StringValue ;
494548 throw new Exception ( outopd . StringValue ) ;
495549 }
496550 value = outopd . Value ;
0 commit comments