本文整理汇总了C#中PythonOperationKind类的典型用法代码示例。如果您正苦于以下问题:C# PythonOperationKind类的具体用法?C# PythonOperationKind怎么用?C# PythonOperationKind使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PythonOperationKind类属于命名空间,在下文中一共展示了PythonOperationKind类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TransformSet
internal override MSAst.Expression TransformSet(SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
if (op == PythonOperationKind.None) {
return GlobalParent.AddDebugInfoAndVoid(
GlobalParent.Set(
typeof(object),
_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
);
}
}
示例2: TransformSet
internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
MSAst.Expression assignment;
if (op != PythonOperationKind.None) {
right = ag.Operation(
typeof(object),
op,
Transform(ag, typeof(object)),
right
);
}
if (_reference.PythonVariable != null) {
assignment = ag.Globals.Assign(
ag.Globals.GetVariable(_reference.PythonVariable),
AstGenerator.ConvertIfNeeded(right, typeof(object))
);
} else {
assignment = Ast.Call(
null,
typeof(ScriptingRuntimeHelpers).GetMethod("SetName"),
new [] {
ag.LocalContext,
ag.Globals.GetSymbol(_name),
AstUtils.Convert(right, typeof(object))
}
);
}
SourceSpan aspan = span.IsValid ? new SourceSpan(Span.Start, span.End) : SourceSpan.None;
return ag.AddDebugInfoAndVoid(assignment, aspan);
}
示例3: TransformSet
internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
if (Items.Length == 0) {
ag.AddError("can't assign to ()", Span);
return null;
}
return base.TransformSet(ag, span, right, op);
}
示例4: DirectOperation
public static PythonOperationKind DirectOperation(PythonOperationKind op)
{
if ((op & PythonOperationKind.InPlace) == 0) {
throw new InvalidOperationException();
}
return op & ~PythonOperationKind.InPlace;
}
示例5: GetExpressionTypeFromUnaryOperator
private static ExpressionType? GetExpressionTypeFromUnaryOperator(PythonOperationKind operatorName) {
switch (operatorName) {
case PythonOperationKind.Positive: return ExpressionType.UnaryPlus;
case PythonOperationKind.Negate: return ExpressionType.Negate;
case PythonOperationKind.OnesComplement: return ExpressionType.OnesComplement;
case PythonOperationKind.Not: return ExpressionType.IsFalse;
}
return null;
}
示例6: BinaryOperationBinder
public static DynamicMetaObjectBinder BinaryOperationBinder(PythonContext state, PythonOperationKind operatorName) {
ExpressionType? et = GetExpressionTypeFromBinaryOperator(operatorName);
if (et == null) {
return state.Operation(
operatorName
);
}
return state.BinaryOperation(et.Value);
}
示例7: BinaryOperationRetType
public static DynamicMetaObjectBinder/*!*/ BinaryOperationRetType(BinderState/*!*/ state, PythonOperationKind operatorName, Type retType) {
return new ComboBinder(
new BinderMappingInfo(
BinaryOperationBinder(state, operatorName),
ParameterMappingInfo.Parameter(0),
ParameterMappingInfo.Parameter(1)
),
new BinderMappingInfo(
state.Convert(retType, ConversionResultKind.ExplicitCast),
ParameterMappingInfo.Action(0)
)
);
}
示例8: SetMemberOperator
private MSAst.Expression SetMemberOperator(AstGenerator ag, MSAst.Expression right, PythonOperationKind op, MSAst.ParameterExpression temp) {
return ag.Set(
typeof(object),
SymbolTable.IdToString(_name),
temp,
ag.Operation(
typeof(object),
op,
ag.Get(
typeof(object),
SymbolTable.IdToString(_name),
temp
),
right
)
);
}
示例9: 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
);
}
}
示例10: IsReverseOperator
internal static bool IsReverseOperator(PythonOperationKind op) {
return (op & PythonOperationKind.Reversed) != 0;
}
示例11: OneOffPowerBinder
public OneOffPowerBinder(string/*!*/ pythonName, PythonOperationKind op) {
Assert.NotNull(pythonName, op);
_pythonName = pythonName;
_op = op;
}
示例12: TransformSet
internal override MSAst.Expression TransformSet(SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
MSAst.Expression assignment;
if (op != PythonOperationKind.None) {
right = GlobalParent.Operation(
typeof(object),
op,
this,
right
);
}
SourceSpan aspan = span.IsValid ? new SourceSpan(Span.Start, span.End) : SourceSpan.None;
if (_reference.PythonVariable != null) {
assignment = AssignValue(
Parent.GetVariableExpression(_reference.PythonVariable),
ConvertIfNeeded(right, typeof(object))
);
} else {
assignment = Ast.Call(
null,
typeof(PythonOps).GetMethod("SetName"),
new[] {
Parent.LocalContext,
Ast.Constant(_name),
AstUtils.Convert(right, typeof(object))
}
);
}
return GlobalParent.AddDebugInfoAndVoid(assignment, aspan);
}
示例13: MakeBinaryOperatorResult
private static DynamicMetaObject/*!*/ MakeBinaryOperatorResult(DynamicMetaObject/*!*/[]/*!*/ types, DynamicMetaObjectBinder/*!*/ operation, PythonOperationKind op, SlotOrFunction/*!*/ fCand, SlotOrFunction/*!*/ rCand, PythonTypeSlot fSlot, PythonTypeSlot rSlot, DynamicMetaObject errorSuggestion) {
Assert.NotNull(operation, fCand, rCand);
SlotOrFunction fTarget, rTarget;
PythonContext state = PythonContext.GetPythonContext(operation);
ConditionalBuilder bodyBuilder = new ConditionalBuilder(operation);
if ((op & PythonOperationKind.InPlace) != 0) {
// in place operator, see if there's a specific method that handles it.
SlotOrFunction function = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), Symbols.OperatorToSymbol(op), types);
// we don't do a coerce for in place operators if the lhs implements __iop__
if (!MakeOneCompareGeneric(function, false, types, MakeCompareReturn, bodyBuilder, typeof(object))) {
// the method handles it and always returns a useful value.
return bodyBuilder.GetMetaObject(types);
}
}
if (!SlotOrFunction.GetCombinedTargets(fCand, rCand, out fTarget, out rTarget) &&
fSlot == null &&
rSlot == null &&
!ShouldCoerce(state, op, types[0], types[1], false) &&
!ShouldCoerce(state, op, types[1], types[0], false) &&
bodyBuilder.NoConditions) {
return MakeRuleForNoMatch(operation, op, errorSuggestion, types);
}
if (ShouldCoerce(state, op, types[0], types[1], false) &&
(op != PythonOperationKind.Mod || !MetaPythonObject.GetPythonType(types[0]).IsSubclassOf(TypeCache.String))) {
// need to try __coerce__ first.
DoCoerce(state, bodyBuilder, op, types, false);
}
if (MakeOneTarget(PythonContext.GetPythonContext(operation), fTarget, fSlot, bodyBuilder, false, types)) {
if (ShouldCoerce(state, op, types[1], types[0], false)) {
// need to try __coerce__ on the reverse first
DoCoerce(state, bodyBuilder, op, new DynamicMetaObject[] { types[1], types[0] }, true);
}
if (rSlot != null) {
MakeSlotCall(PythonContext.GetPythonContext(operation), types, bodyBuilder, rSlot, true);
bodyBuilder.FinishCondition(MakeBinaryThrow(operation, op, types).Expression, typeof(object));
} else if (MakeOneTarget(PythonContext.GetPythonContext(operation), rTarget, rSlot, bodyBuilder, false, types)) {
// need to fallback to throwing or coercion
bodyBuilder.FinishCondition(MakeBinaryThrow(operation, op, types).Expression, typeof(object));
}
}
return bodyBuilder.GetMetaObject(types);
}
示例14: GetOperatorMethods
private static void GetOperatorMethods(DynamicMetaObject/*!*/[]/*!*/ types, PythonOperationKind oper, PythonContext state, out SlotOrFunction fbinder, out SlotOrFunction rbinder, out PythonTypeSlot fSlot, out PythonTypeSlot rSlot) {
oper = NormalizeOperator(oper);
oper &= ~PythonOperationKind.InPlace;
string op, rop;
if (!IsReverseOperator(oper)) {
op = Symbols.OperatorToSymbol(oper);
rop = Symbols.OperatorToReversedSymbol(oper);
} else {
// coming back after coercion, just try reverse operator.
rop = Symbols.OperatorToSymbol(oper);
op = Symbols.OperatorToReversedSymbol(oper);
}
fSlot = null;
rSlot = null;
PythonType fParent, rParent;
if (oper == PythonOperationKind.Multiply &&
IsSequence(types[0]) &&
!PythonOps.IsNonExtensibleNumericType(types[1].GetLimitType())) {
// class M:
// def __rmul__(self, other):
// print "CALLED"
// return 1
//
// print [1,2] * M()
//
// in CPython this results in a successful call to __rmul__ on the type ignoring the forward
// multiplication. But calling the __mul__ method directly does NOT return NotImplemented like
// one might expect. Therefore we explicitly convert the MetaObject argument into an Index
// for binding purposes. That allows this to work at multiplication time but not with
// a direct call to __mul__.
DynamicMetaObject[] newTypes = new DynamicMetaObject[2];
newTypes[0] = types[0];
newTypes[1] = new DynamicMetaObject(
Ast.New(
typeof(Index).GetConstructor(new Type[] { typeof(object) }),
AstUtils.Convert(types[1].Expression, typeof(object))
),
BindingRestrictions.Empty
);
types = newTypes;
}
if (!SlotOrFunction.TryGetBinder(state, types, op, null, out fbinder, out fParent)) {
foreach (PythonType pt in MetaPythonObject.GetPythonType(types[0]).ResolutionOrder) {
if (pt.TryLookupSlot(state.SharedContext, op, out fSlot)) {
fParent = pt;
break;
}
}
}
if (!SlotOrFunction.TryGetBinder(state, types, null, rop, out rbinder, out rParent)) {
foreach (PythonType pt in MetaPythonObject.GetPythonType(types[1]).ResolutionOrder) {
if (pt.TryLookupSlot(state.SharedContext, rop, out rSlot)) {
rParent = pt;
break;
}
}
}
if (fParent != null && (rbinder.Success || rSlot != null) && rParent != fParent && rParent.IsSubclassOf(fParent)) {
// Python says if x + subx and subx defines __r*__ we should call r*.
fbinder = SlotOrFunction.Empty;
fSlot = null;
}
if (!fbinder.Success && !rbinder.Success && fSlot == null && rSlot == null) {
if (op == "__truediv__" || op == "__rtruediv__") {
// true div on a type which doesn't support it, go ahead and try normal divide
PythonOperationKind newOp = op == "__truediv__" ? PythonOperationKind.Divide : PythonOperationKind.ReverseDivide;
GetOperatorMethods(types, newOp, state, out fbinder, out rbinder, out fSlot, out rSlot);
}
}
}
示例15: MakeBinaryThrow
private static DynamicMetaObject/*!*/ MakeBinaryThrow(DynamicMetaObjectBinder/*!*/ action, PythonOperationKind op, DynamicMetaObject/*!*/[]/*!*/ args) {
if (action is IPythonSite) {
// produce the custom Python error message
return new DynamicMetaObject(
action.Throw(
Ast.Call(
typeof(PythonOps).GetMethod("TypeErrorForBinaryOp"),
AstUtils.Constant(Symbols.OperatorToSymbol(NormalizeOperator(op))),
AstUtils.Convert(args[0].Expression, typeof(object)),
AstUtils.Convert(args[1].Expression, typeof(object))
),
typeof(object)
),
BindingRestrictions.Combine(args)
);
}
// let the site produce its own error
return GenericFallback(action, args);
}