Exemplo n.º 1
0
        public AplusScope(AplusScope parent,
            string name,
            Aplus runtime = null,
            DLR.ParameterExpression runtimeParam = null,
            DLR.ParameterExpression moduleParam = null,
            DLR.LabelTarget returnTarget = null,
            bool isEval = false,
            bool isMethod = false,
            bool isAssignment = false)
        {
            this.parent = parent;
            this.name = name;
            this.runtime = runtime;
            this.runtimeParam = runtimeParam;
            this.moduleParam = moduleParam;

            this.returnTarget = returnTarget;

            this.variables = new Dictionary<string, DLR.ParameterExpression>();

            this.callbackInfo = new CallbackInfoStorage();

            this.iseval = isEval;

            this.ismethod = isMethod;
            this.isAssignment = isAssignment;

            InheritProperties(parent);
        }
Exemplo n.º 2
0
        public DLR.Expression FindIdentifier(string name)
        {
            AplusScope currentScope = this;

            DLR.ParameterExpression result;
            while (currentScope != null)
            {
                if (currentScope.Variables.TryGetValue(name, out result))
                {
                    return(result);
                }

                currentScope = currentScope.parent;
            }
            return(null);
        }
Exemplo n.º 3
0
        public DLR.Expression <System.Func <Runtime.Aplus, AType> > ParseToLambda(string code)
        {
            AplusScope scope = new AplusScope(null, "__top__", this.aplus,
                                              DLR.Expression.Parameter(typeof(Aplus), "__aplusRuntime__"),
                                              DLR.Expression.Parameter(typeof(DYN.IDynamicMetaObjectProvider), "__module__")
                                              );

            FunctionInformation funcionInfo = new FunctionInformation(this.aplus.CurrentContext);

            if (this.aplus.Context != null)
            {
                funcionInfo.LoadInfo((this.aplus.Context as Scope).Storage as ScopeStorage);
            }

            DLR.Expression codebody = null;
            AST.Node       tree     = Compiler.Parse.String(code, this.aplus.LexerMode, funcionInfo);

            if (tree == null)
            {
                codebody = DLR.Expression.Constant(null);
            }
            else
            {
                codebody = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { scope.ModuleExpression },
                    DLR.Expression.Assign(
                        scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context")
                        ),
                    tree.Generate(scope)
                    );
            }

            DLR.Expression <System.Func <Aplus, AType> > method =
                DLR.Expression.Lambda <Func <Aplus, AType> >(
                    codebody,
                    scope.RuntimeExpression
                    );
            return(method);
        }
Exemplo n.º 4
0
        public DLR.Expression<System.Func<Runtime.Aplus, AType>> ParseToLambda(string code)
        {
            AplusScope scope = new AplusScope(null, "__top__", this.aplus,
                DLR.Expression.Parameter(typeof(Aplus), "__aplusRuntime__"),
                DLR.Expression.Parameter(typeof(DYN.IDynamicMetaObjectProvider), "__module__")
            );

            FunctionInformation funcionInfo = new FunctionInformation(this.aplus.CurrentContext);
            if (this.aplus.Context != null)
            {
                funcionInfo.LoadInfo((this.aplus.Context as Scope).Storage as ScopeStorage);
            }

            DLR.Expression codebody = null;
            AST.Node tree = Compiler.Parse.String(code, this.aplus.LexerMode, funcionInfo);

            if (tree == null)
            {
                codebody = DLR.Expression.Constant(null);
            }
            else
            {
                codebody = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { scope.ModuleExpression },
                    DLR.Expression.Assign(
                        scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context")
                    ),
                    tree.Generate(scope)
                );
            }

            DLR.Expression<System.Func<Aplus, AType>> method =
                DLR.Expression.Lambda<Func<Aplus, AType>>(
                    codebody,
                    scope.RuntimeExpression
                );
            return method;
        }
Exemplo n.º 5
0
        private void InheritProperties(AplusScope parentScope)
        {
            if (parentScope == null)
            {
                return;
            }

            this.iseval |= parentScope.iseval;
            this.ismethod |= parentScope.IsMethod;
            this.isAssignment |= parentScope.IsAssignment;
            this.assignDone = parent.assignDone;
        }
Exemplo n.º 6
0
        internal static DLR.Expression<Func<Aplus, AType>> BuildExecuteMethod(
            string sourceCode, Aplus environment
        )
        {
            DLR.Expression codebody;
            // TODO: fix the third function info argument
            AplusCore.Compiler.AST.Node tree = Compiler.Parse.String(sourceCode, environment.LexerMode, null);

            AplusScope scope = new AplusScope(null, "__EVAL__", environment,
                DLR.Expression.Parameter(typeof(Aplus), "__EVAL_RUNTIME__"),
                DLR.Expression.Parameter(typeof(DYN.IDynamicMetaObjectProvider), "__EVAL_MODULE__"),
                //DLR.Expression.Parameter(typeof(Aplus), "__EVAL_ENVIRONMENT__"),
                DLR.Expression.Label(typeof(AType), "__EVAL_EXIT__"),
                isEval: true
            );

            if (tree == null)
            {
                codebody = DLR.Expression.Constant(null);
            }
            else if (environment.FunctionScope != null)
            {
                AplusScope functionScope = new AplusScope(scope, "__EVAL_IN_FUNCTION__",
                    moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), "__EVAL_FUNCTION_SCOPE__"),
                    returnTarget: scope.ReturnTarget,
                    isMethod: true
                );

                codebody = DLR.Expression.Block(
                    new DLR.ParameterExpression[] {
                        //scope.RuntimeExpression,         // runtime
                        scope.ModuleExpression,          // root context
                        functionScope.ModuleExpression   // Function local scope
                    },
                    //DLR.Expression.Assign(
                    //    scope.RuntimeExpression, scope.RuntimeExpression
                    //),
                    DLR.Expression.Assign(
                        scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context")
                    ),
                    DLR.Expression.Assign(
                        functionScope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "FunctionScope")
                    ),
                    DLR.Expression.Label(
                        scope.ReturnTarget,
                        tree.Generate(functionScope)
                    )
                );
            }
            else
            {
                codebody = DLR.Expression.Block(
                    new DLR.ParameterExpression[] {
                        //scope.RuntimeExpression,         // runtime
                        scope.ModuleExpression          // root context
                    },
                    //DLR.Expression.Assign(
                    //   scope.RuntimeExpression, scope.RuntimeExpression
                    //),
                    DLR.Expression.Assign(
                        scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context")
                    ),
                    DLR.Expression.Label(
                        scope.ReturnTarget,
                        tree.Generate(scope)
                    )
               );
            }

            DLR.Expression<Func<Aplus, AType>> lambda = DLR.Expression.Lambda<Func<Aplus, AType>>(
                codebody,
                scope.GetRuntimeExpression()
            );

            return lambda;
        }