public override MSAst.Expression Reduce() { if (_statements.Length == 0) { return(GlobalParent.AddDebugInfoAndVoid(AstUtils.Empty(), Span)); } ReadOnlyCollectionBuilder <MSAst.Expression> statements = new ReadOnlyCollectionBuilder <MSAst.Expression>(); int curStart = -1; foreach (var statement in _statements) { // CPython debugging treats multiple statements on the same line as a single step, we // match that behavior here. int newline = GlobalParent.IndexToLocation(statement.StartIndex).Line; if (newline == curStart) { statements.Add(new DebugInfoRemovalExpression(statement, curStart)); } else { if (statement.CanThrow && newline != -1) { statements.Add(UpdateLineNumber(newline)); } statements.Add(statement); } curStart = newline; } return(Ast.Block(statements.ToReadOnlyCollection())); }
private MSAst.Expression ReduceWorker(bool optimizeDynamicConvert) { MSAst.Expression result; if (_else != null) { result = _else; } else { result = AstUtils.Empty(); } // Now build from the inside out int i = _tests.Length; while (i-- > 0) { IfStatementTest ist = _tests[i]; result = GlobalParent.AddDebugInfoAndVoid( Ast.Condition( optimizeDynamicConvert ? TransformAndDynamicConvert(ist.Test, typeof(bool)) : GlobalParent.Convert(typeof(bool), Microsoft.Scripting.Actions.ConversionResultKind.ExplicitCast, ist.Test), TransformMaybeSingleLineSuite(ist.Body, ist.Test.Start), result ), new SourceSpan(ist.Start, ist.Header) ); } return(result); }
public override Ast Reduce() { var left = Left; var right = Right; Type t = Type; var tmp = Variable(t, "__all__"); return(Block( new[] { tmp }, Condition( GlobalParent.Convert( typeof(bool), ConversionResultKind.ExplicitCast, Assign( tmp, AstUtils.Convert( left, t ) ) ), AstUtils.Convert( right, t ), tmp ) )); }
public override MSAst.Expression Reduce() { Debug.Assert(_variable != null, "Shouldn't be called by lambda expression"); MSAst.Expression function = MakeFunctionExpression(); return(GlobalParent.AddDebugInfoAndVoid(AssignValue(Parent.GetVariableExpression(_variable), function), new SourceSpan(Start, Header))); }
public override MSAst.Expression Reduce() { UnicodeWrapper wrapper; if (_value == Ellipsis.Value) { return(Ast.Property( null, typeof(PythonOps).GetProperty("Ellipsis") )); } else if (_value is bool) { if ((bool)_value) { return(Ast.Field(null, typeof(ScriptingRuntimeHelpers).GetField("True"))); } else { return(Ast.Field(null, typeof(ScriptingRuntimeHelpers).GetField("False"))); } } else if ((wrapper = _value as UnicodeWrapper) != null) { return(GlobalParent.Constant(wrapper.Value)); } return(GlobalParent.Constant(_value)); }
public override MSAst.Expression Reduce() { MSAst.MethodCallExpression call; if (_locals == null && _globals == null) { // exec code call = Ast.Call( AstMethods.UnqualifiedExec, Parent.LocalContext, AstUtils.Convert(_code, typeof(object)) ); } else { // exec code in globals [ , locals ] // We must have globals now (locals is last and may be absent) Debug.Assert(_globals != null); call = Ast.Call( AstMethods.QualifiedExec, Parent.LocalContext, AstUtils.Convert(_code, typeof(object)), TransformAndDynamicConvert(_globals, typeof(PythonDictionary)), TransformOrConstantNull(_locals, typeof(object)) ); } return(GlobalParent.AddDebugInfo(call, Span)); }
public override MSAst.Expression Reduce() { UnicodeWrapper wrapper; if (_value == Ellipsis.Value) { return(EllipsisExpr); } else if (_value is bool) { if ((bool)_value) { return(TrueExpr); } else { return(FalseExpr); } } else if ((wrapper = _value as UnicodeWrapper) != null) { return(GlobalParent.Constant(wrapper.Value)); } return(GlobalParent.Constant(_value)); }
public override MSAst.Expression Reduce() { return(GlobalParent.Get( _name, _target )); }
public override MSAst.Expression Reduce() { ReadOnlyCollectionBuilder <MSAst.Expression> statements = new ReadOnlyCollectionBuilder <MSAst.Expression>(); for (int i = 0; i < _names.Length; i++) { statements.Add( // _references[i] = PythonOps.Import(<code context>, _names[i]) GlobalParent.AddDebugInfoAndVoid( AssignValue( Parent.GetVariableExpression(_variables[i]), LightExceptions.CheckAndThrow( Expression.Call( _asNames[i] == null ? AstMethods.ImportTop : AstMethods.ImportBottom, Parent.LocalContext, // 1st arg - code context AstUtils.Constant(_names[i].MakeString()), // 2nd arg - module name AstUtils.Constant(_forceAbsolute ? 0 : -1) // 3rd arg - absolute or relative imports ) ) ), _names[i].Span ) ); } statements.Add(AstUtils.Empty()); return(GlobalParent.AddDebugInfo(Ast.Block(statements.ToReadOnlyCollection()), Span)); }
public override MSAst.Expression Reduce() { // If debugging is off, return empty statement if (Optimize) { return(AstUtils.Empty()); } // Transform into: // if (_test) { // } else { // RaiseAssertionError(_message); // } return(GlobalParent.AddDebugInfoAndVoid( AstUtils.Unless( // if TransformAndDynamicConvert(_test, typeof(bool)), // _test Ast.Call( // else branch AstMethods.RaiseAssertionError, Parent.LocalContext, TransformOrConstantNull(_message, typeof(object)) ) ), Span )); }
internal override MSAst.Expression TransformSet(SourceSpan span, MSAst.Expression right, PythonOperationKind op) { if (op == PythonOperationKind.None) { return(GlobalParent.AddDebugInfoAndVoid( GlobalParent.Set( _name, _target, right ), span )); } else { MSAst.ParameterExpression temp = Ast.Variable(typeof(object), "inplace"); return(GlobalParent.AddDebugInfo( Ast.Block( new[] { temp }, Ast.Assign(temp, _target), SetMemberOperator(right, op, temp), AstUtils.Empty() ), Span.Start, span.End )); } }
public override MSAst.Expression Reduce() { if (Parent.IsGeneratorMethod) { if (_expression != null) { // Statements can't return null, so return a rethrow. // Callers should detecet the ag.AddError and avoid trying to execute the tree, // but if they accidentally do, use Throw instead of empty so that // we'll get an exception. return(Ast.Throw( Ast.New( typeof(InvalidOperationException).GetConstructor(Type.EmptyTypes) ) )); } return(GlobalParent.AddDebugInfo(AstUtils.YieldBreak(GeneratorLabel), Span)); } return(GlobalParent.AddDebugInfo( Ast.Return( FunctionDefinition._returnLabel, TransformOrConstantNull(_expression, typeof(object)) ), Span )); }
public override MSAst.Expression Reduce() { MSAst.Expression raiseExpression; if (_type == null && _value == null && _traceback == null) { raiseExpression = Ast.Call( AstMethods.MakeRethrownException, Parent.LocalContext ); if (!InFinally) { raiseExpression = Ast.Block( UpdateLineUpdated(true), raiseExpression ); } } else { raiseExpression = Ast.Call( AstMethods.MakeException, Parent.LocalContext, TransformOrConstantNull(_type, typeof(object)), TransformOrConstantNull(_value, typeof(object)), TransformOrConstantNull(_traceback, typeof(object)) ); } return(GlobalParent.AddDebugInfo( Ast.Throw(raiseExpression), Span )); }
public override MSAst.Expression Reduce() { MSAst.Expression left = _left; MSAst.Expression right = _right; Type t = Type; MSAst.ParameterExpression tmp = Ast.Variable(t, "__all__"); return(Ast.Block( new[] { tmp }, Ast.Condition( GlobalParent.Convert( typeof(bool), ConversionResultKind.ExplicitCast, Ast.Assign( tmp, AstUtils.Convert( left, t ) ) ), tmp, AstUtils.Convert( right, t ) ) )); }
public override MSAst.Expression Reduce() { return(GlobalParent.Operation( typeof(object), PythonOperatorToOperatorString(_op), _expression )); }
internal override MSAst.Expression TransformDelete() { return(GlobalParent.Delete( typeof(void), _name, _target )); }
public override MSAst.Expression Reduce() { if (IsSlice) { return(GlobalParent.GetSlice(GetActionArgumentsForGetOrDelete())); } return(GlobalParent.GetIndex(GetActionArgumentsForGetOrDelete())); }
internal override void RewriteBody(MSAst.ExpressionVisitor visitor) { _dlrBody = null; // clear the cached body if we've been reduced MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode()); FuncCodeExpr = funcCode; Body = new RewrittenBodyStatement(Body, visitor.Visit(Body)); }
internal override MSAst.Expression Transform(MSAst.Expression body) { return(GlobalParent.AddDebugInfoAndVoid( AstUtils.If( GlobalParent.Convert(typeof(bool), ConversionResultKind.ExplicitCast, _test), body ), Span )); }
internal override MSAst.Expression TransformDelete() { MSAst.Expression[] statements = new MSAst.Expression[_items.Length + 1]; for (int i = 0; i < _items.Length; i++) { statements[i] = _items[i].TransformDelete(); } statements[_items.Length] = AstUtils.Empty(); return(GlobalParent.AddDebugInfo(Ast.Block(statements), Span)); }
private MSAst.Expression ReduceWorker(bool optimizeDynamicConvert) { // Only the body is "in the loop" for the purposes of break/continue // The "else" clause is outside ConstantExpression constTest = _test as ConstantExpression; if (constTest != null && constTest.Value is int) { // while 0: / while 1: int val = (int)constTest.Value; if (val == 0) { // completely optimize the loop away if (_else == null) { return(MSAst.Expression.Empty()); } else { return(_else); } } MSAst.Expression test = MSAst.Expression.Constant(true); MSAst.Expression res = AstUtils.While( test, _body, _else, _break, _continue ); if (GlobalParent.IndexToLocation(_test.StartIndex).Line != GlobalParent.IndexToLocation(_body.StartIndex).Line) { res = GlobalParent.AddDebugInfoAndVoid(res, _test.Span); } return(res); } return(AstUtils.While( GlobalParent.AddDebugInfo( optimizeDynamicConvert ? TransformAndDynamicConvert(_test, typeof(bool)) : GlobalParent.Convert(typeof(bool), Microsoft.Scripting.Actions.ConversionResultKind.ExplicitCast, _test), Header ), _body, _else, _break, _continue )); }
public override MSAst.Expression Reduce() { MSAst.Expression ifTrue = AstUtils.Convert(_trueExpr, typeof(object)); MSAst.Expression ifFalse = AstUtils.Convert(_falseExpr, typeof(object)); return(Ast.Condition( GlobalParent.Convert(typeof(bool), ConversionResultKind.ExplicitCast, _testExpr), ifTrue, ifFalse )); }
protected override Ast Body(MSAst.ParameterExpression res) { return(GlobalParent.AddDebugInfo( Ast.Call( AstMethods.SetAddForComprehension, res, AstUtils.Convert(_item, typeof(object)) ), _item.Span )); }
public override MSAst.Expression Reduce() { // Transform to series of individual del statements. ReadOnlyCollectionBuilder <MSAst.Expression> statements = new ReadOnlyCollectionBuilder <MSAst.Expression>(_expressions.Length + 1); for (int i = 0; i < _expressions.Length; i++) { statements.Add(_expressions[i].TransformDelete()); } statements.Add(AstUtils.Empty()); return(GlobalParent.AddDebugInfo(MSAst.Expression.Block(statements), Span)); }
protected override Ast Body(MSAst.ParameterExpression res) { return(GlobalParent.AddDebugInfo( Ast.Call( AstMethods.DictAddForComprehension, res, AstUtils.Convert(_key, typeof(object)), AstUtils.Convert(_value, typeof(object)) ), new SourceSpan(_key.Span.Start, _value.Span.End) )); }
public override MSAst.Expression Reduce() { if (_value == Ellipsis.Value) { return(EllipsisExpr); } else if (_value is bool) { return((bool)_value ? TrueExpr : FalseExpr); } return(GlobalParent.Constant(_value)); }
private MSAst.Expression ReduceWorker(MSAst.Expression expression) { if (Parent.PrintExpressions) { expression = Ast.Call( AstMethods.PrintExpressionValue, Parent.LocalContext, ConvertIfNeeded(expression, typeof(object)) ); } return(GlobalParent.AddDebugInfoAndVoid(expression, _expression.Span)); }
/// <summary> /// Returns an expression which creates the function object. /// </summary> internal MSAst.Expression MakeFunctionExpression() { List <MSAst.Expression> defaults = new List <MSAst.Expression>(0); foreach (var param in _parameters) { if (param.DefaultValue != null) { defaults.Add(AstUtils.Convert(param.DefaultValue, typeof(object))); } } MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode()); FuncCodeExpr = funcCode; MSAst.Expression ret; if (EmitDebugFunction()) { LightLambdaExpression code = CreateFunctionLambda(); // we need to compile all of the debuggable code together at once otherwise mdbg gets confused. If we're // in tracing mode we'll still compile things one off though just to keep things simple. The code will still // be debuggable but naive debuggers like mdbg will have more issues. ret = Ast.Call( AstMethods.MakeFunctionDebug, // method Parent.LocalContext, // 1. Emit CodeContext FuncCodeExpr, // 2. FunctionCode ((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(), // 3. module name defaults.Count == 0 ? // 4. default values AstUtils.Constant(null, typeof(object[])) : (MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults), IsGenerator ? (MSAst.Expression) new PythonGeneratorExpression(code, GlobalParent.PyContext.Options.CompilationThreshold) : (MSAst.Expression)code ); } else { ret = Ast.Call( AstMethods.MakeFunction, // method Parent.LocalContext, // 1. Emit CodeContext FuncCodeExpr, // 2. FunctionCode ((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(), // 3. module name defaults.Count == 0 ? // 4. default values AstUtils.Constant(null, typeof(object[])) : (MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults) ); } return(AddDecorators(ret, _decorators)); }
internal override MSAst.Expression TransformDelete() { MSAst.Expression index; if (IsSlice) { index = GlobalParent.DeleteSlice(GetActionArgumentsForGetOrDelete()); } else { index = GlobalParent.DeleteIndex(GetActionArgumentsForGetOrDelete()); } return(GlobalParent.AddDebugInfoAndVoid(index, Span)); }
private MSAst.Expression MakeBinaryOperation(PythonOperator op, MSAst.Expression left, MSAst.Expression right, SourceSpan span) { if (op == PythonOperator.NotIn) { return(AstUtils.Convert( Ast.Not( GlobalParent.Operation( typeof(bool), PythonOperationKind.Contains, left, right ) ), typeof(object) )); } else if (op == PythonOperator.In) { return(AstUtils.Convert( GlobalParent.Operation( typeof(bool), PythonOperationKind.Contains, left, right ), typeof(object) )); } PythonOperationKind action = PythonOperatorToAction(op); if (action != PythonOperationKind.None) { return(GlobalParent.Operation( typeof(object), action, left, right )); } else { // Call helper method return(Ast.Call( GetHelperMethod(op), ConvertIfNeeded(left, typeof(object)), ConvertIfNeeded(right, typeof(object)) )); } }