public override CSObject Evaluate(CSState state, CSObject curObj)
        {
            if (ChildCount != 2)
            {
                CSLog.E(this, "assigngment operator has invalid # of children...");
                return(null);
            }
            CSObject left = Left.Evaluate(state, curObj);

            if (left.CanCast <IList> ())
            {
                int         index = IndexNode.EvaluateIndex(state, curObj);
                System.Type type  = ReflectionUtil.GetIListElementType(left.Type);
                return(CSObject.ArrayVariableObject(this, type, left.Value, index));
            }
            else if (left.CanCast <IDictionary> ())
            {
                string      key  = IndexNode.EvaluateKey(state, curObj);
                System.Type type = ReflectionUtil.GetIDictionaryElementType(left.Type);
                return(CSObject.DictionaryVariableObject(this, type, left.Value, key));
            }
            else
            {
                CSLog.E(this, "you cannot use an index to non IList object");
                return(null);
            }
        }
Exemple #2
0
        public override CSNode VisitType(CSScriptParser.TypeContext context)
        {
            System.Type currentType;
            string      currentTypeString = VisitTypeElements(context.type_elements(), out currentType);

            if (context.arraytype() != null)
            {
                currentType = ReflectionUtil.GetType(currentTypeString + "[]");
            }

            if (currentType == null)
            {
                CSLog.E(context.Start.Line, context.Start.Column, "unknown type: " + currentTypeString);
                return(null);
            }

            CSTypeNode node = new CSTypeNode(context.Start.Line, context.Start.Column);

            node._type         = currentType;
            node._typeString   = currentType.AssemblyQualifiedName;
            node._assemblyName = currentType.Assembly.GetCleanName();
            //CSLog.D ("full name: " + node._typeString + " in the assembly: " + node._assemblyName);

            return(node);
        }
Exemple #3
0
        public override CSObject Evaluate(CSState state, CSObject curObj)
        {
            if (curObj == null)
            {
                CSLog.E(this, "curObj is missing");
                return(null);
            }

            int    len = _selectors.Length;
            object obj = curObj.Value;

            for (int i = 0; i < len - 1; ++i)
            {
                if (obj == null)
                {
                    CSLog.E(this, "object is not assgined");
                    return(null);
                }

                obj = ReflectionUtil.Get(obj, _selectors[i]);
            }

            if (obj == null)
            {
                CSLog.E(this, "object is not assgined");
                return(null);
            }
            string variableName = _selectors[len - 1];

            System.Type type = ReflectionUtil.GetFieldType(obj, variableName);

            return(CSObject.InstanceVariableObject(this, type, obj, variableName));
        }
 public static void Set(object target, string name, BindingFlags bindingFlags, object value)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
     }
     _inst._Set(target, name, bindingFlags, value);
 }
 public static string GetCleanNameIfPrimitive(string typeName)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         return(null);
     }
     return(_inst._GetCleanNameIfPrimitive(typeName));
 }
 public static Type GetType(string typeName, params string[] namespaceNames)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         return(null);
     }
     return(_inst._GetType(typeName, namespaceNames));
 }
 public static Type GetFieldType(object target, string name)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         return(null);
     }
     return(_inst._GetFieldType(target, name, BindingFlags.Public | BindingFlags.NonPublic));
 }
 public static void Initialize()
 {
     if (_inst != null)
     {
         CSLog.E("ReflectionUtil has already been initialized...");
         return;
     }
     _inst = new ReflectionUtil();
 }
 public static object Cast(Type type, object o)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         return(false);
     }
     return(_inst._Cast(type, o));
 }
 public static object Get(object target, string name, BindingFlags bindingFlags)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         return(null);
     }
     return(_inst._Get(target, name, bindingFlags));
 }
 public static bool HasField(object target, string name, BindingFlags bindingFlags, object value)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         return(false);
     }
     return(_inst._HasField(target, name, bindingFlags));
 }
 public static object CallMethod(object target, string methodName, BindingFlags bindingFlags, Type[] genericTypes, out System.Type retType, params object[] args)
 {
     if (_inst == null)
     {
         CSLog.E("ReflectionUtil has not been initialized...");
         retType = null;
         return(null);
     }
     return(_inst._CallMethod(target, methodName, bindingFlags, genericTypes, out retType, args));
 }
