本文整理汇总了C#中IronPython.Compiler.Ast.AstGenerator.GetTemporary方法的典型用法代码示例。如果您正苦于以下问题:C# AstGenerator.GetTemporary方法的具体用法?C# AstGenerator.GetTemporary怎么用?C# AstGenerator.GetTemporary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IronPython.Compiler.Ast.AstGenerator
的用法示例。
在下文中一共展示了AstGenerator.GetTemporary方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TransformSet
internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, Operators op) {
if (op == Operators.None) {
return ag.AddDebugInfoAndVoid(
Binders.Set(
ag.BinderState,
typeof(object),
SymbolTable.IdToString(_name),
ag.Transform(_target),
right
),
span
);
} else {
MSAst.ParameterExpression temp = ag.GetTemporary("inplace");
return ag.AddDebugInfo(
Ast.Block(
Ast.Assign(temp, ag.Transform(_target)),
SetMemberOperator(ag, right, op, temp),
Ast.Empty()
),
Span.Start,
span.End
);
}
}
示例2: Transform
internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
MSAst.Expression left = ag.Transform(_left);
MSAst.Expression right = ag.Transform(_right);
Type t = left.Type == right.Type ? left.Type : typeof(object);
MSAst.ParameterExpression tmp = ag.GetTemporary("__all__", t);
return Ast.Condition(
Binders.Convert(
ag.BinderState,
typeof(bool),
ConversionResultKind.ExplicitCast,
AstUtils.Assign(
tmp,
AstUtils.Convert(
left,
t
)
)
),
AstUtils.Convert(
right,
t
),
tmp
);
}
示例3: Transform
internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
MSAst.ParameterExpression list = ag.GetTemporary("list_comprehension_list", typeof(List));
// 1. Initialization code - create list and store it in the temp variable
MSAst.Expression initialize =
Ast.Assign(
list,
Ast.Call(
AstGenerator.GetHelperMethod("MakeList", Type.EmptyTypes) // method
)
);
// 2. Create body from _item: list.Append(_item)
MSAst.Expression body = ag.AddDebugInfo(
Ast.Call(
AstGenerator.GetHelperMethod("ListAddForComprehension"),
list,
ag.TransformAsObject(_item)
),
_item.Span
);
// 3. Transform all iterators in reverse order, building the true body:
int current = _iterators.Length;
while (current-- > 0) {
ListComprehensionIterator iterator = _iterators[current];
body = iterator.Transform(ag, body);
}
return Ast.Block(
initialize,
body,
list // result
);
}
示例4: Transform
internal override MSAst.Expression Transform(AstGenerator ag) {
MSAst.Expression destination = ag.TransformAsObject(_dest);
if (_expressions.Length == 0) {
MSAst.Expression result;
if (destination != null) {
result = Ast.Call(
AstGenerator.GetHelperMethod("PrintNewlineWithDest"),
ag.LocalContext,
destination
);
} else {
result = Ast.Call(
AstGenerator.GetHelperMethod("PrintNewline"),
ag.LocalContext
);
}
return ag.AddDebugInfo(result, Span);
} else {
// Create list for the individual statements
List<MSAst.Expression> statements = new List<MSAst.Expression>();
// Store destination in a temp, if we have one
if (destination != null) {
MSAst.ParameterExpression temp = ag.GetTemporary("destination");
statements.Add(
ag.MakeAssignment(temp, destination)
);
destination = temp;
}
for (int i = 0; i < _expressions.Length; i++) {
string method = (i < _expressions.Length - 1 || _trailingComma) ? "PrintComma" : "Print";
Expression current = _expressions[i];
MSAst.MethodCallExpression mce;
if (destination != null) {
mce = Ast.Call(
AstGenerator.GetHelperMethod(method + "WithDest"),
ag.LocalContext,
destination,
ag.TransformAsObject(current)
);
} else {
mce = Ast.Call(
AstGenerator.GetHelperMethod(method),
ag.LocalContext,
ag.TransformAsObject(current)
);
}
statements.Add(mce);
}
statements.Add(AstUtils.Empty());
return ag.AddDebugInfo(Ast.Block(statements.ToArray()), Span);
}
}
示例5: Transform
internal override MSAst.Expression Transform(AstGenerator ag) {
// Temporary variable for the IEnumerator object
MSAst.ParameterExpression enumerator = ag.GetTemporary("foreach_enumerator", typeof(IEnumerator));
// Only the body is "in the loop" for the purposes of break/continue
// The "else" clause is outside
MSAst.LabelTarget breakLabel, continueLabel;
MSAst.Expression body = ag.TransformLoopBody(_body, _left.Start, out breakLabel, out continueLabel);
if (body == null) {
// error recovery
return null;
}
return TransformForStatement(ag, enumerator, _list, _left, body, _else, Span, _header, breakLabel, continueLabel);
}
示例6: AssignComplex
private MSAst.Expression AssignComplex(AstGenerator ag, MSAst.Expression right) {
// Python assignment semantics:
// - only evaluate RHS once.
// - evaluates assignment from left to right
// - does not evaluate getters.
//
// So
// a=b[c]=d=f()
// should be:
// $temp = f()
// a = $temp
// b[c] = $temp
// d = $temp
List<MSAst.Expression> statements = new List<MSAst.Expression>();
// 1. Create temp variable for the right value
MSAst.ParameterExpression right_temp = ag.GetTemporary("assignment");
// 2. right_temp = right
statements.Add(
ag.MakeAssignment(right_temp, right)
);
// Do left to right assignment
foreach (Expression e in _left) {
if (e == null) {
continue;
}
// 3. e = right_temp
MSAst.Expression transformed = e.TransformSet(ag, Span, right_temp, Operators.None);
if (transformed == null) {
throw PythonOps.SyntaxError(String.Format("can't assign to {0}", e.NodeName), ag.Context.SourceUnit, e.Span, -1);
}
statements.Add(transformed);
}
// 4. Create and return the resulting suite
statements.Add(Ast.Empty());
return ag.AddDebugInfo(
Ast.Block(statements.ToArray()),
Span
);
}
示例7: AssignComplex
private MSAst.Expression AssignComplex(AstGenerator ag, MSAst.Expression right) {
// Python assignment semantics:
// - only evaluate RHS once.
// - evaluates assignment from left to right
// - does not evaluate getters.
//
// So
// a=b[c]=d=f()
// should be:
// $temp = f()
// a = $temp
// b[c] = $temp
// d = $temp
List<MSAst.Expression> statements = new List<MSAst.Expression>();
// 1. Create temp variable for the right value
MSAst.ParameterExpression right_temp = ag.GetTemporary("assignment");
// 2. right_temp = right
statements.Add(
AstGenerator.MakeAssignment(right_temp, right)
);
// Do left to right assignment
foreach (Expression e in _left) {
if (e == null) {
continue;
}
// 3. e = right_temp
MSAst.Expression transformed = e.TransformSet(ag, Span, right_temp, PythonOperationKind.None);
statements.Add(transformed);
}
// 4. Create and return the resulting suite
statements.Add(AstUtils.Empty());
return ag.AddDebugInfoAndVoid(
Ast.Block(statements.ToArray()),
Span
);
}
示例8: TransformSet
internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
if (op == PythonOperationKind.None) {
return ag.AddDebugInfoAndVoid(
ag.Set(
typeof(object),
_name,
ag.Transform(_target),
right
),
span
);
} else {
MSAst.ParameterExpression temp = ag.GetTemporary("inplace");
return ag.AddDebugInfo(
Ast.Block(
Ast.Assign(temp, ag.Transform(_target)),
SetMemberOperator(ag, right, op, temp),
AstUtils.Empty()
),
Span.Start,
span.End
);
}
}
示例9: Transform
internal override MSAst.Expression Transform(AstGenerator ag) {
if (_names == _star) {
// from a[.b] import *
return ag.AddDebugInfo(
Ast.Call(
AstGenerator.GetHelperMethod("ImportStar"),
AstUtils.CodeContext(),
Ast.Constant(_root.MakeString()),
Ast.Constant(GetLevel())
),
Span
);
} else {
// from a[.b] import x [as xx], [ y [ as yy] ] [ , ... ]
List<MSAst.Expression> statements = new List<MSAst.Expression>();
MSAst.ParameterExpression module = ag.GetTemporary("module");
// Create initializer of the array of names being passed to ImportWithNames
MSAst.ConstantExpression[] names = new MSAst.ConstantExpression[_names.Length];
for (int i = 0; i < names.Length; i++) {
names[i] = Ast.Constant(SymbolTable.IdToString(_names[i]));
}
// module = PythonOps.ImportWithNames(<context>, _root, make_array(_names))
statements.Add(
ag.AddDebugInfo(
AstUtils.Assign(
module,
Ast.Call(
AstGenerator.GetHelperMethod("ImportWithNames"),
AstUtils.CodeContext(),
Ast.Constant(_root.MakeString()),
Ast.NewArrayInit(typeof(string), names),
Ast.Constant(GetLevel())
)
),
_root.Span
)
);
// now load all the names being imported and assign the variables
for (int i = 0; i < names.Length; i++) {
statements.Add(
ag.AddDebugInfo(
AstUtils.Assign(
_variables[i].Variable,
Ast.Call(
AstGenerator.GetHelperMethod("ImportFrom"),
AstUtils.CodeContext(),
module,
names[i]
)
),
Span
)
);
}
statements.Add(Ast.Empty());
return ag.AddDebugInfo(Ast.Block(statements.ToArray()), Span);
}
}
示例10: MakeBinaryOperation
private static MSAst.Expression MakeBinaryOperation(AstGenerator ag, PythonOperator op, MSAst.Expression left, MSAst.Expression right, Type type, SourceSpan span) {
if (op == PythonOperator.NotIn) {
return AstUtils.Convert(
Ast.Not(
Binders.Operation(
ag.BinderState,
typeof(bool),
StandardOperators.Contains,
left,
right
)
),
type
);
}
Operators action = PythonOperatorToAction(op);
if (action != Operators.None) {
// Create action expression
if (op == PythonOperator.Divide &&
(ag.DivisionOptions == PythonDivisionOptions.Warn || ag.DivisionOptions == PythonDivisionOptions.WarnAll)) {
MSAst.ParameterExpression tempLeft = ag.GetTemporary("left", left.Type);
MSAst.ParameterExpression tempRight = ag.GetTemporary("right", right.Type);
return Ast.Block(
Ast.Call(
AstGenerator.GetHelperMethod("WarnDivision"),
AstUtils.CodeContext(),
Ast.Constant(ag.DivisionOptions),
AstUtils.Convert(
Ast.Assign(tempLeft, left),
typeof(object)
),
AstUtils.Convert(
Ast.Assign(tempRight, right),
typeof(object)
)
),
Binders.Operation(
ag.BinderState,
type,
StandardOperators.FromOperator(action),
tempLeft,
tempRight
)
);
}
return Binders.Operation(
ag.BinderState,
type,
StandardOperators.FromOperator(action),
left,
right
);
} else {
// Call helper method
return Ast.Call(
AstGenerator.GetHelperMethod(GetHelperName(op)),
AstGenerator.ConvertIfNeeded(left, typeof(object)),
AstGenerator.ConvertIfNeeded(right, typeof(object))
);
}
}
示例11: TransformSet
internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
// if we just have a simple named multi-assignment (e.g. a, b = 1,2)
// then go ahead and step over the entire statement at once. If we have a
// more complex statement (e.g. a.b, c.d = 1, 2) then we'll step over the
// sets individually as they could be property sets the user wants to step
// into. TODO: Enable stepping of the right hand side?
bool emitIndividualSets = false;
foreach (Expression e in _items) {
if (IsComplexAssignment(e)) {
emitIndividualSets = true;
break;
}
}
SourceSpan rightSpan = SourceSpan.None;
SourceSpan leftSpan =
(Span.Start.IsValid && span.IsValid) ?
new SourceSpan(Span.Start, span.End) :
SourceSpan.None;
SourceSpan totalSpan = SourceSpan.None;
if (emitIndividualSets) {
rightSpan = span;
leftSpan = SourceSpan.None;
totalSpan = (Span.Start.IsValid && span.IsValid) ?
new SourceSpan(Span.Start, span.End) :
SourceSpan.None;
}
// 1. Evaluate the expression and assign the value to the temp.
MSAst.ParameterExpression right_temp = ag.GetTemporary("unpacking");
// 2. Add the assignment "right_temp = right" into the suite/block
MSAst.Expression assignStmt1 = AstGenerator.MakeAssignment(right_temp, right);
// 3. Call GetEnumeratorValues on the right side (stored in temp)
MSAst.Expression enumeratorValues = Ast.Call(
AstGenerator.GetHelperMethod("GetEnumeratorValues"), // method
// arguments
ag.LocalContext,
right_temp,
AstUtils.Constant(_items.Length)
);
// 4. Create temporary variable for the array
MSAst.ParameterExpression array_temp = ag.GetTemporary("array", typeof(object[]));
// 5. Assign the value of the method call (mce) into the array temp
// And add the assignment "array_temp = Ops.GetEnumeratorValues(...)" into the block
MSAst.Expression assignStmt2 = ag.MakeAssignment(
array_temp,
enumeratorValues,
rightSpan
);
ReadOnlyCollectionBuilder<MSAst.Expression> sets = new ReadOnlyCollectionBuilder<MSAst.Expression>(_items.Length + 1);
for (int i = 0; i < _items.Length; i ++) {
// target = array_temp[i]
Expression target = _items[i];
if (target == null) {
continue;
}
// 6. array_temp[i]
MSAst.Expression element = Ast.ArrayAccess(
array_temp, // array expression
AstUtils.Constant(i) // index
);
// 7. target = array_temp[i], and add the transformed assignment into the list of sets
MSAst.Expression set = target.TransformSet(
ag,
emitIndividualSets ? // span
target.Span :
SourceSpan.None,
element,
PythonOperationKind.None
);
sets.Add(set);
}
// 9. add the sets as their own block so they can be marked as a single span, if necessary.
sets.Add(AstUtils.Empty());
MSAst.Expression itemSet = ag.AddDebugInfo(Ast.Block(sets.ToReadOnlyCollection()), leftSpan);
// 10. Free the temps
ag.FreeTemp(array_temp);
ag.FreeTemp(right_temp);
// 11. Return the suite statement (block)
return ag.AddDebugInfo(Ast.Block(assignStmt1, assignStmt2, itemSet, AstUtils.Empty()), totalSpan);
}
示例12: Transform
internal override MSAst.Expression Transform(AstGenerator ag, MSAst.Expression body) {
MSAst.ParameterExpression temp = ag.GetTemporary("list_comprehension_for", typeof(IEnumerator));
return ForStatement.TransformForStatement(ag, temp, _list, _lhs, body, null, Span, _lhs.End, null, null);
}
示例13: Transform
internal override MSAst.Expression Transform(AstGenerator ag) {
// allocated all variables here so they won't be shared w/ other
// locals allocated during the body or except blocks.
MSAst.ParameterExpression noNestedException = null;
if (_finally != null) {
noNestedException = ag.GetTemporary("$noException", typeof(bool));
}
MSAst.ParameterExpression lineUpdated = null;
MSAst.ParameterExpression runElse = null;
if (_else != null || (_handlers != null && _handlers.Length > 0)) {
lineUpdated = ag.GetTemporary("$lineUpdated", typeof(bool));
if (_else != null) {
runElse = ag.GetTemporary("run_else", typeof(bool));
}
}
// don't allocate locals below here...
MSAst.Expression body = ag.Transform(_body);
MSAst.Expression @else = ag.Transform(_else);
if (body == null) {
return null;
}
MSAst.ParameterExpression exception;
MSAst.Expression @catch = TransformHandlers(ag, out exception);
MSAst.Expression result;
// We have else clause, must generate guard around it
if (@else != null) {
Debug.Assert(@catch != null);
// run_else = true;
// try {
// try_body
// } catch ( ... ) {
// run_else = false;
// catch_body
// }
// if (run_else) {
// else_body
// }
result =
Ast.Block(
Ast.Assign(runElse, Ast.Constant(true)),
// save existing line updated, we could choose to do this only for nested exception handlers.
ag.PushLineUpdated(false, lineUpdated),
AstUtils.Try(
ag.AddDebugInfo(Ast.Empty(), new SourceSpan(Span.Start, _header)),
body
).Catch(exception,
Ast.Assign(runElse, Ast.Constant(false)),
@catch,
// restore existing line updated after exception handler completes
ag.PopLineUpdated(lineUpdated),
Ast.Default(body.Type)
),
AstUtils.IfThen(runElse,
@else
),
Ast.Empty()
);
} else if (@catch != null) { // no "else" clause
// try {
// <try body>
// } catch (Exception e) {
// ... catch handling ...
// }
//
result = AstUtils.Try(
ag.AddDebugInfo(Ast.Empty(), new SourceSpan(Span.Start, _header)),
// save existing line updated
ag.PushLineUpdated(false, lineUpdated),
body
).Catch(exception,
@catch,
// restore existing line updated after exception handler completes
ag.PopLineUpdated(lineUpdated),
Ast.Default(body.Type)
);
} else {
result = body;
}
try {
return AddFinally(ag, result, noNestedException);
} finally {
// free all locals here after the children nodes have been generated
if (lineUpdated != null) {
ag.FreeTemp(lineUpdated);
}
if (runElse != null) {
ag.FreeTemp(@runElse);
}
}
}
示例14: TransformToFunctionExpression
internal MSAst.Expression TransformToFunctionExpression(AstGenerator ag) {
string name;
if (IsLambda) {
name = "<lambda$" + Interlocked.Increment(ref _lambdaId) + ">";
} else {
name = SymbolTable.IdToString(_name);
}
// Create AST generator to generate the body with
AstGenerator bodyGen = new AstGenerator(ag, name, IsGenerator, false);
bodyGen.DisableInterpreter = true;
// Transform the parameters.
// Populate the list of the parameter names and defaults.
List<MSAst.Expression> defaults = new List<MSAst.Expression>(0);
List<MSAst.Expression> names = new List<MSAst.Expression>();
TransformParameters(ag, bodyGen, defaults, names);
List<MSAst.Expression> statements = new List<MSAst.Expression>();
// Create variables and references. Since references refer to
// parameters, do this after parameters have been created.
CreateVariables(bodyGen, statements);
// Initialize parameters - unpack tuples.
// Since tuples unpack into locals, this must be done after locals have been created.
InitializeParameters(bodyGen, statements);
// For generators, we need to do a check before the first statement for Generator.Throw() / Generator.Close().
// The exception traceback needs to come from the generator's method body, and so we must do the check and throw
// from inside the generator.
if (IsGenerator) {
MSAst.Expression s1 = YieldExpression.CreateCheckThrowExpression(bodyGen, SourceSpan.None);
statements.Add(s1);
}
MSAst.ParameterExpression extracted = null;
if (!IsGenerator && _canSetSysExcInfo) {
// need to allocate the exception here so we don't share w/ exceptions made & freed
// during the body.
extracted = bodyGen.GetTemporary("$ex", typeof(Exception));
}
// Transform the body and add the resulting statements into the list
TransformBody(bodyGen, statements);
if (ag.DebugMode) {
// add beginning and ending break points for the function.
if (GetExpressionEnd(statements[statements.Count - 1]) != Body.End) {
statements.Add(ag.AddDebugInfo(Ast.Empty(), new SourceSpan(Body.End, Body.End)));
}
}
MSAst.Expression body = Ast.Block(new ReadOnlyCollection<MSAst.Expression>(statements.ToArray()));
// If this function can modify sys.exc_info() (_canSetSysExcInfo), then it must restore the result on finish.
//
// Wrap in
// $temp = PythonOps.SaveCurrentException()
// <body>
// PythonOps.RestoreCurrentException($temp)
// Skip this if we're a generator. For generators, the try finally is handled by the PythonGenerator class
// before it's invoked. This is because the restoration must occur at every place the function returns from
// a yield point. That's different than the finally semantics in a generator.
if (extracted != null) {
MSAst.Expression s = AstUtils.Try(
Ast.Assign(
extracted,
Ast.Call(
AstGenerator.GetHelperMethod("SaveCurrentException")
)
),
body
).Finally(
Ast.Call(
AstGenerator.GetHelperMethod("RestoreCurrentException"), extracted
)
);
body = s;
}
body = bodyGen.WrapScopeStatements(body);
bodyGen.Block.Body = bodyGen.AddReturnTarget(body);
FunctionAttributes flags = ComputeFlags(_parameters);
bool needsWrapperMethod = flags != FunctionAttributes.None;
if (_canSetSysExcInfo) {
flags |= FunctionAttributes.CanSetSysExcInfo;
}
MSAst.Expression code;
if (IsGenerator) {
code = bodyGen.Block.MakeGenerator(bodyGen.GeneratorLabel, GetGeneratorDelegateType(_parameters, needsWrapperMethod));
flags |= FunctionAttributes.Generator;
} else {
code = bodyGen.Block.MakeLambda(GetDelegateType(_parameters, needsWrapperMethod));
}
MSAst.Expression ret = Ast.Call(
//.........这里部分代码省略.........
示例15: AddFinally
private MSAst.Expression AddFinally(AstGenerator/*!*/ ag, MSAst.Expression/*!*/ body, MSAst.ParameterExpression noNestedException) {
if (_finally != null) {
Debug.Assert(noNestedException != null);
MSAst.ParameterExpression nestedFrames = ag.GetTemporary("$nestedFrames", typeof(List<DynamicStackFrame>));
bool inFinally = ag.InFinally;
ag.InFinally = true;
MSAst.Expression @finally = ag.Transform(_finally);
ag.InFinally = inFinally;
if (@finally == null) {
// error reported during compilation
return null;
}
if (ag.TrackLines) {
// lots is going on here. We need to consider:
// 1. Exceptions propagating out of try/except/finally. Here we need to save the line #
// from the exception block and not save the # from the finally block later.
// 2. Exceptions propagating out of the finally block. Here we need to report the line number
// from the finally block and leave the existing stack traces cleared.
// 3. Returning from the try block: Here we need to run the finally block and not update the
// line numbers.
body = AstUtils.Try(// we use a filter to know when we have an exception and when control leaves normally (via
// either a return or the body completing successfully).
AstUtils.Try(
ag.AddDebugInfo(Ast.Empty(), new SourceSpan(Span.Start, _header)),
Ast.Assign(noNestedException, Ast.Constant(true)),
body
).Filter(
typeof(Exception),
// condition is never true, just note the exception and let it propagate
Ast.Equal(
Ast.Assign(noNestedException, Ast.Constant(false)),
Ast.Constant(true)
),
Ast.Default(body.Type)
)
).Finally(
// if we had an exception save the line # that was last executing during the try
AstUtils.If(
Ast.Not(noNestedException),
ag.GetLineNumberUpdateExpression(false)
),
// clear the frames incase thae finally throws, and allow line number
// updates to proceed
ag.UpdateLineUpdated(false),
Ast.Assign(
nestedFrames,
Ast.Call(AstGenerator.GetHelperMethod("GetAndClearDynamicStackFrames"))
),
// run the finally code
@finally,
// if the finally exits normally restore any previous exception info
Ast.Call(
AstGenerator.GetHelperMethod("SetDynamicStackFrames"),
nestedFrames
),
ag.UpdateLineUpdated(true)
);
ag.FreeTemp(nestedFrames);
ag.FreeTemp(noNestedException);
} else {
body = AstUtils.Try(body).Finally(
Ast.Assign(
nestedFrames,
Ast.Call(AstGenerator.GetHelperMethod("GetAndClearDynamicStackFrames"))
),
// run the finally code
@finally,
// if the finally exits normally restore any previous exception info
Ast.Call(
AstGenerator.GetHelperMethod("SetDynamicStackFrames"),
nestedFrames
)
);
}
}
return body;
}