Exemplo n.º 1
0
 public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global) {
     return new LookupGlobalVariable(
         globalContext,
         variable.Name,
         variable.Kind == VariableKind.Local
     );
 }
Exemplo n.º 2
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast) {
            // create the CodeContext
            PythonGlobal[] globalArray = new PythonGlobal[_globals.Count];

            // now fill in the dictionary and create the array
            foreach (var global in _globals) {
                SymbolId globalName = SymbolTable.StringToId(global.Key);
                
                globalArray[global.Value.Index] = _globalVals[globalName];
            }
            
            _array.Array = globalArray;

            // finally build the funcion that's closed over the array and
            string name = ((PythonCompilerOptions)context.Options).ModuleName ?? "<unnamed>";
            var func = Ast.Lambda<Func<object>>(
                Ast.Block(
                    new[] { _globalArray, _globalContext },
                    Ast.Assign(_globalArray, Ast.Constant(globalArray)),
                    Ast.Assign(_globalContext, Ast.Constant(_context)),
                    body
                ),
                name,
                new MSAst.ParameterExpression[0]
            );

            return new RuntimeScriptCode(context, func, ast, _context);
        }
Exemplo n.º 3
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast, Dictionary<int, bool> handlerLocations, Dictionary<int, Dictionary<int, bool>> loopAndFinallyLocations) {
            // create the CodeContext
            PythonGlobal[] globalArray = new PythonGlobal[_globals.Count];

            // now fill in the dictionary and create the array
            foreach (var global in _globals) {
                globalArray[global.Value.Index] = _globalVals[global.Key];
            }
            
            _array.Array = globalArray;

            // finally build the funcion that's closed over the array and
            string name = ((PythonCompilerOptions)context.Options).ModuleName ?? "<unnamed>";
            var func = Ast.Lambda<Func<FunctionCode, object>>(
                Ast.Block(
                    new[] { _globalArray, _globalContext },
                    Ast.Assign(_globalArray, Ast.Constant(globalArray)),
                    Ast.Assign(_globalContext, Ast.Constant(_context)),
                    Utils.Convert(body, typeof(object))
                ),
                name,
                new [] { AstGenerator._functionCode }
            );

            return new RuntimeScriptCode(context, func, ast, _context);
        }
        public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global) {
            Assert.NotNull(global);

            return new PythonGlobalVariableExpression(
                Ast.ArrayIndex(
                    PythonAst._globalArray,
                    Ast.Constant(arrayIndex)
                ),
                variable,
                global
            );
        }
Exemplo n.º 5
0
            public FunctionDefinitionInstruction(CodeContext context, FunctionDefinition/*!*/ definition, int defaultCount, PythonGlobal name) {
                Assert.NotNull(definition);

                _context = context;
                _defaultCount = defaultCount;
                _def = definition;
                _name = name;
            }
Exemplo n.º 6
0
        internal override void FinishBind(PythonNameBinder binder) {
            _contextInfo = CompilationMode.GetContext();

            // create global variables for compiler context.
            PythonGlobal[] globalArray = new PythonGlobal[Variables == null ? 0 : Variables.Count];
            Dictionary<string, PythonGlobal> globals = new Dictionary<string, PythonGlobal>();
            GlobalDictionaryStorage storage = new GlobalDictionaryStorage(globals, globalArray);
            var modContext = _modContext = new ModuleContext(new PythonDictionary(storage), PyContext);

            if (_mode == CompilationMode.ToDisk) {
                _arrayExpression = _globalArray;
            } else {
                var newArray = new ConstantExpression(globalArray);
                newArray.Parent = this;
                _arrayExpression = newArray;
            }

            if (Variables != null) {
                int globalIndex = 0;
                foreach (PythonVariable variable in Variables.Values) {
                    PythonGlobal global = new PythonGlobal(modContext.GlobalContext, variable.Name);
                    _globalVariables[variable] = CompilationMode.GetGlobal(GetGlobalContext(), globals.Count, variable, global);
                    globalArray[globalIndex++] = globals[variable.Name] = global;
                }
            }

            CompilationMode.PublishContext(modContext.GlobalContext, _contextInfo);
        }
            public FunctionDefinitionInstruction(CodeContext context, FunctionDefinition /*!*/ definition, int defaultCount, PythonGlobal name)
            {
                Assert.NotNull(definition);

                _context      = context;
                _defaultCount = defaultCount;
                _def          = definition;
                _name         = name;
            }