Exemple #13
0
 public void AddVarible(string variableName, CSObject obj)
 {
     variableName = SkipAtMark(variableName);
     if (_variables.ContainsKey(variableName))
     {
         CSLog.E("Variable " + variableName + " already exists...");
         return;
     }
     _variables[variableName] = obj;
 }
Exemple #14
0
 public object SetVariable(string variableName, CSObject val)
 {
     variableName = SkipAtMark(variableName);
     if (!_variables.ContainsKey(variableName))
     {
         CSLog.E("Variable " + variableName + " does not exist...");
         return(null);
     }
     _variables[variableName] = val;
     return(val);
 }
Exemple #15
0
        public object GetVariable(string variableName)
        {
            variableName = SkipAtMark(variableName);
            CSObject val = null;

            if (!_variables.TryGetValue(variableName, out val))
            {
                CSLog.E("Variable: " + variableName + " does not exist...");
            }
            return(val);
        }
        public static object CallMethod(object target, string methodName, params object[] args)
        {
            if (_inst == null)
            {
                CSLog.E("ReflectionUtil has not been initialized...");
                return(null);
            }
            Type retType;

            return(_inst._CallMethod(target, methodName, BindingFlags.Public | BindingFlags.NonPublic, null, out retType, args));
        }
Exemple #17
0
        public override CSNode VisitIntAtomExp(CSScriptParser.IntAtomExpContext context)
        {
            CSIntNode node = new CSIntNode(context.Start.Line, context.Start.Column);
            int       val  = 0;

            if (!int.TryParse(context.INT().GetText(), out val))
            {
                CSLog.E(node, "failed to parse int: " + context.INT().GetText());
            }
            node._val = val;
            return(node);
        }
Exemple #18
0
        public override CSNode VisitFloatAtomExp(CSScriptParser.FloatAtomExpContext context)
        {
            CSFloatNode node = new CSFloatNode(context.Start.Line, context.Start.Column);
            float       val  = 0;

            if (!float.TryParse(context.FLOAT().GetText().Replace("f", ""), out val))
            {
                CSLog.E(node, "failed to parse float: #" + context.FLOAT().GetText() + "#");
            }
            node._val = val;
            return(node);
        }
Exemple #19
0
        public override CSObject Evaluate(CSState state, CSObject curObj)
        {
            if (ChildCount != 2)
            {
                CSLog.E(this, "assigngment operator has invalid # of children...");
                return(null);
            }
            CSObject left  = Left.Evaluate(state, curObj);
            CSObject right = Right.Evaluate(state, left);

            return(right);
        }
Exemple #20
0
        public CSNode GetChild(int index)
        {
            if (_children == null)
            {
                CSLog.E(this, "_children is null");
                return(null);
            }

            if (index < 0 || index >= ChildCount)
            {
                CSLog.E(this, "index out of bound");
                return(null);
            }

            return(_children[index]);
        }
Exemple #21
0
        public override CSNode VisitDotExp(CSScriptParser.DotExpContext context)
        {
            CSOPDotNode node = new CSOPDotNode(context.Start.Line, context.Start.Column);

            CSScriptParser.ExpressionContext[] expressions = context.expression();
            if (expressions == null || expressions.Length != 2)
            {
                CSLog.E(context.Start.Line, context.Start.Column, "invalid # of children...");
                return(null);
            }
            node._children    = new CSNode[2];
            node._children[0] = Visit(expressions[0]);
            node._children[1] = Visit(expressions[1]);

            return(node);
        }
