@@ -82,11 +82,16 @@ public static TryGet<CompiledExpression> CompileExpression(BaseToken[] tokens)
8282 break ;
8383 }
8484 }
85-
86- var expression = new Expression ( sb . ToString ( ) , EvaluateOptions . None ) ;
85+
86+ var rawRep = sb . ToString ( ) ;
87+ var expression = new Expression ( rawRep , EvaluateOptions . None ) ;
8788
8889 // Now we have the expression string and a variables dictionary.
89- return new CompiledExpression ( expression , variables ) ;
90+ return new CompiledExpression (
91+ expression ,
92+ variables ,
93+ rawRep
94+ ) ;
9095
9196 string MakeTempName ( )
9297 {
@@ -100,67 +105,34 @@ void AppendRaw(string s)
100105 }
101106 }
102107
103- // --- Evaluate helper that sets typed params and uses EvaluateParameter ---
104- private static TryGet < object > Evaluate ( string exprString , Dictionary < string , object > variables , Result rs )
105- {
106- var expression = new Expression ( exprString , EvaluateOptions . None )
107- {
108- // Supply our typed parameter dictionary:
109- Parameters = new Dictionary < string , object > ( variables , StringComparer . OrdinalIgnoreCase )
110- } ;
111-
112- // As a safe fallback, respond to EvaluateParameter requests by resolving from variables
113- expression . EvaluateParameter += ( name , args ) =>
114- {
115- if ( variables . TryGetValue ( name , out var val ) )
116- {
117- args . Result = val ;
118- return ;
119- }
120-
121- // if not found, return a helpful error via exception. NCalc will bubble it.
122- throw new ArgumentException ( $ "Unknown identifier '{ name } ' in expression.") ;
123- } ;
124-
125- try
126- {
127- var result = expression . Evaluate ( ) ;
128- return new TryGet < object > ( result , null ) ; // adapt to your TryGet ctor
129- }
130- catch ( Exception ex )
131- {
132- // Keep error messages friendly and actionable
133- return rs + $ "{ ex . GetType ( ) . AccurateName } : { ex . Message } ";
134- }
135- }
136-
137108 public readonly struct CompiledExpression (
138109 Expression expression ,
139- Dictionary < string , DynamicTryGet < object > > parameters
110+ Dictionary < string , DynamicTryGet < object > > parameters ,
111+ string rawRepresentation
140112 )
141113 {
142114 public TryGet < object > Evaluate ( )
143115 {
144- Dictionary < string , object > values = [ ] ;
145- foreach ( var ( key , dtg ) in parameters )
116+ try
146117 {
147- if ( dtg . Invoke ( ) . HasErrored ( out var err , out var value ) )
118+ Dictionary < string , object > values = [ ] ;
119+ foreach ( var ( key , dtg ) in parameters )
148120 {
149- return err ;
150- }
121+ if ( dtg . Invoke ( ) . HasErrored ( out var err , out var value ) )
122+ {
123+ return err ;
124+ }
151125
152- values [ key ] = value ;
153- }
126+ values [ key ] = value ;
127+ }
154128
155- expression . Parameters = values ;
156-
157- try
158- {
129+ expression . Parameters = values ;
130+
159131 return expression . Evaluate ( ) ;
160132 }
161- catch ( Exception ex )
133+ catch ( Exception )
162134 {
163- return $ "{ ex . GetType ( ) . AccurateName } : { ex . Message } ";
135+ return $ "Expression ' { rawRepresentation } ' is invalid. ";
164136 }
165137 }
166138 }
0 commit comments