Example #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;
 }
    public override void Interpret(Operator op, FunctionBlock block)
    {
        interpret = false;
        if (exprInter == null)
        {
            switches  = Engine.GetPlugin <ContextSwitchesPlugin> ();
            ops       = Engine.GetPlugin <EventFunctionOperators> ();
            exprInter = Engine.GetPlugin <ExpressionInterpreter> ();
        }

        var varName = op.Identifier as string;

        if (ScriptEngine.AnalyzeDebug)
        {
            Debug.Log(block);
        }
        var sCtx    = block.FindStatement <ContextStatement> (v => v.InterpretInContext(op, block) != null && v.ContextVar.IsContext || v.ContextVar.IsArg);
        var context = sCtx != null ? sCtx.ContextVar : null;

        if (ScriptEngine.AnalyzeDebug)
        {
            Debug.LogFormat("FOUND COUNTEXT {0} for {1}", context, op.Identifier);
        }
        if (listT == null)
        {
            if (!(op.Context is Expression))
            {
                return;
            }
            if (ScriptEngine.AnalyzeDebug)
            {
                Debug.Log("PROPERTY " + propName);
            }
            if (context == null)
            {
                block.Statements.Add(String.Format("root.{0} = ({2})({1});", propName, exprInter.InterpretExpression(op.Context as Expression, block).ExprString, TypeName.NameOf(propType)));
            }
            else
            {
                block.Statements.Add(String.Format("{2}.{0} = ({3})({1});", propName, exprInter.InterpretExpression(op.Context as Expression, block).ExprString, context.Name, TypeName.NameOf(propType)));
            }
        }
        else
        {
            Debug.Log("list of" + listT);
            ForStatement statement   = new ForStatement();
            string       listVarName = context == null ? "root." + propName : context.Name + "." + propName;
            string       iterName    = "i" + DeclareVariableStatement.VariableId++;
            statement.InsideExpr = String.Format("int {0} = 0; {1} != null && {0} < {1}.Count; {0}++", iterName,
                                                 listVarName);
            FunctionBlock repeatBlock = new FunctionBlock(block, block.Method, block.Type);
            statement.RepeatBlock = repeatBlock;
            block.Statements.Add(statement);
            Operator listVarOp = new Operator();

            DeclareVariableStatement listVar = new DeclareVariableStatement();
            listVar.Name           = "iter" + DeclareVariableStatement.VariableId++;
            listVar.IsContext      = true;
            listVar.IsNew          = true;
            listVar.Type           = listT;
            listVar.InitExpression = String.Format("{0}[{1}]", listVarName, iterName);

            statement.RepeatBlock.Statements.Add(listVar);
            var inter = switches.GetInterByType(listT);
            //Debug.Log(inter);
            inter.Interpret(op, repeatBlock);
        }
        interpret = true;
    }