Exemple #22
0
        public CSObject GetVariable(string variableName)
        {
            CSScope  next = Current;
            CSObject val;

            while (next != null)
            {
                if (next.TryGetVariable(variableName, out val))
                {
                    return(val);
                }
                next = next._parent;
            }
            CSLog.E("Variable: " + variableName + " does not exist...");
            return(null);
        }
Exemple #23
0
        public CSObject Assign(CSObject obj)
        {
            switch (ObjectType)
            {
            case ObjectType.VARIABLE:
                ReflectionUtil.Set(_object, _name, obj.GetAs(_type));
                break;

            case ObjectType.STATIC:
                ReflectionUtil.Set(_staticType, _name, obj.GetAs(_type));
                break;

            case ObjectType.ARRAY:
                IList array = (IList)_object;
                array[_arrayIndex] = obj.GetAs(_type);
                break;

            case ObjectType.DICTIONARY:
                IDictionary dictionary = (IDictionary)_object;
                dictionary[_name] = obj.GetAs(_type);
                break;

            case ObjectType.TEMP:
            case ObjectType.LOCAL:
                if (_type == null)
                {
                    _object = obj.Value;
                    _type   = obj.Type;
                }
                else
                {
                    _object = obj.GetAs(_type);
                }
                break;

            default:
                CSLog.E(_node, "cannot assign to " + _objectType.ToString());
                break;
            }

            return(this);
        }
Exemple #24
0
        public static CSObject ImmediateObject(CSNode node, System.Type type, object val)
        {
            if (val == null)
            {
                CSLog.E(node, "val cannot be null");
                return(null);
            }
            CSObject obj = new CSObject()
            {
                _node       = node,
                _object     = val,
                _type       = val.GetType(),
                _objectType = ObjectType.IMMEDIATE,
                _name       = "immedidate",
                _staticType = null,
                _arrayIndex = -1,
            };

            return(obj);
        }
Exemple #25
0
        public override CSNode VisitVarDeclExp(CSScriptParser.VarDeclExpContext context)
        {
            CSLocalVariableNode variableNode = new CSLocalVariableNode(context.Start.Line, context.Start.Column);

            variableNode._declaration  = true;
            variableNode._variableName = context.NAME().GetText();

            CSScriptParser.TypeContext vartypes = context.type();
            if (vartypes != null)
            {
                CSTypeNode typeNode = Visit(vartypes) as CSTypeNode;
                if (typeNode == null)
                {
                    CSLog.E(variableNode, "failed to get the type");
                }
                variableNode._type = typeNode._type;
            }

            CSObject objForComplier = CSObject.LocalVariableObject(variableNode, variableNode._type, variableNode._variableName, null);

            _state.AddVariable(variableNode._variableName, objForComplier);

            return(variableNode);
        }
 public override void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
 {
     CSLog.E(line, charPositionInLine, msg);
 }
Exemple #27
0
        public override CSNode VisitNewExp(CSScriptParser.NewExpContext context)
        {
            CSOPNewNode node = new CSOPNewNode(context.Start.Line, context.Start.Column);

            node._children = new CSNode[6];

            CSScriptParser.ParametersContext  parameters = context.parameters();
            CSScriptParser.Array_indexContext arrayIndex = context.array_index();

            System.Type currentType;
            string      currentTypeString = VisitTypeElements(context.type_elements(), out currentType);

            if (arrayIndex != null)
            {
                currentTypeString = currentTypeString + "[]";
                currentType       = ReflectionUtil.GetType(currentTypeString);
            }

            if (currentType == null)
            {
                CSLog.E(node, "unknown type: " + currentTypeString);
                return(null);
            }

            CSTypeNode typeNode = new CSTypeNode(context.Start.Line, context.Start.Column);

            typeNode._type         = currentType;
            typeNode._typeString   = currentTypeString;
            typeNode._assemblyName = currentType.Assembly.GetCleanName();

            node._children[0] = typeNode;

            if (parameters != null)
            {
                node._children[1] = Visit(parameters);
            }

            if (arrayIndex != null)
            {
                node._children[2] = Visit(arrayIndex);
            }

            CSScriptParser.InitializerContext initializer = context.initializer();
            if (initializer != null)
            {
                CSScriptParser.Array_initializerContext arrayInitializer = initializer.array_initializer();
                if (arrayInitializer != null)
                {
                    node._children[3] = Visit(arrayInitializer);
                }

                CSScriptParser.Dictionary_initializerContext dictionaryInitializer = initializer.dictionary_initializer();
                if (dictionaryInitializer != null)
                {
                    node._children[4] = Visit(dictionaryInitializer);
                }

                CSScriptParser.Class_initializerContext classInitializer = initializer.class_initializer();
                if (classInitializer != null)
                {
                    node._children[5] = Visit(classInitializer);
                }
            }
            return(node);
        }
