示例#1
0
        public KeyValuePair <string[], IValue> RunWithResult(SyneryParser.KeyValueAssignmentContext context)
        {
            string identifier = context.keyValueAssignmentIdentifier().GetText();

            string[] key   = IdentifierHelper.ParseComplexIdentifier(identifier);
            IValue   value = Controller.Interpret <SyneryParser.ExpressionContext, IValue>(context.keyValueInitializer().expression());

            return(new KeyValuePair <string[], IValue>(key, value));
        }
        /// <summary>
        /// Gets an object that contains the corresponding .NET Type for the given type in Synery.
        /// </summary>
        /// <exception cref="SyneryException">Thrown when no matching type was found</exception>
        /// <param name="context"></param>
        /// <returns>the representation of the .Net Type</returns>
        public SyneryType RunWithResult(SyneryParser.TypeContext context)
        {
            if (context.primitiveType() != null)
            {
                return(RunWithResult(context.primitiveType()));
            }
            else if (context.recordType() != null)
            {
                // it's a record type

                string recordTypeName = RecordHelper.ParseRecordTypeName(context.recordType().GetText());

                recordTypeName = IdentifierHelper.GetIdentifierBasedOnFunctionScope(Memory, recordTypeName);

                if (Memory.IsInitialized != true)
                {
                    // suppose the record types haven't been initialized yet
                    // but this method is also used during the initialization of record types
                    // for that reason we need to create a new type references

                    return(TypeHelper.GetSyneryType(typeof(IRecord), recordTypeName));
                }
                else
                {
                    // search for an existing record type in the memory to work with the same instance as much as possible

                    SyneryType recordType = (from t in Memory.RecordTypes
                                             where t.Value.FullName == recordTypeName
                                             select t.Key).FirstOrDefault();

                    // check whether the record type was found

                    if (recordType != null)
                    {
                        return(recordType);
                    }
                }

                throw new SyneryInterpretationException(context, string.Format(
                                                            "Record type with name '{0}' was not found.", recordTypeName));
            }

            throw new SyneryInterpretationException(context, string.Format(
                                                        "Unknown type expression in {0}: '{1}'", this.GetType().Name, context.GetText()));
        }
示例#3
0
        /// <summary>
        /// Tries to find the function declaration with the matching function signature. It also considers that a parameter
        /// isn't set because a default value is available
        ///
        /// EXAMPLE:
        ///
        /// myFunc(string first, string second = "default")
        ///     // do something...
        /// END
        ///
        /// // call with one parameter
        /// myFunc("first parameter");
        /// </summary>
        /// <param name="memory">the SyneryMemory that contains a list with all available functions.</param>
        /// <param name="identifier">the name of the function</param>
        /// <param name="listOfParameterTypes">the types of all available parameters</param>
        /// <returns></returns>
        public static IFunctionData FindSyneryFunctionDeclaration(ISyneryMemory memory, string identifier, SyneryType[] listOfParameterTypes)
        {
            // try to find function(s) with the same name with at least the same number of parameters as given
            IEnumerable <IFunctionData> listOfFunctionData =
                from f in memory.Functions
                where f.FullName == IdentifierHelper.GetIdentifierBasedOnFunctionScope(memory, identifier) &&
                f.FunctionDefinition.Parameters.Count >= listOfParameterTypes.Count()
                select f;

            // try to find the matching function signature
            // also consider that a parameter isn't set because a default value is available
            foreach (IFunctionData data in listOfFunctionData)
            {
                bool isMatching = true;

                for (int i = 0; i < data.FunctionDefinition.Parameters.Count; i++)
                {
                    if (listOfParameterTypes.Count() > i && listOfParameterTypes[i] != null)
                    {
                        SyneryType expectedType = data.FunctionDefinition.Parameters[i].Type;
                        SyneryType givenType    = listOfParameterTypes[i];

                        // compare the types of the expected parameter and the given value
                        if (givenType != expectedType)
                        {
                            // maybe these are two record-types which derive from each other
                            if (givenType.UnterlyingDotNetType != typeof(IRecord) ||
                                expectedType.UnterlyingDotNetType != typeof(IRecord))
                            {
                                isMatching = false;
                            }
                            else
                            {
                                string expectedTypeName = IdentifierHelper.GetFullName(expectedType.Name, data.CodeFileAlias);

                                if (!RecordHelper.IsDerivedType(memory, givenType.Name, expectedTypeName))
                                {
                                    isMatching = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        // no value given for the parameter: check whether there is a default value
                        if (data.FunctionDefinition.Parameters[i].DefaultValue == null)
                        {
                            isMatching = false;
                        }
                    }
                }

                // the current function declaration seems to match -> return it
                if (isMatching == true)
                {
                    return(data);
                }
            }

            // no matching function declaration found
            return(null);
        }