Path: redb-route/redb.Route/Expressions/ExpressionResolver.AstIntegration.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/ExpressionResolver.AstIntegration.cs
----
using System;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using redb.Route.Abstractions;
using redb.Route.Telemetry;
using redb.Route.Expressions.Ast;
using SysExpression = System.Linq.Expressions.Expression;
namespace redb.Route.Expressions;
///
/// Partial class ExpressionResolver — AST parsing, compilation, and integration.
///
public static partial class ExpressionResolver
{
#region AST public wrappers for use in CompileAstNode
// Make private methods accessible for use in AST.
// Use different names to avoid recursion.
///
/// Applies addition via AST delegation.
///
/// Left operand.
/// Right operand.
/// Result of the addition.
public static object? Ast_ApplyAddition(object? left, object? right) => _applyAddition(left, right);
///
/// Applies subtraction via AST delegation.
///
/// Left operand.
/// Right operand.
/// Result of the subtraction.
public static object? Ast_ApplySubtraction(object? left, object? right) => _applySubtraction(left, right);
///
/// Applies multiplication via AST delegation.
///
/// Left operand.
/// Right operand.
/// Result of the multiplication.
public static object? Ast_ApplyMultiplication(object? left, object? right) => _applyMultiplication(left, right);
///
/// Applies division via AST delegation.
///
/// Left operand.
/// Right operand.
/// Result of the division.
public static object? Ast_ApplyDivision(object? left, object? right) => _applyDivision(left, right);
///
/// Applies unary plus via AST delegation.
///
/// The operand value.
/// Result of unary plus.
public static object? Ast_ApplyUnaryPlus(object? value) => _applyUnaryPlus(value);
///
/// Applies unary minus via AST delegation.
///
/// The operand value.
/// Result of unary minus.
public static object? Ast_ApplyUnaryMinus(object? value) => _applyUnaryMinus(value);
///
/// Applies unary NOT via AST delegation.
///
/// The operand value.
/// Result of unary NOT.
public static object? Ast_ApplyUnaryNot(object? value) => _applyUnaryNot(value);
///
/// Checks equality of two values via AST delegation.
///
/// Left operand.
/// Right operand.
/// true if the values are equal; otherwise false .
public static bool Ast_AreEqual(object? left, object? right) => _areEqual(left, right);
///
/// Returns true if the values are NOT equal.
///
public static bool Ast_AreNotEqual(object? left, object? right) => !_areEqual(left, right);
///
/// Returns true if left is greater than right.
///
public static bool Ast_GreaterThan(object? left, object? right) => _compareNumeric(left, right) > 0;
///
/// Returns true if left is less than right.
///
public static bool Ast_LessThan(object? left, object? right) => _compareNumeric(left, right) < 0;
///
/// Returns true if left is greater than or equal to right.
///
public static bool Ast_GreaterThanOrEqual(object? left, object? right) => _compareNumeric(left, right) >= 0;
///
/// Returns true if left is less than or equal to right.
///
public static bool Ast_LessThanOrEqual(object? left, object? right) => _compareNumeric(left, right) <= 0;
///
/// Evaluates logical AND between two values.
///
public static bool Ast_LogicalAnd(object? left, object? right)
{
if (_TryConvertToBool(left, out var l) && _TryConvertToBool(right, out var r))
return l && r;
return false;
}
///
/// Evaluates logical OR between two values.
///
public static bool Ast_LogicalOr(object? left, object? right)
{
if (_TryConvertToBool(left, out var l) && _TryConvertToBool(right, out var r))
return l || r;
return false;
}
///
/// Evaluates logical XOR between two values.
///
public static bool Ast_LogicalXor(object? left, object? right)
{
if (_TryConvertToBool(left, out var l) && _TryConvertToBool(right, out var r))
return l ^ r;
return false;
}
///
/// Returns the left operand if it is not null; otherwise returns the right operand (null-coalescing).
///
public static object? Ast_NullCoalesce(object? left, object? right)
=> left ?? right;
///
/// Concatenates the string representations of all arguments.
///
/// The values to concatenate.
/// The concatenated string.
public static object? Ast_Concat(params object?[] args)
=> string.Concat(args.Select(a => a?.ToString() ?? string.Empty));
/// Converts a string to upper case.
public static object? Ast_Upper(object? value) => value?.ToString()?.ToUpperInvariant();
/// Converts a string to lower case.
public static object? Ast_Lower(object? value) => value?.ToString()?.ToLowerInvariant();
/// Trims whitespace from a string.
public static object? Ast_Trim(object? value) => value?.ToString()?.Trim();
/// Returns the length of a string or collection.
public static object? Ast_Length(object? value)
{
if (value == null) return null;
if (value is string s) return s.Length;
if (value is System.Collections.ICollection col) return col.Count;
if (value is System.Collections.IEnumerable en)
return System.Linq.Enumerable.Count(System.Linq.Enumerable.Cast(en));
return value.ToString()?.Length;
}
/// Extracts a substring.
public static object? Ast_Substring(object? value, object? start, object? length)
{
var str = value?.ToString();
if (str == null) return null;
if (!TryConvertToDouble(start, out var startIdx)) return str;
var s = (int)startIdx;
if (s < 0) s = 0;
if (s >= str.Length) return string.Empty;
if (length != null && TryConvertToDouble(length, out var lenVal))
{
var len = (int)lenVal;
if (len <= 0) return string.Empty;
return str.Substring(s, Math.Min(len, str.Length - s));
}
return str.Substring(s);
}
/// Returns the absolute value.
public static object? Ast_Abs(object? value)
{
if (TryConvertToDouble(value, out var d))
return d == (int)d ? (object)(int)Math.Abs(d) : Math.Abs(d);
return null;
}
/// Rounds a number to the specified number of digits.
public static object? Ast_Round(object? value, object? digits)
{
if (!TryConvertToDouble(value, out var v)) return null;
int d = 0;
if (digits != null && TryConvertToDouble(digits, out var dv)) d = (int)dv;
return Math.Round(v, d);
}
/// Returns the minimum of two values.
public static object? Ast_Min(object? a, object? b)
=> Ast_Extremum(a, b, Math.Min);
/// Returns the maximum of two values, or of every numeric value in a collection.
public static object? Ast_Max(object? a, object? b)
=> Ast_Extremum(a, b, Math.Max);
///
/// Shared body of min / max : two scalars compare directly; a single collection
/// argument folds over its numeric items the way sum and avg already did. Until
/// 2026-08-29 the collection form returned null, so min(property.nums) silently read as
/// "no value" while sum(property.nums) worked.
///
private static object? Ast_Extremum(object? a, object? b, Func pick)
{
if (b is null && a is System.Collections.IEnumerable items && a is not string)
{
double? result = null;
foreach (var item in items)
{
if (!TryConvertToDouble(item, out var v))
continue;
result = result is null ? v : pick(result.Value, v);
}
return result;
}
if (TryConvertToDouble(a, out var va) && TryConvertToDouble(b, out var vb))
return pick(va, vb);
return null;
}
/// Checks if a string contains a substring (case-insensitive).
public static object? Ast_Contains(object? value, object? search)
{
var s = value?.ToString();
var q = search?.ToString();
if (s == null || q == null) return false;
return s.Contains(q, StringComparison.OrdinalIgnoreCase);
}
/// Checks if a string starts with a prefix (case-insensitive).
public static object? Ast_StartsWith(object? value, object? prefix)
{
var s = value?.ToString();
var p = prefix?.ToString();
if (s == null || p == null) return false;
return s.StartsWith(p, StringComparison.OrdinalIgnoreCase);
}
/// Checks if a string ends with a suffix (case-insensitive).
public static object? Ast_EndsWith(object? value, object? suffix)
{
var s = value?.ToString();
var x = suffix?.ToString();
if (s == null || x == null) return false;
return s.EndsWith(x, StringComparison.OrdinalIgnoreCase);
}
/// Replaces all occurrences of a substring.
public static object? Ast_Replace(object? value, object? oldStr, object? newStr)
{
var s = value?.ToString();
var o = oldStr?.ToString();
if (s == null || o == null) return s;
return s.Replace(o, newStr?.ToString() ?? string.Empty);
}
/// Returns the current UTC date/time.
public static object? Ast_Now() => DateTime.UtcNow;
/// Formats a date value using the specified format string.
public static object? Ast_DateFormat(object? value, object? format)
{
var fmt = format?.ToString() ?? "yyyy-MM-dd";
if (value is DateTime dt)
return dt.ToString(fmt, System.Globalization.CultureInfo.InvariantCulture);
if (value is DateTimeOffset dto)
return dto.ToString(fmt, System.Globalization.CultureInfo.InvariantCulture);
if (DateTime.TryParse(value?.ToString(), System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out var parsed))
return parsed.ToString(fmt, System.Globalization.CultureInfo.InvariantCulture);
return value?.ToString();
}
///
/// Formats a value with a .NET format string and an optional culture name:
/// format(header.price, 'N2') is invariant, format(header.price, 'N2', 'ru-RU') is
/// the named locale. This is the explicit way to get locale-specific text now that ${...}
/// renders culture-invariant; a string that holds a number or a date is parsed invariantly first,
/// anything else is returned as its own text.
///
public static object? Ast_Format(object? value, object? format, object? culture)
{
if (value is null) return null;
var formatString = format?.ToString();
var cultureInfo = ExpressionFormatting.Culture(culture?.ToString());
if (string.IsNullOrEmpty(formatString))
return value is IFormattable plain ? plain.ToString(null, cultureInfo) : value.ToString();
if (value is IFormattable formattable)
return formattable.ToString(formatString, cultureInfo);
var text = value.ToString();
if (decimal.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var number))
return number.ToString(formatString, cultureInfo);
if (DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
return date.ToString(formatString, cultureInfo);
return text;
}
/// Adds a time interval to a date value.
public static object? Ast_DateAdd(object? value, object? amount, object? unit)
{
DateTime baseDate;
if (value is DateTime d1) baseDate = d1;
else if (value is DateTimeOffset d2) baseDate = d2.UtcDateTime;
else if (DateTime.TryParse(value?.ToString(), System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out var p)) baseDate = p;
else return null;
if (!TryConvertToDouble(amount, out var amt)) return null;
var u = unit?.ToString()?.ToLowerInvariant() ?? "days";
return u switch
{
"days" or "day" => baseDate.AddDays(amt),
"hours" or "hour" => baseDate.AddHours(amt),
"minutes" or "minute" => baseDate.AddMinutes(amt),
"seconds" or "second" => baseDate.AddSeconds(amt),
"months" or "month" => baseDate.AddMonths((int)amt),
"years" or "year" => baseDate.AddYears((int)amt),
_ => null
};
}
///
/// Modulo over the same numeric coercion as division: both operands convert
/// invariantly, a zero divisor yields null (as division does), a whole result collapses to int.
///
public static object? Ast_ApplyModulo(object? left, object? right)
{
if (left is null || right is null)
return null;
if (!TryConvertToNumber(left, out var leftNumber) || !TryConvertToNumber(right, out var rightNumber))
return null;
if (Math.Abs(rightNumber) < double.Epsilon)
return null;
var result = leftNumber % rightNumber;
if (Math.Abs(result - Math.Floor(result)) < double.Epsilon)
return Convert.ToInt32(result);
return result;
}
///
/// A fresh GUID as text. Impure by design: every evaluation yields a new value
/// (the expression cache stores compiled delegates, never results). The optional argument is the
/// standard GUID format specifier ('D' default, 'N' compact, 'B' , 'P' );
/// an unknown specifier throws rather than falling back silently.
///
public static object? Ast_Uuid(object? format)
=> Guid.NewGuid().ToString(format?.ToString() ?? "D");
///
/// Difference between two dates, first minus second, in the requested unit.
/// Dates coerce exactly as does (DateTime, DateTimeOffset, or an
/// invariantly parsed string); the unit vocabulary is 's plus
/// milliseconds (ms ); an unknown unit yields null, matching .
/// A whole result collapses to int, matching division.
///
public static object? Ast_DateDiff(object? left, object? right, object? unit)
{
if (!TryCoerceDate(left, out var a) || !TryCoerceDate(right, out var b))
return null;
var span = a - b;
var u = unit?.ToString()?.ToLowerInvariant() ?? "days";
double result = u switch
{
"days" or "day" => span.TotalDays,
"hours" or "hour" => span.TotalHours,
"minutes" or "minute" => span.TotalMinutes,
"seconds" or "second" => span.TotalSeconds,
"milliseconds" or "millisecond" or "ms" => span.TotalMilliseconds,
_ => double.NaN
};
if (double.IsNaN(result))
return null;
if (Math.Abs(result - Math.Floor(result)) < double.Epsilon)
return Convert.ToInt32(result);
return result;
}
private static bool TryCoerceDate(object? value, out DateTime date)
{
switch (value)
{
case DateTime d:
date = d;
return true;
case DateTimeOffset o:
date = o.UtcDateTime;
return true;
default:
return DateTime.TryParse(value?.ToString(), System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out date);
}
}
/// Sums all numeric values in a collection.
public static object? Ast_Sum(object? value)
{
if (value is System.Collections.IEnumerable en && value is not string)
{
double total = 0;
foreach (var item in en)
if (TryConvertToDouble(item, out var v)) total += v;
return total;
}
return null;
}
/// Averages all numeric values in a collection.
public static object? Ast_Avg(object? value)
{
if (value is System.Collections.IEnumerable en && value is not string)
{
double total = 0; int count = 0;
foreach (var item in en)
if (TryConvertToDouble(item, out var v)) { total += v; count++; }
return count > 0 ? total / count : (object?)null;
}
return null;
}
/// Counts elements in a collection.
public static object? Ast_Count(object? value)
{
if (value == null) return 0;
if (value is System.Collections.ICollection col) return col.Count;
if (value is System.Collections.IEnumerable en && value is not string)
return System.Linq.Enumerable.Count(System.Linq.Enumerable.Cast(en));
return 1;
}
/// Evaluates a logical() function — converts a value to bool.
public static object? Ast_Logical(object? value, IExchange exchange)
{
if (TryConvertToBool(value, out var result))
return result;
return false;
}
/// Evaluates a jpath() function via AST delegation.
public static object? Ast_JPath(object? path, IExchange exchange)
{
var pathStr = path?.ToString();
if (pathStr == null) return null;
return ApplyJPath(exchange, pathStr);
}
///
/// Evaluates the two-argument jpath(path, source) , which reads the source instead of the
/// message body. The source arrives already evaluated, as a value.
///
public static object? Ast_JPathFrom(object? path, object? source, IExchange exchange)
{
var pathStr = path?.ToString();
if (pathStr == null) return null;
// The language is lenient about missing data: the one-argument form yields null on a null
// body, and a named source that produced nothing is the same situation with the data
// missing from somewhere else. The strict reading lives on the object form, where
// From(...) still fails loudly.
if (source is null) return null;
// Same per-call-site cache as the one-argument form: the path is constant, the input is not.
var expression = JsonPathExpressions.GetOrAdd(pathStr, static p => new JsonPathExpression(p));
return expression.EvaluateOn(source, fromSource: true);
}
/// Evaluates an xpath() function via AST delegation.
public static object? Ast_XPath(object? path, IExchange exchange)
{
var pathStr = path?.ToString();
if (pathStr == null) return null;
return ApplyXPath(exchange, pathStr);
}
///
/// Evaluates stats(target, metric) — endpoint statistics as a value in the expression
/// language, so a route can branch on its own measurements:
/// Filter("stats('direct:target', 'health') != 'Critical'") .
///
///
/// target is an endpoint URI or 'current' (the route processing the exchange);
/// metric names an reading,
/// case-insensitive. Unlike a missing data source in xpath() , every failure here is an
/// authoring error — a route that asks for measurements it cannot get should say so loudly,
/// not route messages on a silent null.
///
public static object? Ast_Stats(object? target, object? metric, IExchange exchange)
{
var targetText = target?.ToString();
var metricText = metric?.ToString();
if (string.IsNullOrWhiteSpace(targetText) || string.IsNullOrWhiteSpace(metricText))
throw new InvalidOperationException("stats() needs a target ('current' or an endpoint URI) and a metric name.");
var context = exchange.Context
?? throw new InvalidOperationException(
"stats(): the exchange carries no route context. Only an exchange that entered a " +
"route can read statistics; one built by hand cannot.");
// The OpenTelemetry layer, addressed as otel:instrument[@route][/step]. Its EIP counters and
// .Metered() durations exist nowhere else, and a markup route has no other way to reach them.
if (targetText.StartsWith(OtelPrefix, StringComparison.OrdinalIgnoreCase))
return ReadOtelPoint(targetText[OtelPrefix.Length..], metricText, exchange, context);
IEndpointStatistics? stats;
if (targetText.Equals("current", StringComparison.OrdinalIgnoreCase))
{
var routeId = exchange.RouteId
?? throw new InvalidOperationException("stats('current'): the exchange has no route id.");
stats = (context as Core.RouteContext)?.GetRoute(routeId)?.Endpoint as IEndpointStatistics;
if (stats is null)
throw new InvalidOperationException(
$"stats('current'): route '{routeId}' was not found or its endpoint keeps no statistics.");
}
else
{
stats = context.GetEndpoint(targetText) as IEndpointStatistics;
if (stats is null)
throw new InvalidOperationException(
$"stats(): endpoint '{targetText}' keeps no statistics.");
}
return ReadStatistic(stats, metricText);
}
/// Prefix that sends stats() to the OpenTelemetry layer instead of endpoint statistics.
internal const string OtelPrefix = "otel:";
///
/// Evaluates stats('otel:instrument[@route][/step]', field) — one reading of one instrument of the
/// redb.Route meter (METRICS_IN_ROUTE_PLAN, question 2). Without @route the instrument is read
/// for the route processing the exchange; field is count, sum, min, max or last.
///
///
/// The in-process subscriber stays opt-in, so a missing one is an authoring error and says which call turns
/// it on. An instrument nobody has measured yet reads as zero: that is an answer, not a failure.
///
internal static object? ReadOtelPoint(string spec, string field, IExchange exchange, IRouteContext context)
{
var snapshot = context.GetMetricsSnapshot()
?? throw new InvalidOperationException(
"stats('otel:…'): the in-process metrics subscriber is not running. Call context.UseMetricsSnapshot() " +
"(or services.AddMetricsSnapshot()) before a route reads the OpenTelemetry layer.");
string? step = null;
var slash = spec.IndexOf('/');
if (slash >= 0)
{
step = spec[(slash + 1)..];
spec = spec[..slash];
}
string? routeId = null;
var at = spec.IndexOf('@');
if (at >= 0)
{
routeId = spec[(at + 1)..];
spec = spec[..at];
}
var instrument = spec.Trim();
if (instrument.Length == 0)
throw new InvalidOperationException(
"stats('otel:…'): the instrument name is empty. Write otel:instrument[@route][/step], " +
"e.g. otel:redb.route.step.duration/enrich.");
routeId ??= exchange.RouteId;
var point = snapshot.Point(instrument, routeId, step);
return ReadOtelField(point, field);
}
/// The fields of a snapshot point stats('otel:…') understands.
internal static object? ReadOtelField(MetricSnapshotPoint? point, string field) =>
field.ToLowerInvariant() switch
{
"count" => point?.Count ?? 0L,
"sum" => point?.Sum ?? 0d,
"min" => point?.Min ?? 0d,
"max" => point?.Max ?? 0d,
"last" => point?.Last ?? 0d,
_ => throw new ArgumentException(
$"stats('otel:…'): unknown field '{field}'. Known: count, sum, min, max, last.")
};
/// Whether names a reading of a snapshot point.
internal static bool IsKnownOtelField(string field) =>
field.ToLowerInvariant() is "count" or "sum" or "min" or "max" or "last";
///
/// Evaluates messageHistory(kind) — the trail the exchange left through the route, as a value.
/// table (the default) is the dump Camel prints on failure, compact one line, json an
/// array; count , totalMs and slowestMs are numbers, so markup can branch on cost, and
/// slowest / lastNode name a step.
///
///
/// Message history is opt-in (RouteEngineOptions.EnableMessageHistory , .MessageHistory() ), so an
/// exchange that recorded none reads as an empty string and zero — the one place this language answers a
/// missing measurement instead of failing. An unknown kind is still an authoring error.
///
public static object? Ast_MessageHistory(object? kind, IExchange exchange)
{
var kindText = kind?.ToString();
if (string.IsNullOrWhiteSpace(kindText))
kindText = "table";
kindText = kindText.Trim();
if (!IsKnownHistoryKind(kindText))
throw new ArgumentException(HistoryKindMessage(kindText));
var entries = Core.MessageHistory.GetEntries(exchange);
return kindText.ToLowerInvariant() switch
{
"table" => Core.MessageHistory.Format(exchange),
"compact" => string.Join(" > ", entries.Select(e => e.Label)),
"json" => System.Text.Json.JsonSerializer.Serialize(entries.Select(e => new
{
routeId = e.RouteId,
nodeId = e.NodeId,
label = e.Label,
elapsedMs = e.ElapsedMs,
})),
"count" => entries.Count,
"totalms" => entries.Sum(e => e.ElapsedMs),
"slowestms" => entries.Count == 0 ? 0d : entries.Max(e => e.ElapsedMs),
"slowest" => entries.Count == 0 ? string.Empty : entries.OrderByDescending(e => e.ElapsedMs).First().Label,
"lastnode" => entries.Count == 0 ? string.Empty : entries[^1].Label,
_ => throw new ArgumentException(HistoryKindMessage(kindText)),
};
}
/// Whether names a shape of the message history.
internal static bool IsKnownHistoryKind(string kind) =>
kind.ToLowerInvariant() is "table" or "compact" or "json" or "count" or "totalms"
or "slowestms" or "slowest" or "lastnode";
private static string HistoryKindMessage(string kind) =>
$"messageHistory(): unknown kind '{kind}'. Known: table, compact, json, count, totalMs, slowestMs, " +
"slowest, lastNode.";
/// Whether the first argument of stats() is a literal that names the OpenTelemetry layer.
private static bool IsOtelTarget(AstNode target) =>
target is LiteralNode { Value: string literalTarget }
&& literalTarget.StartsWith(OtelPrefix, StringComparison.OrdinalIgnoreCase);
/// The statistic names stats() understands, and their readings.
internal static object? ReadStatistic(IEndpointStatistics stats, string metric) =>
metric.ToLowerInvariant() switch
{
"messagesin" => stats.MessagesIn,
"messagesout" => stats.MessagesOut,
"errors" => stats.Errors,
"warnings" => stats.Warnings,
"rejected" => stats.Rejected,
"cancelled" => stats.Cancelled,
"refused" => stats.Refused,
"bytesin" => stats.BytesIn,
"bytesout" => stats.BytesOut,
"throughputpersecond" => stats.ThroughputPerSecond,
// Milliseconds rather than a TimeSpan: the language compares numbers.
"averageprocessingtimems" => stats.AverageProcessingTime.TotalMilliseconds,
"health" => stats.HealthStatus.ToString(),
"healthreason" => stats.HealthReason,
"lasterror" => stats.LastErrorMessage,
"lastrefusal" => stats.LastRefusalMessage,
_ => throw new ArgumentException(
$"stats(): unknown statistic '{metric}'. Known: messagesIn, messagesOut, errors, " +
"warnings, rejected, cancelled, refused, bytesIn, bytesOut, throughputPerSecond, " +
"averageProcessingTimeMs, health, healthReason, lastError, lastRefusal.")
};
/// True when is a name accepts.
internal static bool IsKnownStatistic(string metric) => metric.ToLowerInvariant() is
"messagesin" or "messagesout" or "errors" or "warnings" or "rejected" or "cancelled" or "refused"
or "bytesin" or "bytesout" or "throughputpersecond" or "averageprocessingtimems"
or "health" or "healthreason" or "lasterror" or "lastrefusal";
///
/// Evaluates the two-argument xpath(path, source) , which reads the source instead of the
/// message body. The source arrives already evaluated, as a value.
///
public static object? Ast_XPathFrom(object? path, object? source, IExchange exchange)
{
var pathStr = path?.ToString();
if (pathStr == null) return null;
// Lenient like the one-argument form on a null body — see Ast_JPathFrom.
if (source is null) return null;
return new XPathExpression(pathStr).EvaluateOn(source, fromSource: true);
}
///
/// Accesses an element by index in a collection, array, or dictionary-like object.
///
/// The collection or array object.
/// The index value (integer or string key).
/// The element at the given index, or null if access fails.
public static object? Ast_IndexAccess(object? obj, object? index)
{
if (obj == null || index == null) return null;
// Try numeric indexing
if (index is int intIndex || (index is string s && int.TryParse(s, out intIndex)))
{
if (obj is System.Collections.IList list)
{
if (intIndex >= 0 && intIndex < list.Count)
return list[intIndex];
return null;
}
if (obj is Array arr)
{
if (intIndex >= 0 && intIndex < arr.Length)
return arr.GetValue(intIndex);
return null;
}
// Try IEnumerable via ElementAt
if (obj is System.Collections.IEnumerable enumerable)
{
int i = 0;
foreach (var item in enumerable)
{
if (i == intIndex) return item;
i++;
}
return null;
}
}
// Try string key (for dictionaries)
var keyStr = index.ToString();
if (keyStr != null)
{
if (obj is System.Collections.IDictionary dict)
{
if (dict.Contains(keyStr)) return dict[keyStr];
return null;
}
// Try generic IDictionary
if (obj is IDictionary genDict)
{
genDict.TryGetValue(keyStr, out var value);
return value;
}
}
return null;
}
///
/// Compares two numeric values via AST delegation.
///
/// Left operand.
/// Right operand.
/// A negative, zero, or positive integer indicating the comparison result.
public static int Ast_CompareNumeric(object? left, object? right) => _compareNumeric(left, right);
///
/// Attempts to convert a value to a boolean via AST delegation.
///
/// The value to convert.
/// The boolean result if conversion succeeds.
/// true if conversion succeeded; otherwise false .
public static bool Ast_TryConvertToBool(object? value, out bool result)
{
return _TryConvertToBool(value, out result);
}
///
/// Converts a value to boolean. Returns false for null, empty strings, zero, and "false";
/// returns true for non-null/non-empty values that can't be parsed as a known boolean format.
///
/// The value to convert.
/// The boolean interpretation of the value.
public static bool Ast_ConvertToBool(object? value)
=> Predicates.RouteTruthiness.ToBoolean(value);
///
/// Resolves a property path on an object via AST delegation.
///
/// The source object.
/// The dot-separated property path.
/// The resolved property value.
public static object? Ast_ResolvePropertyPath(object? obj, string path) => _resolvePropertyPath(obj, path);
// Store references to the original methods.
private static readonly Func _applyAddition = ApplyAddition;
private static readonly Func _applySubtraction = ApplySubtraction;
private static readonly Func _applyMultiplication = ApplyMultiplication;
private static readonly Func _applyDivision = ApplyDivision;
private static readonly Func _applyUnaryPlus = ApplyUnaryPlus;
private static readonly Func _applyUnaryMinus = ApplyUnaryMinus;
private static readonly Func _applyUnaryNot = ApplyUnaryNot;
private static readonly Func _areEqual = AreEqual;
private static readonly Func _compareNumeric = CompareNumeric;
// Cannot use an out parameter in a Func delegate, so we do not store a reference to TryConvertToBool.
private static readonly Func _resolvePropertyPath = ResolvePropertyPath;
#endregion
#region Caching helper methods
#endregion
#region Compilation via AST
///
/// Compiles an expression using the AST pipeline.
///
/// The expression string to compile.
/// A compiled delegate that evaluates the expression against an .
private static Func CompileExpressionWithAst(string expression)
{
try
{
DebugLog($"Compiling expression via AST: '{expression}'");
// Use Tokenizer and Parser to build the AST
var tokenizer = new Ast.Tokenizer(expression);
var tokens = tokenizer.GetAllTokens();
var parser = new Ast.Parser(tokens);
var ast = parser.Parse();
DebugLog($"Built AST: {ast}");
return CompileTree(ast);
}
catch (Exception ex) when (ex is not ExpressionSandboxViolationException)
{
throw new ExpressionCompilationException($"Error compiling expression '{expression}' using AST: {ex.Message}", ex);
}
}
///
/// Compiles an already parsed tree into the engine's delegate: what runs, so a
/// node evaluated directly and an expression compiled from text share one evaluator.
///
internal static Func CompileAstTree(Ast.AstNode node)
{
ArgumentNullException.ThrowIfNull(node);
try
{
return CompileTree(node);
}
catch (Exception ex) when (ex is not ExpressionSandboxViolationException and not ExpressionCompilationException)
{
throw new ExpressionCompilationException($"Error compiling expression '{node}' using AST: {ex.Message}", ex);
}
}
private static Func CompileTree(Ast.AstNode ast)
{
var exchangeParam = SysExpression.Parameter(typeof(IExchange), "exchange");
var body = CompileAstNode(ast, exchangeParam);
// Ensure the body is boxed to object? (comparison operators return bool/int)
if (body.Type != typeof(object) && body.Type.IsValueType)
body = SysExpression.Convert(body, typeof(object));
var lambda = SysExpression.Lambda>(body, exchangeParam);
DebugLog($"Compiled lambda expression: {lambda}");
return lambda.Compile();
}
///
/// Compiles an AST node into a .
///
/// The AST node to compile.
/// The exchange parameter expression.
/// The compiled .
private static SysExpression CompileAstNode(AstNode node, ParameterExpression exchangeParam)
{
if (node == null)
throw new ArgumentNullException(nameof(node));
DebugLog($"Compiling AST node: {node.GetType().Name}");
switch (node)
{
case LiteralNode literalNode:
return SysExpression.Constant(literalNode.Value);
case IdentifierNode identifierNode:
// Get property from the exchange
var getPropertyMethod = typeof(ExpressionResolver).GetMethod(
nameof(GetExchangeProperty),
BindingFlags.NonPublic | BindingFlags.Static);
return SysExpression.Call(
getPropertyMethod,
exchangeParam,
SysExpression.Constant(identifierNode.Name));
case PropertyAccessNode propertyAccessNode:
// Get the object and its property
var objExpr = CompileAstNode(propertyAccessNode.Object, exchangeParam);
var resolvePropertyMethod = typeof(ExpressionResolver).GetMethod(
nameof(ResolvePropertyPathWithExchange),
BindingFlags.NonPublic | BindingFlags.Static);
return SysExpression.Call(
resolvePropertyMethod,
objExpr,
SysExpression.Constant(propertyAccessNode.PropertyName),
exchangeParam);
case IndexAccessNode indexNode:
var indexObjExpr = CompileAstNode(indexNode.Object, exchangeParam);
var indexExpr = CompileAstNode(indexNode.Index, exchangeParam);
// Ensure both are object?
if (indexObjExpr.Type != typeof(object))
indexObjExpr = SysExpression.Convert(indexObjExpr, typeof(object));
if (indexExpr.Type != typeof(object))
indexExpr = SysExpression.Convert(indexExpr, typeof(object));
var indexAccessMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_IndexAccess),
BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(indexAccessMethod, indexObjExpr, indexExpr);
case UnaryOperationNode unaryNode:
if (unaryNode.Operator == "++" || unaryNode.Operator == "--")
{
// Get the property name for increment/decrement
string propertyName = GetPropertyNameFromNode(unaryNode.Operand);
if (string.IsNullOrEmpty(propertyName))
{
throw new ExpressionCompilationException(
$"Failed to get property name for operation {unaryNode.Operator}");
}
// Select the appropriate method for prefix increment/decrement
MethodInfo method = unaryNode.Operator == "++"
? typeof(ExpressionResolver).GetMethod(
nameof(ApplyPrefixIncrement),
BindingFlags.NonPublic | BindingFlags.Static)
: typeof(ExpressionResolver).GetMethod(
nameof(ApplyPrefixDecrement),
BindingFlags.NonPublic | BindingFlags.Static);
return SysExpression.Call(
method,
exchangeParam,
SysExpression.Constant(propertyName));
}
else
{
// Compile the operand, ensuring it is boxed to object?
var operandExpr = CompileAstNode(unaryNode.Operand, exchangeParam);
if (operandExpr.Type != typeof(object))
operandExpr = SysExpression.Convert(operandExpr, typeof(object));
// Select the appropriate method for the unary operation
MethodInfo method;
switch (unaryNode.Operator)
{
case "+":
method = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyUnaryPlus),
BindingFlags.Public | BindingFlags.Static);
break;
case "-":
method = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyUnaryMinus),
BindingFlags.Public | BindingFlags.Static);
break;
case "NOT":
case "!":
method = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyUnaryNot),
BindingFlags.Public | BindingFlags.Static);
break;
default:
throw new ExpressionCompilationException(
$"Unsupported unary operator: {unaryNode.Operator}");
}
return SysExpression.Call(method, operandExpr);
}
case BinaryOperationNode binaryNode:
// Compile the left and right operands, ensuring they are boxed to object?
var leftExpr = CompileAstNode(binaryNode.Left, exchangeParam);
var rightExpr = CompileAstNode(binaryNode.Right, exchangeParam);
if (leftExpr.Type != typeof(object))
leftExpr = SysExpression.Convert(leftExpr, typeof(object));
if (rightExpr.Type != typeof(object))
rightExpr = SysExpression.Convert(rightExpr, typeof(object));
// AND / OR short-circuit over the truthiness rule (findings register row 25): the right side runs only
// when the left one does not decide. The values are what Ast_LogicalAnd / Ast_LogicalOr gave, since
// their conversion never refuses; only a side effect or a fault on the right stops happening.
// ?? evaluates its right side only when the left one is null (findings register row 26), as the
// interpreted form always did; the value is Ast_NullCoalesce's, left ?? right.
if (binaryNode.Operator == "??")
return SysExpression.Coalesce(leftExpr, rightExpr);
if (binaryNode.Operator is "AND" or "OR")
{
var toBool = typeof(ExpressionResolver).GetMethod(nameof(Ast_ConvertToBool), BindingFlags.Public | BindingFlags.Static)!;
var leftBool = SysExpression.Call(toBool, leftExpr);
var rightBool = SysExpression.Call(toBool, rightExpr);
return binaryNode.Operator == "AND"
? SysExpression.AndAlso(leftBool, rightBool)
: SysExpression.OrElse(leftBool, rightBool);
}
// Select the appropriate method for the binary operation
MethodInfo binaryMethod;
switch (binaryNode.Operator)
{
case "+":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyAddition),
BindingFlags.Public | BindingFlags.Static);
break;
case "-":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplySubtraction),
BindingFlags.Public | BindingFlags.Static);
break;
case "*":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyMultiplication),
BindingFlags.Public | BindingFlags.Static);
break;
case "/":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyDivision),
BindingFlags.Public | BindingFlags.Static);
break;
case "%":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ApplyModulo),
BindingFlags.Public | BindingFlags.Static);
break;
case "==":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_AreEqual),
BindingFlags.Public | BindingFlags.Static);
break;
case "!=":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_AreNotEqual),
BindingFlags.Public | BindingFlags.Static);
break;
case "=~":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(EqualsIgnoreCase),
BindingFlags.NonPublic | BindingFlags.Static);
break;
case ">":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_GreaterThan),
BindingFlags.Public | BindingFlags.Static);
break;
case "<":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_LessThan),
BindingFlags.Public | BindingFlags.Static);
break;
case ">=":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_GreaterThanOrEqual),
BindingFlags.Public | BindingFlags.Static);
break;
case "<=":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_LessThanOrEqual),
BindingFlags.Public | BindingFlags.Static);
break;
case "AND":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_LogicalAnd),
BindingFlags.Public | BindingFlags.Static);
break;
case "OR":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_LogicalOr),
BindingFlags.Public | BindingFlags.Static);
break;
case "XOR":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_LogicalXor),
BindingFlags.Public | BindingFlags.Static);
break;
case "??":
binaryMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_NullCoalesce),
BindingFlags.Public | BindingFlags.Static);
break;
default:
throw new ExpressionCompilationException(
$"Unsupported binary operator: {binaryNode.Operator}");
}
return SysExpression.Call(binaryMethod, leftExpr, rightExpr);
case PostfixOperationNode postfixNode:
// Get the property name for increment/decrement
string postfixPropertyName = GetPropertyNameFromNode(postfixNode.Operand);
if (string.IsNullOrEmpty(postfixPropertyName))
{
throw new ExpressionCompilationException(
$"Failed to get property name for postfix operation {postfixNode.Operator}");
}
// Select the appropriate method for postfix increment/decrement
MethodInfo postfixMethod = postfixNode.Operator == "++"
? typeof(ExpressionResolver).GetMethod(
nameof(ApplyPostfixIncrement),
BindingFlags.NonPublic | BindingFlags.Static)
: typeof(ExpressionResolver).GetMethod(
nameof(ApplyPostfixDecrement),
BindingFlags.NonPublic | BindingFlags.Static);
return SysExpression.Call(
postfixMethod,
exchangeParam,
SysExpression.Constant(postfixPropertyName));
case Ast.BeanCallNode beanCall:
{
var site = new BeanCallSite(beanCall.Reference, beanCall.Method, beanCall.Arguments.Count);
var values = beanCall.Arguments.Select(a =>
{
var compiled = CompileAstNode(a, exchangeParam);
return compiled.Type == typeof(object) ? compiled : SysExpression.Convert(compiled, typeof(object));
});
return SysExpression.Call(
SysExpression.Constant(site),
typeof(BeanCallSite).GetMethod(nameof(BeanCallSite.Invoke))!,
exchangeParam,
SysExpression.NewArrayInit(typeof(object), values));
}
case Ast.MethodCallNode methodCall:
{
var targetExpr = CompileAstNode(methodCall.Target, exchangeParam);
if (targetExpr.Type != typeof(object)) targetExpr = SysExpression.Convert(targetExpr, typeof(object));
var argumentExprs = methodCall.Arguments.Select(a =>
{
var compiled = CompileAstNode(a, exchangeParam);
return compiled.Type == typeof(object) ? compiled : SysExpression.Convert(compiled, typeof(object));
});
return SysExpression.Call(
typeof(ExpressionResolver).GetMethod(nameof(Ast_CallMethod), BindingFlags.NonPublic | BindingFlags.Static)!,
targetExpr,
SysExpression.Constant(methodCall.Method),
SysExpression.NewArrayInit(typeof(object), argumentExprs),
SysExpression.Constant(methodCall.Written),
SysExpression.Constant(methodCall.Position));
}
case Ast.EnvNode envNode:
return SysExpression.Call(
typeof(ExpressionResolver).GetMethod(nameof(ReadEnvironment), BindingFlags.NonPublic | BindingFlags.Static)!,
SysExpression.Constant(envNode.Name));
case Ast.RegistryRefNode registryRef:
return SysExpression.Call(
SysExpression.Constant(new RegistryRefSite(registryRef.Reference)),
typeof(RegistryRefSite).GetMethod(nameof(RegistryRefSite.Get))!,
exchangeParam);
case FunctionCallNode functionNode:
switch (functionNode.Name.ToLowerInvariant())
{
case "concat":
// Compile all arguments and call Ast_Concat
var concatArgs = functionNode.Arguments
.Select(arg => CompileAstNode(arg, exchangeParam))
.ToArray();
// Create an array of object? from the compiled arguments
var arrayInit = SysExpression.NewArrayInit(typeof(object),
concatArgs.Select(a => a.Type == typeof(object)
? a
: SysExpression.Convert(a, typeof(object))));
var concatMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_Concat),
BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(concatMethod, arrayInit);
case "upper":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Upper));
case "lower":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Lower));
case "trim":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Trim));
case "length":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Length));
case "abs":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Abs));
case "substring":
{
var args = functionNode.Arguments.Select(a => CompileAstNode(a, exchangeParam)).ToArray();
var val = args.Length > 0 ? BoxToObject(args[0]) : SysExpression.Constant(null, typeof(object));
var start = args.Length > 1 ? BoxToObject(args[1]) : SysExpression.Constant(null, typeof(object));
var len = args.Length > 2 ? BoxToObject(args[2]) : SysExpression.Constant(null, typeof(object));
var m = typeof(ExpressionResolver).GetMethod(nameof(Ast_Substring), BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(m, val, start, len);
}
case "round":
{
var args = functionNode.Arguments.Select(a => CompileAstNode(a, exchangeParam)).ToArray();
var val = args.Length > 0 ? BoxToObject(args[0]) : SysExpression.Constant(null, typeof(object));
var digits = args.Length > 1 ? BoxToObject(args[1]) : SysExpression.Constant(null, typeof(object));
var m = typeof(ExpressionResolver).GetMethod(nameof(Ast_Round), BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(m, val, digits);
}
case "min":
return CompileTwoArgFunction(functionNode, exchangeParam, nameof(Ast_Min));
case "max":
return CompileTwoArgFunction(functionNode, exchangeParam, nameof(Ast_Max));
case "contains":
return CompileTwoArgFunction(functionNode, exchangeParam, nameof(Ast_Contains));
case "startswith":
return CompileTwoArgFunction(functionNode, exchangeParam, nameof(Ast_StartsWith));
case "endswith":
return CompileTwoArgFunction(functionNode, exchangeParam, nameof(Ast_EndsWith));
case "replace":
return CompileThreeArgFunction(functionNode, exchangeParam, nameof(Ast_Replace));
case "now":
{
var nowMethod = typeof(ExpressionResolver).GetMethod(nameof(Ast_Now), BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(nowMethod);
}
case "format":
return CompileThreeArgFunction(functionNode, exchangeParam, nameof(Ast_Format));
case "uuid":
{
var uuidArgs = functionNode.Arguments.Select(a => CompileAstNode(a, exchangeParam)).ToArray();
var fmt = uuidArgs.Length > 0 ? BoxToObject(uuidArgs[0]) : SysExpression.Constant(null, typeof(object));
var m = typeof(ExpressionResolver).GetMethod(nameof(Ast_Uuid), BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(m, fmt);
}
case "datediff":
return CompileThreeArgFunction(functionNode, exchangeParam, nameof(Ast_DateDiff));
case "dateformat":
return CompileTwoArgFunction(functionNode, exchangeParam, nameof(Ast_DateFormat));
case "dateadd":
return CompileThreeArgFunction(functionNode, exchangeParam, nameof(Ast_DateAdd));
case "sum":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Sum));
case "avg":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Avg));
case "count":
return CompileSingleArgFunction(functionNode, exchangeParam, nameof(Ast_Count));
case "logical":
return CompileSingleArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_Logical));
case "messagehistory":
{
// A literal kind is checked while the route is being built, as stats() checks its metric.
if (functionNode.Arguments.Count > 0
&& functionNode.Arguments[0] is LiteralNode { Value: string literalKind }
&& !IsKnownHistoryKind(literalKind))
{
Ast_MessageHistory(literalKind, null!); // throws the ArgumentException with the known kinds
}
var historyArgs = functionNode.Arguments.Select(a => CompileAstNode(a, exchangeParam)).ToArray();
var historyKind = historyArgs.Length > 0
? BoxToObject(historyArgs[0])
: SysExpression.Constant(null, typeof(object));
var historyMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_MessageHistory), BindingFlags.Public | BindingFlags.Static)!;
return SysExpression.Call(historyMethod, historyKind, exchangeParam);
}
case "attachmentcontent":
return CompileSingleArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_AttachmentContent));
case "attachmentcontentastext":
return CompileSingleArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_AttachmentContentAsText));
case "attachmentcontenttype":
return CompileSingleArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_AttachmentContentType));
case "attachmentheader":
return CompileTwoArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_AttachmentHeader));
case "stats":
// A literal metric name is checked while the route is being built — the
// V4 rule: a condition that can never work fails at build, not on the
// first message. A dynamic metric argument is checked at evaluation.
if (functionNode.Arguments.Count > 1
&& functionNode.Arguments[1] is LiteralNode { Value: string literalMetric }
&& !IsOtelTarget(functionNode.Arguments[0])
&& !IsKnownStatistic(literalMetric))
{
ReadStatistic(null!, literalMetric); // throws the ArgumentException with the known-names list
}
// The otel: branch has fields of its own, checked at build the same way.
if (functionNode.Arguments.Count > 1
&& functionNode.Arguments[1] is LiteralNode { Value: string literalField }
&& IsOtelTarget(functionNode.Arguments[0])
&& !IsKnownOtelField(literalField))
{
ReadOtelField(null, literalField); // throws the ArgumentException with the known fields
}
return CompileTwoArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_Stats));
// A second argument names what to read instead of the body. Dispatching on
// arity to a separate method keeps "no source given" distinguishable from "the
// source evaluated to null", which one nullable parameter could not express.
case "jpath":
return functionNode.Arguments.Count >= 2
? CompileTwoArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_JPathFrom))
: CompileSingleArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_JPath));
case "xpath":
return functionNode.Arguments.Count >= 2
? CompileTwoArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_XPathFrom))
: CompileSingleArgFunctionWithExchange(functionNode, exchangeParam, nameof(Ast_XPath));
default:
throw new NotImplementedException(
$"Compilation of function '{functionNode.Name}' is not yet implemented.");
}
case TernaryNode ternaryNode:
var conditionExpr = CompileAstNode(ternaryNode.Condition, exchangeParam);
var ifTrueExpr = CompileAstNode(ternaryNode.IfTrue, exchangeParam);
var ifFalseExpr = CompileAstNode(ternaryNode.IfFalse, exchangeParam);
// Ensure condition is boxed to object? for Ast_ConvertToBool
if (conditionExpr.Type != typeof(object))
conditionExpr = SysExpression.Convert(conditionExpr, typeof(object));
var toBoolMethod = typeof(ExpressionResolver).GetMethod(
nameof(Ast_ConvertToBool),
BindingFlags.Public | BindingFlags.Static);
var conditionBool = SysExpression.Call(toBoolMethod, conditionExpr);
// Ensure both branches return object?
if (ifTrueExpr.Type != typeof(object))
ifTrueExpr = SysExpression.Convert(ifTrueExpr, typeof(object));
if (ifFalseExpr.Type != typeof(object))
ifFalseExpr = SysExpression.Convert(ifFalseExpr, typeof(object));
return SysExpression.Condition(conditionBool, ifTrueExpr, ifFalseExpr);
case ListLiteralNode listNode:
{
var items = listNode.Items
.Select(item =>
{
var compiled = CompileAstNode(item, exchangeParam);
return compiled.Type == typeof(object) ? compiled : SysExpression.Convert(compiled, typeof(object));
})
.ToArray();
return SysExpression.NewArrayInit(typeof(object), items);
}
case InOperationNode inNode:
{
var valueExpr = CompileAstNode(inNode.Value, exchangeParam);
var listExpr = CompileAstNode(inNode.List, exchangeParam);
if (valueExpr.Type != typeof(object)) valueExpr = SysExpression.Convert(valueExpr, typeof(object));
if (listExpr.Type != typeof(object)) listExpr = SysExpression.Convert(listExpr, typeof(object));
var inMethod = typeof(ExpressionResolver).GetMethod(nameof(Ast_In), BindingFlags.Public | BindingFlags.Static)!;
return SysExpression.Convert(
SysExpression.Call(inMethod, valueExpr, listExpr,
SysExpression.Constant(inNode.Negated), SysExpression.Constant(inNode.ToString())),
typeof(object));
}
case Ast.RangeNode rangeNode:
{
var valueExpr = CompileAstNode(rangeNode.Value, exchangeParam);
if (valueExpr.Type != typeof(object)) valueExpr = SysExpression.Convert(valueExpr, typeof(object));
var rangeMethod = typeof(ExpressionResolver).GetMethod(nameof(InRange), BindingFlags.NonPublic | BindingFlags.Static)!;
return SysExpression.Convert(
SysExpression.Call(rangeMethod, valueExpr,
SysExpression.Constant(rangeNode.From), SysExpression.Constant(rangeNode.To),
SysExpression.Constant(rangeNode.Negated), SysExpression.Constant(rangeNode.ToString())),
typeof(object));
}
default:
throw new ExpressionCompilationException(
$"Unsupported AST node type: {node.GetType().Name}");
}
}
///
/// value in list / value not in list , shared by the interpreted and the compiled
/// path so the two can never disagree. Elements compare with — the
/// equality == uses. A null list is empty (nothing is a member of it), a string is refused
/// rather than split into characters, anything else that is not a collection is refused.
///
/// The value tested for membership.
/// The collection, or null.
/// True for not in .
/// The expression as written, for the error message.
public static bool Ast_In(object? value, object? list, bool negated, string display)
{
bool found;
switch (list)
{
case null:
found = false;
break;
case string:
throw new InvalidOperationException(
$"'{display}': the right side of in is a string, and a string is not split into a list. " +
"Reference a collection the message carries (header.codes) or write a list literal ('a','b').");
case System.Collections.IEnumerable items:
found = false;
foreach (var item in items)
{
if (Ast_AreEqual(value, item)) { found = true; break; }
}
break;
default:
throw new InvalidOperationException(
$"'{display}': the right side of in is a {list.GetType().Name}, not a collection. " +
"Reference a collection the message carries (header.codes) or write a list literal ('a','b').");
}
return negated ? !found : found;
}
///
/// Extracts the property name from an AST node.
///
/// The AST node to extract the property name from.
/// The extracted property name, or an empty string if extraction fails.
private static string GetPropertyNameFromNode(AstNode node)
{
DebugLog($"Extracting property name from node {node?.GetType().Name ?? "null"}");
switch (node)
{
case IdentifierNode identifierNode:
return identifierNode.Name;
case PropertyAccessNode propertyAccessNode:
var objName = GetObjectNameFromNode(propertyAccessNode.Object);
return string.IsNullOrEmpty(objName)
? propertyAccessNode.PropertyName
: $"{objName}.{propertyAccessNode.PropertyName}";
default:
return string.Empty;
}
}
///
/// Gets the object name from an AST node.
///
/// The AST node to extract the object name from.
/// The extracted object name, or an empty string if extraction fails.
private static string GetObjectNameFromNode(AstNode node)
{
if (node is IdentifierNode idNode)
{
return idNode.Name;
}
else if (node is PropertyAccessNode propNode)
{
var objName = GetObjectNameFromNode(propNode.Object);
return string.IsNullOrEmpty(objName)
? propNode.PropertyName
: $"{objName}.{propNode.PropertyName}";
}
return string.Empty;
}
///
/// Boxes an expression to typeof(object) if needed.
///
private static SysExpression BoxToObject(SysExpression expr)
=> expr.Type == typeof(object) ? expr : SysExpression.Convert(expr, typeof(object));
///
/// Compiles a single-argument function call (e.g. upper, lower, trim, length, abs).
///
private static SysExpression CompileSingleArgFunction(FunctionCallNode node, ParameterExpression exchangeParam, string methodName)
{
var arg = node.Arguments.Count > 0
? BoxToObject(CompileAstNode(node.Arguments[0], exchangeParam))
: SysExpression.Constant(null, typeof(object));
var method = typeof(ExpressionResolver).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(method, arg);
}
///
/// Compiles a two-argument function call (e.g. min, max, contains, startsWith, endsWith).
///
private static SysExpression CompileTwoArgFunction(FunctionCallNode node, ParameterExpression exchangeParam, string methodName)
{
var args = node.Arguments.Select(a => CompileAstNode(a, exchangeParam)).ToArray();
var a = args.Length > 0 ? BoxToObject(args[0]) : SysExpression.Constant(null, typeof(object));
var b = args.Length > 1 ? BoxToObject(args[1]) : SysExpression.Constant(null, typeof(object));
var method = typeof(ExpressionResolver).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(method, a, b);
}
///
/// Compiles a three-argument function call (e.g. replace, dateAdd).
///
private static SysExpression CompileThreeArgFunction(FunctionCallNode node, ParameterExpression exchangeParam, string methodName)
{
var args = node.Arguments.Select(a => CompileAstNode(a, exchangeParam)).ToArray();
var a = args.Length > 0 ? BoxToObject(args[0]) : SysExpression.Constant(null, typeof(object));
var b = args.Length > 1 ? BoxToObject(args[1]) : SysExpression.Constant(null, typeof(object));
var c = args.Length > 2 ? BoxToObject(args[2]) : SysExpression.Constant(null, typeof(object));
var method = typeof(ExpressionResolver).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(method, a, b, c);
}
///
/// Compiles a single-argument function that also needs the exchange parameter.
///
private static SysExpression CompileSingleArgFunctionWithExchange(FunctionCallNode node, ParameterExpression exchangeParam, string methodName)
{
var arg = node.Arguments.Count > 0
? BoxToObject(CompileAstNode(node.Arguments[0], exchangeParam))
: SysExpression.Constant(null, typeof(object));
var method = typeof(ExpressionResolver).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(method, arg, exchangeParam);
}
///
/// Compiles a two-argument function that also needs the exchange parameter.
///
private static SysExpression CompileTwoArgFunctionWithExchange(FunctionCallNode node, ParameterExpression exchangeParam, string methodName)
{
var first = node.Arguments.Count > 0
? BoxToObject(CompileAstNode(node.Arguments[0], exchangeParam))
: SysExpression.Constant(null, typeof(object));
var second = node.Arguments.Count > 1
? BoxToObject(CompileAstNode(node.Arguments[1], exchangeParam))
: SysExpression.Constant(null, typeof(object));
var method = typeof(ExpressionResolver).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
return SysExpression.Call(method, first, second, exchangeParam);
}
#endregion
#region AST helper methods — increment/decrement operations
///
/// Applies prefix increment (++x) via AST.
///
/// The current value (unused; operation reads from exchange).
/// The property name to increment.
/// The exchange containing the property.
/// The incremented value.
internal static object? Ast_ApplyPrefixIncrement(object? value, string propertyName, IExchange exchange)
=> ApplyIncrementDecrementOperation(exchange, propertyName, OperationType.PrefixIncrement);
///
/// Applies prefix decrement (--x) via AST.
///
/// The current value (unused; operation reads from exchange).
/// The property name to decrement.
/// The exchange containing the property.
/// The decremented value.
internal static object? Ast_ApplyPrefixDecrement(object? value, string propertyName, IExchange exchange)
=> ApplyIncrementDecrementOperation(exchange, propertyName, OperationType.PrefixDecrement);
///
/// Applies postfix increment (x++) via AST.
///
/// The current value (unused; operation reads from exchange).
/// The property name to increment.
/// The exchange containing the property.
/// The original value before incrementing.
internal static object? Ast_ApplyPostfixIncrement(object? value, string propertyName, IExchange exchange)
=> ApplyIncrementDecrementOperation(exchange, propertyName, OperationType.PostfixIncrement);
///
/// Applies postfix decrement (x--) via AST.
///
/// The current value (unused; operation reads from exchange).
/// The property name to decrement.
/// The exchange containing the property.
/// The original value before decrementing.
internal static object? Ast_ApplyPostfixDecrement(object? value, string propertyName, IExchange exchange)
=> ApplyIncrementDecrementOperation(exchange, propertyName, OperationType.PostfixDecrement);
#endregion
}