Exemplo n.º 8
0
        public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal /*!*/ global)
        {
            Assert.NotNull(global);

            lock (StorageData.Globals) {
                ConstantInfo info = NextGlobal(0);

                StorageData.GlobalStorageType(StorageData.GlobalCount + 1);

                PublishWorker(StorageData.GlobalCount, StorageData.GlobalTypes, info, global, StorageData.Globals);

                StorageData.GlobalCount += 1;

                return(new PythonGlobalVariableExpression(info.Expression, variable, global));
            }
        }
Exemplo n.º 9
0
        public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global)
        {
            Assert.NotNull(global);

            return(new PythonGlobalVariableExpression(
                       Ast.ArrayIndex(
                           PythonAst._globalArray,
                           Ast.Constant(arrayIndex)
                           ),
                       variable,
                       global
                       ));
        }
Exemplo n.º 10
0
 protected internal override void Initialize(CodeContext codeContext, Dictionary <string, PythonGlobal> globals)
 {
     _test_attr = globals["test_attr"];
     _test_min  = globals["min"];
     base.Initialize(codeContext, globals);
 }
Exemplo n.º 11
0
        private void CreateFunctionInstructions(LightCompiler compiler)
        {
            // emit context if we have a special local context
            CodeContext globalContext = null;

            compiler.Compile(Parent.LocalContext);

            // emit name if necessary
            PythonGlobalVariableExpression name = GetVariableExpression(_nameVariable) as PythonGlobalVariableExpression;
            PythonGlobal globalName             = null;

            if (name == null)
            {
                compiler.Compile(((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue());
            }
            else
            {
                globalName = name.Global;
            }

            // emit defaults
            int defaultCount = 0;

            for (int i = _parameters.Length - 1; i >= 0; i--)
            {
                var param = _parameters[i];
                if (param.Kind == ParameterKind.Normal && param.DefaultValue != null)
                {
                    compiler.Compile(AstUtils.Convert(param.DefaultValue, typeof(object)));
                    defaultCount++;
                }
            }

            // emit kwdefaults
            int kwdefaultCount = 0;

            for (int i = _parameters.Length - 1; i >= 0; i--)
            {
                var param = _parameters[i];
                if (param.Kind == ParameterKind.KeywordOnly && param.DefaultValue != null)
                {
                    compiler.Compile(AstUtils.Convert(param.DefaultValue, typeof(object)));
                    compiler.Compile(AstUtils.Constant(param.Name, typeof(string)));
                    kwdefaultCount++;
                }
            }

            // emit annotations
            int annotationCount = 0;

            if (ReturnAnnotation != null)
            {
                compiler.Compile(AstUtils.Convert(ReturnAnnotation, typeof(object)));
                compiler.Compile(AstUtils.Constant("return", typeof(string)));
                annotationCount++;
            }

            for (int i = _parameters.Length - 1; i >= 0; i--)
            {
                var param = _parameters[i];
                if (param.Annotation != null)
                {
                    compiler.Compile(AstUtils.Convert(param.Annotation, typeof(object)));
                    compiler.Compile(AstUtils.Constant(param.Name, typeof(string)));
                    annotationCount++;
                }
            }

            compiler.Instructions.Emit(new FunctionDefinitionInstruction(globalContext, this, defaultCount, kwdefaultCount, annotationCount, globalName));
        }
Exemplo n.º 12
0
 protected override MSAst.Expression/*!*/ GetGlobal(string/*!*/ name, AstGenerator/*!*/ ag, bool isLocal) {
     SymbolId idname = SymbolTable.StringToId(name);
     PythonGlobal global = _globalVals[idname] = new PythonGlobal(_context, idname);
     return new PythonGlobalVariableExpression(GetGlobalInfo(name).Expression, global);
 }
Exemplo n.º 13
0
 public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global)
 {
     return(new LookupGlobalVariable(
                globalContext,
                variable.Name,
                variable.Kind == VariableKind.Local
                ));
 }
Exemplo n.º 14
0
 protected override MSAst.Expression/*!*/ GetGlobal(string/*!*/ name, AstGenerator/*!*/ ag, bool isLocal) {
     PythonGlobal global = _globalVals[name] = new PythonGlobal(_context, name);
     return new PythonGlobalVariableExpression(GetGlobalInfo(name).Expression, global);
 }