public override IPyValue TranslateToPython(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { var mi = src.MethodInfo; var type = mi.DeclaringType; if (mi.IsGenericMethod) { mi = mi.GetGenericMethodDefinition(); } if (type == typeof(Enumerable)) { switch (mi.Name) { case "ToArray": case "ToList": return(ctx.TranslateValue(src.Arguments[0])); } var mn = mi.ToString(); switch (mn) { case "System.Collections.Generic.IEnumerable`1[System.Int32] Range(Int32, Int32)": return(Translate_Enumerable_Range(ctx, src)); default: throw new NotImplementedException(mn); } } return(null); }
protected override IPhpValue VisitMethodCallExpression(CsharpMethodCallExpression src) { var x = state.Principles.NodeTranslators.Translate(state, src); if (x is PhpMethodCallExpression) { var t = (PhpMethodCallExpression)x; if (t.Arguments != null && t.Arguments.Any()) { foreach (var i in t.Arguments) { if (i.Expression == null) { throw new Exception("Invalid translation"); } } } } if (x != null) { return(SimplifyPhpExpression(x)); } state.Principles.NodeTranslators.Translate(state, src); throw new NotSupportedException(src.ToString()); }
public override IPyValue TranslateToPython(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { if (src.MethodInfo.DeclaringType == typeof(Console)) { var info = MapNamedParameters(src); if (src.MethodInfo.Name == nameof(Console.WriteLine)) { if (info.MethodParameters.Length == 1) { var v = ctx.TranslateValue(info.CallArguments[0].MyValue); var result = new PyMethodCallExpression(null, "print", v); // In Python 3, print is a function, whereas it used to be a statement in previous versions. /* * result.OnSkipBracketRequest += (mce, args) => * { * args.CanSkipBrackets = GeneralRulesForMetodBrackets.Bla(mce); * }; */ return(result); } } var mn = src.MethodInfo.ToString(); throw new NotImplementedException(mn); } return(null); }
public override IPyValue TranslateToPython(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { var dt = src.GenericDeclaringType; if (!IsDictionary(dt)) { return(null); } switch (src.MethodName) { case nameof(DictionaryReplacer <int, int> .Remove): // del myDict['key'] var args = MapNamedParameters(src); var key = ctx.TranslateValue(args.GetArgumentValue(0)); var target = ctx.TranslateValue(src.TargetObject); IPyValue arg = new PyArrayAccessExpression(target, key); var m = new PyMethodCallExpression("del", arg); m.OnSkipBracketRequest += (mce, args2) => { args2.CanSkipBrackets = GeneralRulesForMetodBrackets.Bla(mce); }; return(m); } return(null); }
// Public Methods public void CheckAccesibility(CsharpMethodCallExpression m) { if (m == null) { return; } CheckAccesibility(m.MethodInfo); }
private IValue _Make_DotnetMethodCall(MethodBase mi, IValue targetObject, FunctionArgument[] functionArguments, Type[] genericTypes) { if (mi == null) { throw new ArgumentNullException("mi"); } IValue result; bool isDelegate = mi.DeclaringType.IsMulticastDelegate(); if (mi.IsGenericMethod) { var genericArguments = mi.GetGenericArguments(); if (!mi.IsGenericMethodDefinition && (genericTypes == null || genericTypes.Length != genericArguments.Length)) { genericTypes = genericArguments; } if (genericTypes == null || genericTypes.Length != genericArguments.Length) { throw new NotSupportedException("Brak automatycznego rozpoznawania typów generycznych (na razie)"); } // var pa = mi.GetParameters(); } else { genericTypes = new Type[0]; } if (mi is MethodInfo) { var mii = mi as MethodInfo; if (mii.IsExtensionMethod()) { var list = functionArguments.ToList(); if (targetObject == null) { throw new NotSupportedException(); } list.Insert(0, new FunctionArgument("", targetObject)); result = new CsharpMethodCallExpression(mii, null, list.ToArray(), genericTypes, isDelegate); } else { #warning 'poprawić z typami generycznymi !!!!!' result = new CsharpMethodCallExpression(mii, targetObject, functionArguments, genericTypes, isDelegate); } } else if (mi is ConstructorInfo) { // ReSharper disable once UnusedVariable var mii = mi as ConstructorInfo; throw new NotSupportedException(); } else { throw new NotSupportedException(); } result = Simplify(result); return(result); }
private IPhpValue TranslateAddInterval(IExternalTranslationContext ctx, CsharpMethodCallExpression src, int mnoznik) { var intervalString = GetIntervalString(ctx, src.Arguments[0], mnoznik); var interval = PhpMethodCallExpression.MakeConstructor("DateInterval", null, intervalString); interval.DontIncludeClass = true; var targetObject = ctx.TranslateValue(src.TargetObject); var methd = new PhpMethodCallExpression("date_add", null, targetObject, interval); return(methd); }
public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { if (src.MethodInfo.DeclaringType != typeof(DateTime)) { return(null); } var fn = src.MethodInfo.ToString(); if (src.MethodInfo.Name == "ToString") { var to = ctx.TranslateValue(src.TargetObject); var c = new PhpMethodCallExpression(to, "format", new PhpConstValue("Y-m-d H:i:s")); return(c); } if (fn == "System.TimeSpan op_Subtraction(System.DateTime, System.DateTime)") { var l = ctx.TranslateValue(src.Arguments[0].MyValue); var r = ctx.TranslateValue(src.Arguments[1].MyValue); var c = new PhpMethodCallExpression("date_diff", r, l); return(c); } if (fn == "Boolean op_GreaterThanOrEqual(System.DateTime, System.DateTime)") { return(new PhpBinaryOperatorExpression(">=", ctx.TranslateValue(src.Arguments[0]), ctx.TranslateValue(src.Arguments[1]))); } if (fn == "Boolean op_Inequality(System.DateTime, System.DateTime)") { return(new PhpBinaryOperatorExpression("!=", ctx.TranslateValue(src.Arguments[0]), ctx.TranslateValue(src.Arguments[1]))); } if (fn == "Boolean op_Equality(System.DateTime, System.DateTime)") { return(new PhpBinaryOperatorExpression("==", ctx.TranslateValue(src.Arguments[0]), ctx.TranslateValue(src.Arguments[1]))); } if (fn == "System.DateTime AddDays(Double)") { return(TranslateAddInterval(ctx, src, SecPerDay)); } if (fn == "System.DateTime AddHours(Double)") { return(TranslateAddInterval(ctx, src, SecPerHour)); } throw new NotImplementedException(fn); }
private IValue ImplicitConvert(IValue v, ModelExtensions2.MyInfo info) { var methodSymbol = info.GetMethodSymbol(); if (methodSymbol == null) { return(v); } var m = context.Roslyn_ResolveMethod(methodSymbol); var a = new CsharpMethodCallExpression(m as MethodInfo, null, new[] { new FunctionArgument("", v) }, null, false ); return(a); }
private void FillTagOpen(int argsFrom, IExternalTranslationContext ctx, CsharpMethodCallExpression src, string end) { var tagName = src.Arguments[0]; Append("<"); Append(tagName); for (var i = argsFrom + 1; i < src.Arguments.Length; i += 2) { var key = src.Arguments[i - 1]; var value = src.Arguments[i]; Append(" "); Append(key); Append("=\""); Append(value); Append("\""); } Append(end); }
protected override IPyStatement[] VisitMethodCallExpression(CsharpMethodCallExpression src) { var a = TransValue(src); return(MkArray(a)); //var u = new DotnetMethodCallTranslator(state); //PyMethodStatement PyMethod; //IPyValue simplaeValue; //bool ok = u.TryReplaceByPyDirectCall(src, out PyMethod, out simplaeValue); //if (ok) //{ // if (PyMethod == null) // throw new NotSupportedException("smooth horse"); // return MkArray(PyMethod); //} //var mi = src.MethodInfo; //{ // if (mi.IsStatic) // { // throw new Exception(src.GetType().FullName); // } // else // { // System.Diagnostics.Debug.Assert(src.TargetObject != null); // var PyTargetObject = TV(src.TargetObject); // var method = new PyMethodStatement(src.MethodInfo.Name); // method.TargetObject = TV(src.TargetObject); // new PyValueTranslator(state).CopyArguments(src.Arguments, method); // return MkArray(method); // } //} //if (state.CurrentType == src.MethodInfo.ReflectedType) //{ // var method = new PyMethodStatement(src.MethodInfo.Name); // method.TargetObject = new PyThisExpression(); // new PyValueTranslator(state).CopyArguments(src.Arguments, method); // return MkArray(method); //} //throw new Exception(src.GetType().FullName); }
PhpMethodCallExpression Make(IExternalTranslationContext ctx, CsharpMethodCallExpression src, bool addoffset) { var p = new List <IPhpValue>(src.Arguments.Length + 1); p.Add(ctx.TranslateValue(src.Arguments[0].MyValue)); p.Add(ctx.TranslateValue(src.Arguments[1].MyValue)); for (int i = 2; i < src.Arguments.Length; i++) { p.Add(ctx.TranslateValue(src.Arguments[i].MyValue)); if (addoffset && i == 2) { p.Add(new PhpDefinedConstExpression("PREG_OFFSET_CAPTURE", null)); } } var a = new PhpMethodCallExpression("preg_match", p.ToArray()); return(a); }
private static IPyValue Translate_Enumerable_Range(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { var info = MapNamedParameters(src); // mapujemy na arange var methods = typeof(Np).GetMethods(BindingFlags.Public | BindingFlags.Static) .ToDictionary(a => a.ToString(), a => a); var tmp = methods["System.Collections.Generic.List`1[System.Int32] ARange(Int32, Int32, Int32)"]; var v1 = info.GetArgumentValue(0); var v2 = info.GetArgumentValue(1); var max = new BinaryOperatorExpression(v1, v2, "+", typeof(int), null); var newCall = new CsharpMethodCallExpression(tmp, null, new[] { new FunctionArgument(v1), new FunctionArgument(max) }, null, false); return(ctx.TranslateValue(newCall)); }
public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { if (src.MethodInfo.DeclaringType != typeof(Preg)) { return(null); } var mn = src.MethodInfo.ToString(); Console.WriteLine(mn); switch (mn) { case "Lang.Php.PregMatchResult MatchWithOffset(System.String, System.String, System.Collections.Generic.Dictionary`2[System.Object,Lang.Php.PregMatchWithOffset] ByRef, Int32)": return(Make(ctx, src, true)); case "Lang.Php.PregMatchResult Match(System.String, System.String, System.Collections.Generic.Dictionary`2[System.Object,System.String] ByRef, Int32)": // case "Lang.Php.PregMatchResult Match(System.String, System.String, Int32)": case "Lang.Php.PregMatchResult Match(System.String, System.String)": return(Make(ctx, src, false)); default: return(null); } }
// Public Methods public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { if (src.MethodInfo.DeclaringType == typeof(Html)) { this.ctx = ctx; list = new List <IPhpValue>(); var methodName = src.MethodInfo.Name; var isecho = methodName.StartsWith("Echo"); if (isecho) { methodName = methodName.Substring(4); } if (methodName == "TagBound") { FillTagOpen(2, ctx, src, ">"); Append(src.Arguments[1]); FillTagEnd(src.Arguments[0]); return(MakeEchoIfNecessary(isecho)); } if (methodName == "Attributes") { for (var i = 1; i < src.Arguments.Length; i += 2) { var key = src.Arguments[i - 1]; var value = src.Arguments[i]; Append(" "); Append(key); Append("=\""); Append(value); Append("\""); } return(MakeEchoIfNecessary(isecho)); } if (methodName == "TagOpen" || methodName == "TagSingle") { var end = methodName == "TagOpen" ? ">" : " />"; FillTagOpen(1, ctx, src, end); return(MakeEchoIfNecessary(isecho)); } if (methodName == "TagOpenOpen") { FillTagOpen(1, ctx, src, ""); return(MakeEchoIfNecessary(isecho)); } if (methodName == "TagClose") { FillTagEnd(src.Arguments[0]); if (src.Arguments.Length == 2) { // throw new NotSupportedException(); Append("\r\n"); } return(MakeEchoIfNecessary(isecho)); } if (methodName == "Comment") { Append("<!-- "); Append(src.Arguments[0]); Append(" -->"); return(MakeEchoIfNecessary(isecho)); } if (methodName == "Pixels" || methodName == "Percent") { Append(src.Arguments[0]); Append(methodName == "Pixels" ? "px" : "%"); return(MakeEchoIfNecessary(isecho)); } if (methodName == "Mm") { Append(src.Arguments[0]); Append("mm"); return(MakeEchoIfNecessary(isecho)); } if (methodName == "Css") { for (var i = 1; i < src.Arguments.Length; i += 2) { var key = src.Arguments[i - 1]; var value = src.Arguments[i]; Append(key); Append(":"); Append(value); Append(";"); } return(MakeEchoIfNecessary(isecho)); } if (methodName == "CssBorder") { Append(src.Arguments[0]); Append(" "); Append(src.Arguments[1]); Append(" "); Append(src.Arguments[2]); return(MakeEchoIfNecessary(isecho)); } throw new NotSupportedException(); } return(null); }
private static IValue _Make_DotnetMethodCall(MethodBase mi, IValue targetObject, FunctionArgument[] functionArguments, Type[] genericTypes) { FunctionArgument[] ResolveExtensionMethod() { if (!(mi is MethodInfo methodInfo) || !methodInfo.IsExtensionMethod()) { return(functionArguments); } if (targetObject == null) { throw new NotSupportedException(); } var fa = new FunctionArgument("", targetObject, null); if (functionArguments == null || functionArguments.Length == 0) { return new[] { fa } } ; var list = functionArguments.ToList(); list.Insert(0, fa); return(list.ToArray()); } if (mi == null) { throw new ArgumentNullException(nameof(mi)); } functionArguments = ResolveExtensionMethod(); IValue result; var isDelegate = mi.DeclaringType.IsMulticastDelegate(); if (mi.IsGenericMethod) { var genericArguments = mi.GetGenericArguments(); if (!mi.IsGenericMethodDefinition && (genericTypes == null || genericTypes.Length != genericArguments.Length)) { genericTypes = genericArguments; } if (genericTypes == null || genericTypes.Length != genericArguments.Length) { var a = new GenericTypesResolver(mi, functionArguments); genericTypes = a.Find(); } } else { genericTypes = new Type[0]; } if (mi is MethodInfo mii) { if (mii.IsExtensionMethod()) { result = new CsharpMethodCallExpression(mii, null, functionArguments, genericTypes, isDelegate); } else { #warning 'poprawić z typami generycznymi !!!!!' result = new CsharpMethodCallExpression(mii, targetObject, functionArguments, genericTypes, isDelegate); } } else if (mi is ConstructorInfo) { // ReSharper disable once UnusedVariable var constructorInfo = mi as ConstructorInfo; throw new NotSupportedException(); } else { throw new NotSupportedException(); } result = Simplify(result); return(result); }
protected override IPhpValue VisitBinaryOperatorExpression(BinaryOperatorExpression src) { var leftType = src.Left.ValueType; var rightType = src.Right.ValueType; if (src.OperatorMethod != null) { if (!src.OperatorMethod.IsStatic) { throw new NotSupportedException(); } var a = new CsharpMethodCallExpression( src.OperatorMethod, null, new[] { new FunctionArgument("", src.Left, null), new FunctionArgument("", src.Right, null) }, new Type[0], false); var trans = TransValue(a); return(trans); } var leftValue = TransValue(src.Left); if (src.Operator == "as") { return(leftValue); } var rightValue = TransValue(src.Right); var ss1 = leftValue as PhpConstValue; var ss2 = rightValue as PhpConstValue; var isConst = ss1 != null && ss2 != null; // if (isConst) // return new PhpBinaryOperatorExpression(src.Operator, leftValue, rightValue); if (leftType == typeof(string) || rightType == typeof(string)) { var phpOperator = src.Operator == "+" ? "." : src.Operator; return(src.Operator == "+" && isConst ? (IPhpValue) new PhpConstValue(ss1.Value as string + ss2.Value) : new PhpBinaryOperatorExpression(phpOperator, leftValue, rightValue)); } if (isConst) { if (leftType == typeof(int) && rightType == typeof(int)) { var s1 = (int)ss1.Value; var s2 = (int)ss2.Value; switch (src.Operator) { case "+": return(new PhpConstValue(s1 + s2)); case "-": return(new PhpConstValue(s1 - s2)); case "*": return(new PhpConstValue(s1 * s2)); case "/": return(new PhpConstValue(s1 / s2)); } } if (leftType.IsEnum && rightType.IsEnum && src.Operator == "|") { var s1 = (int)ss1.Value; var s2 = (int)ss2.Value; return(new PhpConstValue(Enum.ToObject(leftType, s1 | s2))); } } return(new PhpBinaryOperatorExpression(src.Operator, leftValue, rightValue)); }
public override IPyValue TranslateToPython(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { string GetOperatorName() { if (!src.MethodInfo.IsSpecialName) { return(null); } switch (src.MethodInfo.Name) { case "op_Addition": return("+"); case "op_Subtraction": return("-"); case "op_Multiply": return("*"); case "op_Division": return("/"); case "op_Equality": return("=="); case "op_Inequality ": return("!="); case "op_Implicit": return("i"); } return(null); } var ats = src.MethodInfo.GetCustomAttribute <DirectCallAttribute>(); if (ats == null) { return(null); } var name = ats.Name; var opName = GetOperatorName(); if (string.IsNullOrEmpty(name)) { name = src.MethodInfo.Name; } if (opName != null) { name = opName; } var cSharpParameters = src.MethodInfo.GetParameters(); var agrsIndexByName = Enumerable.Range(0, cSharpParameters.Length) .ToDictionary(a => cSharpParameters[a].Name, a => a); var argsValues = new Argument[cSharpParameters.Length]; var info = MapNamedParameters(src); for (var index = 0; index < src.Arguments.Length; index++) { var i = src.Arguments[index]; var pythonArgName = cSharpParameters[index].Name; if (!string.IsNullOrEmpty(i.ExplicitName)) { pythonArgName = i.ExplicitName; } var b = ctx.TranslateValue(i.MyValue); var pyIndex = agrsIndexByName[pythonArgName]; argsValues[pyIndex] = new Argument { Value = b, Name = cSharpParameters[pyIndex].Name }; } if (opName != null) { if (opName == "i" && argsValues.Length == 1) { return(argsValues[0].Value); } switch (argsValues.Length) { case 2: return(new PyBinaryOperatorExpression(opName, argsValues[0].Value, argsValues[1].Value)); case 1: return(new PyUnaryOperatorExpression(argsValues[0].Value, opName)); } throw new NotSupportedException("Unable to convert operator " + opName + " with " + argsValues.Length + " arguments"); } var result = new PyMethodCallExpression(name); var setName = false; foreach (var i in argsValues) { if (i == null) { setName = true; continue; } var item = new PyMethodInvokeValue(i.Value); if (setName) { item.Name = i.Name; } result.Arguments.Add(item); } result.TrySetTargetObjectFromModule(src.MethodInfo.DeclaringType); return(result); }