Esempio n. 1
0
 public ContextFunctionCallInterpreter(MethodInfo method, ScriptEngine engine)
 {
     ops      = engine.GetPlugin <EventFunctionOperators> ();
     switches = engine.GetPlugin <ContextSwitchesPlugin> ();
     argsDef  = method.GetParameters();
     if (argsDef.Length > 0)
     {
         addContextInter = switches.GetInterByType(argsDef [argsDef.Length - 1].ParameterType);
     }
     Engine     = engine;
     returnType = method.ReturnType;
     if (returnType != typeof(void) && returnType != typeof(string) && (returnType.IsClass || (returnType.IsValueType && !returnType.IsEnum &&
                                                                                               returnType != typeof(bool) && returnType != typeof(float) && returnType != typeof(int))) &&
         !(returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(List <>)))
     {
         if (ContextPropertySwitchInterpreter.allPropSwitches.ContainsKey(new ContextPropertySwitchInterpreter.PropKey(returnType, "")))
         {
             ctxInter = ContextPropertySwitchInterpreter.allPropSwitches[new ContextPropertySwitchInterpreter.PropKey(returnType, "")];
         }
         else
         {
             ctxInter = new ContextPropertySwitchInterpreter("", returnType, engine);
         }
     }
     funcName = method.Name;
 }
Esempio n. 2
0
 public void AddInterpreter(string name, FunctionOperatorInterpreter inter)
 {
     if (interpreters.ContainsKey(name))
     {
         Debug.LogWarning("Already has an interpreter " + name);
         return;
     }
     inter.Engine = Engine;
     interpreters.Add(name, inter);
 }
 public void AddInterToType(Type type, FunctionOperatorInterpreter inter)
 {
     if (!intersByType.ContainsKey(type))
     {
         intersByType.Add(type, inter);
     }
     if (type == typeof(GameObject) && inter.GetType() == typeof(ContextSwitchInterpreter))
     {
         if (!intersByType.ContainsKey(type))
         {
             intersByType.Add(type, inter);
         }
         else
         {
             intersByType[type] = inter;
         }
     }
 }
    public FunctionOperatorInterpreter InterpretInContext(Operator op, FunctionBlock block)
    {
        //Debug.Log ("interpret in context");
        FunctionOperatorInterpreter opInter = null;

        if (!contextSwitches.TryGetValue(op.Identifier as string, out opInter))
        {
            if (!functions.TryGetValue(op.Identifier as string, out opInter))
            {
                if (!properties.TryGetValue(op.Identifier as string, out opInter))
                {
                    if (ScriptEngine.AnalyzeDebug)
                    {
                        Debug.LogFormat("Can't interpret context operator {1} in {0}", block.Method.Name, op.Identifier);
                    }
                    return(null);
                }
            }
        }
        return(opInter);
    }
    public override void Interpret(Operator op, FunctionBlock block)
    {
        base.Interpret(op, block);
        if (interpret)
        {
            if (ScriptEngine.AnalyzeDebug)
            {
                Debug.LogFormat("Property {0} was interpreted by ContextPropertyInterpreter {1} already", op, propName);
            }
            return;
        }
        if (!(op.Context is Context))
        {
            if (ScriptEngine.AnalyzeDebug)
            {
                Debug.LogFormat("Property context switch {0} has no context", op);
            }
            return;
        }
        if (ops == null)
        {
            ops = Engine.GetPlugin <EventFunctionOperators> ();
        }
        var varName = op.Identifier as string;

        FunctionBlock contextBlock = new FunctionBlock(block, block.Method, block.Type);

        block.Statements.Add(contextBlock);
        DeclareVariableStatement declareVar = new DeclareVariableStatement();

        declareVar.Type      = propType;
        declareVar.Name      = "subContext" + DeclareVariableStatement.VariableId++;
        declareVar.IsContext = true;

        DeclareVariableStatement contextVar = block.FindStatement <DeclareVariableStatement> (v => v.Name == varName);

        if (contextVar == null)
        {
            var sCtx = block.FindStatement <ContextStatement> (v => v.InterpretInContext(op, block) != null && v.ContextVar.IsContext);
            contextVar = sCtx != null ? sCtx.ContextVar : null;
        }

        if (contextVar == null)
        {
            declareVar.InitExpression = String.Format("root.{0}", propName);
        }
        else
        {
            declareVar.InitExpression = String.Format("{1}.{0}", propName, contextVar.Name);
        }
        contextBlock.Statements.Add(declareVar);
        contextBlock.Statements.Add(new ContextStatement()
        {
            ContextVar         = declareVar,
            InterpretInContext = InterpretInContext
        });
        IfStatement isNotNull = new IfStatement();

        isNotNull.CheckExpression = String.Format("{0} != null", declareVar.Name);
        isNotNull.TrueBlock       = new FunctionBlock(contextBlock);
        contextBlock.Statements.Add(isNotNull);
        contextBlock = isNotNull.TrueBlock;
        foreach (var entry in (op.Context as Context).Entries)
        {
            //Debug.LogFormat("Interpreting {0} as part of {1} context", (entry as Operator).Identifier, op.Identifier);
            var subOp = entry as Operator;
            if (subOp == null)
            {
                continue;
            }
            FunctionOperatorInterpreter opInter = null;
            if ((opInter = ops.GetInterpreter(subOp, contextBlock)) == null)
            {
                if (!contextSwitches.TryGetValue(subOp.Identifier as string, out opInter))
                {
                    if (!functions.TryGetValue(subOp.Identifier as string, out opInter))
                    {
                        if (!properties.TryGetValue(subOp.Identifier as string, out opInter))
                        {
                            //Debug.LogFormat ("Can't interpret context operator {1} in {0}", block.Method.Name, subOp.Identifier);
                            continue;
                        }
                    }
                }
            }
            opInter.Interpret(subOp, contextBlock);
        }
    }
    public override void Interpret(Operator op, FunctionBlock block)
    {
        if (ops == null)
        {
            ops = Engine.GetPlugin <EventFunctionOperators> ();
        }
        FunctionBlock contextBlock = new FunctionBlock(block, block.Method, block.Type);

        block.Statements.Add(contextBlock);
        DeclareVariableStatement addVar = block.FindStatement <DeclareVariableStatement> (v => v.Name == op.Identifier as string);
        bool setContext = false;

        if (addVar != null && !addVar.IsContext)
        {
            setContext = addVar.IsContext = true;
        }
        if (addVar == null)
        {
            addVar = block.FindStatement <DeclareVariableStatement> (v => v.IsContext && v.Type == contextType);
        }
        //TODO: probably will fail
        DeclareVariableStatement contextVar = contextType == typeof(GameObject)? addVar : block.FindStatement <DeclareVariableStatement> (v => v.IsContext && v.Type == typeof(GameObject) && v != addVar);
        DeclareVariableStatement declareVar = null;

        if (addVar == null)
        {
            declareVar           = new DeclareVariableStatement();
            declareVar.Type      = contextType;
            declareVar.Name      = "subContext" + DeclareVariableStatement.VariableId++;
            declareVar.IsContext = true;

            if (contextVar == null)
            {
                declareVar.InitExpression = String.Format("({0})root.GetComponent(typeof({0}))", contextType);
            }
            else
            {
                declareVar.InitExpression = String.Format("({0}){1}.GetComponent(typeof({0}))", contextType, contextVar.Name);
            }
            contextBlock.Statements.Add(declareVar);
        }
        else
        {
            declareVar = addVar;
        }

        contextBlock.Statements.Add(new ContextStatement()
        {
            ContextVar         = declareVar,
            InterpretInContext = InterpretInContext
        });
        IfStatement isNotNull = new IfStatement();

        isNotNull.CheckExpression = String.Format("{0} != null", declareVar.Name);
        isNotNull.TrueBlock       = new FunctionBlock(contextBlock);
        contextBlock.Statements.Add(isNotNull);
        contextBlock = isNotNull.TrueBlock;
        contextBlock.Statements.Add(new DeclareVariableStatement()
        {
            Name = declareVar.Name, IsArg = true, IsContext = true, Type = declareVar.Type
        });
        foreach (var entry in (op.Context as Context).Entries)
        {
            var subOp = entry as Operator;
            if (subOp == null)
            {
                continue;
            }
            FunctionOperatorInterpreter opInter = null;

            if ((opInter = ops.GetInterpreter(subOp, contextBlock)) == null)
            {
                if (!contextSwitches.TryGetValue(subOp.Identifier as string, out opInter))
                {
                    if (!functions.TryGetValue(subOp.Identifier as string, out opInter))
                    {
                        if (!properties.TryGetValue(subOp.Identifier as string, out opInter))
                        {
                            Debug.LogWarningFormat("Can't interpret context operator {1} in {0}", block.Method.Name, subOp.Identifier);
                            continue;
                        }
                    }
                }
            }
            if (ScriptEngine.AnalyzeDebug)
            {
                Debug.LogFormat("Interpret {0} via {1}", subOp.Identifier, opInter);
            }
            opInter.Interpret(subOp, contextBlock);
        }

        if (setContext)
        {
            addVar.IsContext = false;
        }
    }