Exemple #28
0
        CSNode VisitSelectors(CSScriptParser.SelectorContext[] selectors, int line, int col)
        {
            int selectorLen = selectors.Length;

            string firstName = selectors[0].NAME().GetText();

            if (_state.HasVariable(firstName))
            {
                CSLocalVariableNode node = new CSLocalVariableNode(line, col);
                node._declaration  = false;
                node._variableName = firstName;
                if (selectorLen == 1)
                {
                    return(node);
                }
                else
                {
                    CSSelectorNode selectorNode = new CSSelectorNode(line, col);
                    selectorNode._selectors = GetSelectorStrings(selectors, 1);
                    CSOPDotNode dotNode = new CSOPDotNode(line, col);
                    dotNode._children    = new CSNode[2];
                    dotNode._children[0] = node;
                    dotNode._children[1] = selectorNode;
                    return(dotNode);
                }
            }

            string currentTypeString = null;

            System.Type currentType = null;
            int         typeStart   = -1;
            int         typeEnd     = 0;

            for (int i = 0; i < selectorLen; ++i)
            {
                CSScriptParser.SelectorContext next = selectors[i];
                string name = next.NAME().GetText();
                currentTypeString = GetTypeString(selectors, i + 1, typeStart);
                System.Type nextType = ReflectionUtil.GetType(currentTypeString);

                if (typeStart == -1 && nextType != null)
                {
                    typeStart = i;
                }

                if (currentType != null && nextType == null)
                {
                    typeEnd = i;
                    break;
                }
                currentType = nextType;
            }

            if (currentType != null)
            {
                if (typeEnd == 0)
                {
                    CSTypeNode node = new CSTypeNode(line, col);
                    node._typeString   = currentTypeString;
                    node._type         = currentType;
                    node._assemblyName = currentType.Assembly.GetCleanName();
                    return(node);
                }
                else
                {
                    CSStaticVariableNode node = new CSStaticVariableNode(line, col);
                    string varName            = selectors[typeEnd].NAME().GetText();
                    node._variableName = varName;
                    node._staticType   = currentType;
                    node._type         = ReflectionUtil.GetFieldType(currentType, varName);

                    if (node._type == null)
                    {
                        CSLog.E(node, "type: " + currentType.FullName + " doesn't have: " + varName);
                    }

                    if (selectorLen == typeEnd + 1)
                    {
                        return(node);
                    }
                    else
                    {
                        CSSelectorNode selectorNode = new CSSelectorNode(line, col);
                        selectorNode._selectors = GetSelectorStrings(selectors, typeEnd + 1);
                        CSOPDotNode dotNode = new CSOPDotNode(line, col);
                        dotNode._children    = new CSNode[2];
                        dotNode._children[0] = node;
                        dotNode._children[1] = selectorNode;
                        return(dotNode);
                    }
                }
            }
            else
            {
                CSSelectorNode node = new CSSelectorNode(line, col);
                node._selectors = GetSelectorStrings(selectors, 0);
                return(node);
            }
        }
Exemple #29
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);
        }