/// <summary> /// Assigns the specified value to a property. /// </summary> /// <param name="propertyReference">The reference to the property to set.</param> /// <param name="value">The value to assign.</param> private void AssignProperty(PropertyReferenceExpression propertyReference, object value) { if (string.IsNullOrWhiteSpace(propertyReference.PropertyName?.Identifier)) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.UndefinedName), propertyReference); } var targetObjectValue = ParentInterpreter.RunExpression(propertyReference.TargetObject); if (ParentInterpreter.IsAborted) { return; } if (targetObjectValue == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.NullValue), propertyReference); return; } if (propertyReference.TargetObject is ClassReferenceExpression && targetObjectValue is Type) { BaZicInterpreter.Reflection.SetStaticProperty((Type)targetObjectValue, propertyReference.PropertyName.Identifier, value); } else if (targetObjectValue is FrameworkElement) { BaZicInterpreter.ProgramInterpreter.UIDispatcher.Invoke(() => { BaZicInterpreter.Reflection.SetProperty(targetObjectValue, propertyReference.PropertyName.Identifier, value); }, System.Windows.Threading.DispatcherPriority.Background); } else { BaZicInterpreter.Reflection.SetProperty(targetObjectValue, propertyReference.PropertyName.Identifier, value); } }
/// <inheritdoc/> internal override void Run() { var methodInterpreter = ParentInterpreter.GetParentMethodInterpreter(); if (ParentInterpreter.IsAborted) { return; } var returnValue = ParentInterpreter.RunExpression(Statement.Expression); if (ParentInterpreter.IsAborted) { return; } methodInterpreter.ReturnedValue = returnValue; ParentInterpreter.State.ExitMethod = true; if (BaZicInterpreter.Verbose && !ParentInterpreter.IsAborted) { var valueString = methodInterpreter.ReturnedValue == null ? L.BaZic.Runtime.Debugger.ValueInfo.Null : $"{returnValue} ({ValueInfo.GetValueInfo(returnValue)})"; ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.ReturnInterpreter.FormattedReturn(valueString)); } }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { object type; var fullName = Expression.ToString(); if (DebugMode) { ParentInterpreter.Log(this, $"Reference to the class : {fullName}"); } if (Expression._type != null) { return(Expression._type); } if (string.IsNullOrWhiteSpace(Expression._namespace)) { type = GetProjectClassReference(fullName); } else { type = GetCoreClassReference(fullName); } if (type != null) { return(type); } ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException(fullName, $"Unable to find the class '{fullName}' because it does not exist or it is not accessible."), Expression), ParentInterpreter.GetDebugInfo())); return(null); }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { if (DebugMode) { ParentInterpreter.Log(this, "Primitive value : {0}", Expression._value == null ? "{null}" : $"'{Expression._value}' (type:{Expression._value.GetType().FullName})"); } return(Expression._value); }
/// <summary> /// Run the interpretation /// </summary> internal override void Execute() { if (DebugMode) { ParentInterpreter.Log(this, "Breakpoint intercepted"); ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(AlgorithmInterpreterState.PauseBreakpoint, ParentInterpreter.GetDebugInfo(false))); } }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { object left; object right; MethodInfo operatorMethod; left = ParentInterpreter.RunExpression(Expression._leftExpression); if (ParentInterpreter.FailedOrStop) { return(null); } right = ParentInterpreter.RunExpression(Expression._rightExpression); if (ParentInterpreter.FailedOrStop) { return(null); } if (DebugMode) { ParentInterpreter.Log(this, $"Doing an operation '{Expression._operator}'"); } if (Expression._operator == AlgorithmBinaryOperatorType.Equals) { if (left == null) { if (right == null) { return(true); // null == null } return(right.Equals(null)); } return(left.Equals(right)); } operatorMethod = OperatorHelperCache.GetOperator(Expression._operator, left.GetType(), right.GetType()); if (operatorMethod == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new OperatorNotFoundException(Expression._operator.ToString(), $"Operator '{Expression._operator}' cannot be applied to operands of type '{left.GetType().FullName}' and '{right.GetType().FullName}'"), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (Expression._operator == AlgorithmBinaryOperatorType.Division) { long num; var convertedToLong = long.TryParse(right.ToString(), out num); if (convertedToLong && num == 0) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new DivideByZeroException("Attempted to divide by zero."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } } return(operatorMethod.Invoke(null, new[] { left, right })); }
/// <summary> /// Invoke a code method /// </summary> /// <param name="type">The type which contains the method</param> /// <param name="obj">The instance</param> /// <returns>Returns the result of the invoke</returns> private object InvokeMethod(Type type, object obj) { if (Expression._argumentsTypes == null) { Expression._argumentsTypes = new Type[0]; } object result; Collection <object> arguments; var method = type.GetRuntimeMethod(Expression._methodName.ToString(), Expression._argumentsTypes); if (method == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException(Expression._methodName.ToString(), $"The method '{Expression._methodName}' does not exists in the current class or is not accessible."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (obj == null && !method.IsStatic) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NoInstanceReferenceException($"Unable to invoke the non-static core method '{method.Name}' without instanciate the class."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } arguments = Expressions.InvokeMethod.GetArgumentValues(Expression, ParentInterpreter); if (ParentInterpreter.FailedOrStop) { return(null); } result = null; _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { try { result = method.Invoke(obj, arguments.ToArray()); } catch (Exception ex) { if (ex is ArgumentException) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new BadArgumentException("{Unknow}", ex.Message), Expression), ParentInterpreter.GetDebugInfo())); } else if (ex is TargetParameterCountException) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException(Expression._methodName.ToString(), $"There is a method '{Expression._methodName}' in the class '{Expression._targetObject}', but it does not have {arguments.Count} argument(s)."), Expression), ParentInterpreter.GetDebugInfo())); } else { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(ex, Expression), ParentInterpreter.GetDebugInfo())); } } }).AsTask().Wait(); return(result); }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { if (Expression._targetObject == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to invoke a method when the TargetObject of an AlgorithmInvokeMethodExpression is null."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (DebugMode) { ParentInterpreter.Log(this, $"Calling method '{Expression._targetObject}.{Expression._methodName}'"); } var referenceClass = ParentInterpreter.RunExpression(Expression._targetObject) as ClassInterpreter; var callerMethod = ParentInterpreter.ParentMethodInterpreter; if (ParentInterpreter.FailedOrStop) { return(null); } if (referenceClass == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "It looks like the reference class does not exists."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (!referenceClass.IsInstance) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NoInstanceReferenceException("Unable to invoke a method of a not instancied class."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } var argumentValues = GetArgumentValues(Expression, ParentInterpreter); if (!ParentInterpreter.FailedOrStop) { var callStackService = ParentInterpreter.ParentProgramInterpreter.DebugInfo.CallStackService; _result = null; if (callStackService.CallCount > Consts.InvokeMethodCountBeforeNewThread) { // Make a new thread avoid the stack overflow. callStackService.CallCount = 0; CallMethodNewThread(referenceClass, argumentValues, callerMethod, callStackService).Wait(); } else { callStackService.CallCount++; _result = referenceClass.InvokeMethod(ParentInterpreter, Expression, argumentValues, callerMethod, callStackService); } return(_result); } return(null); }
/// <summary> /// Run the interpretation /// </summary> internal override void Execute() { var conditionResult = false; if (Statement._initializationStatement != null) { ParentInterpreter.RunStatement(Statement._initializationStatement); if (ParentInterpreter.FailedOrStop) { return; } } _IterationLoop: if (!Statement._conditionAfterBody) { conditionResult = RunCondition(); if (!conditionResult) { return; } } var block = new BlockInterpreter(Statement._statements, DebugMode, ParentInterpreter.ParentProgramInterpreter, ParentInterpreter.ParentMethodInterpreter, ParentInterpreter.ParentBlockInterpreter, ParentInterpreter.ParentClassInterpreter); block.OnGetParentInterpreter += new Func<BlockInterpreter>(() => ParentInterpreter); block.StateChanged += ParentInterpreter.ChangeState; block.Initialize(); ReturnOccured = block.Run(); block.StateChanged -= ParentInterpreter.ChangeState; block.Dispose(); if (ReturnOccured || ParentInterpreter.FailedOrStop) { return; } if (Statement._incrementStatement != null) { ParentInterpreter.RunStatement(Statement._incrementStatement); } if (ParentInterpreter.FailedOrStop) { return; } if (Statement._conditionAfterBody) { conditionResult = RunCondition(); } if (conditionResult) { goto _IterationLoop; } }
/// <summary> /// Returns the corresponding assignable object /// </summary> /// <returns>the assignable object</returns> public object GetAssignableObject() { if (Expression._targetObject == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to access to a property or variable when the TargetObject of an AlgorithmArrayIndexerExpression is null."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (DebugMode) { ParentInterpreter.Log(this, $"Getting the value '{Expression}'"); } var indexableValue = ParentInterpreter.RunExpression(Expression._targetObject); if (ParentInterpreter.FailedOrStop) { return(null); } if (indexableValue == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NoInstanceReferenceException("Unable to get a value because the array is null."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } // ReSharper disable once UseIsOperator.1 // ReSharper disable once UseMethodIsInstanceOfType if (!typeof(IList).IsAssignableFrom(indexableValue.GetType())) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new IndexerException(Expression._propertyName.ToString()), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (ParentInterpreter.FailedOrStop) { return(null); } var indiceValue = ParentInterpreter.RunExpression(Expression._indice); if (ParentInterpreter.FailedOrStop) { return(null); } var indexValue = indiceValue as int?; if (indexValue != null) { IndexValue = indexValue.Value; return(indexableValue); } ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new InvalidCastException("Unable to cast this value to a number."), Expression), ParentInterpreter.GetDebugInfo())); return(null); }
/// <inheritdoc/> internal override object Run() { var array = new ObservableDictionary(); foreach (var value in Expression.Values) { array.Add(ParentInterpreter.RunExpression(value)); } return(array); }
/// <inheritdoc/> internal override object Run() { var variable = ParentInterpreter.GetVariable(Expression.VariableDeclarationID, Expression.Name.Identifier, true); if (variable != null) { return(variable.Value); } return(null); }
/// <inheritdoc/> internal override void Run() { var expression = ParentInterpreter.RunExpression(Statement.Expression); var exception = expression as Exception; if (exception == null) { BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Statements.ThrowInterpreter.FormattedExceptionExpected(typeof(Exception).FullName))); return; } throw exception; }
/// <summary> /// Run the interpretation /// </summary> internal override void Execute() { object defaultValue = null; if (Statement._defaultValue != null) { defaultValue = ParentInterpreter.RunExpression(Statement._defaultValue); } if (ParentInterpreter.FailedOrStop) { return; } ParentInterpreter.AddVariable((IAlgorithmVariable)Statement, defaultValue); }
/// <summary> /// Returns the corresponding assignable object /// </summary> /// <returns>the assignable object</returns> public object GetAssignableObject() { var variable = ParentInterpreter.FindVariable(Expression._name.ToString()); if (variable == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new VariableNotFoundException(Expression._name.ToString()), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (DebugMode) { ParentInterpreter.Log(this, "Value of the variable '{0}' is {1}", variable.Name, variable.Value == null ? "{null}" : $"'{variable.Value}' (type:{variable.Value.GetType().FullName})"); } return(variable); }
/// <summary> /// Run the interpretation /// </summary> internal override void Execute() { var methodInterpreter = ParentInterpreter.ParentMethodInterpreter; if (methodInterpreter == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException("{Unknow}", "It looks like the caller/parent's method does not exists."), Statement), ParentInterpreter.GetDebugInfo())); return; } methodInterpreter.ReturnedValue = ParentInterpreter.RunExpression(Statement._expression); if (DebugMode && !ParentInterpreter.FailedOrStop) { ParentInterpreter.Log(this, "({0}) Return : {1}", methodInterpreter.MethodDeclaration._name, methodInterpreter.ReturnedValue == null ? "{null}" : $"'{methodInterpreter.ReturnedValue}' (type:{methodInterpreter.ReturnedValue.GetType().FullName})"); } }
/// <inheritdoc/> internal override object Run() { if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.FormattedGettingProperty(Expression)); } if (Expression.TargetObject == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.NullValue), Expression); return(null); } else if (string.IsNullOrWhiteSpace(Expression.PropertyName?.Identifier)) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.UndefinedName), Expression); return(null); } var targetObjectValue = ParentInterpreter.RunExpression(Expression.TargetObject); if (ParentInterpreter.IsAborted) { return(null); } if (targetObjectValue == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.NullValue), Expression); return(null); } if (Expression.TargetObject is ClassReferenceExpression && targetObjectValue is Type) { return(BaZicInterpreter.Reflection.GetStaticPropertyOrEnum((Type)targetObjectValue, Expression.PropertyName.Identifier)); } if (targetObjectValue is FrameworkElement) { return(BaZicInterpreter.ProgramInterpreter.UIDispatcher.Invoke(() => { return BaZicInterpreter.Reflection.GetProperty(targetObjectValue, Expression.PropertyName.Identifier); }, System.Windows.Threading.DispatcherPriority.Background)); } return(BaZicInterpreter.Reflection.GetProperty(targetObjectValue, Expression.PropertyName.Identifier)); }
/// <inheritdoc/> internal override void Run() { if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedAssign(Statement.LeftExpression, Statement.RightExpression)); } if (!typeof(IAssignable).IsAssignableFrom(Statement.LeftExpression.GetType())) { BaZicInterpreter.ChangeState(this, new NotAssignableException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.NotAssignable), Statement); return; } var rightValue = ParentInterpreter.RunExpression(Statement.RightExpression); if (ParentInterpreter.IsAborted) { return; } switch (Statement.LeftExpression) { case ArrayIndexerExpression arrayIndexer: AssignArrayValue(arrayIndexer, rightValue); break; case PropertyReferenceExpression propertyReference: AssignProperty(propertyReference, rightValue); break; case VariableReferenceExpression variableReference: ParentInterpreter.SetVariable(variableReference, rightValue); break; default: throw new InternalException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedNoInterpreter(Statement.LeftExpression.GetType().FullName)); } if (BaZicInterpreter.Verbose && !ParentInterpreter.IsAborted) { var rightValueString = rightValue == null ? L.BaZic.Runtime.Debugger.ValueInfo.Null : $"'{rightValue}'(type:{ rightValue.GetType().FullName})"; ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedNowEqualsTo(Statement.LeftExpression, rightValueString)); } }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { IndexValue = -1; var indexableValue = (IList)GetAssignableObject(); if (ParentInterpreter.FailedOrStop || indexableValue == null) { return(null); } if (IndexValue < 0 || IndexValue >= indexableValue.Count) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new IndexOutOfRangeException($"Unable to get the item number '{IndexValue}' because the limit of the array is '{indexableValue.Count - 1}'."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } return(indexableValue[IndexValue]); }
/// <inheritdoc/> internal override void Run() { if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.FormattedExecutingCondition(Statement.Condition)); } var conditionResult = RunCondition(BaZicInterpreter, ParentInterpreter, Statement.Condition); if (ParentInterpreter.IsAborted || conditionResult == null) { return; } IReadOnlyList <Code.AbstractSyntaxTree.Statement> statements; if (conditionResult.Value) { statements = Statement.TrueStatements; } else { statements = Statement.FalseStatements; } if (statements == null || statements.Count == 0) { return; } // Execute statements var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, ParentInterpreter.State.IsInIteration, ParentInterpreter.CaughtException, statements); block.Run(); ChildBlockState = block.State; if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.FormattedEndExecutingCondition(Statement.Condition)); } }
/// <inheritdoc/> internal override object Run() { if (Expression.CreateType == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.InstantiateInterpreter.FormattedCreateTypeNull(nameof(InstantiateExpression.CreateType), nameof(InstantiateExpression))), Expression); return(null); } var createType = ParentInterpreter.RunExpression(Expression.CreateType) as Type; if (ParentInterpreter.IsAborted) { return(null); } if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Expressions.InstantiateInterpreter.FormattedCreateInstance(Expression.CreateType)); } // Execute argument's values. if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.MethodInterpreter.ExecutingArguments); } var argumentValues = new List <object>(); for (var i = 0; i < Expression.Arguments.Count; i++) { var argumentValue = ParentInterpreter.RunExpression(Expression.Arguments[i]); argumentValues.Add(argumentValue); } if (ParentInterpreter.IsAborted) { return(null); } return(BaZicInterpreter.Reflection.Instantiate(createType, argumentValues.ToArray())); }
/// <inheritdoc/> internal override void Run() { try { // Execute statements var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, ParentInterpreter.State.IsInIteration, ParentInterpreter.CaughtException, Statement.TryStatements); block.Run(); ChildBlockState = block.State; } catch (Exception exception) { if (BaZicInterpreter.Verbose) { ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.TryCatchInterpreter.FormattedExceptionCaught(exception.GetType().FullName)); } // Execute statements var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, ParentInterpreter.State.IsInIteration, exception, Statement.CatchStatements); block.Run(); ChildBlockState = block.State; } }
/// <inheritdoc/> internal override object Run() { if (Expression.Expression == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.NotOperatorInterpreter.FormattedExpressionNull(nameof(NotOperatorExpression))), Expression); return(null); } var expressionValue = ParentInterpreter.RunExpression(Expression.Expression); if (expressionValue == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.NotOperatorInterpreter.ValueNull), Expression); return(null); } else if (expressionValue.GetType() != typeof(bool)) { BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Expressions.NotOperatorInterpreter.FormattedBooleanExpected(expressionValue.GetType().Name)), Expression); return(null); } return(!(bool)expressionValue); }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { var parentClass = ParentInterpreter.ParentClassInterpreter; if (parentClass == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "It looks like the parent class does not exists."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (!parentClass.IsInstance) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NoInstanceReferenceException("Unable to get the instance of the parent class of a static method."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } if (DebugMode) { ParentInterpreter.Log(this, $"Reference to the current instance : {parentClass.ClassDeclaration.Name}"); } return(parentClass); }
/// <inheritdoc/> internal override void Run() { ParentInterpreter.RunExpression(Statement.Expression); }
/// <summary> /// Run the interpretation /// </summary> /// <returns>Returns the result of the interpretation</returns> internal override object Execute() { Collection <object> argumentValues; var createType = Expression._createType; var reference = ParentInterpreter.RunExpression(createType); if (ParentInterpreter.FailedOrStop) { return(null); } if (DebugMode) { ParentInterpreter.Log(this, $"Creating a new instance of '{createType}'"); } var classInterpreter = reference as ClassInterpreter; if (classInterpreter != null) { var program = ParentInterpreter.ParentProgramInterpreter; var classInstance = classInterpreter.CreateNewInstance(); classInstance.StateChanged += ParentInterpreter.ChangeState; classInstance.OnGetParentInterpreter += new Func <ProgramInterpreter>(() => program); classInstance.OnDone += new Action <ClassInterpreter>((cl) => { cl.StateChanged -= ParentInterpreter.ChangeState; }); classInstance.Initialize(); argumentValues = InvokeMethod.GetArgumentValues(Expression, ParentInterpreter); if (ParentInterpreter.FailedOrStop) { return(null); } classInstance.CreateNewInstanceCallConstructors(argumentValues); return(classInstance); } var type = reference as Type; if (type != null) { object classInstance = null; argumentValues = InvokeMethod.GetArgumentValues(Expression, ParentInterpreter); if (ParentInterpreter.FailedOrStop) { return(null); } try { classInstance = Activator.CreateInstance(type, argumentValues.ToArray()); } catch (Exception ex) { if (ex is ArgumentException) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new BadArgumentException("{Unknow}", ex.Message), Expression), ParentInterpreter.GetDebugInfo())); } else if (ex is TargetParameterCountException) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException("ctor", $"There is no constructor with {argumentValues.Count} argument(s) in the class '{Expression._createType}'."), Expression), ParentInterpreter.GetDebugInfo())); } else { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(ex, Expression), ParentInterpreter.GetDebugInfo())); } return(null); } return(classInstance); } return(null); }
/// <inheritdoc/> internal override object Run() { var expressionValue = ParentInterpreter.RunExpression(Expression.TargetObject); if (ParentInterpreter.IsAborted) { return(null); } if (expressionValue == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.NullValue), Expression); return(null); } var valueInfo = ValueInfo.GetValueInfo(expressionValue); if (!valueInfo.IsArray) { BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedIndexerForbidden(valueInfo.Type.Name)), Expression); return(null); } if (Expression.Indexes.Length != 1) { BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.OneIndexerAllowed), Expression); return(null); } var index = ParentInterpreter.RunExpression(Expression.Indexes[0]); if (ParentInterpreter.IsAborted) { return(null); } if (index == null) { BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.IndexMustNotBeNull), Expression); return(null); } if (valueInfo.Type == typeof(ObservableDictionary)) { var dictionary = (ObservableDictionary)expressionValue; object val = null; if (dictionary.TryGetValue(index, out val)) { return(val); } BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedKeyDoesNotExist(index?.ToString())), Expression); return(null); } else if (typeof(IDictionary).IsAssignableFrom(valueInfo.Type)) { return(((IDictionary)expressionValue)[index]); } else { var indexValue = index as int?; if (indexValue == null) { BaZicInterpreter.ChangeState(this, new BadArgumentException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.CastToNumber), Expression); return(null); } if (indexValue < 0 || indexValue >= valueInfo.Length) { BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedOutOfRange(indexValue.ToString(), (valueInfo.Length - 1).ToString())), Expression); return(null); } if (valueInfo.Type.IsArray || valueInfo.Type == typeof(Array)) { return(((Array)expressionValue).GetValue(indexValue.Value)); } else if (typeof(IList).IsAssignableFrom(valueInfo.Type)) { return(((IList)expressionValue)[indexValue.Value]); } } BaZicInterpreter.ChangeState(this, new InternalException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedUnsupportedArray(valueInfo.Type.FullName)), Expression); return(null); }
/// <summary> /// Returns the corresponding assignable object /// </summary> /// <returns>the assignable object</returns> public object GetAssignableObject() { if (Expression._targetObject == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to access to a property when the TargetObject of an AlgorithmPropertyReferenceExpression is null."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } object property; object value = null; PropertyInfo propertyInfo; ClassInterpreter classTargetObject; Variable propertyVariable; if (DebugMode) { ParentInterpreter.Log(this, $"Getting the property '{Expression}'"); } TargetObject = ParentInterpreter.RunExpression(Expression._targetObject); if (ParentInterpreter.FailedOrStop) { return(null); } if (TargetObject == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "It looks like the reference object does not exists."), Expression), ParentInterpreter.GetDebugInfo())); return(null); } classTargetObject = TargetObject as ClassInterpreter; if (classTargetObject != null) { propertyVariable = classTargetObject.FindVariableInTheCurrentInterpreter(Expression._propertyName.ToString()); if (propertyVariable == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new PropertyNotFoundException(Expression._propertyName.ToString()), Expression), ParentInterpreter.GetDebugInfo())); return(null); } property = propertyVariable; if (DebugMode) { value = propertyVariable.Value; } } else { propertyInfo = TargetObject.GetType().GetProperty(Expression._propertyName.ToString()); if (propertyInfo == null) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new PropertyNotFoundException(Expression._propertyName.ToString()), Expression), ParentInterpreter.GetDebugInfo())); return(null); } property = propertyInfo; if (DebugMode) { value = propertyInfo.GetValue(TargetObject); } } if (DebugMode) { ParentInterpreter.Log(this, "Value of the property '{0}' is {1}", Expression._propertyName.ToString(), value == null ? "{null}" : $"'{value}' (type:{value.GetType().FullName})"); } return(property); }
/// <summary> /// Run the interpretation /// </summary> internal override void Execute() { int indexValue = -1; object targetObject = null; object leftValue = null; object rightValue; PropertyInfo propertyInfo; Variable propertyVariable; IList propertyVariableList; var leftExpression = Statement._leftExpression; var rightExpression = Statement._rightExpression; if (DebugMode) { ParentInterpreter.Log(this, $"Assign '{leftExpression}' to '{rightExpression}'"); if (!typeof(IAlgorithmAssignable).IsAssignableFrom(leftExpression.GetType())) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"The left expression is not assignable."), Statement), ParentInterpreter.GetDebugInfo())); return; } } switch (leftExpression.DomType) { case AlgorithmDomType.PropertyReferenceExpression: var propertyReferenceInterpreter = new PropertyReference(DebugMode, ParentInterpreter, leftExpression); leftValue = propertyReferenceInterpreter.GetAssignableObject(); targetObject = propertyReferenceInterpreter.TargetObject; break; case AlgorithmDomType.VariableReferenceExpression: leftValue = new VariableReference(DebugMode, ParentInterpreter, leftExpression).GetAssignableObject(); break; case AlgorithmDomType.ArrayIndexerExpression: var arrayIndexerInterpreter = new ArrayIndexerExpression(DebugMode, ParentInterpreter, leftExpression); leftValue = arrayIndexerInterpreter.GetAssignableObject(); indexValue = arrayIndexerInterpreter.IndexValue; break; default: ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new InvalidCastException($"Unable to find an interpreter for this expression : '{leftExpression.GetType().FullName}'"), Statement), ParentInterpreter.GetDebugInfo())); break; } if (ParentInterpreter.FailedOrStop) { return; } rightValue = ParentInterpreter.RunExpression(rightExpression); if (ParentInterpreter.FailedOrStop) { return; } propertyInfo = leftValue as PropertyInfo; if (propertyInfo != null) { if (!propertyInfo.CanWrite) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"This core property is not assignable."), Statement), ParentInterpreter.GetDebugInfo())); return; } propertyInfo.SetValue(targetObject, rightValue); } propertyVariable = leftValue as Variable; if (propertyVariable != null) { if (propertyVariable.IsArray && !(typeof(Array).IsAssignableFrom(rightValue.GetType()) || typeof(IList).IsAssignableFrom(rightValue.GetType()))) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"The left expression wait for an array, but the right value is not an array."), Statement), ParentInterpreter.GetDebugInfo())); return; } if (!propertyVariable.IsArray && (typeof(Array).IsAssignableFrom(rightValue.GetType()) || typeof(IList).IsAssignableFrom(rightValue.GetType()))) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"The left expression does not support array value, but the right value is an array."), Statement), ParentInterpreter.GetDebugInfo())); return; } propertyVariable.Value = rightValue; } propertyVariableList = leftValue as IList; if (propertyVariableList != null) { if (indexValue < 0 || indexValue >= propertyVariableList.Count) { ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new IndexOutOfRangeException($"Unable to get the item number '{indexValue}' because the limit of the array is '{propertyVariableList.Count - 1}'."), Statement), ParentInterpreter.GetDebugInfo())); return; } propertyVariableList[indexValue] = rightValue; } if (DebugMode) { ParentInterpreter.Log(this, "'{0}' is now equal to {1}", leftExpression.ToString(), rightValue == null ? "{null}" : $"'{rightValue}' (type:{rightValue.GetType().FullName})"); } }
/// <summary> /// Assigns the specified value to an array. /// </summary> /// <param name="arrayIndexer">The reference to the position in the array to set.</param> /// <param name="value">The value to assign.</param> private void AssignArrayValue(ArrayIndexerExpression arrayIndexer, object value) { var expressionValue = ParentInterpreter.RunExpression(arrayIndexer.TargetObject); if (ParentInterpreter.IsAborted) { return; } if (expressionValue == null) { BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.TargetObjectNull), arrayIndexer); return; } var valueInfo = ValueInfo.GetValueInfo(expressionValue); if (!valueInfo.IsArray) { BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedIndexerForbidden(valueInfo.Type.Name)), arrayIndexer); return; } if (arrayIndexer.Indexes.Length != 1) { BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.OneIndexerAllowed), arrayIndexer); return; } var index = ParentInterpreter.RunExpression(arrayIndexer.Indexes[0]); if (ParentInterpreter.IsAborted) { return; } if (index == null) { BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.IndexMustNotBeNull), arrayIndexer.Indexes[0]); return; } if (valueInfo.Type == typeof(ObservableDictionary)) { ((ObservableDictionary)expressionValue)[index] = value; return; } else if (typeof(IDictionary).IsAssignableFrom(valueInfo.Type)) { ((IDictionary)expressionValue)[index] = value; return; } else { var indexValue = index as int?; if (indexValue == null) { BaZicInterpreter.ChangeState(this, new BadArgumentException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.CastToNumber), arrayIndexer); return; } if (indexValue < 0 || indexValue >= valueInfo.Length) { BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedOutOfRange(indexValue, valueInfo.Length - 1)), arrayIndexer); return; } if (valueInfo.Type.IsArray || valueInfo.Type == typeof(Array)) { ((Array)expressionValue).SetValue(value, indexValue.Value); return; } else if (typeof(IList).IsAssignableFrom(valueInfo.Type)) { ((IList)expressionValue)[indexValue.Value] = value; return; } } BaZicInterpreter.ChangeState(this, new InternalException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedUnsupportedArray(valueInfo.Type.FullName)), arrayIndexer); }