Example #3
0
 public override void Init()
 {
     switches  = Engine.GetPlugin <ContextSwitchesPlugin> ();
     exprInter = Engine.GetPlugin <ExpressionInterpreter> ();
 }
    public override void Interpret(InternalDSL.Operator op, FunctionBlock block)
    {
        if (ops == null)
        {
            ctxs      = Engine.GetPlugin <ContextSwitchesPlugin> ();
            ops       = Engine.GetPlugin <EventFunctionOperators> ();
            exprInter = Engine.GetPlugin <ExpressionInterpreter> ();
        }
        if (op.Args.Count > 0)
        {
            var varName = ((op.Args [0].Operands [0] as ExprAtom).Content as Scope).Parts [0] as string;
            if (op.Context is Expression)
            {
                //cache(some_name) = expression
//				var varDecl = block.FindStatement<DeclareVariableStatement> (v => v.Name == varName);
//				varDecl.IsArg = true;

                var exprValue = exprInter.InterpretExpression(op.Context as Expression, block);
                Debug.Log(exprValue.ExprString);
                var field = new CodeMemberField(exprValue.Type, varName);
                field.UserData.Add("type", exprValue.Type);
                block.Type.Members.Add(field);
                block.Statements.Add(String.Format("{0} = {1};", varName, exprValue.ExprString));
                block.Statements.Add(new DeclareVariableStatement()
                {
                    Name = varName, IsArg = true, Type = exprValue.Type
                });
                //var varDecl = block.FindStatement<DeclareVariableStatement> (v => v.Name == varName);
            }
            else
            {
                //cache(some_name, scope) = { ... }
                var exprValue = exprInter.InterpretExpression(op.Args [1], block);
                var field     = new CodeMemberField(exprValue.Type, varName);
                field.UserData.Add("type", exprValue.Type);
                block.Type.Members.Add(field);
                DeclareVariableStatement cachedVar = new DeclareVariableStatement();
                cachedVar.Name      = varName;
                cachedVar.IsContext = true;
                cachedVar.Type      = exprValue.Type;
                cachedVar.IsArg     = true;
                block.Statements.Add(cachedVar);
                block.Statements.Add(String.Format("{0} = {1};", varName, exprValue.ExprString));
                block.Statements.Add(new DeclareVariableStatement()
                {
                    Name = varName, IsArg = true, Type = exprValue.Type
                });
                var inter = ctxs.GetInterByType(exprValue.Type);
                inter.Interpret(op, block);
            }
        }
        else
        {
            //cache = some_name

            var varName = (((op.Context as Expression).Operands [0] as ExprAtom).Content as Scope).Parts [0] as string;
            var varDecl = block.FindStatement <DeclareVariableStatement> (v => v.Name == varName);
            //varDecl.IsArg = true;
            varDecl.IsDeclaration = false;

            var field = new CodeMemberField(varDecl.Type, varDecl.Name);
            field.UserData.Add("type", varDecl.Type);
            block.Type.Members.Add(field);
            //block.Statements.Add (String.Format ("this.{0} = {0};", varName));
        }

        //new CodeMemberField()
        //block.Type.Members.Add ();
    }
