コード例 #1
0
        public override CSNode VisitArray_initializer(CSScriptParser.Array_initializerContext context)
        {
            CSArrayInitializerNode node = new CSArrayInitializerNode(context.Start.Line, context.Start.Column);

            VisitExpressions(node, context.expression());
            return(node);
        }
コード例 #2
0
        public override CSObject Evaluate(CSState state, CSObject curObj)
        {
            if (ChildCount != 6)
            {
                CSLog.E(this, "new operator has invalid # of children...");
                return(null);
            }

            if (NewType == null || NewType._type == null)
            {
                CSLog.E(this, "new operator does not have a proper type...");
                return(null);
            }

            object newInstance = null;

            System.Type type = NewType._type;
            if (IsArray)
            {
                System.Type innerType = type.GetElementType();
                int         count     = ArrayIndex.EvaluateIndex(null, null);
                if (ArrayInitializer != null)
                {
                    object[] elements   = ArrayInitializer.EvaluateElements(state, curObj, innerType);
                    int      len        = elements.Length;
                    int      allocCount = (len > count? len : count);
                    newInstance = System.Activator.CreateInstance(type, allocCount);
                    System.Array array = newInstance as System.Array;
                    for (int i = 0; i < len; ++i)
                    {
                        array.SetValue(elements[i], i);
                    }
                }
                else
                {
                    if (count < 0)
                    {
                        throw new System.ArgumentException("array needs an initializer or count");
                    }
                    else
                    {
                        newInstance = System.Activator.CreateInstance(type, count);
                    }
                }
            }
            else
            {
                object[] parameters = null;

                if (NewParameters != null && NewParameters.ChildCount > 0)
                {
                    CSNode[] pchildren = NewParameters._children;
                    int      pCount    = pchildren.Length;
                    parameters = new object[pCount];
                    for (int i = 0; i < pCount; ++i)
                    {
                        parameters[i] = pchildren[i].Evaluate(state, curObj).Value;
                    }
                }

                if (parameters == null)
                {
                    newInstance = System.Activator.CreateInstance(type);
                }
                else
                {
                    newInstance = System.Activator.CreateInstance(type, parameters);
                }

                CSDictionaryInitializerNode dictInitializer = DictionaryInitializer;
                if (dictInitializer != null)
                {
                    IDictionary dict = newInstance as IDictionary;
                    if (dict == null)
                    {
                        throw new System.InvalidOperationException("you cannot use a dictionary initializer on non dictionary object");
                    }

                    System.Type keyType   = typeof(object);
                    System.Type valueType = typeof(object);

                    System.Type[] genericTypes = NewType._type.GetGenericArguments();

                    if (genericTypes != null)
                    {
                        if (genericTypes.Length > 2)
                        {
                            keyType   = genericTypes[0];
                            valueType = genericTypes[1];
                        }
                    }

                    int len = dictInitializer.Count;
                    for (int i = 0; i < len; ++i)
                    {
                        KeyValuePair <object, object> element = dictInitializer.Evaluate(state, curObj, i, keyType, valueType);
                        dict.Add(element.Key, element.Value);
                    }
                }

                CSClassInitializerNode classInitializer = ClassInitializer;
                if (classInitializer != null)
                {
                    int len = classInitializer.Count;
                    for (int i = 0; i < len; ++i)
                    {
                        KeyValuePair <string, object> element = classInitializer.Evaluate(state, curObj, i, newInstance);
                        ReflectionUtil.Set(newInstance, element.Key, element.Value);
                    }
                }

                CSArrayInitializerNode arrayInitializer = ArrayInitializer;
                IList list = newInstance as IList;
                if (ArrayInitializer != null)
                {
                    if (list == null)
                    {
                        CSLog.E("array initializer cannot used for : " + type.ToString());
                    }
                    else
                    {
                        System.Type elementType = ReflectionUtil.GetIListElementType(NewType._type);
                        if (elementType == null)
                        {
                            CSLog.E("array initializer cannot used for : " + type.ToString());
                        }
                        object[] elements = ArrayInitializer.EvaluateElements(state, curObj, elementType);
                        int      len      = elements.Length;
                        for (int i = 0; i < len; ++i)
                        {
                            list.Add(elements[i]);
                        }
                    }
                }
            }

            CSObject obj = CSObject.TempVariableObject(this, type, newInstance);

            return(obj);
        }