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.DefaultValue != null) { compiler.Compile(AstUtils.Convert(param.DefaultValue, typeof(object))); defaultCount++; } } // 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, annotationCount, globalName)); }
protected override MSAst.Expression VisitExtension(MSAst.Expression node) { if (node == _globalContext) { return(PythonAst._globalContext); } // we need to re-write nested scoeps ScopeStatement scope = node as ScopeStatement; if (scope != null) { return(base.VisitExtension(VisitScope(scope))); } LambdaExpression lambda = node as LambdaExpression; if (lambda != null) { return(base.VisitExtension(new LambdaExpression((FunctionDefinition)VisitScope(lambda.Function)))); } GeneratorExpression generator = node as GeneratorExpression; if (generator != null) { return(base.VisitExtension(new GeneratorExpression((FunctionDefinition)VisitScope(generator.Function), generator.Iterable))); } // update the global get/set/raw gets variables PythonGlobalVariableExpression global = node as PythonGlobalVariableExpression; if (global != null) { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, global.Variable.Name, global.Variable.Kind == VariableKind.Local )); } // set covers sets and deletes var setGlobal = node as PythonSetGlobalVariableExpression; if (setGlobal != null) { if (setGlobal.Value == PythonGlobalVariableExpression.Uninitialized) { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, setGlobal.Global.Variable.Name, setGlobal.Global.Variable.Kind == VariableKind.Local ).Delete()); } else { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, setGlobal.Global.Variable.Name, setGlobal.Global.Variable.Kind == VariableKind.Local ).Assign(Visit(setGlobal.Value))); } } var rawValue = node as PythonRawGlobalValueExpression; if (rawValue != null) { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, rawValue.Global.Variable.Name, rawValue.Global.Variable.Kind == VariableKind.Local )); } return(base.VisitExtension(node)); }