Example #5
0
    public override void Interpret(Operator op, FunctionBlock block)
    {
        if (ops == null)
        {
            ops      = Engine.GetPlugin <EventFunctionOperators> ();
            switches = Engine.GetPlugin <ContextSwitchesPlugin> ();
            var cmps = Engine.FindTypesCastableTo <MonoBehaviour> ();
            foreach (var cmp in cmps)
            {
                components.Add(NameTranslator.ScriptNameFromCSharp(cmp.Name), cmp);
            }
        }
        //add = cmp_type - no args, context is Expression
        //add(var) = cmp_type - 1 arg, context is Expression
        //add(cmp_type) = { ... } - 1 arg, context is Context
        //add(var, cmp_type) = { ... } - 2 args, context is Context
        DeclareVariableStatement cmpVar     = null;
        DeclareVariableStatement contextVar = block.FindStatement <DeclareVariableStatement> (v => (v.IsContext | v.IsArg) && v.Type == typeof(GameObject));
        string ctxVar      = contextVar == null ? "root" : contextVar.Name;
        string cmpTypeName = null;

        try
        {
            if (op.Args.Count == 0)
            {
                var expr = op.Context as Expression;
                if (expr != null)
                {
                    var cmpType = (((op.Context as Expression).Operands[0] as ExprAtom).Content as Scope).Parts[0] as string;
                    block.Statements.Add(string.Format("{0}.AddComponent<{1}>();", ctxVar, components[cmpType]));
                }
            }
            else if (op.Args.Count == 1)
            {
                var ctx  = op.Context as Context;
                var expr = op.Context as Expression;
                if (expr != null)
                {
                    cmpTypeName = (((op.Context as Expression).Operands[0] as ExprAtom).Content as Scope).Parts[0] as string;
                    var varName = ((op.Args[0].Operands[0] as ExprAtom).Content as Scope).Parts[0] as string;
                    var cmpType = components[cmpTypeName];
                    cmpVar = new DeclareVariableStatement();
                    cmpVar.InitExpression = string.Format("{0}.AddComponent<{1}>();", ctxVar, cmpType);
                    cmpVar.Type           = cmpType;
                    cmpVar.Name           = varName;
                    cmpVar.IsNew          = true;
                    cmpVar.IsContext      = true;
                    block.Statements.Add(cmpVar);
                }
                else if (ctx != null)
                {
                    cmpTypeName = ((op.Args[0].Operands[0] as ExprAtom).Content as Scope).Parts[0] as string;
                    var cmpType = components[cmpTypeName];
                    cmpVar = new DeclareVariableStatement();
                    cmpVar.InitExpression = string.Format("{0}.AddComponent<{1}>();", ctxVar, cmpType);
                    cmpVar.Type           = cmpType;
                    cmpVar.IsContext      = true;
                    cmpVar.IsNew          = true;
                    cmpVar.Name           = "ContextVar" + DeclareVariableStatement.VariableId++;
                    block.Statements.Add(cmpVar);
                    switches.GetInterByType(cmpType).Interpret(op, block);
                }
            }
            else if (op.Args.Count == 2)
            {
                var ctx = op.Context as Context;
                if (ctx != null)
                {
                    cmpTypeName = ((op.Args[1].Operands[0] as ExprAtom).Content as Scope).Parts[0] as string;
                    var varName = ((op.Args[0].Operands[0] as ExprAtom).Content as Scope).Parts[0] as string;
                    var cmpType = components[cmpTypeName];
                    cmpVar = new DeclareVariableStatement();
                    cmpVar.InitExpression = string.Format("{0}.AddComponent<{1}>();", ctxVar, cmpType);
                    cmpVar.Type           = cmpType;
                    cmpVar.IsContext      = true;
                    cmpVar.IsNew          = true;
                    cmpVar.Name           = varName;
                    block.Statements.Add(cmpVar);
                    switches.GetInterByType(cmpType).Interpret(op, block);
                }
            }


            var entVar = block.FindStatement <DeclareVariableStatement>(v => v.ctxEntity == ctxVar && v.Type == typeof(Entity));
            if (entVar == null)
            {
                entVar           = new DeclareVariableStatement();
                entVar.Name      = "EntVar" + DeclareVariableStatement.VariableId++;
                entVar.Type      = typeof(Entity);
                entVar.ctxEntity = ctxVar;

                entVar.InitExpression = String.Format("(Entity){0}.GetComponent(typeof(Entity));", ctxVar);
                block.Statements.Add(entVar);
                var updateSt = new StatementStringContainer(String.Format("if({0} != null) {0}.ComponentAdded();", entVar.Name));
                entVar.Cnt = updateSt;
                block.Statements.Add(updateSt);
            }
            else
            {
                var updateSt = new StatementStringContainer(entVar.Cnt.Data);
                entVar.Cnt.Data = "";
                entVar.Cnt      = updateSt;
                block.Statements.Add(updateSt);
            }

            if (cmpVar != null)
            {
                cmpVar.IsContext = false;
            }
        }
        catch (KeyNotFoundException e)
        {
            Debug.LogErrorFormat("Can't create {0} in {1}", cmpTypeName, op);
        }
    }
