Path: redb-route/redb.Route/Expressions/Ast/AstNode.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/Ast/AstNode.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using redb.Route.Abstractions;
namespace redb.Route.Expressions.Ast;
///
/// Abstract base class for all AST (Abstract Syntax Tree) nodes.
///
public abstract class AstNode
{
private Func? _compiled;
///
/// Evaluates this AST node against the given exchange context. The tree under the node is compiled once, by the same
/// compiler every expression of the engine goes through, and the compiled delegate is evaluated: there is one
/// evaluator, and a node evaluated directly gives what the engine gives (findings register row 27).
///
/// The exchange context used for expression resolution.
/// The result of evaluating this node.
public virtual object? Evaluate(IExchange exchange)
=> (_compiled ??= ExpressionResolver.CompileAstTree(this))(exchange);
}
///
/// AST node representing a literal (constant) value.
///
public class LiteralNode : AstNode
{
///
/// Gets the literal value held by this node.
///
public object? Value { get; }
///
/// Initializes a new instance of the class.
///
/// The literal value.
public LiteralNode(object? value)
{
Value = value;
}
///
public override string ToString() => Value?.ToString() ?? "null";
}
///
/// AST node representing an identifier (variable reference).
///
public class IdentifierNode : AstNode
{
///
/// Gets the name of the identifier.
///
public string Name { get; }
///
/// Initializes a new instance of the class.
///
/// The identifier name.
public IdentifierNode(string name)
{
Name = name;
}
///
public override string ToString() => Name;
}
///
/// AST node representing a binary operation (e.g. addition, comparison, logical operations).
///
public class BinaryOperationNode : AstNode
{
///
/// Gets the left operand of the binary operation.
///
public AstNode Left { get; }
///
/// Gets the operator symbol (e.g. "+", "==", "AND").
///
public string Operator { get; }
///
/// Gets the right operand of the binary operation.
///
public AstNode Right { get; }
///
/// Initializes a new instance of the class.
///
/// The left operand node.
/// The operator string.
/// The right operand node.
public BinaryOperationNode(AstNode left, string op, AstNode right)
{
Left = left;
Operator = op;
Right = right;
}
///
public override string ToString() => $"({Left} {Operator} {Right})";
}
///
/// AST node representing a unary (prefix) operation (e.g. negation, logical NOT, prefix increment/decrement).
///
public class UnaryOperationNode : AstNode
{
///
/// Gets the unary operator symbol (e.g. "+", "-", "!", "NOT", "++", "--").
///
public string Operator { get; }
///
/// Gets the operand of the unary operation.
///
public AstNode Operand { get; }
///
/// Initializes a new instance of the class.
///
/// The unary operator string.
/// The operand node.
public UnaryOperationNode(string op, AstNode operand)
{
Operator = op;
Operand = operand;
}
///
public override string ToString() => $"{Operator}({Operand})";
}
///
/// AST node representing a function call with arguments.
///
public class FunctionCallNode : AstNode
{
///
/// Gets the name of the function being called.
///
public string Name { get; }
///
/// Gets the list of argument nodes passed to the function.
///
public List Arguments { get; }
///
/// Initializes a new instance of the class.
///
/// The function name.
/// The list of argument AST nodes.
public FunctionCallNode(string name, List arguments)
{
Name = name;
Arguments = arguments;
}
///
public override string ToString() => $"{Name}({string.Join(", ", Arguments)})";
}
///
/// AST node representing property access on an object (e.g. obj.PropertyName).
///
public class PropertyAccessNode : AstNode
{
///
/// Gets the AST node representing the object being accessed.
///
public AstNode Object { get; }
///
/// Gets the name of the property being accessed.
///
public string PropertyName { get; }
///
/// Initializes a new instance of the class.
///
/// The object node whose property is accessed.
/// The name of the property to access.
public PropertyAccessNode(AstNode obj, string propertyName)
{
Object = obj;
PropertyName = propertyName;
}
///
public override string ToString() => $"{Object}.{PropertyName}";
}
///
/// A namespaced call, bean:name.Method(arg, ...): the bean from the context registry
/// and its with the argument expressions. Compiled by the AST compiler, which resolves it.
///
internal sealed class BeanCallNode(string ns, string reference, string method, List arguments, int position) : AstNode
{
/// The namespace, bean.
public string Namespace { get; } = ns;
/// The registry name, without #.
public string Reference { get; } = reference;
/// The method name.
public string Method { get; } = method;
/// The argument expressions, in order.
public List Arguments { get; } = arguments;
/// Zero-based position of the namespace within the expression.
public int Position { get; } = position;
///
public override string ToString() => $"{Namespace}:{Reference}.{Method}({string.Join(", ", Arguments)})";
}
///
/// value range 'from..to' and value not range 'from..to': a numeric value within both ends, inclusive, as
/// in Apache Camel's Simple. The ends are read when the expression is compiled.
///
internal sealed class RangeNode(AstNode value, string literal, decimal from, decimal to, bool negated) : AstNode
{
/// The value tested.
public AstNode Value { get; } = value;
/// The literal as written, without its quotes.
public string Literal { get; } = literal;
/// The lower end, inclusive.
public decimal From { get; } = from;
/// The upper end, inclusive.
public decimal To { get; } = to;
/// True for not range.
public bool Negated { get; } = negated;
///
public override string ToString() => $"{Value} {(Negated ? "not range" : "range")} '{Literal}'";
}
///
/// value.method(args): a method called on a value, a built-in dot helper (length, contains, ...) or a
/// public method of the value's type.
///
internal sealed class MethodCallNode(AstNode target, string method, List arguments, string written, int position) : AstNode
{
/// The value the method is called on.
public AstNode Target { get; } = target;
/// The method's name as written.
public string Method { get; } = method;
/// The argument expressions, in order.
public List Arguments { get; } = arguments;
/// The call as written, for error messages (header.o.Boom()).
public string Written { get; } = written;
/// Zero-based position of the call within the expression.
public int Position { get; } = position;
///
public override string ToString() => $"{Target}.{Method}({string.Join(", ", Arguments)})";
}
/// env:NAME: the OS environment variable of that name, read when the expression is evaluated.
internal sealed class EnvNode(string name, int position) : AstNode
{
/// The variable's name.
public string Name { get; } = name;
/// Zero-based position of the namespace within the expression.
public int Position { get; } = position;
///
public override string ToString() => $"env:{Name}";
}
/// ref:name: the object registered under that name in the route context's registry.
internal sealed class RegistryRefNode(string reference, int position) : AstNode
{
/// The registry name, without #.
public string Reference { get; } = reference;
/// Zero-based position of the namespace within the expression.
public int Position { get; } = position;
///
public override string ToString() => $"ref:{Reference}";
}