Esempio n. 7
0
    public FunctionOperatorInterpreter GetInterpreter(Operator op, FunctionBlock block)
    {
        FunctionOperatorInterpreter inter = null;

        if (op.Identifier as string == null)
        {
            exprInter.TransformScopedOperator(op, block);
            if (ScriptEngine.AnalyzeDebug)
            {
                Debug.Log("Transformed op " + op.ToString());
            }
        }
        interpreters.TryGetValue(op.Identifier as string, out inter);

        if (inter == null)
        {
            var customVar = block.FindStatement <DeclareVariableStatement> (v => v.Name == (op.Identifier as string));
            if (customVar == null)
            {
                foreach (var interPair in interpreters)
                {
                    if (interPair.Value.Match(op, block))
                    {
                        return(interPair.Value);
                    }
                }
                var context = block.FindStatement <ContextStatement> (c => (inter = c.InterpretInContext(op, block)) != null);
                if (ScriptEngine.AnalyzeDebug)
                {
                    if (context != null)
                    {
                        Debug.LogFormat("{0} is not an operator of context, found one {1}", op.Identifier, context.ContextVar);
                    }
                    else
                    {
                        Debug.LogWarningFormat("{0} is not an operator of context, not found one", op.Identifier);
                    }
                }

//				if (context != null)
//					inter = context.InterpretInContext (op, block);
                if (inter == null && op.Context is Expression)
                {
                    VarDeclareInterpreter declInter = new VarDeclareInterpreter();
                    declInter.Inter = exprInter;
                    inter           = declInter;
                }
            }
            else
            {
                if (op.Context is Expression)
                {
                    VarAssignInterpreter varInter = new VarAssignInterpreter();
                    varInter.Var   = customVar;
                    varInter.Inter = exprInter;
                    inter          = varInter;
                }
                else
                {
                    inter = switches.GetInterByType(customVar.Type);
                }
                //if(op.Context is Expression)
            }
        }

        if (ScriptEngine.AnalyzeDebug)
        {
            Debug.LogFormat("{0} - {1}", op, inter);
        }
        return(inter);
    }