public void TestReferenceGetting()
        {
            CodeContext codeContext = new CodeContext(null);
            Sprite      sprite      = new Sprite();

            codeContext.VariableStack[0].Add("spriteObject", sprite);

            var result = mExpressionParser.EvaluateExpression("spriteObject.X", codeContext, ExpressionParseType.GetReference);

            if (result is AssignableReference == false)
            {
                throw new Exception("Evaluation of reference types is not working properly");
            }

            mPlugin.ApplyLinesInternal(new string[] { "spriteObject.X = 2;" }, 0, 1, null, codeContext);

            if (sprite.X != 2.0f)
            {
                throw new Exception("Assignmnt of code context variables isn't working");
            }

            //codeContext.VariableStack[stackDepth][memberName];
            mPlugin.ApplyLinesInternal(new string[] { "int m = 3;" }, 0, 1, null, codeContext);
            result = mExpressionParser.EvaluateExpression("m", codeContext, ExpressionParseType.GetReference);
            if (result is IAssignableReference == false)
            {
                throw new Exception("Getting reference to local variables is not working properly");
            }

            mPlugin.ApplyLinesInternal(new string[] { "m = 4;" }, 0, 1, null, codeContext);
            //codeContext.VariableStack[stackDepth][memberName];
            if ((int)(codeContext.VariableStack[0]["m"]) != 4)
            {
                throw new Exception("Assignmnt of code context variables isn't working");
            }

            result = mExpressionParser.EvaluateExpression("Sprite someSprite;", codeContext, ExpressionParseType.GetReference);

            int stackDepth;

            codeContext.GetVariableInformation("someSprite", out stackDepth);
            if (stackDepth == -1)
            {
                throw new Exception("Declaring variables like int j is not working");
            }
        }
        internal object EvaluateStatement(Statement statement, CodeContext codeContext, ExpressionParseType parseType)
        {
            if (statement is VariableDeclarationStatement)
            {
                VariableDeclarationStatement vds = statement as VariableDeclarationStatement;

                var astType = vds.Type;

                Type type = TypeManager.GetTypeFromString(astType.GetText());

                var variables = vds.Variables;

                string addedVariable = null;

                foreach (var variable in variables)
                {
                    // right now we only support one declaration per line, will need to
                    // expand this later
                    addedVariable = variable.Name;
                    codeContext.VariableStack.Last().Add(addedVariable, null);
                    break;
                }

                if (parseType == ExpressionParseType.Evaluate)
                {
                    return(null); // hasn't been assigned yet
                }
                else
                {
                    StackVariableReference svr = new StackVariableReference();
                    int index;
                    codeContext.GetVariableInformation(addedVariable, out index);
                    svr.StackIndex      = index;
                    svr.VariableName    = addedVariable;
                    svr.TypeOfReference = type;
                    return(svr);
                }
            }
            else
            {
                return(null);
            }
        }
        private static void GetObjectFromContainerAndNameEvaluate(object container, string memberName, CodeContext codeContext, ref object foundValue, ref bool wasFound)
        {
            if (container is CsvEntry)
            {
                foundValue = (container as CsvEntry).GetValue(memberName);
            }

            if (codeContext.VariableStack.Count == 0)
            {
                throw new Exception("codeContext doesn't have any entries.  It needs to have at least one");
            }

            int index = -1;

            codeContext.GetVariableInformation(memberName, out index);

            if (index != -1)
            {
                foundValue = codeContext.VariableStack[index][memberName];
                wasFound   = true;
            }

            if (wasFound == false && foundValue == null && container != null)
            {
                object instance = container;
                Type   type     = container.GetType();
                if (container is Type)
                {
                    instance = null;
                    type     = container as Type;
                }

                // First let's do reflection
                if (container is ElementRuntime && (container as ElementRuntime).DirectObjectReference != null)
                {
                    ElementRuntime containerElementRuntime = container as ElementRuntime;

                    if (LateBinder.GetInstance(containerElementRuntime.DirectObjectReference.GetType()).TryGetValue(containerElementRuntime.DirectObjectReference, memberName, out foundValue))
                    {
                        // do nothing.
                        wasFound = true;
                    }
                }
                else
                {
                    if (LateBinder.GetInstance(type).TryGetValue(instance, memberName, out foundValue))
                    {
                        // do nothing.
                        wasFound = true;
                    }
                }

                if (foundValue == null && container is ElementRuntime)
                {
                    ElementRuntime containerElementRuntime = container as ElementRuntime;

                    IElement containerElement = (container as ElementRuntime).AssociatedIElement;


                    foundValue = TryToGetStateCategoryFromElement(memberName, containerElement);

                    if (foundValue == null)
                    {
                        foundValue = containerElementRuntime.GetContainedElementRuntime(memberName);
                    }

                    if (foundValue == null)
                    {
                        foundValue = containerElementRuntime.GetReferencedFileSaveRuntime(memberName);
                    }

                    if (foundValue == null && containerElement != null)
                    {
                        // Some values like X or Y are stored inside the element runtime
                        // (because it actually stores those values locally).  However, if
                        // a value doesn't have an underlying value,
                        CustomVariable variable = containerElementRuntime.GetCustomVariable(memberName, VariableGetType.AsExistsAtRuntime);
                        //CustomVariable variable = containerElement.GetCustomVariableRecursively(memberName);
                        if (variable != null)
                        {
                            if (variable.GetIsCsv())
                            {
                                string rfsToLookFor = FileManager.RemoveExtension(variable.Type);
                                foundValue = containerElementRuntime.GetReferencedFileSaveRuntime(rfsToLookFor);
                                // if it's null, maybe it's a global file
                                if (foundValue == null)
                                {
                                    ReferencedFileSave rfs = ObjectFinder.Self.GetReferencedFileSaveFromFile(variable.Type);
                                    if (rfs != null)
                                    {
                                        foundValue = GluxManager.GlobalContentFilesRuntime.LoadReferencedFileSave(rfs, true, containerElement);
                                    }
                                }

                                if (foundValue != null)
                                {
                                    foundValue = GetCsvEntryByRequiredKey(variable.DefaultValue as string, foundValue as RuntimeCsvRepresentation);
                                    // We have a RFS, so let's get values out of it
                                }
                            }
                            else
                            {
                                foundValue = variable.DefaultValue;
                                wasFound   = true;
                            }
                        }
                    }
                    wasFound = foundValue != null;
                }
                else if (container is StateSaveCategory)
                {
                    foundValue = (container as StateSaveCategory).States.FirstOrDefault(state => state.Name == memberName);
                    wasFound   = foundValue != null;
                }
                else if (container is IElement)
                {
                    foundValue = TryToGetStateCategoryFromElement(memberName, container as IElement);
                }
            }
            if (wasFound == false && foundValue == null)
            {
                foundValue = ObjectFinder.Self.GetElementUnqualified(memberName);
                wasFound   = foundValue != null;
            }
            if (wasFound == false && foundValue == null)
            {
                foundValue = TypeManager.GetTypeFromString(memberName);

                wasFound = foundValue != null;
            }
        }
        private static void GetObjectFromContainerAndNameReference(object container, string memberName, CodeContext codeContext,
                                                                   ref object foundValue, ref bool wasFound)
        {
            object originalContainer = container;

            if (container == null)
            {
                int stackDepth = -1;
                codeContext.GetVariableInformation(memberName, out stackDepth);

                if (stackDepth != -1)
                {
                    StackVariableReference reference = new StackVariableReference();
                    reference.StackIndex   = stackDepth;
                    reference.VariableName = memberName;
                    reference.CodeContext  = codeContext;

                    foundValue = reference;
                    wasFound   = foundValue != null;
                }
            }
            else
            {
                Type typeToGetFrom = null;

                if (container is IAssignableReference)
                {
                    container = ((IAssignableReference)container).CurrentValue;
                }

                if (container is Type)
                {
                    typeToGetFrom = container as Type;
                }
                else if (container != null)
                {
                    typeToGetFrom = container.GetType();
                }

                AssignableReference assignableReference = null;

                var fieldInfo = typeToGetFrom.GetField(memberName);
                if (fieldInfo != null)
                {
                    assignableReference           = new AssignableReference();
                    assignableReference.FieldInfo = fieldInfo;
                    assignableReference.Owner     = container;

                    if (originalContainer is IAssignableReference)
                    {
                        assignableReference.Parent = originalContainer as IAssignableReference;
                    }
                }
                else
                {
                    var propertyInfo = typeToGetFrom.GetProperty(memberName);
                    if (propertyInfo != null)
                    {
                        assignableReference = new AssignableReference();
                        assignableReference.PropertyInfo = propertyInfo;
                        assignableReference.Owner        = container;

                        if (originalContainer is IAssignableReference)
                        {
                            assignableReference.Parent = originalContainer as IAssignableReference;
                        }
                    }
                }

                foundValue = assignableReference;
                wasFound   = foundValue != null;
            }
        }