33
44using System ;
55using System . Collections . Generic ;
6- using System . Linq ;
76using System . Linq . Expressions ;
87
98using Simpleflow . Exceptions ;
@@ -13,57 +12,38 @@ namespace Simpleflow.CodeGenerator
1312{
1413 partial class SimpleflowCodeVisitor < TArg >
1514 {
16- public override Expression VisitLetStmt ( SimpleflowParser . LetStmtContext context )
17- {
18- if ( context . exception != null )
19- throw context . exception ;
20-
21-
22- var letIdentifierList = context . Identifier ( ) ;
23- var letIdentifier = context . IgnoreIdentifier ( ) != null ? null : letIdentifierList [ 0 ] . GetText ( ) ;
24-
25- // Validate variable name with reserved words
26- if ( SimpleflowKeywords . Keywords . Any ( keyword => string . Equals ( keyword , letIdentifier , StringComparison . Ordinal ) ) )
27- {
28- throw new VariableNameViolationException ( letIdentifier ) ;
29- }
30-
31- return GetLetVariableExpression ( context . expression ( ) ,
32- letIdentifier ,
33- errorVariableName : context . IgnoreIdentifier ( ) != null &&
34- letIdentifierList . Length == 1
35- ? letIdentifierList [ 0 ] . GetText ( )
36- : letIdentifierList . Length == 2
37- ? letIdentifierList [ 1 ] . GetText ( )
38- : null ) ;
39- }
40-
4115 // Mutate statement
4216 public override Expression VisitSetStmt ( SimpleflowParser . SetStmtContext context )
4317 {
4418 // Find variable and assign it
45- var variableName = context . IgnoreIdentifier ( ) != null
46- ? null
47- : context . Identifier ( ) [ 0 ] . GetText ( ) ;
19+ string variableName = context . IgnoreIdentifier ( ) != null
20+ ? null
21+ : context . Identifier ( ) [ 0 ] . GetText ( ) ;
4822
4923 // Assume that SmartVariable has already created as part of function invocation if available
5024 Expression variableExpression = variableName != null
51- ? GetVariable ( variableName ) ?? GetSmartVariable ( variableName ) ? . VariableExpression ? . Left
52- : null ;
25+ ? GetVariable ( variableName ) ?? GetSmartVariable ( variableName ) ? . VariableExpression ? . Left
26+ : null ;
5327
5428 if ( variableName != null && variableExpression == null )
5529 {
5630 throw new UndeclaredVariableException ( variableName ) ;
5731 }
5832
59- var rightSideSetExpression = GetRightSideExpressionOfSetStmt ( context , variableName , ref variableExpression ) ;
33+ // Get indexed object if defined as left expression
34+ variableExpression = GetIndexObjectExpIfDefined ( variableExpression , context . index ( ) ) ;
35+
36+ // Get value expression as right one
37+ Expression valueExpression = GetValueExpressionOfSetStmt ( context , variableName , ref variableExpression ) ;
6038
6139 // return expression
62- return GetSetStmtExpression ( context , variableName , variableExpression , rightSideSetExpression ) ;
40+ return GetSetStmtExpression ( context , variableName , variableExpression , valueExpression ) ;
6341
6442 }
6543
66- private Expression GetRightSideExpressionOfSetStmt ( SimpleflowParser . SetStmtContext context , string variableName , ref Expression variableExpression )
44+ private Expression GetValueExpressionOfSetStmt ( SimpleflowParser . SetStmtContext context ,
45+ string variableName , /* Pass variable name for discardable cheking */
46+ ref Expression variableExpression )
6747 {
6848 var expression = context . expression ( ) ;
6949 Expression rightSideSetExpression ;
@@ -155,108 +135,6 @@ private Expression AssignWithHandlingError(Expression variable, Expression right
155135 ) ;
156136 }
157137
158- private TryExpression AddTryCatchToExpression ( Expression rightsideExpression )
159- {
160- if ( rightsideExpression . Type == typeof ( void ) )
161- {
162- return AddTryCatchToExpressionForVoidValue ( rightsideExpression ) ;
163- }
164-
165- var varTupleConstructor = typeof ( VarTuple < > )
166- . MakeGenericType ( rightsideExpression . Type )
167- . GetConstructor ( new Type [ ] { rightsideExpression . Type , typeof ( Exception ) } ) ;
168-
169- ParameterExpression ex = ParameterExpression . Parameter ( typeof ( Exception ) ) ;
170- TryExpression tryCatchExpr =
171- Expression . TryCatch (
172- Expression . New ( varTupleConstructor ,
173- rightsideExpression , // it may throw
174- Expression . Constant ( null , typeof ( Exception ) )
175- ) ,
176- Expression . Catch (
177- ex ,
178- Expression . New ( varTupleConstructor ,
179- Expression . Default ( rightsideExpression . Type ) ,
180- ex )
181- )
182- ) ;
183-
184- return tryCatchExpr ;
185- }
186-
187- private TryExpression AddTryCatchToExpressionForVoidValue ( Expression rightsideExpression )
188- {
189- var varTupleConstructor = typeof ( VarTuple ) . GetConstructor ( new Type [ ] { typeof ( Exception ) } ) ;
190-
191- ParameterExpression ex = ParameterExpression . Parameter ( typeof ( Exception ) ) ;
192-
193- TryExpression tryCatchExpr =
194- Expression . TryCatch (
195- Expression . Block (
196- rightsideExpression , // it may throw
197- Expression . New ( varTupleConstructor ,
198- Expression . Constant ( null , typeof ( Exception ) )
199- )
200- ) ,
201- Expression . Catch (
202- ex ,
203- Expression . New ( varTupleConstructor , ex )
204- )
205- ) ;
206-
207- return tryCatchExpr ;
208- }
209-
210-
211- private Expression GetLetVariableExpression ( SimpleflowParser . ExpressionContext context , string variableName , string errorVariableName )
212- {
213- if ( context . jsonObj ( ) != null )
214- {
215- if ( variableName == null )
216- {
217- throw new SimpleflowException ( Resources . Message . CannotIgnoreIdentifierForJsonObj ) ;
218- }
219-
220- return new SmartJsonObjectParameterExpression ( context , variableName ) ;
221- }
222- var expression = Visit ( context . GetChild ( 0 ) ) ;
223-
224- // if there's a error variable not declared then return value, else catch it and return
225- if ( string . IsNullOrWhiteSpace ( errorVariableName ) )
226- {
227- if ( variableName == null ) //variableName is null means Ignore variable
228- {
229- return expression ;
230- }
231- else
232- {
233- return Expression . Assign ( Expression . Variable ( expression . Type , variableName ) , expression ) ;
234- }
235- }
236- else
237- {
238- // add try catch if there's error variable defined
239- var tryExpression = AddTryCatchToExpression ( expression ) ;
240- var varFortryExpression = Expression . Variable ( tryExpression . Type ) ;
241-
242- // Add variables
243- if ( variableName != null )
244- {
245- return Expression . Block (
246- Expression . Assign ( varFortryExpression , tryExpression ) , // run expression with try catch and capture value
247- Expression . Assign ( Expression . Variable ( expression . Type , variableName ) , Expression . Field ( varFortryExpression , "Value" ) ) ,
248- Expression . Assign ( Expression . Variable ( typeof ( Exception ) , errorVariableName ) , Expression . Field ( varFortryExpression , "Error" ) )
249- ) ;
250- }
251-
252- // assign error only, not regular variable
253- return Expression . Block (
254- Expression . Assign ( varFortryExpression , tryExpression ) , // run expression with try catch and capture value
255- Expression . Assign ( Expression . Variable ( typeof ( Exception ) , errorVariableName ) , Expression . Field ( varFortryExpression , "Error" ) )
256- ) ;
257- }
258- }
259-
260138 private Expression VisitPartialSet ( SimpleflowParser . SetStmtContext context , Expression variable )
261139 {
262140 var pairs = context . expression ( ) . jsonObj ( ) . pair ( ) ;
0 commit comments