private void VisitAndConvertArgs(NyaParser.FunctionExpContext context, Type[] argTypes) { if (context.arguments() == null) { return; } var args = context.arguments().children.OfType <NyaParser.ArgumentContext>().ToList(); for (int i = 0; i < args.Count; i++) { Type t = (Type)Visit(args[i]); if (t != argTypes[i]) { TypeHelper.TryConvert(_ilg, t, argTypes[i]); } } }
public override object VisitFunctionExp([NotNull] NyaParser.FunctionExpContext context) { // TODO: Account for different parameters String name = context.identifier().GetText(); Type[] argTypes; Type retType; switch (name) { case "sqrt": argTypes = new[] { typeof(double) }; VisitAndConvertArgs(context, argTypes); return(CallMethod(typeof(Math), "Sqrt", argTypes, new Type[] { })); case "log": argTypes = new[] { typeof(double) }; VisitAndConvertArgs(context, argTypes); return(CallMethod(typeof(Math), "Log10", argTypes, new Type[] { })); case "print": argTypes = new[] { typeof(string) }; VisitAndConvertArgs(context, argTypes); return(CallMethod(typeof(Console), "WriteLine", argTypes, new Type[] { })); } Type callingType; bool isThisCall = methodTypeStack.Count == 0; if (isThisCall) { callingType = _currTypeBuilder; _ilg.Emit(OpCodes.Ldarg_0); } else { callingType = methodTypeStack.Peek(); } argTypes = VisitMethodArgs(context.arguments()); bool wasStatic; retType = CallMethod(callingType, name, argTypes, new Type[] { }, out wasStatic); if (wasStatic && isThisCall) { // I hope this works _ilg.Emit(OpCodes.Pop); } return(retType); }