////////////////////////////////////////////// private static Boolean ExitCurrentContext( DynLanContext DynLanContext, Object Result, Exception ex) { DynLanState context = DynLanContext.CurrentState; context.CurrentLineID = Guid.Empty; // jeśli został tylko ostatni główny context if (DynLanContext.Stack.Count == 1) { DynLanContext.Result = Result; DynLanContext.IsFinished = true; } else { DynLanContext.PopContext(ex); if (DynLanContext.CurrentExpressionState != null) { DynLanContext.CurrentExpressionState.PushValue(Result); } } return(true); }
private static Boolean ExitCurrentContext( DynLanContext DynLanContext, Exception ex) { DynLanState state = DynLanContext.CurrentState; state.CurrentLineID = Guid.Empty; Object result = null; if (state != null && state.ContextType == DynLanContextType.CLASS) { result = state.Object; } // jeśli został tylko ostatni główny context if (DynLanContext.Stack.Count == 1) { DynLanContext.Result = result; DynLanContext.IsFinished = true; } else { DynLanContext.PopContext(ex); if (DynLanContext.CurrentExpressionState != null) { DynLanContext.CurrentExpressionState.PushValue(result); } } return(true); }
private static Exception PrepareDynLanException(DynLanContext DynLanContext, Exception Error) { Exception outError = null; if (Error != null) { IDynLanException DynLanException = Error as IDynLanException; if (DynLanException != null) { //if (String.IsNullOrEmpty(DynLanException.DynLanStacktrace)) DynLanException.DynLanStacktrace = GetStacktrace(DynLanContext); outError = Error; } else if (Error.GetType() == typeof(Exception)) { outError = new DynLanExecuteException(Error.Message) { DynLanStacktrace = GetStacktrace(DynLanContext) }; } else { outError = new DynLanExecuteException(Error.Message, Error) { DynLanStacktrace = GetStacktrace(DynLanContext) }; } } return(outError); }
public static Boolean NextStep( DynLanContext DynLanContext) { if (DynLanContext == null || DynLanContext.CurrentState == null) { return(true); } ExpressionContext curExpressionContext = DynLanContext. CurrentState. ExpressionContext; ExpressionGroup curExpressionGroup = curExpressionContext. ExpressionGroup; if (curExpressionContext == null || curExpressionContext.IsFinished || curExpressionContext.Current == null) { return(true); } if (curExpressionContext.Current.Expression.IsOnpExecution) { return(ExpressionEvaluatorOnp.EvaluateOnp( DynLanContext)); } else { return(ExpressionEvaluatorQueue.EvaluateQueue( DynLanContext)); } }
//////////////////////////////////////////////////////////////////////// public static Object Execute(DynLanContext EvaluateContext, Object obj, IList <Object> Parameters) { Boolean isValueSet = false; String propertyPath = UniConvert.ToUniString(Parameters != null && Parameters.Count > 0 ? Parameters[0] : null); Object value = (Parameters != null && Parameters.Count > 1 ? Parameters[1] : null); if (obj is IDictionaryWithGetter) { IDictionaryWithGetter dict = obj as IDictionaryWithGetter; if (dict.CanSetValueToDictionary(propertyPath)) { dict[propertyPath] = value; isValueSet = true; } } else if (obj is IDictionary) { IDictionary dict = obj as IDictionary; dict[propertyPath] = value; isValueSet = true; } else if (obj is IDictionary <string, object> ) { IDictionary <string, object> dict = obj as IDictionary <string, object>; dict[propertyPath] = value; isValueSet = true; } if (obj is DynLanObject) { #if CASE_INSENSITIVE propertyPath = propertyPath.ToUpper(); #endif DynLanObject DynLanObj = obj as DynLanObject; DynLanObj[propertyPath] = value; return(null); } if (!isValueSet) { isValueSet = RefSensitiveHelper.I.SetValue(obj, propertyPath, value); } //if (!isValueSet) // isValueSet = RefUnsensitiveHelper.I.SetValue(obj, propertyPath, value); if (isValueSet) { return(value); } return(null); }
public static Boolean EvaluateValue( String FieldOrMethodName, DynLanContext DynLanContext) { return(EvaluateValueOrMethod( null, FieldOrMethodName, -1, DynLanContext)); }
////////////////////////////// public Expression FindExpression(String VariableName, DynLanContext DynLanContext) { Expression foundExpression = null; if (Expressions != null) { Expressions.TryGetValue(VariableName, out foundExpression); } return(foundExpression); }
private static Boolean?CheckIfFinishedOrEmptyLine( DynLanContext DynLanContext, DynLanState currentState) { if (currentState == null || DynLanContext.IsFinished) { DynLanContext.IsFinished = true; return(true); } if (DynLanContext.ExceptionToThrow != null) { Exception ex = DynLanContext.ExceptionToThrow; DynLanContext.ExceptionToThrow = null; throw ex; } DynLanCodeLines lines = currentState.GetCurrentLines(); DynLanCodeLine currentLine = currentState.GetCurrentLine(); if (currentLine == null) { return(ExitCurrentContext( DynLanContext, null)); } // jeśli linia jest pusta to przechodzimy do nastepnej if (currentLine.IsLineEmpty) { DynLanCodeLine nextLine = DynLanCodeLinesExtender.NextLine(lines, currentLine); if (nextLine == null) { return(ExitCurrentContext( DynLanContext, null)); } else { currentState.CurrentLineID = nextLine.ID; } return(true); } return(null); }
private static Boolean?ExecuteCalculations( DynLanContext DynLanContext, DynLanState currentState, out Object Result) { Result = null; DynLanCodeLines lines = currentState.GetCurrentLines(); DynLanCodeLine currentLine = currentState.GetCurrentLine(); // wykonanie kalkulacji if (currentLine.ContainsAnyExpressions() && currentLine.OperatorType != EOperatorType.ELSE && currentLine.OperatorType != EOperatorType.PASS && currentLine.OperatorType != EOperatorType.CATCH) { if (DynLanContext.CurrentState.ExpressionContext == null) { DynLanContext.CurrentState.ExpressionContext = new ExpressionContext(currentLine.ExpressionGroup); } if (DynLanContext.CurrentState.ExpressionContext != null && DynLanContext.CurrentState.ExpressionContext.IsFinished) { Result = DynLanContext.CurrentState.ExpressionContext.Result; DynLanContext.CurrentState.ExpressionContext = null; } else { try { Boolean result = ExpressionEvaluator.NextStep( DynLanContext); return(result); } catch { throw; } } } return(null); }
//////////////////////////////////////////////////////////////////////// public static ExpressionMethodResult Execute(DynLanContext EvaluateContext, IList <Object> Parameters) { String variableName = UniConvert.ToUniString(Parameters != null && Parameters.Count > 0 ? Parameters[0] : null); Object value = Parameters != null && Parameters.Count > 1 ? Parameters[1] : null; if (EvaluateContext != null) { Boolean isValueSet = EvaluateContext.SetValue( EvaluateContext, variableName, value); if (isValueSet) { return(new ExpressionMethodResult(value)); } } return(null); }
private static String GetStacktrace(DynLanContext DynLanContext) { String DynLanStacktrace = ""; if (DynLanContext != null && DynLanContext.Stack != null) { for (Int32 i = DynLanContext.Stack.Count - 1; i >= 0; i--) { DynLanState state = DynLanContext.Stack[i]; if (state == null) { continue; } String name = state.Program is DynLanMethod ? ((DynLanMethod)state.Program).Name : ""; //if (DynLanStacktrace.Length > 0) // DynLanStacktrace += Environment.NewLine; // " | "; String code = ""; if (state.Program.Lines != null && state.CurrentLineIndex >= 0 && state.CurrentLineIndex < state.Program.Lines.Count) { code = (state.Program.Lines[state.CurrentLineIndex].Code ?? "").Trim(); } DynLanStacktrace += "at " + (name == "" ? (state.Program.ContextType + "; ") : (name + " {" + state.Program.ContextType + "}; ")) + "index: " + (state.CurrentLineIndex) + "; " + "code: " + code + "\r\n"; /*DynLanStacktrace += * "at " + * (name == "" ? (state.Program.ContextType + "; ") : (name + " {" + state.Program.ContextType + "}; ")) + * "line index: " + (state.CurrentLineIndex + 1) + "; " + * "code: {" + state.Program.Lines[state.CurrentLineIndex].Code + "}";*/ } } return(DynLanStacktrace); }
////////////////////////////////////////////// private static Boolean GotoNextLine( DynLanContext DynLanContext, DynLanState currentState, Object currentValue) { try { DynLanCodeLines lines = currentState.GetCurrentLines(); DynLanCodeLine currentLine = currentState.GetCurrentLine(); // jesli return to konczymy if (currentLine.OperatorType == EOperatorType.RETURN) { return(ExitCurrentContext( DynLanContext, currentValue, null)); } // throw błędu else if (currentLine.OperatorType == EOperatorType.THROW) { if (currentValue is Exception) { throw (Exception)currentValue; } else { String message = UniConvert.ToString(currentValue ?? ""); throw String.IsNullOrEmpty(message) ? new Exception() : new Exception(message); } /*return ExitCurrentContext( * DynLanContext, * new Exception(message));*/ } // jesli return to konczymy else if (currentLine.OperatorType == EOperatorType.BREAK) { return(ExitCurrentLoop( DynLanContext, currentValue, null)); } if (currentLine.OperatorType == EOperatorType.WHILE || currentLine.OperatorType == EOperatorType.IF || currentLine.OperatorType == EOperatorType.ELIF) { Boolean conditionResult = BooleanHelper.IfTrue(currentValue); if (conditionResult) { DynLanCodeLine nextLine = DynLanCodeLinesExtender. NextOnSameOrHigher(lines, currentLine); if (nextLine != null) { currentState.CurrentLineID = nextLine.ID; } else { throw new NotImplementedException(); } } else { DynLanCodeLine nextLine = DynLanCodeLinesExtender. NextOnSameOrLower(lines, currentLine); if (nextLine == null) { return(ExitCurrentContext( DynLanContext, null)); } else { if (nextLine.Depth < currentLine.Depth) { while ( nextLine != null & (nextLine.OperatorType == EOperatorType.ELSE || nextLine.OperatorType == EOperatorType.ELIF /*|| * nextLine.OperatorType == EOperatorType.FINALLY*/)) { nextLine = DynLanCodeLinesExtender.ExitParentIf(lines, nextLine); if (nextLine == null) { break; } } if (nextLine == null) { return(ExitCurrentContext( DynLanContext, null)); } if (nextLine.Depth < currentLine.Depth) { //DynLanCodeLine prevIf = lines. // PrevLineWithLessDepth(currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF); while (true) { DynLanCodeLine prevConditionLine = DynLanCodeLinesExtender.PrevLineWithLessDepth( lines, currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF || l.OperatorType == EOperatorType.ELSE || l.OperatorType == EOperatorType.WHILE); if (prevConditionLine != null && prevConditionLine.Depth >= nextLine.Depth && prevConditionLine.OperatorType == EOperatorType.WHILE) { currentState.CurrentLineID = prevConditionLine.ID; break; } else if (prevConditionLine != null) { currentLine = prevConditionLine; } else { currentState.CurrentLineID = nextLine.ID; break; } } } else { currentState.CurrentLineID = nextLine.ID; } } else { currentState.CurrentLineID = nextLine.ID; } } } } else if ( currentLine.OperatorType == EOperatorType.TRY || currentLine.OperatorType == EOperatorType.ELSE) { DynLanCodeLine nextLine = DynLanCodeLinesExtender. NextOnSameOrHigher(lines, currentLine); if (nextLine != null) { currentState.CurrentLineID = nextLine.ID; } else { throw new NotImplementedException(); } } else if ( (currentLine.OperatorType == EOperatorType.FINALLY)) { throw new NotImplementedException("FINALLY"); } else if ( (currentLine.OperatorType == EOperatorType.CATCH)) { if (DynLanContext.Error != null) { DynLanContext.Error = null; DynLanCodeLine nextLine = DynLanCodeLinesExtender. NextOnSameOrHigher(lines, currentLine); if (nextLine != null) { currentState.CurrentLineID = nextLine.ID; } else { throw new NotImplementedException(); } } else { DynLanCodeLine nextLine = DynLanCodeLinesExtender.NextOnSameOrLower( lines, currentLine); if (nextLine != null) { currentState.CurrentLineID = nextLine.ID; } else { return(ExitCurrentContext( DynLanContext, null)); } } } else if (currentLine.OperatorType == EOperatorType.NONE || currentLine.OperatorType == EOperatorType.PASS) { DynLanCodeLine nextLine = DynLanCodeLinesExtender.NextLine(lines, currentLine); if (nextLine != null) { while ( nextLine != null & (nextLine.OperatorType == EOperatorType.ELSE || nextLine.OperatorType == EOperatorType.ELIF /*|| * nextLine.OperatorType == EOperatorType.FINALLY*/)) { nextLine = DynLanCodeLinesExtender.ExitParentIf(lines, nextLine); if (nextLine == null) { return(ExitCurrentContext( DynLanContext, null)); } } if (nextLine == null) { return(ExitCurrentContext( DynLanContext, null)); } if (nextLine.Depth < currentLine.Depth) { //DynLanCodeLine prevIf = lines. // PrevLineWithLessDepth(currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF); while (true) { DynLanCodeLine prevConditionLine = DynLanCodeLinesExtender. PrevLineWithLessDepth( lines, currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF || l.OperatorType == EOperatorType.ELSE || l.OperatorType == EOperatorType.WHILE); if (prevConditionLine != null && prevConditionLine.Depth >= nextLine.Depth && prevConditionLine.OperatorType == EOperatorType.WHILE) { currentState.CurrentLineID = prevConditionLine.ID; break; } else if (prevConditionLine != null) { currentLine = prevConditionLine; } else { currentState.CurrentLineID = nextLine.ID; break; } } } else { currentState.CurrentLineID = nextLine.ID; } } // jeśli ostatnia linia i jesteśmy w while'u else { //DynLanCodeLine prevIf = lines. // PrevLineWithLessDepth(currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF); while (true) { DynLanCodeLine prevConditionLine = DynLanCodeLinesExtender. PrevLineWithLessDepth( lines, currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF || l.OperatorType == EOperatorType.ELSE || l.OperatorType == EOperatorType.WHILE); if (prevConditionLine != null && prevConditionLine.OperatorType == EOperatorType.WHILE) { currentState.CurrentLineID = prevConditionLine.ID; break; } else if (prevConditionLine != null) { currentLine = prevConditionLine; } else { return(ExitCurrentContext( DynLanContext, null)); } } } } return(false); } catch (Exception ex) { throw; } }
private static ExpressionMethodResult EvaluateInlineMethod( Object Object, Object Method, IList <Object> MethodParameters, DynLanContext DynLanContext) { if (Method is OnpMethodInfo) { OnpMethodInfo methodInfo = Method as OnpMethodInfo; DynamicCallResult callResult = MyReflectionHelper.CallMethod( methodInfo.Obj, methodInfo.MethodName, CorrectParameters(MethodParameters)); if (callResult != null) { return(new ExpressionMethodResult(callResult.Value)); } } else if (Method is OnpActionMethodInfo) { OnpActionMethodInfo methodInfo = Method as OnpActionMethodInfo; if (methodInfo != null && methodInfo.Action != null) { return(new ExpressionMethodResult(methodInfo.Action(CorrectParameters(MethodParameters)))); } } else if (Method is ExpressionMethod) { ExpressionMethod onpMethod = Method as ExpressionMethod; ExpressionMethodResult result = null; if (Object is EmptyObject) { result = onpMethod. CalculateValueDelegate( DynLanContext, CorrectParameters(MethodParameters)); } else { #if !NET20 var tmp = new[] { Object }.Union(MethodParameters); #else var tmp = Linq2.From(new[] { Object }).Union(MethodParameters).ToArray(); #endif result = onpMethod. CalculateValueDelegate( DynLanContext, CorrectParameters(tmp)); } return(result == null ? new ExpressionMethodResult(null) : result); } else if (Method is ExpressionMethodInfo) { ExpressionMethodInfo onpMethodInfo = Method as ExpressionMethodInfo; ExpressionMethod onpMethod = BuildinMethods.GetByID(onpMethodInfo.ID); ExpressionMethodResult result = null; if (onpMethod == null) { return(new ExpressionMethodResult(result)); } if (Object is EmptyObject) { result = onpMethod. CalculateValueDelegate( DynLanContext, CorrectParameters(MethodParameters)); } else { #if !NET20 var tmp = new[] { Object }.Union(MethodParameters); #else var tmp = Linq2.From(new[] { Object }).Union(MethodParameters).ToArray(); #endif result = onpMethod. CalculateValueDelegate( DynLanContext, CorrectParameters(tmp)); } if (result != null) { result.Value = InternalTypeConverter.ToInner(result.Value); } return(result == null ? new ExpressionMethodResult(null) : result); } else if (Method is ExpressionExtender) { ExpressionExtender onpExtender = Method as ExpressionExtender; return(new ExpressionMethodResult( onpExtender. CalculateValueDelegate( DynLanContext, Object, MethodParameters))); } else if (Method is ExpressionExtenderInfo) { ExpressionExtenderInfo onpExtenderInfo = Method as ExpressionExtenderInfo; ExpressionExtender onpExtender = BuildinExtenders.GetByID(onpExtenderInfo.ID); if (onpExtender == null) { return(new ExpressionMethodResult(null)); } return(new ExpressionMethodResult( onpExtender. CalculateValueDelegate( DynLanContext, Object, MethodParameters))); } else if (Method is Delegate) { #if NETCE throw new NotSupportedException("Calling delegates is forbidden on wince2.0!"); #else Delegate m = Method as Delegate; DynamicCallResult callResult = MyReflectionHelper.CallMethod( m.Target, m.Method, CorrectParameters(MethodParameters)); if (callResult != null) { return(new ExpressionMethodResult(callResult.Value)); } #endif } if (Method == null) { if (Object == null) { throw new DynLanMethodNotFoundException("Cannot find a method to call"); } else { throw new DynLanMethodNotFoundException("Cannot find a method to call in object " + Object.GetType().Name + ""); } } throw new DynLanUnsupportedMethodTypeException("Unsupported method type " + Method.GetType() + "!"); }
public static Boolean EvaluateMethod( Object Object, Object MethodObject, IList <Object> Parameters, DynLanContext DynLanContext) { if (MethodObject is DynLanMethod) { if (Parameters == null) { Parameters = new Object[0]; } DynLanMethod method = (DynLanMethod)MethodObject; DynLanContextType contextType = DynLanContextType.METHOD; // jesli tworzenie klasy (wolanie konstruktora) if (MethodObject is DynLanClass) { contextType = DynLanContextType.CLASS; } DynLanState newContext = DynLanContext. PushContext(method, contextType, Parameters); newContext.Object.ParentObject = method.ParentObject; return(true); } else if (MethodObject is DynLanProgram) { DynLanProgram program = (DynLanProgram)MethodObject; IDictionary <String, Object> currentValues = (DynLanContext == null || DynLanContext.CurrentState == null || DynLanContext.CurrentState.Object == null ? null : DynLanContext. CurrentState. Object. DynamicValues); DynLanState newState = DynLanContext.PushContext( program, DynLanContextType.METHOD, null); if (currentValues != null) { foreach (String key in currentValues.Keys) { newState.Object.DynamicValues[key] = currentValues[key]; } } return(true); } else { ExpressionMethodResult methodResult = EvaluateInlineMethod( Object, MethodObject, Parameters, DynLanContext); if (methodResult != null && methodResult.NewContextCreated) { return(true); } else { var v = methodResult == null ? null : methodResult.Value; DynLanContext.CurrentExpressionState.PushValue(v); return(false); } } }
//////////////////////////////////////////////////////////////////////// public static Object Execute(DynLanContext EvaluateContext, Object obj, IList <Object> Parameters) { Object Collection = obj; Object value = Parameters != null && Parameters.Count > 0 ? Parameters[0] : null; Object Key = Parameters != null && Parameters.Count > 1 ? Parameters[1] : null; if (Collection == null) { return(null); } if (Collection is DynLanObject) { DynLanObject DynLanObj = Collection as DynLanObject; String finalKey = (String)(Key.GetType() == typeof(String) ? Key : Convert.ChangeType(Key, typeof(String), System.Globalization.CultureInfo.InvariantCulture)); Object finValue = value == null ? null : (value.GetType() == typeof(Object) ? value : Convert.ChangeType(value, typeof(Object), System.Globalization.CultureInfo.InvariantCulture)); DynLanObj[finalKey] = finValue; return(value); } if (Collection is IDictionaryWithGetter) { IDictionaryWithGetter dict = (IDictionaryWithGetter)Collection; Type[] arguments = dict.GetType().GetGenericArguments(); Type keyType = arguments[0]; Type valueType = arguments[1]; Object finalKey = Key.GetType() == keyType ? Key : Convert.ChangeType(Key, keyType, System.Globalization.CultureInfo.InvariantCulture); Object finValue = value == null ? null : (value.GetType() == valueType ? value : Convert.ChangeType(value, valueType, System.Globalization.CultureInfo.InvariantCulture)); if (dict.CanSetValueToDictionary(finalKey)) { lock (dict) { dict.Remove(finalKey); dict.Add(finalKey, finValue); } } return(value); } else if (Collection is IDictionary) { IDictionary dict = (IDictionary)Collection; Type[] arguments = dict.GetType().GetGenericArguments(); Type keyType = arguments[0]; Type valueType = arguments[1]; Object finalKey = Key.GetType() == keyType ? Key : Convert.ChangeType(Key, keyType, System.Globalization.CultureInfo.InvariantCulture); Object finValue = value == null ? null : (value.GetType() == valueType ? value : Convert.ChangeType(value, valueType, System.Globalization.CultureInfo.InvariantCulture)); lock (dict) { dict.Remove(finalKey); dict.Add(finalKey, finValue); } return(value); } else if (Collection is IDictionary <string, object> ) { IDictionary <string, object> dict = (IDictionary <string, object>)Collection; lock (dict) { string finalKey = UniConvert.ToString(Key); dict.Remove(finalKey); dict.Add(finalKey, value); } return(value); } if (Collection is IList) { Int32?index = UniConvert.ToInt32N(Key); if (index == null || index < 0) { return(null); } IList list = (IList)Collection; if (index >= list.Count) { return(null); } Type listType = MyTypeHelper.GetListType(list); Object finValue = value == null ? null : (value.GetType() == listType ? value : Convert.ChangeType(value, listType, System.Globalization.CultureInfo.InvariantCulture)); list[index.Value] = finValue; return(value); } return(null); }
public static Boolean EvaluateOnp( DynLanContext DynLanContext) { Boolean result = false; ExpressionState expState = DynLanContext.CurrentExpressionState; ExpressionContext expContext = DynLanContext.CurrentExpressionContext; // czy zakończyć i zapisać wynik if (expState.TokenIndex >= expState.Expression.OnpTokens.Count) { Object finResult = null; if (expState.ValueStack.Count > 0) { finResult = MyCollectionsExtenders.Pop(expState.ValueStack); } expState.ValueStack.Clear(); expState.Finished = true; expState.Result = InternalTypeConverter.ToOuter(finResult); MyCollectionsExtenders.Pop(expContext.Stack); if (expContext.Current != null) { expContext.Current.PushValue(InternalTypeConverter.ToOuter(finResult)); return(false); } else { expContext.Result = InternalTypeConverter.ToOuter(finResult); expContext.IsFinished = true; return(true); } } ExpressionToken token = expState.Expression.OnpTokens[expState.TokenIndex]; // wykonanie następnej operacji if (token.TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(token.TokenChars); Object value = operationValue == null ? null : InternalTypeConverter.ToInner(operationValue.Value); expState.PushValue(value); } else if (token.TokenType == TokenType.PROPERTY_NAME) { expState.PushValue(token.TokenName); } if (token.TokenType == TokenType.VARIABLE) { result = ObjectValueGetter.EvaluateValue( token.TokenName, DynLanContext); } else if (token.TokenType == TokenType.OPERATOR) { Object valueA = InternalTypeConverter.ToInner( MyCollectionsExtenders.Pop(expState.ValueStack)); Object valueB = InternalTypeConverter.ToInner( MyCollectionsExtenders.Pop(expState.ValueStack)); Object value = null; OperatorType operatorType = OperatorTypeHelper. GetOperationType(token.TokenChars); try { value = OperationHelper.Do( operatorType, valueB, valueA); expState.PushValue(value); } catch { throw; } } expState.TokenIndex++; return(result); }
//////////////////////////////////////////////////////////////////////// public static Object Execute(DynLanContext EvaluateContext, Object obj, IList <Object> Parameters) { Object Collection = obj; #if !NET20 Object Key = Parameters == null ? null : Parameters.FirstOrDefault(); #else Object Key = Parameters == null ? null : Linq2.FirstOrDefault(Parameters); #endif if (Collection == null) { return(null); } if (Collection is String) { Int32?index = UniConvert.ToInt32N(Key); if (index == null || index < 0) { return(null); } String str = (String)Collection; if (index >= str.Length) { return(null); } return(str[index.Value]); } if (Collection is DynLanObject) { DynLanObject DynLanObj = Collection as DynLanObject; if (DynLanObj.TotalCount == 0) { return(null); } /*IDictionary<String, Object> dict = ((DynLanObject)Collection).Values; * if (dict.Count == 0) * return null;*/ String finalKey = ((String)(Key.GetType() == typeof(String) ? Key : Convert.ChangeType(Key, typeof(String), System.Globalization.CultureInfo.InvariantCulture))); return(DynLanObj[finalKey]); } if (Collection is IDictionaryWithGetter) { IDictionaryWithGetter dict = Collection as IDictionaryWithGetter; if (dict.CanGetValueFromDictionary(Key)) { return(dict.GetValueFromDictionary(Key)); } } else if (Collection is IDictionary) { IDictionary dict = (IDictionary)Collection; if (dict.Count == 0) { return(null); } Type[] arguments = dict.GetType().GetGenericArguments(); Type keyType = arguments[0]; Object finalKey = Key.GetType() == keyType ? Key : Convert.ChangeType(Key, keyType, System.Globalization.CultureInfo.InvariantCulture); return(dict[finalKey]); } else if (Collection is IDictionary <string, object> ) { IDictionary <string, object> dict = (IDictionary <string, object>)Collection; if (dict.Count == 0) { return(null); } return(dict[UniConvert.ToString(Key)]); } if (Collection is IList) { Int32?index = UniConvert.ToInt32N(Key); if (index == null || index < 0) { return(null); } IList list = (IList)Collection; if (index >= list.Count) { return(null); } return(list[index.Value]); } if (Collection is IEnumerable) { Int32?index = UniConvert.ToInt32N(Key); if (index == null || index < 0) { return(null); } Int32 i = -1; foreach (Object item in ((IEnumerable)Collection)) { i++; if (i == index.Value) { return(item); } } } return(null); }
public static Boolean ExecuteNext( DynLanContext DynLanContext) { try { Object currentValue = null; DynLanState currentState = DynLanContext. CurrentState; Boolean?checkResult = CheckIfFinishedOrEmptyLine( DynLanContext, currentState); if (checkResult != null) { return(checkResult.Value); } Boolean?executeResult = ExecuteCalculations( DynLanContext, currentState, out currentValue); if (executeResult != null) { return(executeResult.Value); } Boolean gotoResult = GotoNextLine( DynLanContext, currentState, currentValue); return(gotoResult); } catch (Exception ex) { Exception error = (ex is TargetInvocationException ? ex.InnerException : ex); DynLanContext.Error = error; Boolean result = false; DynLanState currentState = DynLanContext. CurrentState; Exception outError = PrepareDynLanException( DynLanContext, error); // próba obsługi błędu Boolean handled = DynLanContext.RaiseError( currentState, outError); if (outError is DynLanAbortException) { handled = false; } if (!handled) { result = GotoCatch( DynLanContext, outError); } else { DynLanContext.Error = null; result = true; } if (DynLanContext.IsFinished && DynLanContext.Error != null) { throw outError; } return(result); } }
public static Boolean EvaluateQueue( DynLanContext DynLanContext) { ExpressionState expState = DynLanContext.CurrentExpressionState; ExpressionContext expContext = DynLanContext.CurrentExpressionContext; // policzenie nastepnego parametru if (expState.AreParametersCalculating) { expState.ParameterIndex++; if (expState.ParameterIndex < expState.ParameterTokens.Count) { Boolean result = false; ExpressionToken parameterToken = expState.ParameterTokens[expState.ParameterIndex]; if (parameterToken.TokenType == TokenType.VARIABLE) { result = ObjectValueGetter.EvaluateValue( parameterToken.TokenName, DynLanContext); } else if (parameterToken.TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(parameterToken.TokenChars); Object value = ( operationValue == null ? null : operationValue.Value); expState.Parameters.Add(value); } else { throw new DynLanIncorrectExpressionFormatException(); } return(result); } } // czy zakończyć i zapisać wynik if (expState.TokenIndex >= expState.Expression.Tokens.Count) { Object finResult = null; if (expState.ValueStack.Count > 0) { finResult = MyCollectionsExtenders.Pop(expState.ValueStack); } expState.ValueStack.Clear(); expState.Finished = true; expState.Result = InternalTypeConverter.ToOuter(finResult); MyCollectionsExtenders.Pop(expContext.Stack); if (expContext.Current != null) { expContext.Current.PushValue(InternalTypeConverter.ToOuter(finResult)); return(false); } else { expContext.Result = InternalTypeConverter.ToOuter(finResult); expContext.IsFinished = true; return(true); } } Boolean isFirstToken = expState.TokenIndex == 0; ExpressionToken token = expState. Expression. Tokens[expState.TokenIndex]; ExpressionTokens sequence = new ExpressionTokens( new TokenizerQueue(). GetNextTokensOnSameLevel(expState.Expression.Tokens, expState.TokenIndex)); Int32 originalSequenceCount = sequence.Count; sequence.RemoveBrackets(); // wykonanie następnej operacji if (token.TokenType == TokenType.BRACKET_BEGIN) { IList <ExpressionToken> prevSequenceTokens = new TokenizerQueue(). GetPrevTokensOnSameLevel(expState.Expression.Tokens, expState.TokenIndex - 1); ExpressionTokens prev_sequence = prevSequenceTokens == null ? null : new ExpressionTokens(prevSequenceTokens); // jeśli poprzedni operator to @ if ( prev_sequence != null && prev_sequence.Count == 1 && OnpOnpTokenHelper.IsFunctionOperatorToken(prev_sequence[0])) { Boolean result = false; if (sequence.Count == 0 || expState.AreParametersCalculated) { Object obj = expState.ValueStack.Count == 1 ? new EmptyObject() : MyCollectionsExtenders.Peek(expState.ValueStack, 1); Object methodObject = MyCollectionsExtenders. Peek(expState.ValueStack, 0); result = EvaluatorForMethods.EvaluateMethod( obj, methodObject, expState.Parameters, DynLanContext); expState.CleanParametersState(); expState.TokenIndex += originalSequenceCount; } else { expState.ParameterTokens = GetParameterTokens(sequence); expState.ParameterIndex = -1; expState.Parameters = new List <Object>(); result = false; } return(result); } // jeśli poprzedni operator to . else if ( prev_sequence != null && prev_sequence.Count == 1 && OnpOnpTokenHelper.IsPropertyOperatorToken(prev_sequence[0])) { throw new DynLanIncorrectExpressionFormatException(); } // jeśli brak poprzedniego operatora else if ( prev_sequence == null && sequence.Count == 1) { Boolean result = false; if (sequence[0].TokenType == TokenType.VARIABLE) { result = ObjectValueGetter.EvaluateValue( sequence[0].TokenName, DynLanContext); } else if (sequence[0].TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(sequence[0].TokenChars); Object value = ( operationValue == null ? null : operationValue.Value); expState.PushValue(value); } else { throw new DynLanIncorrectExpressionFormatException(); } expState.TokenIndex += originalSequenceCount; // -1; return(result); } else { throw new DynLanInvalidExpressionException("Incorrect expression " + expState.Expression.Tokens.JoinToString(expState.TokenIndex) + "!"); } } else if (token.TokenType == TokenType.VALUE) { ExpressionValue operationValue = StringHelper. GetValueFromText(token.TokenChars); Object value = ( operationValue == null ? null : operationValue.Value); if (isFirstToken) { expState.PushValue(value); } else { Object prevValue = MyCollectionsExtenders.Peek(expState.ValueStack); prevValue = InternalTypeConverter. ToOuter(prevValue); Object method = ObjectValueGetter.GetValueFromObject( prevValue, UniConvert.ToString(value)); expState.PushValue(method); } expState.TokenIndex += originalSequenceCount; // -1 } else if (token.TokenType == TokenType.PROPERTY_NAME) { if (isFirstToken) { throw new DynLanIncorrectExpressionFormatException(); } else { Object prevValue = MyCollectionsExtenders.Peek(expState.ValueStack); prevValue = InternalTypeConverter. ToOuter(prevValue); Object value = ObjectValueGetter.GetValueFromObject( prevValue, token.TokenName); expState.PushValue(value); } expState.TokenIndex += originalSequenceCount; // -1 } else if (token.TokenType == TokenType.VARIABLE) { Boolean result = false; ExpressionToken next_token = expState.TokenIndex + 1 < expState.Expression.Tokens.Count ? expState.Expression.Tokens[expState.TokenIndex + 1] : null; Object prevValue = null; if (isFirstToken) { prevValue = new EmptyObject(); /*result = ObjectValueGetter.EvaluateValueOrMethod( * new EMPTY_OBJECT(), * sequence[0].TokenName, * -1, * DynLanContext);*/ } else { prevValue = ( expState.ValueStack.Count == 0 ? null : MyCollectionsExtenders.Peek(expState.ValueStack)); } prevValue = InternalTypeConverter. ToOuter(prevValue); Int32 paramCount = -1; // jeśli następny operator to operator wołania funkcji @ to pobieramy z niego liczbę parametrów dla funkcji if (OnpOnpTokenHelper.IsFunctionOperatorToken(next_token) && next_token.TokenData != null && next_token.TokenData.FunctionParametersCount != null) { paramCount = next_token.TokenData.FunctionParametersCount.Value; } else { paramCount = -1; } result = ObjectValueGetter.EvaluateValueOrMethod( prevValue, sequence[0].TokenName, paramCount, DynLanContext); if (MyCollectionsExtenders.Peek(DynLanContext.CurrentExpressionState.ValueStack) == null) { ExpressionToken next_next_token = expState.TokenIndex + 2 < expState.Expression.Tokens.Count ? expState.Expression.Tokens[expState.TokenIndex + 2] : null; if (next_token.TokenType == TokenType.OPERATOR && OnpOnpTokenHelper.IsPropertyOperatorToken(next_token) && next_next_token != null) { } else { next_next_token = null; } if (paramCount < 0) { if (next_next_token != null) { ExpressionToken next_next_next_next_next_token = expState.TokenIndex + 5 < expState.Expression.Tokens.Count ? expState.Expression.Tokens[expState.TokenIndex + 5] : null; if (next_next_token.TokenName == "__EXT_SET" && next_next_next_next_next_token != null) { throw new DynLanMethodNotFoundException("Cannot find property " + next_next_next_next_next_token.TokenName + " in undefined object '" + sequence[0].TokenName + "'"); } else { throw new DynLanMethodNotFoundException("Cannot call method '" + next_next_token.TokenName + "' in undefined object '" + sequence[0].TokenName + "'"); } } else { throw new DynLanMethodNotFoundException("Undefined object '" + sequence[0].TokenName + "'"); } } else { throw new DynLanMethodNotFoundException("Undefined method '" + sequence[0].TokenName + "' " + (prevValue == null ? "in undefined object" : ("in object of type " + prevValue.GetType().Name))); } } expState.TokenIndex += originalSequenceCount; // -1 return(result); } else if (token.TokenType == TokenType.OPERATOR) { if (!OnpOnpTokenHelper.IsFunctionOperatorToken(token) && !OnpOnpTokenHelper.IsPropertyOperatorToken(token)) { throw new DynLanIncorrectExpressionFormatException(); } expState.TokenIndex += originalSequenceCount; // -1 } if (MyCollectionsExtenders.Peek(expState.ValueStack) == null) { return(false); } return(false); }
private static Boolean GotoCatch( DynLanContext DynLanContext, Exception exception) { while (true) { DynLanState currentState = DynLanContext. CurrentState; // reset dla kontekstu obliczeń, ponieważ przechodzimy do catch'a currentState.ExpressionContext = null; DynLanCodeLines lines = currentState.GetCurrentLines(); DynLanCodeLine currentLine = currentState.GetCurrentLine(); // poszukanie poprzedniego catch'a DynLanCodeLine prevCatch = DynLanCodeLinesExtender. PrevLineWithLessDepth(lines, currentLine, l => l.OperatorType == EOperatorType.CATCH); // poszukanie poprzedniego try'a DynLanCodeLine prevTry = DynLanCodeLinesExtender. PrevLineWithLessDepth(lines, currentLine, l => l.OperatorType == EOperatorType.TRY); if (exception is DynLanAbortException) { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } else if (prevTry == null) { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } // jeśli znalazł try'a i nie jesteśmy w catch'u else if (prevTry.Depth < currentLine.Depth && (prevCatch == null || lines.IndexOf(prevCatch) < lines.IndexOf(prevTry))) { DynLanCodeLine nextCatch = DynLanCodeLinesExtender.NextOnSameOrLower( lines, prevTry, i => i.OperatorType == EOperatorType.CATCH); if (nextCatch != null) { ExpressionToken variableForException = null; if (nextCatch.ExpressionGroup != null && nextCatch.ExpressionGroup.MainExpression != null && nextCatch.ExpressionGroup.MainExpression.Tokens != null && nextCatch.ExpressionGroup.MainExpression.Tokens.Count > 0) { #if !NET20 variableForException = nextCatch. ExpressionGroup.MainExpression.Tokens. FirstOrDefault(i => i.TokenType != TokenType.BRACKET_BEGIN); #else variableForException = Linq2.FirstOrDefault(nextCatch. ExpressionGroup.MainExpression.Tokens, i => i.TokenType != TokenType.BRACKET_BEGIN); #endif } currentState.CurrentLineID = nextCatch.ID; if (variableForException != null && !String.IsNullOrEmpty(variableForException.TokenName)) { currentState.Object[variableForException.TokenName] = exception; } break; } else { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } } else { ExitCurrentContext( DynLanContext, exception); if (DynLanContext.IsFinished) { break; } } } return(false); }
public static Boolean EvaluateValueOrMethod( Object Obj, String FieldOrMethodName, Int32 ParametersCount, DynLanContext DynLanContext) { Boolean seekInObject = (Obj != null && !(Obj is EmptyObject)); Boolean wasValueFoundFromContext = false; Boolean wasValueFoundInObject = false; Boolean wasValueFoundInExpressions = false; ExpressionValue expressionValue = DynLanContext.GetValue( DynLanContext, FieldOrMethodName, seekInObject, !seekInObject, !seekInObject); if (expressionValue != null) { wasValueFoundFromContext = true; } if (seekInObject) { Boolean foundValue = false; Object value = GetValueFromObject( Obj, FieldOrMethodName, ParametersCount, expressionValue != null, out foundValue); if (foundValue) { wasValueFoundInObject = true; if (value is MethodInfo) { OnpMethodInfo methodInfo = new OnpMethodInfo(); methodInfo.Obj = Obj; methodInfo.MethodName = FieldOrMethodName; DynLanContext.CurrentExpressionState.PushValue(methodInfo); return(false); } else { DynLanContext.CurrentExpressionState.PushValue(value); return(false); } } } Expression expression = DynLanContext. CurrentExpressionGroup. FindExpression(FieldOrMethodName, DynLanContext); if (expression == null) { Object value = expressionValue == null ? null : expressionValue.Value; value = InternalTypeConverter.ToInner( value); #if !IGNORE_NOT_DECLARED_VARIABLES if (!wasValueFoundFromContext && !wasValueFoundInObject) { if (seekInObject) { throw new DynLanVariableNotFoundException("Variable " + FieldOrMethodName + " not found in object " + Obj + "!") { Variable = FieldOrMethodName } } ; throw new DynLanVariableNotFoundException("Variable " + FieldOrMethodName + " not found!") { Variable = FieldOrMethodName }; } #endif DynLanContext.CurrentExpressionState.PushValue(value); return(false); } else { wasValueFoundInExpressions = true; ExpressionState newExpressionState = new ExpressionState(); newExpressionState.Expression = expression; DynLanContext.CurrentExpressionContext.Stack.Add(newExpressionState); return(false); } } }