Example #1
0
 public static IList <IValue> GetListOfParameterValues(IInterpretationController controller, SyneryParser.ExpressionListContext context)
 {
     if (context != null)
     {
         return(controller.Interpret <SyneryParser.ExpressionListContext, IList <IValue> >(context));
     }
     else
     {
         return(new List <IValue>());
     }
 }
        public static void InterpretHandleBlock(IInterpretationController controller, IHandleBlockData data, IValue eventRecord)
        {
            SyneryParser.BlockContext blockContext = data.Context as SyneryParser.BlockContext;

            if (blockContext != null)
            {
                // create a new block scope and set the parent scope of the OBSERVE-block as parent
                INestedScope blockScope = new BlockScope(data.ParentScope);

                // add the event record as local variable
                blockScope.DeclareVariable(data.ParameterName, eventRecord.Type);
                blockScope.AssignVariable(data.ParameterName, eventRecord.Value);

                // start interpreting the given HANDLE-block
                controller.Interpret <SyneryParser.BlockContext, INestedScope, INestedScope>(blockContext, blockScope);
            }
            else
            {
                throw new SyneryException("Can not interpret handle block because the given context is not a BlockContext");
            }
        }
Example #3
0
 public static IEnumerable <IExpressionValue> GetListOfParameterExpressionValues(IInterpretationController controller, QueryMemory queryMemory, SyneryParser.RequestExpressionListContext context)
 {
     if (context != null)
     {
         return(controller.Interpret <SyneryParser.RequestExpressionListContext, IList <IExpressionValue>, QueryMemory>(context, queryMemory));
     }
     else
     {
         return(new List <IExpressionValue>());
     }
 }
Example #4
0
        /// <summary>
        /// Executes the function from the given <paramref name="functionData"/> using the given <paramref name="controller"/> and by setting the <paramref name="listOfParameters"/> as local scope variables.
        /// </summary>
        /// <param name="controller">the controller that is used to interpret the function</param>
        /// <param name="functionData">contains the data for the function that should be executed</param>
        /// <param name="listOfParameters"></param>
        /// <param name="functionCallContext">the synery context where the function is called (only used in exceptions)</param>
        /// <returns>the return value may be null if the function definition says HasReturnValue=false</returns>
        public static IValue ExecuteSyneryFunction(IInterpretationController controller, IFunctionData functionData, object[] listOfParameters, Antlr4.Runtime.ParserRuleContext functionCallContext)
        {
            IValue returnValue;
            IDictionary <string, IValue> scopeVariables = new Dictionary <string, IValue>();
            int sizeOfSignatureParameters = functionData.FunctionDefinition.Parameters.Count;
            int sizeOfListOfParameters    = listOfParameters.Count();

            // prepare the parameters as local scope variables

            for (int i = 0; i < sizeOfSignatureParameters; i++)
            {
                object     variableValue = null;
                string     variableName  = functionData.FunctionDefinition.Parameters[i].Name;
                SyneryType variableType  = functionData.FunctionDefinition.Parameters[i].Type;

                if (sizeOfListOfParameters >= (i + 1) &&
                    listOfParameters[i] != null)
                {
                    variableValue = listOfParameters[i];
                }
                else if (functionData.FunctionDefinition.Parameters[i].DefaultValue != null)
                {
                    variableValue = functionData.FunctionDefinition.Parameters[i].DefaultValue.Value;
                }

                scopeVariables.Add(variableName, new TypedValue(variableType, variableValue));
            }

            // start executing the function context

            if (functionData.Context is SyneryParser.BlockContext)
            {
                /* 1. Create scope for the function block with the given parameters set as variables.
                 *    Set the global scope as parent to avoid accessing variables of this scope.
                 * 2. Execute the function block.
                 * 3. Get the return value from the function block.
                 */

                IFunctionScope functionScope = new FunctionScope(controller.Memory.GlobalScope, functionData, scopeVariables);

                controller.Interpret <SyneryParser.BlockContext, INestedScope, INestedScope>((SyneryParser.BlockContext)functionData.Context, functionScope);

                returnValue = functionScope.ReturnValue;
            }
            else
            {
                throw new SyneryInterpretationException(functionData.Context, "Unknown function content cannot be interpreted.");
            }

            // validate the return value

            if (functionData.FunctionDefinition.HasReturnValue == true && returnValue == null)
            {
                string message = String.Format(
                    "Function has no return value but is expected to return '{0}'. Function called on line: '{1}'.",
                    functionData.FunctionDefinition.ReturnType.PublicName,
                    functionCallContext.Start.Line);

                throw new SyneryInterpretationException(functionData.Context, message);
            }
            else if (functionData.FunctionDefinition.HasReturnValue == true &&
                     functionData.FunctionDefinition.ReturnType != returnValue.Type)
            {
                string message = String.Format(
                    "Function has wrong return value. Expected: '{0}' / Given: '{1}'. Function called on line: '{2}'.",
                    functionData.FunctionDefinition.ReturnType.PublicName,
                    returnValue.Type.PublicName,
                    functionCallContext.Start.Line);
                throw new SyneryInterpretationException(functionData.Context, message);
            }

            return(returnValue);
        }