/// <inheritdoc/> protected override string Convert(Expression <TMethod> expression) { var visitor = new DelegateVisitor(this, 1); visitor.Visit(expression.Body); return(visitor.Resolution); }
public void OnWebApiCommand() { string input = "namespace Foo { webapi Bar { command void Baz(); } }"; var results = TypesLanguageCompiler.Compile(new[] { new TypesLanguageCompilerInput(input, "") }, Enumerable.Empty<Assembly>()); if (!results.WasSuccessful || !results.HasValue) Assert.Fail("Expected compilation success.\r\n" + results.Observations.Delimit("\r\n")); string output = ""; var visitor = new DelegateVisitor { OnWebApiCommand = (cmd, b) => output += cmd.Name }; visitor.Visit(results.Value); Assert.AreEqual("Baz", output); }
protected override Expression VisitUnary(UnaryExpression node) { if (node.NodeType != ExpressionType.Not) { throw new NotSupportedException($"Unary expression {node.NodeType}"); } var visitor = new DelegateVisitor(_converter, _methodDepth, _objectInstance); visitor.Visit(node.Operand); Resolution += $"not ({visitor.Resolution})"; return(node); }
protected override Expression VisitBinary(BinaryExpression node) { var op = node.NodeType switch { ExpressionType.Add => "+", ExpressionType.AndAlso => "and", ExpressionType.OrElse => "or", _ => throw new NotSupportedException($"Binary operator {node.NodeType}") }; var leftVisitor = new DelegateVisitor(_converter, _methodDepth, _objectInstance); leftVisitor.Visit(node.Left); var rightVisitor = new DelegateVisitor(_converter, _methodDepth, _objectInstance); rightVisitor.Visit(node.Right); Resolution += $"{leftVisitor.Resolution} {op} {rightVisitor.Resolution}"; return(node); }
protected override Expression VisitMethodCall(MethodCallExpression node) { var objectInstance = (node.Object is ConstantExpression constant) ? constant.Value : _objectInstance; var methodCallFormatter = new Func <string, ReadOnlyCollection <Expression>, string>((name, args) => { var arguments = args.Select(arg => { var argVisitor = new DelegateVisitor(_converter, _methodDepth + 1, objectInstance); argVisitor.Visit(arg); return(argVisitor.Resolution); }); return(string.Format(_converter._methodCallFormat, name, string.Join(", ", arguments))); }); var supportMethods = new Func <IEnumerable <MethodInfo> >(() => typeof(Installation).GetMethods(BindingFlags.Instance | BindingFlags.Public) .Where(m => m.GetCustomAttribute <BuiltInSymbolAttribute>() != null)); switch (_methodDepth) { case 1: var supportMethod = supportMethods().FirstOrDefault(m => m.Name == node.Method.Name); if (supportMethod == null) { var handler = BindingSearch.Flags .Select(f => objectInstance.GetType().GetMethod(node.Method.Name, f)) .FirstOrDefault(m => m != null); if (handler == null) { var msg = $"Only class instance methods may be invoked; {node.Method.Name} is not supported"; throw new NotSupportedException(msg); } _converter._visitMethodHandler(node.Method); } break; case 2: var expandConstantMethod = supportMethods().FirstOrDefault(m => m.Name == nameof(Installation.ExpandConstant)); if (expandConstantMethod == null || node.Method.MetadataToken != expandConstantMethod.MetadataToken) { var msg = $"Only the method call {nameof(Installation.ExpandConstant)} is allowed at this depth; {node.Method.Name} is not supported"; throw new NotSupportedException(msg); } break; default: methodCallFormatter = (name, args) => { var visitor = new InvokingVisitor(objectInstance); visitor.Visit(node); return(visitor.Results[0] is string?$"'{visitor.Results[0]}'" : visitor.Results[0].ToString()); }; break; } var methodName = node.Method.GetCustomAttribute <AliasAttribute>()?.Name ?? node.Method.Name; Resolution += methodCallFormatter(methodName, node.Arguments); return(node); }