Example #6
0
    public override void Interpret(Operator op, FunctionBlock block)
    {
        interpret = false;
        if (exprInter == null)
        {
            switches  = Engine.GetPlugin <ContextSwitchesPlugin> ();
            ops       = Engine.GetPlugin <EventFunctionOperators> ();
            exprInter = Engine.GetPlugin <ExpressionInterpreter> ();
        }

        var varName = op.Identifier as string;

        if (ScriptEngine.AnalyzeDebug)
        {
            Debug.Log(block);
        }
        var sCtx = block.FindStatement <ContextStatement> (v => v.InterpretInContext(op, block) != null && (v.ContextVar.IsContext || v.ContextVar.IsArg));

        var context = sCtx != null ? sCtx.ContextVar : null;

        if (ScriptEngine.AnalyzeDebug)
        {
            Debug.LogFormat("FOUND COUNTEXT {0} for {1}", context, op.Identifier);
        }
        if (listT == null)
        {
            if (op.Context is Expression)
            {
                if (ScriptEngine.AnalyzeDebug)
                {
                    Debug.Log("PROPERTY " + propName);
                }

                if (context == null)
                {
                    block.Statements.Add(String.Format("root.{0} = ({2})({1});", propName, exprInter.InterpretExpression(op.Context as Expression, block, propType).ExprString, TypeName.NameOf(propType)));
                }
                else
                {
                    block.Statements.Add(String.Format("{2}.{0} = ({3})({1});", propName, exprInter.InterpretExpression(op.Context as Expression, block, propType).ExprString, context.Name, TypeName.NameOf(propType)));
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(propType))
            {
                Debug.Log("LAMBDA!");
                //Interpret as lambda
                LambdaStatement lambda = new LambdaStatement();
                lambda.DelegateType = propType;
                var method = propType.GetMethod("Invoke");
                lambda.Params = method.GetParameters();
                lambda.Name   = "Lambda" + DeclareVariableStatement.VariableId++;
                block.Statements.Add(lambda);
                lambda.Block = new FunctionBlock(block);

                //DeclareVariableStatement lastVar = null;
                foreach (var param in lambda.Params)
                {
                    var argVar = new DeclareVariableStatement();
                    //	lastVar = argVar;
                    argVar.Name  = param.Name;
                    argVar.IsArg = true;
                    argVar.Type  = param.ParameterType;
                    lambda.Block.Statements.Add(argVar);
                }
                //if (lastVar != null)
                //	lastVar.IsContext = true;

                var  retType   = lambda.DelegateType.GetMethod("Invoke").ReturnType;
                bool hasReturn = false;
                if (retType != null && retType != typeof(void))
                {
                    hasReturn = true;
                    lambda.Block.Statements.Add(new DeclareVariableStatement()
                    {
                        Name           = "return_value",
                        InitExpression = string.Format("default({0})", retType),
                        IsReturn       = true,
                        Type           = retType
                    });
                }

                foreach (var entry in (op.Context as Context).Entries)
                {
                    var subOp = entry as Operator;
                    ops.GetInterpreter(subOp, lambda.Block).Interpret(subOp, lambda.Block);
                }
                if (hasReturn)
                {
                    lambda.Block.Statements.Add("return return_value;");
                }
                if (context == null)
                {
                    block.Statements.Add(String.Format("root.{0} = ({2})({1});", propName, lambda.Name, TypeName.NameOf(propType)));
                }
                else
                {
                    block.Statements.Add(String.Format("{2}.{0} = ({3})({1});", propName, lambda.Name, context.Name, TypeName.NameOf(propType)));
                }
            }
            else
            {
                return;
            }
        }
        else
        {
            //Debug.Log ("list of" + listT);
            ForStatement             statement      = new ForStatement();
            string                   listVarContent = context == null ? "root." + propName : context.Name + "." + propName;
            string                   listVarName    = "list" + DeclareVariableStatement.VariableId++;
            DeclareVariableStatement listVar        = new DeclareVariableStatement();
            listVar.IsTemp         = true;
            listVar.Name           = listVarName;
            listVar.InitExpression = listVarContent;
            listVar.Type           = propType;

            block.Statements.Add(listVar);
            string iterName = "i" + DeclareVariableStatement.VariableId++;
            statement.InsideExpr = String.Format("int {0} = 0; {1} != null && {0} < {1}.Count; {0}++", iterName,
                                                 listVarName);
            FunctionBlock repeatBlock = new FunctionBlock(block, block.Method, block.Type);
            statement.RepeatBlock = repeatBlock;
            block.Statements.Add(statement);
            Operator listVarOp = new Operator();

            DeclareVariableStatement iterVar = new DeclareVariableStatement();
            iterVar.Name           = "iter" + DeclareVariableStatement.VariableId++;
            iterVar.IsContext      = true;
            iterVar.IsNew          = true;
            iterVar.Type           = listT;
            iterVar.IsListIter     = true;
            iterVar.InitExpression = String.Format("{0}[{1}]", listVarName, iterName);

            statement.RepeatBlock.Statements.Add(iterVar);
            var inter = switches.GetInterByType(listT);
            //Debug.Log(inter);
            inter.Interpret(op, repeatBlock);
        }
        interpret = true;
    }