feat: Add function defs to DataStream DSL
This commit is contained in:
1 parent
0550078de0
commit
03294ffe37
55 files changed
+3332
-259
No files matched your search
@@ -60,7 +60,7 @@ public class SubscribeRequest {
|
||||
}
|
||||
Exception? exception = null;
|
||||
try {
|
||||
stream = exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
||||
stream = exp.Evaluate(streamFactory) as ViewStream;
|
||||
} catch (Exception ex) {
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
@@ -20,9 +20,7 @@ namespace bts.BlueOrb {
|
||||
static ViewStream? build_stream(IDataStreamFactory streamFactory, string stream_expression) {
|
||||
try {
|
||||
var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression);
|
||||
var rv = exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
||||
return rv;
|
||||
|
||||
return exp.Evaluate(streamFactory) as ViewStream;
|
||||
} catch (Exception ex) {
|
||||
Log.Gen.error($"Could not build stream: {stream_expression} ex: {ex}");
|
||||
return null;
|
||||
|
||||
@@ -41,9 +41,7 @@ namespace BCG {
|
||||
try
|
||||
{
|
||||
var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression);
|
||||
var rv = exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
||||
return rv;
|
||||
|
||||
return exp.Evaluate(streamFactory) as ViewStream;
|
||||
} catch (Exception ex) {
|
||||
bts.Log.assert(false, $"Could not build stream: {stream_expression} ex: {ex}");
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Bts.DataStream.Primitives;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
|
||||
class FilterEvaluator : IStreamFilter {
|
||||
readonly IFunctionEvaluator evaluator;
|
||||
|
||||
public FilterEvaluator(IFunctionEvaluator evaluator) =>
|
||||
this.evaluator = evaluator;
|
||||
|
||||
public bool filter(IBlueRow dr) =>
|
||||
evaluator.Evaluate(string.Empty, new (string?, int?, object?)[] { (null, 0, dr) })
|
||||
as bool? ?? throw new InvalidOperationException("FunctionEvaluator did not return bool");
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStream.Primitives;
|
||||
using Bts.DataStream.Primitives.Filter;
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
@@ -26,6 +27,9 @@ namespace BCG.StreamWorld.Addition {
|
||||
public FilterStream(ViewStream vs, string filterExp)
|
||||
: this(vs, new BooleanExpressionFilter(filterExp)) { }
|
||||
|
||||
public FilterStream(ViewStream vs, IFunctionEvaluator functionEvaluator)
|
||||
: this(vs, new FilterEvaluator(functionEvaluator)) { }
|
||||
|
||||
public FilterStream(ViewStream vs, IStreamFilter filter) {
|
||||
myBaseStream = vs;
|
||||
myFilter = filter;
|
||||
@@ -190,6 +194,8 @@ namespace BCG
|
||||
: this(vs, new OnFilterWrapper(f))
|
||||
{ }
|
||||
|
||||
public FilterStream(ViewStream vs, IFunctionEvaluator functionEvaluator)
|
||||
: this(vs, new FilterEvaluator(functionEvaluator)) { }
|
||||
|
||||
public FilterStream(ViewStream vs, IStreamFilter filter)
|
||||
{
|
||||
|
||||
@@ -94,6 +94,47 @@ namespace BCG {
|
||||
return BlueDataUtility.get_row_datetime_value(r, col_no, err_value);
|
||||
}
|
||||
|
||||
public static bool? GetBool(this IBlueRow r, int colNo) =>
|
||||
GetBool(r[colNo]);
|
||||
|
||||
public static bool? GetBool(this IBlueRow r, string colName) =>
|
||||
GetBool(r[colName]);
|
||||
|
||||
public static bool? GetBool(object obj) {
|
||||
if (obj is bool b) {
|
||||
return b;
|
||||
}
|
||||
if (obj is string s) {
|
||||
if (bool.TryParse(s, out b)) {
|
||||
return b;
|
||||
}
|
||||
return s.ToLower() switch {
|
||||
"y" or "yes" or "on" or "1" => true,
|
||||
"n" or "no" or "off" or "0" => false,
|
||||
_ => throw new ArgumentException("", nameof(obj)),
|
||||
};
|
||||
}
|
||||
if (obj is int i32) {
|
||||
return i32 > 0;
|
||||
}
|
||||
if (obj is long i64) {
|
||||
return i64 > 0;
|
||||
}
|
||||
if (obj is uint u32) {
|
||||
return u32 > 0;
|
||||
}
|
||||
if (obj is ulong u64) {
|
||||
return u64 > 0;
|
||||
}
|
||||
if (obj is short i16) {
|
||||
return i16 > 0;
|
||||
}
|
||||
if (obj is ushort u16) {
|
||||
return u16 > 0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static double? GetFloat64(this IBlueRow r, int colNo) =>
|
||||
GetFloat64(r[colNo]);
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class FunctionEvaluator {
|
||||
public static IFunctionEvaluator Create<T, TResult>(Func<T, TResult> evaluate) =>
|
||||
new Impl<T, TResult>(evaluate);
|
||||
|
||||
class Impl<T, TResult> : IFunctionEvaluator {
|
||||
readonly Func<T, TResult> evaluate;
|
||||
|
||||
public Impl(Func<T, TResult> evaluate) {
|
||||
this.evaluate = evaluate;
|
||||
}
|
||||
|
||||
public object Evaluate(string functionId, IEnumerable<(string Name, int? Index, object Value)> args) {
|
||||
foreach (var a in args) {
|
||||
if (a is T ta) {
|
||||
return evaluate(ta);
|
||||
}
|
||||
}
|
||||
throw new ArgumentException($"Arguments does not contain item of expected input type {typeof(T)}", nameof(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,16 @@ using BCG;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public interface IDataStreamFactory {
|
||||
public interface IDataStreamFactory : IFunctionEvaluator {
|
||||
IEnumerable<IDataStreamMetadata> GetSupportedStreams();
|
||||
|
||||
ViewStream? Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
|
||||
bool CanCreate(string streamId) =>
|
||||
GetSupportedStreams().Any(s => s.IsMatch(streamId));
|
||||
|
||||
object? IFunctionEvaluator.Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args) =>
|
||||
Create(functionId, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#nullable enable
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public interface IFunctionEvaluator {
|
||||
bool CanCompile => false;
|
||||
object? Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
void Compile() =>
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public interface IExtensionFunctionEvaluator : IFunctionEvaluator {
|
||||
Type TargetType { get; }
|
||||
|
||||
public IEnumerable<string> SupportedFunctions { get; }
|
||||
|
||||
object? Evaluate(string functionId, object target, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
|
||||
object? IFunctionEvaluator.Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args) {
|
||||
var en = args.GetEnumerator();
|
||||
if (!en.MoveNext()) {
|
||||
throw new ArgumentException("Expected at least 1 arg (first arg is the target obj)", nameof(args));
|
||||
}
|
||||
var targetObj = en.Current.Value;
|
||||
if (targetObj is null) {
|
||||
return null;
|
||||
}
|
||||
return Evaluate(functionId, targetObj, RemainingArgs());
|
||||
|
||||
IEnumerable<(string? Name, int? Index, object? Value)> RemainingArgs() {
|
||||
while (en.MoveNext()) {
|
||||
yield return en.Current;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IExtensionFunctionEvaluator<T> : IExtensionFunctionEvaluator {
|
||||
Type IExtensionFunctionEvaluator.TargetType => typeof(T);
|
||||
|
||||
object? Evaluate(string functionId, T target, IEnumerable<(string? Name, int? Index, object? Value)> args);
|
||||
|
||||
object? IExtensionFunctionEvaluator.Evaluate(string functionId, object target, IEnumerable<(string? Name, int? Index, object? Value)> args) =>
|
||||
Evaluate(functionId, ExceptionAssert.Argument.Is<T>(target), args);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#nullable enable
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class SingleValueStream : ViewStream {
|
||||
internal readonly IBlueTableSchema Schema;
|
||||
|
||||
readonly Row row;
|
||||
|
||||
public SingleValueStream(object value)
|
||||
: this(value, value.GetType()) { }
|
||||
|
||||
public SingleValueStream(object? value, Type type) {
|
||||
Schema = new BlueTableSchema(
|
||||
new ColumnInfo[] { new("id", 0, typeof(int)), new("Value", 1, type) },
|
||||
"SingleValue");
|
||||
row = new(0, value, Schema);
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
var rows = new RowCollection(row);
|
||||
myFireEvent(new DataPacket(rows, rows, new BlueTable(Schema)));
|
||||
}
|
||||
|
||||
class Row : IBlueRow {
|
||||
internal readonly int Id;
|
||||
internal readonly object? Value;
|
||||
internal readonly IBlueTableSchema Schema;
|
||||
|
||||
public Row(int id, object? value, IBlueTableSchema schema) {
|
||||
Id = id;
|
||||
Value = value;
|
||||
Schema = schema;
|
||||
}
|
||||
|
||||
public object? this[int index] =>
|
||||
index switch {
|
||||
0 => Id,
|
||||
1 => Value,
|
||||
_ => throw new IndexOutOfRangeException(),
|
||||
};
|
||||
|
||||
public object? this[string column] =>
|
||||
column switch {
|
||||
"id" => Id,
|
||||
"Value" => Value,
|
||||
_ => throw new KeyNotFoundException(),
|
||||
};
|
||||
|
||||
IBlueTableSchema IBlueRow.schema => Schema;
|
||||
}
|
||||
|
||||
class RowCollection : IBlueRowCollection {
|
||||
readonly Row row;
|
||||
IBlueRow? blueRow;
|
||||
|
||||
public RowCollection(Row row) =>
|
||||
this.row = row;
|
||||
|
||||
public IBlueRow this[int id] =>
|
||||
id == row.Id ? blueRow ??= row : throw new KeyNotFoundException();
|
||||
|
||||
public int Count => 1;
|
||||
|
||||
IBlueTableSchema IBlueRowCollection.schema => row.Schema;
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
index == 0 ? row : throw new IndexOutOfRangeException();
|
||||
|
||||
public int IndexOf(IBlueRow item) =>
|
||||
item.schema.columnInfo.Select(c => item[c.colNo]).SequenceEqual(row.Schema.columnInfo.Select(c => row[c.colNo]))
|
||||
? 0
|
||||
: -1;
|
||||
|
||||
public bool containsID(int id) =>
|
||||
id == row.Id;
|
||||
|
||||
public IList<object?> GetColumn(int index) =>
|
||||
new[] { row[0] };
|
||||
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
yield return row;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class AddExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public AddExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftAdd = ToAdd(leftVal);
|
||||
return leftAdd.Add(rightVal);
|
||||
|
||||
IAdd ToAdd(object? val) {
|
||||
if (val is int valInt32) {
|
||||
return new Int32Add(valInt32);
|
||||
}
|
||||
if (val is long valInt64) {
|
||||
return new Int64Add(valInt64);
|
||||
}
|
||||
if (val is uint valUint32) {
|
||||
return new Uint32Add(valUint32);
|
||||
}
|
||||
if (val is ulong valUint64) {
|
||||
return new Uint64Add(valUint64);
|
||||
}
|
||||
if (val is float valFloat32) {
|
||||
return new Float32Add(valFloat32);
|
||||
}
|
||||
if (val is double valFloat64) {
|
||||
return new Float64Add(valFloat64);
|
||||
}
|
||||
throw new ArgumentException($"Cannot convert obj of type '{val?.GetType()}' to IAdd", nameof(val));
|
||||
}
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" + ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
|
||||
interface IAdd {
|
||||
object Add(object? other);
|
||||
}
|
||||
|
||||
class Int32Add : IAdd {
|
||||
readonly int left;
|
||||
|
||||
public Int32Add(int left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Add(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left + rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left + rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left + rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left + rightUint32;
|
||||
}
|
||||
if (other is IAdd rightAdd) {
|
||||
return rightAdd.Add(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Add obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Int64Add : IAdd {
|
||||
readonly long left;
|
||||
|
||||
public Int64Add(long left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Add(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left + rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left + rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left + rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left + rightUint32;
|
||||
}
|
||||
if (other is IAdd rightAdd) {
|
||||
return rightAdd.Add(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Add obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint32Add : IAdd {
|
||||
readonly uint left;
|
||||
|
||||
public Uint32Add(uint left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Add(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left + rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left + rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left + rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left + rightUint32;
|
||||
}
|
||||
throw new ArgumentException($"Cannot Add obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint64Add : IAdd {
|
||||
readonly ulong left;
|
||||
|
||||
public Uint64Add(ulong left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Add(object? other) {
|
||||
checked {
|
||||
if (other is int rightIn32) {
|
||||
return (long)left + rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left + rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left + rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left + rightUint32;
|
||||
}
|
||||
}
|
||||
if (other is IAdd rightAdd) {
|
||||
return rightAdd.Add(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Add obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float32Add : IAdd {
|
||||
readonly float left;
|
||||
|
||||
public Float32Add(float left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Add(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left + rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left + rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left + rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left + rightUint32;
|
||||
}
|
||||
if (other is IAdd rightAdd) {
|
||||
return rightAdd.Add(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Add obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float64Add : IAdd {
|
||||
readonly double left;
|
||||
|
||||
public Float64Add(double left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Add(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left + rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left + rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left + rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left + rightUint32;
|
||||
}
|
||||
if (other is IAdd rightAdd) {
|
||||
return rightAdd.Add(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Add obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
public class Argument {
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
public class Argument : IDataStreamExpression {
|
||||
public Argument() { }
|
||||
public Argument(string str, string? name = null) {
|
||||
String = str;
|
||||
@@ -26,24 +31,25 @@ public class Argument {
|
||||
Null = @null;
|
||||
Name = name;
|
||||
}
|
||||
public Argument(StatementExpression? statementExp, string? name = null) {
|
||||
public Argument(IDataStreamExpression? statementExp, string? name = null) {
|
||||
StatementExp = statementExp;
|
||||
Name = name;
|
||||
}
|
||||
public Argument(QuotedView? quotedView, string? name = null) {
|
||||
QuotedView = quotedView;
|
||||
public Argument(ImmutableList<Argument>? argumentArray, string? name = null) {
|
||||
ArgumentArray = argumentArray;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string? Name { get; }
|
||||
public string? Name { get; private init; }
|
||||
public string? String { get; }
|
||||
public bool? Boolean { get; }
|
||||
public int? Integer { get; }
|
||||
public double? Real { get; }
|
||||
public DateTime? DateTime { get; }
|
||||
public DBNull? Null { get; }
|
||||
public StatementExpression? StatementExp { get; }
|
||||
public QuotedView? QuotedView { get; }
|
||||
public IDataStreamExpression? StatementExp { get; }
|
||||
public ImmutableList<Argument>? ArgumentArray { get; }
|
||||
|
||||
public object? Obj =>
|
||||
String ??
|
||||
Boolean ??
|
||||
@@ -51,7 +57,100 @@ public class Argument {
|
||||
Real ??
|
||||
DateTime ??
|
||||
Null ??
|
||||
(object?)StatementExp ??
|
||||
QuotedView;
|
||||
ArgumentArray ??
|
||||
(object?)StatementExp;
|
||||
|
||||
public Argument WithName(string? name) {
|
||||
if (String is not null) {
|
||||
return new(String, name);
|
||||
}
|
||||
if (Boolean.HasValue) {
|
||||
return new(Boolean.Value, name);
|
||||
}
|
||||
if (Integer.HasValue) {
|
||||
return new(Integer.Value, name);
|
||||
}
|
||||
if (Real.HasValue) {
|
||||
return new(Real.Value, name);
|
||||
}
|
||||
if (DateTime.HasValue) {
|
||||
return new(DateTime.Value, name);
|
||||
}
|
||||
if (Null is not null) {
|
||||
return new(Null, name);
|
||||
}
|
||||
if (ArgumentArray is not null) {
|
||||
return new(ArgumentArray, name);
|
||||
}
|
||||
return new() { Name = name };
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
if (StatementExp is null) {
|
||||
if (ArgumentArray is not null) {
|
||||
var vals = new object?[ArgumentArray.Count];
|
||||
for (int i = 0; i < vals.Length; i++) {
|
||||
vals[i] = ArgumentArray[i].Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
// Return something hashable so it can be used as a key for GroupByApply streams.
|
||||
return new HashableTuple(vals);
|
||||
}
|
||||
return Obj;
|
||||
}
|
||||
if (StatementExp is Variable variable) {
|
||||
if (env.TryGetValue(variable.Id, out var val)) {
|
||||
if (val is IDataStreamExpression exp) {
|
||||
return exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
} else if (functions.TryGetValue(variable.Id, out var fn)) {
|
||||
return fn.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
}
|
||||
return StatementExp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
if (Name is not null) {
|
||||
builder.Append(Name);
|
||||
return;
|
||||
}
|
||||
if (StatementExp is not null) {
|
||||
StatementExp.ToCode(builder, new List<string>(0));
|
||||
return;
|
||||
}
|
||||
builder.Append(Obj?.ToString() ?? string.Empty);
|
||||
}
|
||||
|
||||
public Argument? Multiply(Argument right) =>
|
||||
new(new MultiplyExpression(this, right));
|
||||
|
||||
public Argument? Divide(Argument right) =>
|
||||
new(new DivideExpression(this, right));
|
||||
|
||||
public Argument? Add(Argument right) =>
|
||||
new(new AddExpression(this, right));
|
||||
|
||||
public Argument? Subtract(Argument right) =>
|
||||
new(new SubtractExpression(this, right));
|
||||
|
||||
public Argument? Negate() =>
|
||||
new(new SubtractExpression(new Argument(0), this));
|
||||
|
||||
public Argument? Equal(Argument right) =>
|
||||
new(new EqualExpression(this, right));
|
||||
|
||||
public Argument? NotEqual(Argument right) =>
|
||||
new(new NotEqualExpression(this, right));
|
||||
|
||||
public Argument? Greater(Argument right) =>
|
||||
new(new GreaterExpression(this, right));
|
||||
|
||||
public Argument? GreaterEqual(Argument right) =>
|
||||
new(new GreaterEqualExpression(this, right));
|
||||
|
||||
public Argument? Less(Argument right) =>
|
||||
new(new LessExpression(this, right));
|
||||
|
||||
public Argument? LessEqual(Argument right) =>
|
||||
new(new LessEqualExpression(this, right));
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
class ArgumentExtensionFunction : IDataStreamExpression {
|
||||
static readonly Type[] typeKeys = new[] { typeof(IBlueRowCollection), typeof(ViewStream) };
|
||||
readonly IDataStreamExpression target;
|
||||
readonly string methodName;
|
||||
readonly ImmutableList<Argument> arguments;
|
||||
readonly bool isProperty;
|
||||
|
||||
public ArgumentExtensionFunction(IDataStreamExpression target, string methodName)
|
||||
: this(target, methodName, ImmutableList<Argument>.Empty) { }
|
||||
|
||||
public ArgumentExtensionFunction(IDataStreamExpression target, string methodName, bool isProperty)
|
||||
: this(target, methodName, ImmutableList<Argument>.Empty) {
|
||||
this.isProperty = isProperty;
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunction(IDataStreamExpression target, string methodName, ImmutableList<Argument> arguments) {
|
||||
if (target is ArgumentExtensionFunction targetExtFn) {
|
||||
if (targetExtFn.target is PrimitiveExpression targetPe) {
|
||||
}
|
||||
}
|
||||
this.target = target;
|
||||
this.methodName = methodName;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public string Id => methodName;
|
||||
public ImmutableList<Argument> Arguments => arguments;
|
||||
|
||||
public IDataStreamExpression Extend(IDataStreamExpression ext, string name) {
|
||||
if (ext is Variable) {
|
||||
throw new ArgumentException("Variable is invalid type for extension function", nameof(ext));
|
||||
}
|
||||
return new ArgumentExtensionFunction(this, name);
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var targetObj = target.Evaluate(env, functions, streamFactory);
|
||||
if (targetObj is null) {
|
||||
return null;
|
||||
}
|
||||
var targetType = targetObj.GetType();
|
||||
if (isProperty) {
|
||||
if (targetObj is IBlueRow r) {
|
||||
return r[methodName];
|
||||
}
|
||||
var prop = targetType.GetProperty(methodName);
|
||||
if (prop is null) {
|
||||
return null;
|
||||
}
|
||||
return prop.GetValue(targetObj);
|
||||
}
|
||||
if (TryGetExtFn(out var extObj)) {
|
||||
var extEval = ExceptionAssert.Argument.Is<IFunctionEvaluator>(extObj);
|
||||
var argArray = new (string?, int?, object?)[arguments.Count + 1];
|
||||
argArray[0] = (null, 0, targetObj);
|
||||
int argIdx = 1;
|
||||
var fakeStreamFactory = new DataStreamFactory();
|
||||
foreach (var arg in arguments) {
|
||||
var argVal = arg.Evaluate(env, functions, fakeStreamFactory);
|
||||
argArray[argIdx] = (arg.Name, argIdx, argVal);
|
||||
argIdx++;
|
||||
}
|
||||
return extEval.Evaluate(methodName, argArray);
|
||||
}
|
||||
var method = targetType.GetMethod(methodName);
|
||||
if (method is null) {
|
||||
return null;
|
||||
}
|
||||
return method.Invoke(targetObj, Array.Empty<object>());
|
||||
|
||||
bool TryGetExtFn([NotNullWhen(true)] out IFunctionEvaluator? eval) {
|
||||
for (int i = 0; i < typeKeys.Length; i++) {
|
||||
if (targetType.IsAssignableTo(typeKeys[i]) && env.TryGetValue(typeKeys[i].Name, out var evalObj)) {
|
||||
eval = ExceptionAssert.Argument.Is<IFunctionEvaluator>(evalObj);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
eval = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
target.ToCode(builder, activeBindings);
|
||||
builder.Append('.');
|
||||
builder.Append(methodName);
|
||||
builder.Append("()");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
class ArgumentExtensionFunctionChain : IDataStreamExpression {
|
||||
static readonly Type[] typeKeys = new[] {
|
||||
typeof(IBlueRow),
|
||||
typeof(IBlueRowCollection),
|
||||
typeof(ViewStream),
|
||||
typeof(short),
|
||||
typeof(int),
|
||||
typeof(long),
|
||||
typeof(ushort),
|
||||
typeof(uint),
|
||||
typeof(ulong),
|
||||
typeof(float),
|
||||
typeof(double),
|
||||
typeof(string),
|
||||
typeof(DateTime),
|
||||
typeof(TimeSpan)
|
||||
};
|
||||
readonly IDataStreamExpression target;
|
||||
readonly ImmutableList<(string Name, ImmutableList<Argument> Arguments, bool IsProperty)> callStack;
|
||||
|
||||
public ArgumentExtensionFunctionChain(IDataStreamExpression target, string methodName)
|
||||
: this(target, methodName, ImmutableList<Argument>.Empty, false) { }
|
||||
|
||||
public ArgumentExtensionFunctionChain(IDataStreamExpression target, string methodName, IEnumerable<Argument>? arguments, bool isProperty) {
|
||||
this.target = target;
|
||||
var argsi = arguments as ImmutableList<Argument> ?? arguments?.ToImmutableList() ?? ImmutableList<Argument>.Empty;
|
||||
callStack = ImmutableList<(string Name, ImmutableList<Argument> Arguments, bool IsProperty)>.Empty.Add((methodName, argsi, isProperty));
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunctionChain(IDataStreamExpression target, ImmutableList<(string Name, ImmutableList<Argument> Arguments, bool IsProperty)> callStack) {
|
||||
this.target = target;
|
||||
this.callStack = callStack;
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunctionChain Extend(PrimitiveExpression pe) {
|
||||
var cs = callStack.Add((pe.Id, pe.Arguments.ToImmutableList(), false));
|
||||
return new(target, cs);
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunctionChain Extend(Variable v) {
|
||||
var cs = callStack.Add((v.Id, ImmutableList<Argument>.Empty, true));
|
||||
return new(target, cs);
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunctionChain Extend(ArgumentExtensionFunctionChain ch) {
|
||||
if (ch.target is PrimitiveExpression pe) {
|
||||
var cs = callStack.Insert(0, (pe.Id, pe.Arguments.ToImmutableList(), false));
|
||||
return new(target, cs);
|
||||
} else if (target is Variable v) {
|
||||
var cs = callStack.Insert(0, (v.Id, ImmutableList<Argument>.Empty, true));
|
||||
return new(target, cs);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunctionChain Prepend(Variable v) {
|
||||
if (target is PrimitiveExpression pe) {
|
||||
var cs = callStack.Insert(0, (pe.Id, pe.Arguments.ToImmutableList(), false));
|
||||
return new(v, cs);
|
||||
} else if (target is Variable targetVar) {
|
||||
var cs = callStack.Insert(0, (targetVar.Id, ImmutableList<Argument>.Empty, true));
|
||||
return new(v, cs);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public ArgumentExtensionFunctionChain Prepend(PrimitiveExpression pe) {
|
||||
if (target is PrimitiveExpression targetPe) {
|
||||
var cs = callStack.Insert(0, (targetPe.Id, targetPe.Arguments.ToImmutableList(), false));
|
||||
return new(pe, cs);
|
||||
} else if (target is Variable targetVar) {
|
||||
var cs = callStack.Insert(0, (targetVar.Id, ImmutableList<Argument>.Empty, true));
|
||||
return new(pe, cs);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var targetObj = target.Evaluate(env, functions, streamFactory);
|
||||
foreach (var (name, arguments, isProperty) in callStack) {
|
||||
// var extFn = new ArgumentExtensionFunction()
|
||||
targetObj = tryInvoke(targetObj, name, isProperty, arguments, env, functions);
|
||||
}
|
||||
return targetObj;
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
// throw new NotImplementedException();
|
||||
}
|
||||
|
||||
static object? tryInvoke(object? targetObj, string methodName, bool isProperty, ImmutableList<Argument> arguments, IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions) {
|
||||
if (targetObj is null) {
|
||||
return null;
|
||||
}
|
||||
var targetType = targetObj.GetType();
|
||||
if (isProperty) {
|
||||
if (targetObj is IBlueRow r) {
|
||||
return r[methodName];
|
||||
}
|
||||
var prop = targetType.GetProperty(methodName);
|
||||
if (prop is null) {
|
||||
return null;
|
||||
}
|
||||
return prop.GetValue(targetObj);
|
||||
}
|
||||
if (TryGetExtFn(out var extObj)) {
|
||||
var extEval = ExceptionAssert.Argument.Is<IFunctionEvaluator>(extObj);
|
||||
var argArray = new (string?, int?, object?)[arguments.Count + 1];
|
||||
argArray[0] = (null, 0, targetObj);
|
||||
int argIdx = 1;
|
||||
var fakeStreamFactory = new DataStreamFactory();
|
||||
foreach (var arg in arguments) {
|
||||
var argVal = arg.Evaluate(env, functions, fakeStreamFactory);
|
||||
argArray[argIdx] = (arg.Name, argIdx, argVal);
|
||||
argIdx++;
|
||||
}
|
||||
return extEval.Evaluate(methodName, argArray);
|
||||
}
|
||||
var method = targetType.GetMethod(methodName);
|
||||
if (method is null) {
|
||||
return null;
|
||||
}
|
||||
return method.Invoke(targetObj, Array.Empty<object>());
|
||||
|
||||
bool TryGetExtFn([NotNullWhen(true)] out IFunctionEvaluator? eval) {
|
||||
for (int i = 0; i < typeKeys.Length; i++) {
|
||||
if (targetType.IsAssignableTo(typeKeys[i]) && env.TryGetValue(typeKeys[i].Name, out var evalObj)) {
|
||||
eval = ExceptionAssert.Argument.Is<IFunctionEvaluator>(evalObj);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
eval = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,30 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
class Assignment : StatementExpression {
|
||||
readonly StatementExpression expression;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class Assignment : IDataStreamExpression {
|
||||
readonly IDataStreamExpression expression;
|
||||
readonly string id;
|
||||
|
||||
public Assignment(string identifier, StatementExpression st) {
|
||||
public Assignment(string identifier, IDataStreamExpression st) {
|
||||
id = identifier;
|
||||
expression = st;
|
||||
}
|
||||
|
||||
public override string print(int startCol, IList<string> activeBindings) {
|
||||
string prelude = id + " = ";
|
||||
string result = prelude + expression.print(startCol + prelude.Length, activeBindings);
|
||||
if (activeBindings.IndexOf(id) == -1)
|
||||
public void ToCode(IStringBuilder bd, IList<string> activeBindings) {
|
||||
bd.Append(id);
|
||||
bd.Append(" = ");
|
||||
expression.ToCode(bd, activeBindings);
|
||||
if (!activeBindings.Contains(id)) {
|
||||
activeBindings.Add(id);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public override ViewStream? myGetView(IDictionary<string, StatementExpression> env, IDataStreamFactory streamFactory) {
|
||||
var result = expression.getView(env, streamFactory);
|
||||
env[id] = expression;
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var result = expression.Evaluate(env, functions, streamFactory);
|
||||
env[id] = result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
static class CodeFormatter {
|
||||
public const int TAB_SIZE = 3;
|
||||
|
||||
public static string CsString(string? s) =>
|
||||
s is null ? "(string?)null" : "\"" + s + "\"";
|
||||
|
||||
public static string Boolean(bool b) =>
|
||||
b ? "true" : "false";
|
||||
|
||||
public static string WrapDoubleQuotes(string? s) =>
|
||||
"\"" + s + "\"";
|
||||
|
||||
public static string DateTimeParse(DateTime dt) =>
|
||||
$"DateTime.Parse(\"{dt}\")";
|
||||
}
|
||||
@@ -4,39 +4,37 @@ using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System.Text;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
public class DataStreamExpression {
|
||||
public DataStreamExpression(ImmutableList<StatementExpression> statements) =>
|
||||
public class DataStreamExpression : IDataStreamExpression {
|
||||
public DataStreamExpression()
|
||||
: this(ImmutableList<IDataStreamExpression>.Empty) { }
|
||||
|
||||
public DataStreamExpression(ImmutableList<IDataStreamExpression> statements) =>
|
||||
Statements = statements;
|
||||
|
||||
public ImmutableList<StatementExpression> Statements { get; }
|
||||
public ImmutableList<IDataStreamExpression> Statements { get; }
|
||||
|
||||
public string print(int startPos, IList<string> activeBindings) {
|
||||
var sb = new StringBuilder();
|
||||
public DataStreamExpression Add(IDataStreamExpression statement) =>
|
||||
new(Statements.Add(statement));
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
bool isFirst = true;
|
||||
foreach (var st in Statements) {
|
||||
sb.Append(new string(' ', startPos));
|
||||
sb.Append(st.print(startPos, activeBindings));
|
||||
sb.AppendLine(";");
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
builder.AppendLine();
|
||||
}
|
||||
st.ToCode(builder, activeBindings);
|
||||
builder.Append(';');
|
||||
}
|
||||
sb.AppendLine();
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string printView(int startPos) {
|
||||
var sb = new StringBuilder();
|
||||
foreach (var st in Statements) {
|
||||
sb.Append(new string(' ', startPos));
|
||||
sb.AppendLine(st.printView(startPos));
|
||||
}
|
||||
sb.AppendLine();
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public ViewStream? getView(IDictionary<string, StatementExpression> env, IDataStreamFactory streamFactory) {
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
for (int i = 0; i < Statements.Count - 1; i++) {
|
||||
Statements[i].getView(env, streamFactory);
|
||||
Statements[i].Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return Statements[^1].getView(env, streamFactory);
|
||||
return Statements[^1].Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
public static class DataStreamExpressionExtensions {
|
||||
public static object? Evaluate(this IDataStreamExpression expression, IDataStreamFactory streamFactory) =>
|
||||
expression.Evaluate(new Dictionary<string, object?>(0), new Dictionary<string, FunctionDefinition>(0), streamFactory);
|
||||
}
|
||||
@@ -13,82 +13,170 @@
|
||||
public DateTime DateTime;
|
||||
public DBNull Null;
|
||||
public DataStreamExpression DataStream;
|
||||
public StatementExpression Statement;
|
||||
public PrimitiveExpression Primitive;
|
||||
public IDataStreamExpression Statement;
|
||||
public ImmutableList<Argument> Arguments;
|
||||
public Argument BinaryArgument;
|
||||
public Argument ArithmeticArgument;
|
||||
public Argument Argument;
|
||||
public string ViewName;
|
||||
public QuotedView QuotedView;
|
||||
public Argument PrimitiveConst;
|
||||
public IDataStreamExpression SimpleArg;
|
||||
}
|
||||
|
||||
%token BOOLEAN_CONSTANT, INTEGER_CONSTANT, REAL_CONSTANT, DATE_TIME_CONSTANT, CS_STRING_CONSTANT, NULL_CONSTANT
|
||||
%token PARENTHESIS_OPEN, PARENTHESIS_CLOSE, COMMA, COLON, IDENTIFIER, EQUALS, QUOTE_OPEN, QUOTE_CLOSE
|
||||
%token LINE_COMMENT
|
||||
%token PARENTHESIS_OPEN, PARENTHESIS_CLOSE, BRACKET_OPEN, BRACKET_CLOSE, COMMA, COLON, IDENTIFIER, EQUALS
|
||||
/* Arithmetic Operators */
|
||||
%token PLUS, MINUS, MULTIPLY, DIVIDE, EQUAL, NOT_EQUAL, GREATER, GREATER_EQUAL, LESS, LESS_EQUAL
|
||||
%token LINE_COMMENT, DOT
|
||||
|
||||
%nonassoc EQUALS
|
||||
%left PLUS, MINUS
|
||||
%left MULTIPLY, DIVIDE
|
||||
%left DOT
|
||||
%nonassoc EQUALS, LESS, LESS_EQUAL, GREATER, GREATER_EQUAL
|
||||
|
||||
%type <DataStream> DataStream
|
||||
%type <Statement> Statement
|
||||
%type <BinaryArgument> BinaryArgument
|
||||
%type <ArithmeticArgument> ArithmeticArgument
|
||||
%type <Argument> Argument
|
||||
%type <PrimitiveConst> PrimitiveConst
|
||||
%type <SimpleArg> SimpleArg
|
||||
%type <Arguments> Arguments
|
||||
%type <QuotedView> QuotedView
|
||||
|
||||
%%
|
||||
|
||||
DataStream :
|
||||
Statement { $$ = new(System.Collections.Immutable.ImmutableList<StatementExpression>.Empty.Add($1)); }
|
||||
Statement { $$ = new DataStreamExpression().Add($1); }
|
||||
| Statement DataStream { $$ = new($2.Statements.Insert(0, $1)); }
|
||||
| LINE_COMMENT DataStream { $$ = $2; }
|
||||
| DataStream LINE_COMMENT { $$ = $1; }
|
||||
;
|
||||
|
||||
BinaryArgument :
|
||||
Argument EQUAL Argument {
|
||||
$$ = $1.Equal($3);
|
||||
}
|
||||
| Argument NOT_EQUAL Argument {
|
||||
$$ = $1.NotEqual($3);
|
||||
}
|
||||
| Argument GREATER Argument {
|
||||
$$ = $1.Greater($3);
|
||||
}
|
||||
| Argument GREATER_EQUAL Argument {
|
||||
$$ = $1.GreaterEqual($3);
|
||||
}
|
||||
| Argument LESS Argument {
|
||||
$$ = $1.Less($3);
|
||||
}
|
||||
| Argument LESS_EQUAL Argument {
|
||||
$$ = $1.LessEqual($3);
|
||||
}
|
||||
;
|
||||
|
||||
Statement :
|
||||
IDENTIFIER PARENTHESIS_OPEN Arguments PARENTHESIS_CLOSE {
|
||||
$$ = new PrimitiveExpression($1.Identifier, $3);
|
||||
}
|
||||
| IDENTIFIER {
|
||||
$$ = new ViewNameExpression($1.Identifier);
|
||||
}
|
||||
| IDENTIFIER EQUALS Statement {
|
||||
$$ = new Assignment($1.Identifier, $3);
|
||||
}
|
||||
| LINE_COMMENT Statement {
|
||||
$$ = $2;
|
||||
}
|
||||
| Statement LINE_COMMENT {
|
||||
Argument {
|
||||
$$ = $1;
|
||||
}
|
||||
| IDENTIFIER EQUALS Argument {
|
||||
$$ = new Assignment($1.Identifier, $3);
|
||||
}
|
||||
;
|
||||
|
||||
Arguments :
|
||||
{
|
||||
$$ = System.Collections.Immutable.ImmutableList<Argument>.Empty;
|
||||
IDENTIFIER COLON Argument {
|
||||
$$ = ImmutableList<Argument>.Empty.Add($3.WithName($1.Identifier));
|
||||
}
|
||||
| Argument {
|
||||
$$ = System.Collections.Immutable.ImmutableList<Argument>.Empty.Add($1);
|
||||
| Argument COMMA Argument {
|
||||
$$ = ImmutableList<Argument>.Empty.Add($1).Add($3);
|
||||
}
|
||||
| Arguments COMMA Argument {
|
||||
$$ = $1.Add(new($3));
|
||||
}
|
||||
| Arguments COMMA IDENTIFIER COLON Argument {
|
||||
$$ = $1.Add($5.WithName($3.Identifier));
|
||||
}
|
||||
;
|
||||
|
||||
ArithmeticArgument :
|
||||
Argument PLUS Argument {
|
||||
$$ = $1.Add($3);
|
||||
}
|
||||
| Argument MINUS Argument {
|
||||
$$ = $1.Subtract($3);
|
||||
}
|
||||
| Argument MULTIPLY Argument {
|
||||
$$ = $1.Multiply($3);
|
||||
}
|
||||
| Argument DIVIDE Argument {
|
||||
$$ = $1.Divide($3);
|
||||
}
|
||||
;
|
||||
|
||||
Argument :
|
||||
CS_STRING_CONSTANT { $$ = new($1.CsString); }
|
||||
| IDENTIFIER COLON CS_STRING_CONSTANT { $$ = new($3.CsString, $1.Identifier); }
|
||||
| BOOLEAN_CONSTANT { $$ = new($1.Boolean); }
|
||||
| IDENTIFIER COLON BOOLEAN_CONSTANT { $$ = new($3.Boolean, $1.Identifier); }
|
||||
| INTEGER_CONSTANT { $$ = new($1.Integer); }
|
||||
| IDENTIFIER COLON INTEGER_CONSTANT { $$ = new($3.Integer, $1.Identifier); }
|
||||
| REAL_CONSTANT { $$ = new($1.Real); }
|
||||
| IDENTIFIER COLON REAL_CONSTANT { $$ = new($3.Real, $1.Identifier); }
|
||||
| DATE_TIME_CONSTANT { $$ = new($1.DateTime); }
|
||||
| IDENTIFIER COLON DATE_TIME_CONSTANT { $$ = new($3.DateTime, $1.Identifier); }
|
||||
| NULL_CONSTANT { $$ = new($1.Null); }
|
||||
| IDENTIFIER COLON NULL_CONSTANT { $$ = new($3.Null, $1.Identifier); }
|
||||
| Statement { $$ = new($1); }
|
||||
| IDENTIFIER COLON Statement { $$ = new($3, $1.Identifier); }
|
||||
| QuotedView { $$ = new($1); }
|
||||
| IDENTIFIER COLON QuotedView { $$ = new($3, $1.Identifier); }
|
||||
PrimitiveConst { $$ = $1; }
|
||||
| SimpleArg { $$ = new($1); }
|
||||
| ArithmeticArgument { $$ = $1; }
|
||||
| BinaryArgument { $$ = $1; }
|
||||
| BRACKET_OPEN Argument BRACKET_CLOSE {
|
||||
$$ = new(ImmutableList<Argument>.Empty.Add($2));
|
||||
}
|
||||
| BRACKET_OPEN Arguments BRACKET_CLOSE {
|
||||
$$ = new($2);
|
||||
}
|
||||
;
|
||||
|
||||
QuotedView :
|
||||
QUOTE_OPEN DataStream QUOTE_CLOSE { $$ = new($2.printView(0)); }
|
||||
SimpleArg :
|
||||
IDENTIFIER {
|
||||
$$ = new Variable($1.Identifier);
|
||||
}
|
||||
| IDENTIFIER PARENTHESIS_OPEN PARENTHESIS_CLOSE {
|
||||
$$ = new PrimitiveExpression($1.Identifier, ImmutableList<Argument>.Empty);
|
||||
}
|
||||
| IDENTIFIER PARENTHESIS_OPEN Argument PARENTHESIS_CLOSE {
|
||||
$$ = new PrimitiveExpression($1.Identifier, ImmutableList<Argument>.Empty.Add($3));
|
||||
}
|
||||
| IDENTIFIER PARENTHESIS_OPEN Arguments PARENTHESIS_CLOSE {
|
||||
$$ = new PrimitiveExpression($1.Identifier, $3);
|
||||
}
|
||||
| SimpleArg DOT SimpleArg {
|
||||
if ($3 is PrimitiveExpression p) {
|
||||
if ($1 is Variable) {
|
||||
$$ = new ArgumentExtensionFunctionChain($1, p.Id, p.Arguments, false);
|
||||
} else if ($1 is PrimitiveExpression) {
|
||||
$$ = new ArgumentExtensionFunctionChain($1, p.Id, p.Arguments, false);
|
||||
} else if ($1 is ArgumentExtensionFunctionChain _ch) {
|
||||
$$ = _ch.Extend(p);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
} else if ($3 is Variable v) {
|
||||
if ($1 is Variable) {
|
||||
$$ = new ArgumentExtensionFunctionChain($1, v.Id, null, true);
|
||||
} else if ($1 is PrimitiveExpression) {
|
||||
$$ = new ArgumentExtensionFunctionChain($1, v.Id, null, true);
|
||||
} else if ($1 is ArgumentExtensionFunctionChain _ch) {
|
||||
$$ = _ch.Extend(v);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
} else if ($3 is ArgumentExtensionFunctionChain ch) {
|
||||
if ($1 is Variable _v) {
|
||||
$$ = ch.Prepend(_v);
|
||||
} else if ($1 is PrimitiveExpression _pe) {
|
||||
$$ = ch.Prepend(_pe);
|
||||
} else if ($1 is ArgumentExtensionFunctionChain _ch) {
|
||||
$$ = _ch.Extend(ch);
|
||||
}
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
PrimitiveConst :
|
||||
CS_STRING_CONSTANT { $$ = new($1.CsString); }
|
||||
| BOOLEAN_CONSTANT { $$ = new($1.Boolean); }
|
||||
| INTEGER_CONSTANT { $$ = new($1.Integer); }
|
||||
| REAL_CONSTANT { $$ = new($1.Real); }
|
||||
| DATE_TIME_CONSTANT { $$ = new($1.DateTime); }
|
||||
| NULL_CONSTANT { $$ = new($1.Null); }
|
||||
;
|
||||
@@ -2,6 +2,11 @@ namespace Bts.DataStreamExpressions;
|
||||
|
||||
using System.Text;
|
||||
|
||||
public enum DataStreamScriptType {
|
||||
Main,
|
||||
Function
|
||||
}
|
||||
|
||||
partial class DataStreamExpressionParser {
|
||||
public DataStreamExpressionParser() : base(null) { }
|
||||
|
||||
|
||||
@@ -24,16 +24,26 @@ RealConstant -?[0-9]+\.[0-9]+
|
||||
|
||||
/* Relational Operators */
|
||||
Equals =
|
||||
Greater >
|
||||
Less <
|
||||
Equal ==
|
||||
NotEqual !=
|
||||
LessEqual <=
|
||||
GreaterEqual >=
|
||||
|
||||
|
||||
Identifier {Letter}({Letter}*[0-9]*_*)*
|
||||
|
||||
/* Utility Tokens */
|
||||
OpenParenthesis \(
|
||||
CloseParenthesis \)
|
||||
OpenBracket \[
|
||||
CloseBracket \]
|
||||
Comma ,
|
||||
Colon :
|
||||
QuoteOpen \[
|
||||
QuoteClose \]
|
||||
Plus +
|
||||
Minus -
|
||||
Dot \.
|
||||
|
||||
/* Comments */
|
||||
LineComment \/\/[^\n\r]*(\n|\r|\r\n)
|
||||
@@ -44,6 +54,30 @@ LineComment \/\/[^\n\r]*(\n|\r|\r\n)
|
||||
return (int)DataStreamExpressionToken.EQUALS;
|
||||
}
|
||||
|
||||
{Equal} {
|
||||
return (int)DataStreamExpressionToken.EQUAL;
|
||||
}
|
||||
|
||||
{NotEqual} {
|
||||
return (int)DataStreamExpressionToken.NOT_EQUAL;
|
||||
}
|
||||
|
||||
{LessEqual} {
|
||||
return (int)DataStreamExpressionToken.LESS_EQUAL;
|
||||
}
|
||||
|
||||
{GreaterEqual} {
|
||||
return (int)DataStreamExpressionToken.GREATER_EQUAL;
|
||||
}
|
||||
|
||||
{Less} {
|
||||
return (int)DataStreamExpressionToken.LESS;
|
||||
}
|
||||
|
||||
{Greater} {
|
||||
return (int)DataStreamExpressionToken.GREATER;
|
||||
}
|
||||
|
||||
{CSStringConstant} {
|
||||
yylval.CsString = yytext.Substring(1, yytext.Length - 2);
|
||||
return (int)DataStreamExpressionToken.CS_STRING_CONSTANT;
|
||||
@@ -87,6 +121,14 @@ LineComment \/\/[^\n\r]*(\n|\r|\r\n)
|
||||
return (int)DataStreamExpressionToken.PARENTHESIS_CLOSE;
|
||||
}
|
||||
|
||||
{OpenBracket} {
|
||||
return (int)DataStreamExpressionToken.BRACKET_OPEN;
|
||||
}
|
||||
|
||||
{CloseBracket} {
|
||||
return (int)DataStreamExpressionToken.BRACKET_CLOSE;
|
||||
}
|
||||
|
||||
{Comma} {
|
||||
return (int)DataStreamExpressionToken.COMMA;
|
||||
}
|
||||
@@ -95,14 +137,6 @@ LineComment \/\/[^\n\r]*(\n|\r|\r\n)
|
||||
return (int)DataStreamExpressionToken.COLON;
|
||||
}
|
||||
|
||||
{QuoteOpen} {
|
||||
return (int)DataStreamExpressionToken.QUOTE_CLOSE;
|
||||
}
|
||||
|
||||
{QuoteClose} {
|
||||
return (int)DataStreamExpressionToken.QUOTE_OPEN;
|
||||
}
|
||||
|
||||
{Identifier} {
|
||||
switch (yytext.ToLower()) {
|
||||
case "true":
|
||||
@@ -121,4 +155,16 @@ LineComment \/\/[^\n\r]*(\n|\r|\r\n)
|
||||
return (int)DataStreamExpressionToken.LINE_COMMENT;
|
||||
}
|
||||
|
||||
{Plus} {
|
||||
return (int)DataStreamExpressionToken.PLUS;
|
||||
}
|
||||
|
||||
{Minus} {
|
||||
return (int)DataStreamExpressionToken.MINUS;
|
||||
}
|
||||
|
||||
{Dot} {
|
||||
return (int)DataStreamExpressionToken.DOT;
|
||||
}
|
||||
|
||||
%%
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.CodeDom" Version="9.0.9" />
|
||||
<PackageReference Include="YaccLexTools" Version="1.2.2" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -20,6 +21,8 @@
|
||||
<ItemGroup Label="DataStreamExpression">
|
||||
<Scanner Include="DataStreamExpressionTokens.lex" />
|
||||
<Parser Include="DataStreamExpressionGrammar.y" />
|
||||
<Scanner Include="FunctionSignatureTokens.lex" />
|
||||
<Parser Include="FunctionSignatureGrammar.y" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
public class DataStreamScript : IDataStreamExpression {
|
||||
public DataStreamScript(ImmutableList<(FunctionDefinition, int)>? functions = null, ImmutableList<(IDataStreamExpression, int)>? statements = null) {
|
||||
Functions = functions ?? ImmutableList<(FunctionDefinition, int)>.Empty;
|
||||
Statements = statements ?? ImmutableList<(IDataStreamExpression, int)>.Empty;
|
||||
}
|
||||
|
||||
public static DataStreamScript Empty => new();
|
||||
|
||||
public ImmutableList<(FunctionDefinition, int)> Functions { get; }
|
||||
public ImmutableList<(IDataStreamExpression, int)> Statements { get; }
|
||||
|
||||
public DataStreamScript Add(FunctionDefinition fn) =>
|
||||
new(Functions.Add((fn, Functions.Count + Statements.Count)), Statements);
|
||||
|
||||
public DataStreamScript Add(IDataStreamExpression statement) =>
|
||||
new(Functions, Statements.Add((statement, Functions.Count + Statements.Count)));
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
bool isFirst = true;
|
||||
var count = Functions.Count + Statements.Count;
|
||||
var statements = Statements.Select(s => ((IFormattableCode)s.Item1, s.Item2));
|
||||
var functions = Functions.Select(s => ((IFormattableCode)s.Item1, s.Item2));
|
||||
var comp = new ComparerBuilder<(IFormattableCode, int)>() { t => t.Item2 };
|
||||
var formattableCodes = statements.MergeSorted(functions, comp);
|
||||
foreach (var (fc, i) in formattableCodes) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
builder.AppendLine();
|
||||
}
|
||||
fc.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
foreach (var fn in Functions) {
|
||||
functions[fn.Item1.Name] = fn.Item1;
|
||||
}
|
||||
for (int i = 0; i < Statements.Count - 1; i++) {
|
||||
Statements[i].Item1.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return Statements[^1].Item1.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamExpressions.Internal;
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
public class DataStreamScriptInterpreter {
|
||||
public static DataStreamScript Evaluate(string str) {
|
||||
using var ms = new MemoryStream(Encoding.Default.GetBytes(str));
|
||||
return Evaluate(ms);
|
||||
}
|
||||
|
||||
public static DataStreamScript Evaluate(Stream stream) {
|
||||
using var reader = new StreamReader(stream);
|
||||
var line = reader.ReadLine();
|
||||
var script = new DataStreamScript();
|
||||
while (line is not null) {
|
||||
if (!Consume(reader, ref line, ref script)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return script;
|
||||
|
||||
static bool Consume(StreamReader reader, ref string? line, ref DataStreamScript script) {
|
||||
if (line is null) {
|
||||
return false;
|
||||
}
|
||||
var ms = new MemoryStream();
|
||||
var sw = new StreamWriter(ms, leaveOpen: true);
|
||||
if (IsFunctionStart(line)) {
|
||||
var functionSigStr = line.Trim();
|
||||
if (line.EndsWith("{")) {
|
||||
functionSigStr = functionSigStr.Substring(0, functionSigStr.Length - 1).TrimEnd();
|
||||
}
|
||||
var fnSig = FunctionSignatureInterpreter.Evaluate(functionSigStr);
|
||||
bool fnClosed = false;
|
||||
for (line = reader.ReadLine(); line is not null; line = reader.ReadLine()) {
|
||||
var trimmedLine = line.Trim();
|
||||
if (trimmedLine == "}") {
|
||||
fnClosed = true;
|
||||
line = reader.ReadLine();
|
||||
break;
|
||||
}
|
||||
sw.WriteLine(trimmedLine);
|
||||
}
|
||||
if (!fnClosed) {
|
||||
sw.Close();
|
||||
ms.Dispose();
|
||||
throw new ArgumentException("Unclosed function", nameof(stream));
|
||||
}
|
||||
sw.Close();
|
||||
ms.Position = 0;
|
||||
var expression = DataStreamExpressionsInterpreter.Evaluate(ms);
|
||||
var fnDef = new FunctionDefinition(fnSig.Name, fnSig.Variables).AddStatement(expression);
|
||||
script = script.Add(fnDef);
|
||||
} else {
|
||||
do {
|
||||
sw.WriteLine(line);
|
||||
line = reader.ReadLine();
|
||||
} while (line is not null && !IsFunctionStart(line));
|
||||
sw.Close();
|
||||
ms.Position = 0;
|
||||
var expression = DataStreamExpressionsInterpreter.Evaluate(ms);
|
||||
script = script.Add(expression);
|
||||
// script = script.Add(Evaluate)
|
||||
// Don't read next line (if it's not null, it's already the first
|
||||
// line of the next section).
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool IsFunctionStart(string line) =>
|
||||
line is not null && line.StartsWith("def ") && line.TrimEnd().EndsWith("{");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class DivideExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public DivideExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftDivide = ToDivide(leftVal);
|
||||
return leftDivide.Divide(rightVal);
|
||||
|
||||
IDivide ToDivide(object? val) {
|
||||
if (val is int valInt32) {
|
||||
return new Int32Divide(valInt32);
|
||||
}
|
||||
if (val is long valInt64) {
|
||||
return new Int64Divide(valInt64);
|
||||
}
|
||||
if (val is uint valUint32) {
|
||||
return new Uint32Divide(valUint32);
|
||||
}
|
||||
if (val is ulong valUint64) {
|
||||
return new Uint64Divide(valUint64);
|
||||
}
|
||||
if (val is float valFloat32) {
|
||||
return new Float32Divide(valFloat32);
|
||||
}
|
||||
if (val is double valFloat64) {
|
||||
return new Float64Divide(valFloat64);
|
||||
}
|
||||
throw new ArgumentException($"Cannot convert obj of type '{val?.GetType()}' to IDivide", nameof(val));
|
||||
}
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" / ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
|
||||
interface IDivide {
|
||||
object Divide(object? other);
|
||||
}
|
||||
|
||||
class Int32Divide : IDivide {
|
||||
readonly int left;
|
||||
|
||||
public Int32Divide(int left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Divide(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left / rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left / rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left / rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left / rightUint32;
|
||||
}
|
||||
if (other is IDivide rightDivide) {
|
||||
return rightDivide.Divide(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Divide obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Int64Divide : IDivide {
|
||||
readonly long left;
|
||||
|
||||
public Int64Divide(long left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Divide(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left / rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left / rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left / rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left / rightUint32;
|
||||
}
|
||||
if (other is IDivide rightDivide) {
|
||||
return rightDivide.Divide(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Divide obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint32Divide : IDivide {
|
||||
readonly uint left;
|
||||
|
||||
public Uint32Divide(uint left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Divide(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left / rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left / rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left / rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left / rightUint32;
|
||||
}
|
||||
throw new ArgumentException($"Cannot Divide obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint64Divide : IDivide {
|
||||
readonly ulong left;
|
||||
|
||||
public Uint64Divide(ulong left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Divide(object? other) {
|
||||
checked {
|
||||
if (other is int rightIn32) {
|
||||
return (long)left / rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left / rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left / rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left / rightUint32;
|
||||
}
|
||||
}
|
||||
if (other is IDivide rightDivide) {
|
||||
return rightDivide.Divide(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Divide obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float32Divide : IDivide {
|
||||
readonly float left;
|
||||
|
||||
public Float32Divide(float left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Divide(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left / rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left / rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left / rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left / rightUint32;
|
||||
}
|
||||
if (other is IDivide rightDivide) {
|
||||
return rightDivide.Divide(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Divide obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float64Divide : IDivide {
|
||||
readonly double left;
|
||||
|
||||
public Float64Divide(double left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Divide(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left / rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left / rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left / rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left / rightUint32;
|
||||
}
|
||||
if (other is IDivide rightDivide) {
|
||||
return rightDivide.Divide(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Divide obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class EqualExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public EqualExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
if (leftVal is null || leftVal == DBNull.Value) {
|
||||
return rightVal is null || rightVal == DBNull.Value;
|
||||
}
|
||||
if (rightVal is null || rightVal == DBNull.Value) {
|
||||
return false;
|
||||
}
|
||||
return Equals(leftVal, rightVal);
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" == ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
public class FunctionDefinition : IFormattableCode, IDataStreamExpression {
|
||||
public FunctionDefinition(string name, ImmutableList<Variable>? parameters = null) {
|
||||
Name = name;
|
||||
Parameters = parameters ?? ImmutableList<Variable>.Empty;
|
||||
Statements = ImmutableList<IDataStreamExpression>.Empty;
|
||||
}
|
||||
|
||||
public FunctionDefinition(string name, ImmutableList<IDataStreamExpression> statements, ImmutableList<Variable>? parameters = null) {
|
||||
Name = name;
|
||||
Parameters = parameters ?? ImmutableList<Variable>.Empty;
|
||||
Statements = statements;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public ImmutableList<IDataStreamExpression> Statements { get; }
|
||||
public ImmutableList<Variable> Parameters { get; }
|
||||
|
||||
public Type ResultType => typeof(IFunctionEvaluator);
|
||||
|
||||
public FunctionDefinition AddStatement(IDataStreamExpression statement) =>
|
||||
new(Name, Statements.Add(statement), Parameters);
|
||||
|
||||
public FunctionDefinition AddParameter(Variable parameter) =>
|
||||
new(Name, Statements, Parameters.Add(parameter));
|
||||
|
||||
public IFunctionEvaluator Evaluate(
|
||||
IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) =>
|
||||
new MyEvaluator(Statements, Parameters, env, functions, streamFactory);
|
||||
|
||||
public Func<IBlueRow, bool> ToPredicate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
if (Parameters.Count != 1) {
|
||||
throw new InvalidOperationException("Expected 1 parameter");
|
||||
}
|
||||
var paramName = Parameters[0].Id;
|
||||
return Predicate;
|
||||
|
||||
bool Predicate(IBlueRow row) {
|
||||
var dict = new Dictionary<string, object?>(env) {
|
||||
[paramName] = row
|
||||
};
|
||||
var res = Statements.Aggregate((object?)null, (vs, s) => s.Evaluate(env, functions, streamFactory));
|
||||
if (res is SingleValueStream svs) {
|
||||
return svs.rows.Cast<IBlueRow>().FirstOrDefault()?.GetBool(1)
|
||||
?? throw new InvalidOperationException("Failed to convert predicate result to bool");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder bd, IList<string> activeBindings) {
|
||||
bd.Append("def ");
|
||||
bd.Append(Name);
|
||||
bd.Append('(');
|
||||
for (int i = 0; i < Parameters.Count; i++) {
|
||||
if (i > 0) {
|
||||
bd.Append(", ");
|
||||
}
|
||||
var p = Parameters[i];
|
||||
bd.Append(p.Type.ToString());
|
||||
bd.Append(' ');
|
||||
bd.Append(p.Id);
|
||||
}
|
||||
{
|
||||
using var ibd = bd.PushIndentation(CodeFormatter.TAB_SIZE);
|
||||
ibd.AppendLine(") {");
|
||||
bool isFirst = true;
|
||||
foreach (var st in Statements) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
ibd.AppendLine();
|
||||
}
|
||||
// We're using an IndentedStringBuilder, so pass indentationLevel 1 to each statement
|
||||
// as an offset to this indentationLevel param.
|
||||
st.ToCode(ibd, activeBindings);
|
||||
}
|
||||
}
|
||||
bd.AppendLine();
|
||||
bd.Append("}");
|
||||
}
|
||||
|
||||
object? IDataStreamExpression.Evaluate(
|
||||
IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) =>
|
||||
new MyEvaluator(Statements, Parameters, env, functions, streamFactory);
|
||||
|
||||
class MyEvaluator : IFunctionEvaluator {
|
||||
readonly ImmutableList<IDataStreamExpression> statements;
|
||||
readonly ImmutableList<Variable> parameters;
|
||||
readonly IDictionary<string, object?> env;
|
||||
readonly IDictionary<string, FunctionDefinition> functions;
|
||||
readonly IDataStreamFactory streamFactory;
|
||||
|
||||
public MyEvaluator(ImmutableList<IDataStreamExpression> statements, ImmutableList<Variable> parameters, IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
this.statements = statements;
|
||||
this.parameters = parameters;
|
||||
this.env = env;
|
||||
this.functions = functions;
|
||||
this.streamFactory = streamFactory;
|
||||
}
|
||||
|
||||
public object? Evaluate(string functionId, IEnumerable<(string? Name, int? Index, object? Value)> args) {
|
||||
// Add function input args to env
|
||||
var mergedEnv = AggregateDictionary.Create(parameters.Zip(args).ToDictionary(t => t.First.Id, t => t.Second.Value), env, false);
|
||||
var res = statements.Aggregate((object?)null, (vs, s) => s.Evaluate(mergedEnv, functions, streamFactory));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
public class FunctionSignature {
|
||||
public FunctionSignature(string name, ImmutableList<Variable>? variables = null) {
|
||||
Name = name;
|
||||
Variables = variables ?? ImmutableList<Variable>.Empty;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public ImmutableList<Variable> Variables { get; }
|
||||
|
||||
public FunctionSignature AddVariable(Variable variable) =>
|
||||
new(Name, Variables.Add(variable));
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
%namespace Bts.DataStreamExpressions.Internal
|
||||
%partial
|
||||
%visibility internal
|
||||
%tokentype FunctionSignatureToken
|
||||
%parsertype FunctionSignatureParser
|
||||
|
||||
%union {
|
||||
public string DefName;
|
||||
public string Identifier;
|
||||
public FunctionSignature FunctionSignature;
|
||||
public System.Collections.Immutable.ImmutableList<Variable> Variables;
|
||||
}
|
||||
|
||||
%token DEF, IDENTIFIER, PARENTHESIS_OPEN, PARENTHESIS_CLOSE
|
||||
%token COMMA
|
||||
|
||||
%type <FunctionSignature> FunctionSignature
|
||||
%type <Variables> Variables
|
||||
|
||||
%%
|
||||
|
||||
FunctionSignature :
|
||||
DEF IDENTIFIER PARENTHESIS_OPEN PARENTHESIS_CLOSE {
|
||||
$$ = new($2.Identifier, ImmutableList<Variable>.Empty);
|
||||
}
|
||||
| DEF IDENTIFIER PARENTHESIS_OPEN Variables PARENTHESIS_CLOSE {
|
||||
$$ = new($2.Identifier, $4);
|
||||
}
|
||||
;
|
||||
|
||||
Variables :
|
||||
IDENTIFIER {
|
||||
$$ = ImmutableList<Variable>.Empty.Add(new($1.Identifier));
|
||||
}
|
||||
| Variables COMMA IDENTIFIER {
|
||||
$$ = $1.Add(new($3.Identifier));
|
||||
}
|
||||
;
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Bts.DataStreamExpressions.Internal;
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
public class FunctionSignatureInterpreter {
|
||||
public static FunctionSignature Evaluate(Stream stream) {
|
||||
var parser = new FunctionSignatureParser();
|
||||
var res = parser.Parse(stream);
|
||||
var program = res?.FunctionSignature ?? throw new Exception("Failed to parse FunctionSignature");
|
||||
return program;
|
||||
}
|
||||
|
||||
public static FunctionSignature Evaluate(string str) {
|
||||
using var ms = new MemoryStream(Encoding.Default.GetBytes(str));
|
||||
return Evaluate(ms);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Bts.DataStreamExpressions.Internal;
|
||||
|
||||
using System.Text;
|
||||
|
||||
partial class FunctionSignatureParser {
|
||||
public FunctionSignatureParser() : base(null) { }
|
||||
|
||||
public ValueType? Parse(string s) {
|
||||
using var ms = new MemoryStream(Encoding.Default.GetBytes(s));
|
||||
return Parse(ms);
|
||||
}
|
||||
|
||||
public ValueType? Parse(Stream stream) {
|
||||
Scanner = new FunctionSignatureScanner(stream);
|
||||
Parse();
|
||||
return CurrentSemanticValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
%namespace Bts.DataStreamExpressions.Internal
|
||||
%scannertype FunctionSignatureScanner
|
||||
%visibility internal
|
||||
%tokentype FunctionSignatureToken
|
||||
|
||||
%option stack, minimize, parser, verbose, persistbuffer, noembedbuffers
|
||||
|
||||
Def def
|
||||
Letter [a-zA-Z]
|
||||
|
||||
Identifier {Letter}({Letter}*[0-9]*_*)*
|
||||
|
||||
OpenParenthesis \(
|
||||
CloseParenthesis \)
|
||||
Comma ,
|
||||
|
||||
%%
|
||||
|
||||
{OpenParenthesis} {
|
||||
return (int)FunctionSignatureToken.PARENTHESIS_OPEN;
|
||||
}
|
||||
|
||||
{CloseParenthesis} {
|
||||
return (int)FunctionSignatureToken.PARENTHESIS_CLOSE;
|
||||
}
|
||||
|
||||
{Comma} {
|
||||
return (int)FunctionSignatureToken.COMMA;
|
||||
}
|
||||
|
||||
{Def} {
|
||||
return (int)FunctionSignatureToken.DEF;
|
||||
}
|
||||
|
||||
{Identifier} {
|
||||
yylval.Identifier = yytext;
|
||||
return (int)FunctionSignatureToken.IDENTIFIER;
|
||||
}
|
||||
|
||||
%%
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class GreaterEqualExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public GreaterEqualExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftCmp = leftVal as IComparable ?? throw new InvalidOperationException("Left argument is not IComparable");
|
||||
var res = leftCmp.CompareTo(rightVal);
|
||||
return res >= 0;
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" >= ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class GreaterExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public GreaterExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
if (leftVal is short leftInt16) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftInt16 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftInt16 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftInt16 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftInt16 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftInt16 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftInt16 > (long)rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftInt16 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftInt16 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is int leftInt32) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftInt32 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftInt32 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftInt32 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftInt32 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftInt32 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftInt32 > (long)rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftInt32 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftInt32 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is long leftInt64) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftInt64 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftInt64 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftInt64 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftInt64 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftInt64 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftInt64 > (long)rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftInt64 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftInt64 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is ushort leftUint16) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftUint16 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftUint16 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftUint16 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftUint16 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftUint16 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftUint16 > (long)rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftUint16 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftUint16 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is uint leftUint32) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftUint32 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftUint32 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftUint32 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftUint32 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftUint32 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftUint32 > (long)rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftUint32 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftUint32 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is ulong leftUint64) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return (long)leftUint64 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return (long)leftUint64 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return (long)leftUint64 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return (long)leftUint64 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return (long)leftUint64 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return (long)leftUint64 > (long)rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftUint64 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftUint64 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is float leftFloat32) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftFloat32 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftFloat32 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftFloat32 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftFloat32 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftFloat32 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftFloat32 > rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftFloat32 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftFloat32 > rightFloat64;
|
||||
}
|
||||
} else if (leftVal is double leftFloat64) {
|
||||
if (rightVal is short rightInt16) {
|
||||
return leftFloat64 > rightInt16;
|
||||
} else if (rightVal is int rightInt32) {
|
||||
return leftFloat64 > rightInt32;
|
||||
} else if (rightVal is long rightInt64) {
|
||||
return leftFloat64 > rightInt64;
|
||||
} else if (rightVal is ushort rightUint16) {
|
||||
return leftFloat64 > rightUint16;
|
||||
} else if (rightVal is uint rightUint32) {
|
||||
return leftFloat64 > rightUint32;
|
||||
} else if (rightVal is ulong rightUint64) {
|
||||
return leftFloat64 > rightUint64;
|
||||
} else if (rightVal is float rightFloat32) {
|
||||
return leftFloat64 > rightFloat32;
|
||||
} else if (rightVal is double rightFloat64) {
|
||||
return leftFloat64 > rightFloat64;
|
||||
}
|
||||
}
|
||||
var leftCmp = leftVal as IComparable ?? throw new InvalidOperationException("Left argument is not IComparable");
|
||||
var res = leftCmp.CompareTo(rightVal);
|
||||
return res > 0;
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" > ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System.CodeDom;
|
||||
|
||||
public interface IFormattableCode {
|
||||
void ToCode(IStringBuilder builder, IList<string> activeBindings);
|
||||
}
|
||||
|
||||
public interface IDataStreamExpression : IFormattableCode {
|
||||
Type ResultType => typeof(object);
|
||||
object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory);
|
||||
|
||||
CodeStatement? ToCodeStatement() => null;
|
||||
CodeExpression? ToCodeExpression() => null;
|
||||
}
|
||||
|
||||
class FunctionArg : IDataStreamExpression {
|
||||
public string? Name { get; set; }
|
||||
public int? Position { get; set; }
|
||||
public object? Value { get; set; }
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
builder.Append(Name);
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class FunctionArgGroup : IDataStreamExpression {
|
||||
public List<Argument?> Arguments { get; } = new();
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
return;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class LessEqualExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public LessEqualExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftCmp = leftVal as IComparable ?? throw new InvalidOperationException("Left argument is not IComparable");
|
||||
var res = leftCmp.CompareTo(rightVal);
|
||||
return res <= 0;
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" >= ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class LessExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public LessExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftCmp = leftVal as IComparable ?? throw new InvalidOperationException("Left argument is not IComparable");
|
||||
var res = leftCmp.CompareTo(rightVal);
|
||||
return res < 0;
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" < ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class MultiplyExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public MultiplyExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftMultiply = ToMultiply(leftVal);
|
||||
return leftMultiply.Multiply(rightVal);
|
||||
|
||||
IMultiply ToMultiply(object? val) {
|
||||
if (val is int valInt32) {
|
||||
return new Int32Multiply(valInt32);
|
||||
}
|
||||
if (val is long valInt64) {
|
||||
return new Int64Multiply(valInt64);
|
||||
}
|
||||
if (val is uint valUint32) {
|
||||
return new Uint32Multiply(valUint32);
|
||||
}
|
||||
if (val is ulong valUint64) {
|
||||
return new Uint64Multiply(valUint64);
|
||||
}
|
||||
if (val is float valFloat32) {
|
||||
return new Float32Multiply(valFloat32);
|
||||
}
|
||||
if (val is double valFloat64) {
|
||||
return new Float64Multiply(valFloat64);
|
||||
}
|
||||
throw new ArgumentException($"Cannot convert obj of type '{val?.GetType()}' to IMultiply", nameof(val));
|
||||
}
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" * ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
|
||||
interface IMultiply {
|
||||
object Multiply(object? other);
|
||||
}
|
||||
|
||||
class Int32Multiply : IMultiply {
|
||||
readonly int left;
|
||||
|
||||
public Int32Multiply(int left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Multiply(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left * rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left * rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left * rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left * rightUint32;
|
||||
}
|
||||
if (other is IMultiply rightMultiply) {
|
||||
return rightMultiply.Multiply(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot multiply obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Int64Multiply : IMultiply {
|
||||
readonly long left;
|
||||
|
||||
public Int64Multiply(long left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Multiply(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left * rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left * rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left * rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left * rightUint32;
|
||||
}
|
||||
if (other is IMultiply rightMultiply) {
|
||||
return rightMultiply.Multiply(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot multiply obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint32Multiply : IMultiply {
|
||||
readonly uint left;
|
||||
|
||||
public Uint32Multiply(uint left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Multiply(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left * rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left * rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left * rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left * rightUint32;
|
||||
}
|
||||
throw new ArgumentException($"Cannot multiply obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint64Multiply : IMultiply {
|
||||
readonly ulong left;
|
||||
|
||||
public Uint64Multiply(ulong left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Multiply(object? other) {
|
||||
checked {
|
||||
if (other is int rightIn32) {
|
||||
return (long)left * rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left * rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left * rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left * rightUint32;
|
||||
}
|
||||
}
|
||||
if (other is IMultiply rightMultiply) {
|
||||
return rightMultiply.Multiply(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot multiply obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float32Multiply : IMultiply {
|
||||
readonly float left;
|
||||
|
||||
public Float32Multiply(float left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Multiply(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left * rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left * rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left * rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left * rightUint32;
|
||||
}
|
||||
if (other is IMultiply rightMultiply) {
|
||||
return rightMultiply.Multiply(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot multiply obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float64Multiply : IMultiply {
|
||||
readonly double left;
|
||||
|
||||
public Float64Multiply(double left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Multiply(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left * rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left * rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left * rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left * rightUint32;
|
||||
}
|
||||
if (other is IMultiply rightMultiply) {
|
||||
return rightMultiply.Multiply(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot multiply obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class NotEqualExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public NotEqualExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
return !Equals(leftVal, rightVal);
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" != ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,17 @@ namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System.Collections.Immutable;
|
||||
|
||||
public class PrimitiveExpression : StatementExpression {
|
||||
private string myName;
|
||||
public class PrimitiveExpression : IDataStreamExpression {
|
||||
string myName;
|
||||
IList<Argument> myArgs;
|
||||
|
||||
public PrimitiveExpression(string name) {
|
||||
@@ -20,102 +25,79 @@ public class PrimitiveExpression : StatementExpression {
|
||||
myArgs = args;
|
||||
}
|
||||
|
||||
public override string print(int startCol, IList<string> activeBindings) {
|
||||
string prelude = "new " + myName + " (";
|
||||
public string Id => myName;
|
||||
public IList<Argument> Arguments => myArgs;
|
||||
|
||||
return prelude + myPrintArgs(startCol + prelude.Length, activeBindings) + ")";
|
||||
}
|
||||
|
||||
public override string printView(int startCol) {
|
||||
string prelude = myName + " (";
|
||||
|
||||
return prelude + myPrintViewArgs(startCol + prelude.Length) + ")";
|
||||
}
|
||||
|
||||
public override ViewStream? myGetView(IDictionary<string, StatementExpression> env, IDataStreamFactory streamFactory) {
|
||||
var resolvedArgs = myArgs.Select((arg, i) => ((string?)arg.Name, string.IsNullOrEmpty(arg.Name) ? (int?)i : null, ResolveArg(arg)));
|
||||
return streamFactory.Create(myName, resolvedArgs);
|
||||
|
||||
object? ResolveArg(Argument arg) {
|
||||
if (arg.StatementExp is not null) {
|
||||
return arg.StatementExp.myGetView(env, streamFactory);
|
||||
}
|
||||
if (arg.QuotedView is not null) {
|
||||
return arg.QuotedView.Value;
|
||||
}
|
||||
return arg.Obj;
|
||||
public void ToCode(IStringBuilder bd, IList<string> activeBindings) {
|
||||
bd.Append("new ");
|
||||
bd.Append(myName);
|
||||
bd.Append('(');
|
||||
switch (myArgs.Count) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
ArgToCode(bd, myArgs[0], activeBindings);
|
||||
break;
|
||||
case > 1:
|
||||
bd = bd.SetIndentation(CodeFormatter.TAB_SIZE);
|
||||
bd.AppendLine();
|
||||
ArgToCode(bd, myArgs[0], activeBindings);
|
||||
for (int i = 1; i < myArgs.Count; i++) {
|
||||
bd.AppendLine(",");
|
||||
ArgToCode(bd, myArgs[i], activeBindings);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
bd.Append(')');
|
||||
|
||||
string myPrintSingleArg(int atColumn, object? o, IList<string> activeBindings) {
|
||||
/// Unwrap <see cref="Argument"/> to get its underlying value
|
||||
while (o is Argument arg) {
|
||||
o = arg.Obj;
|
||||
// Safeguard against infinite loop
|
||||
if (ReferenceEquals(o, arg)) {
|
||||
throw new ArgumentException(
|
||||
$"Detected circular reference in argument " +
|
||||
$"('{nameof(arg)}.{nameof(arg.Obj)}' is '{nameof(arg)}' itself)");
|
||||
static void ArgToCode(IStringBuilder bd, object? o, IList<string> activeBindings) {
|
||||
/// Unwrap <see cref="Argument"/> to get its underlying value
|
||||
while (o is Argument arg) {
|
||||
o = arg.Obj;
|
||||
// Safeguard against infinite loop
|
||||
if (ReferenceEquals(o, arg)) {
|
||||
throw new ArgumentException(
|
||||
$"Detected circular reference in argument " +
|
||||
$"('{nameof(arg)}.{nameof(arg.Obj)}' is '{nameof(arg)}' itself)");
|
||||
}
|
||||
}
|
||||
|
||||
if (o is null) {
|
||||
return;
|
||||
}
|
||||
if (o is IDataStreamExpression exp) {
|
||||
exp.ToCode(bd, activeBindings);
|
||||
} else if (o is string str) {
|
||||
bd.Append(CodeFormatter.CsString(str));
|
||||
} else if (o is DateTime dt) {
|
||||
bd.Append(CodeFormatter.DateTimeParse(dt));
|
||||
} else if (o is QuotedView qv) {
|
||||
bd.Append(CodeFormatter.WrapDoubleQuotes(qv.Value));
|
||||
} else if (o is bool b) {
|
||||
bd.Append(CodeFormatter.Boolean(b));
|
||||
} else {
|
||||
bd.Append(o.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
string result = new string(' ', atColumn);
|
||||
if (o is null) {
|
||||
return result;
|
||||
}
|
||||
if (o is StatementExpression)
|
||||
return new string(' ', atColumn) +
|
||||
((o as StatementExpression)?.print(atColumn, activeBindings) ?? string.Empty);
|
||||
else {
|
||||
if (o is string)
|
||||
result += makeCSString((string)o);
|
||||
else if (o is DateTime)
|
||||
result += "DateTime.parse(\"" + ((DateTime)o).ToString() + "\")";
|
||||
else if (o is QuotedView)
|
||||
result += "\"" + ((QuotedView)o).Value + "\"";
|
||||
else if (o is bool)
|
||||
result += o.Equals(true) ? "true" : "false";
|
||||
else
|
||||
result += o.ToString();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
string myPrintArgs(int atColumn, IList<string> activeBindings) {
|
||||
string result = "";
|
||||
for (int i = 0; i < myArgs.Count; i++)
|
||||
if (i == 0)
|
||||
result += myPrintSingleArg(0, myArgs[i], activeBindings);
|
||||
else
|
||||
result += ",\n" + myPrintSingleArg(atColumn, myArgs[i], activeBindings);
|
||||
return result;
|
||||
}
|
||||
|
||||
string myPrintViewArgs(int atColumn) {
|
||||
string result = "";
|
||||
for (int i = 0; i < myArgs.Count; i++)
|
||||
if (i == 0)
|
||||
result += myPrintSingleViewArg(0, myArgs[i]);
|
||||
else
|
||||
result += ",\n" + myPrintSingleViewArg(atColumn, myArgs[i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
string myPrintSingleViewArg(int atColumn, object o) {
|
||||
if (o is StatementExpression exp) {
|
||||
return new string(' ', atColumn) + exp.printView(atColumn);
|
||||
}
|
||||
var result = new string(' ', atColumn);
|
||||
if (o is string str) {
|
||||
result += new CSharpString(str).ToString();
|
||||
} else if (o is QuotedView quotedView) {
|
||||
result += "[" + quotedView.Value + "]";
|
||||
} else if (o is bool b) {
|
||||
result += b ? "true" : "false";
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
if (functions.TryGetValue(myName, out var fn)) {
|
||||
var resolvedArgs = fn.Parameters.Zip(myArgs).ToDictionary(p => p.First.Id, p => p.Second.Evaluate(env, functions, streamFactory));
|
||||
var aggEnv = AggregateDictionary.Create(env, resolvedArgs);
|
||||
// This is a local function, defined in the dsl. For example
|
||||
// def foo() {
|
||||
// someStream(arg1, arg2, ...)
|
||||
// }
|
||||
// foo()
|
||||
var res = fn.Statements.Aggregate((object?)null, (vs, s) => s.Evaluate(aggEnv, functions, streamFactory));
|
||||
return res;
|
||||
} else {
|
||||
result += o.ToString();
|
||||
var resolvedArgs = myArgs.Select((arg, i) => (arg.Name, (int?)i, arg.Evaluate(env, functions, streamFactory)));
|
||||
// This is a system function/ctr. For example
|
||||
// someStream(arg1, arg2, ...)
|
||||
var res = streamFactory.Create(myName, resolvedArgs);
|
||||
return res;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System.Collections;
|
||||
|
||||
class PropertyGetter : IDataStreamExpression {
|
||||
readonly string identifier;
|
||||
readonly string property;
|
||||
|
||||
public PropertyGetter(string identifier, string property) {
|
||||
this.identifier = identifier;
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder bd, IList<string> activeBindings) {
|
||||
bd.Append(identifier);
|
||||
bd.Append('.');
|
||||
bd.Append(property);
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
if (env.TryGetValue(identifier, out var obj)) {
|
||||
if (obj is IDictionary r) {
|
||||
return r[property];
|
||||
} else if (obj is not null) {
|
||||
var objType = obj.GetType();
|
||||
var prop = objType.GetProperty(property);
|
||||
if (prop is not null) {
|
||||
return prop.GetValue(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new InvalidOperationException($"Failed to Evaluate PropertyGetter '{identifier}.{property}'");
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
public abstract class StatementExpression {
|
||||
ViewStream? view;
|
||||
|
||||
protected StatementExpression() { }
|
||||
|
||||
public virtual string? print(int startCol, IList<string> activeBindings) =>
|
||||
null;
|
||||
|
||||
public virtual string? printView(int startCol) =>
|
||||
null;
|
||||
|
||||
public ViewStream? getView(IDictionary<string, StatementExpression> env, IDataStreamFactory streamFactory) =>
|
||||
view ??= myGetView(env, streamFactory);
|
||||
|
||||
public virtual ViewStream? myGetView(IDictionary<string, StatementExpression> env, IDataStreamFactory streamFactory) =>
|
||||
null;
|
||||
|
||||
public static string makeCSString(string s) {
|
||||
if (s == null)
|
||||
return "(string) null";
|
||||
else
|
||||
return "\"" + s + "\"";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Strings;
|
||||
|
||||
class SubtractExpression : IDataStreamExpression {
|
||||
readonly Argument left;
|
||||
readonly Argument right;
|
||||
|
||||
public SubtractExpression(Argument left, Argument right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var leftVal = GetUnderlyingValue(left);
|
||||
var rightVal = GetUnderlyingValue(right);
|
||||
var leftSubtract = ToSubtract(leftVal);
|
||||
return leftSubtract.Subtract(rightVal);
|
||||
|
||||
ISubtract ToSubtract(object? val) {
|
||||
if (val is int valInt32) {
|
||||
return new Int32Subtract(valInt32);
|
||||
}
|
||||
if (val is long valInt64) {
|
||||
return new Int64Subtract(valInt64);
|
||||
}
|
||||
if (val is uint valUint32) {
|
||||
return new Uint32Subtract(valUint32);
|
||||
}
|
||||
if (val is ulong valUint64) {
|
||||
return new Uint64Subtract(valUint64);
|
||||
}
|
||||
if (val is float valFloat32) {
|
||||
return new Float32Subtract(valFloat32);
|
||||
}
|
||||
if (val is double valFloat64) {
|
||||
return new Float64Subtract(valFloat64);
|
||||
}
|
||||
throw new ArgumentException($"Cannot convert obj of type '{val?.GetType()}' to ISubtract", nameof(val));
|
||||
}
|
||||
|
||||
object? GetUnderlyingValue(object? obj) {
|
||||
object? val = obj;
|
||||
while (val is IDataStreamExpression exp) {
|
||||
val = exp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) {
|
||||
left.ToCode(builder, activeBindings);
|
||||
builder.Append(" - ");
|
||||
right.ToCode(builder, activeBindings);
|
||||
}
|
||||
|
||||
interface ISubtract {
|
||||
object Subtract(object? other);
|
||||
}
|
||||
|
||||
class Int32Subtract : ISubtract {
|
||||
readonly int left;
|
||||
|
||||
public Int32Subtract(int left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Subtract(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left - rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left - rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left - rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left - rightUint32;
|
||||
}
|
||||
if (other is ISubtract rightSubtract) {
|
||||
return rightSubtract.Subtract(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Subtract obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Int64Subtract : ISubtract {
|
||||
readonly long left;
|
||||
|
||||
public Int64Subtract(long left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Subtract(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left - rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left - rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left - rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left - rightUint32;
|
||||
}
|
||||
if (other is ISubtract rightSubtract) {
|
||||
return rightSubtract.Subtract(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Subtract obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint32Subtract : ISubtract {
|
||||
readonly uint left;
|
||||
|
||||
public Uint32Subtract(uint left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Subtract(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left - rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left - rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left - rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left - rightUint32;
|
||||
}
|
||||
throw new ArgumentException($"Cannot Subtract obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Uint64Subtract : ISubtract {
|
||||
readonly ulong left;
|
||||
|
||||
public Uint64Subtract(ulong left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Subtract(object? other) {
|
||||
checked {
|
||||
if (other is int rightIn32) {
|
||||
return (long)left - rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left - rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left - rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left - rightUint32;
|
||||
}
|
||||
}
|
||||
if (other is ISubtract rightSubtract) {
|
||||
return rightSubtract.Subtract(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Subtract obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float32Subtract : ISubtract {
|
||||
readonly float left;
|
||||
|
||||
public Float32Subtract(float left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Subtract(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left - rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left - rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left - rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left - rightUint32;
|
||||
}
|
||||
if (other is ISubtract rightSubtract) {
|
||||
return rightSubtract.Subtract(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Subtract obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
|
||||
class Float64Subtract : ISubtract {
|
||||
readonly double left;
|
||||
|
||||
public Float64Subtract(double left) =>
|
||||
this.left = left;
|
||||
|
||||
public object Subtract(object? other) {
|
||||
if (other is int rightIn32) {
|
||||
return left - rightIn32;
|
||||
}
|
||||
if (other is double rightFloat64) {
|
||||
return left - rightFloat64;
|
||||
}
|
||||
if (other is float rightFloat32) {
|
||||
return left - rightFloat32;
|
||||
}
|
||||
if (other is uint rightUint32) {
|
||||
return left - rightUint32;
|
||||
}
|
||||
if (other is ISubtract rightSubtract) {
|
||||
return rightSubtract.Subtract(left);
|
||||
}
|
||||
throw new ArgumentException($"Cannot Subtract obj of type '{other?.GetType()}'", nameof(other));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System.CodeDom;
|
||||
using System.Collections;
|
||||
|
||||
public class Variable : IDataStreamExpression {
|
||||
readonly ImmutableList<string> selector;
|
||||
string? id;
|
||||
|
||||
public Variable(string id)
|
||||
: this(id, typeof(IBlueRow)) { }
|
||||
|
||||
public Variable(string id, Type type)
|
||||
: this(ImmutableList.CreateRange(id.Split('.')), type) { }
|
||||
|
||||
Variable(ImmutableList<string> selector, Type type) {
|
||||
this.selector = selector;
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public string Id =>
|
||||
id ??= string.Join('.', selector);
|
||||
|
||||
public Type Type { get; }
|
||||
|
||||
public Variable Add(string property) {
|
||||
var newType = Type;
|
||||
var bd = selector.ToBuilder();
|
||||
foreach (var prop in property.Split('.')) {
|
||||
bd.Add(prop);
|
||||
newType = newType?.GetProperty(prop)?.PropertyType ?? typeof(object);
|
||||
}
|
||||
return new(bd.ToImmutable(), newType);
|
||||
}
|
||||
|
||||
public void ToCode(IStringBuilder builder, IList<string> activeBindings) =>
|
||||
builder.Append(Id);
|
||||
|
||||
public object? Evaluate(IDictionary<string, object?> env, IDictionary<string, FunctionDefinition> functions, IDataStreamFactory streamFactory) {
|
||||
var res = selector.Aggregate(ToNode(env), (node, prop) => ToNode(node.GetChild(prop)));
|
||||
return res.Item;
|
||||
|
||||
object? EvaluateNode(object? obj) {
|
||||
if (obj is IDataStreamExpression dsExp) {
|
||||
return dsExp.Evaluate(env, functions, streamFactory);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
(object Item, Func<string, object?> GetChild) ToNode(object? obj) {
|
||||
ArgumentNullException.ThrowIfNull(obj);
|
||||
if (obj is IDictionary<string, object?> dict) {
|
||||
return (obj, GetChild);
|
||||
object? GetChild(string k) => EvaluateNode(dict[k]);
|
||||
} else if (obj is IDictionary ngDict) {
|
||||
return (obj, GetChild);
|
||||
object? GetChild(string k) => EvaluateNode(ngDict[k]);
|
||||
} else {
|
||||
var propDict = new Lazy<IDictionary<string, object?>>(() => new PropertyInfoDictionary(obj));
|
||||
return (obj, GetChild);
|
||||
object? GetChild(string k) => EvaluateNode(propDict.Value[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CodeParameterDeclarationExpression ToParameterDeclaration() {
|
||||
if (selector.Count != 1) {
|
||||
throw new InvalidOperationException(
|
||||
"Cannot create CodeParameterDeclarationExpression from variable with a selector '.'");
|
||||
}
|
||||
var rv = new CodeParameterDeclarationExpression(new CodeTypeReference(Type), Id);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
namespace Bts.DataStreamExpressions;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
public class ViewNameExpression : StatementExpression {
|
||||
readonly string id;
|
||||
|
||||
public ViewNameExpression(string id) =>
|
||||
this.id = id;
|
||||
|
||||
public override string print(int startCol, IList<string> activeBindings) =>
|
||||
activeBindings.IndexOf(id) != -1 ? id : "new TableStream(\"" + id + "\", db)";
|
||||
|
||||
public override string printView(int startCol) =>
|
||||
id;
|
||||
|
||||
public override ViewStream? myGetView(IDictionary<string, StatementExpression> env, IDataStreamFactory streamFactory) =>
|
||||
env.TryGetValue(id, out var exp)
|
||||
? exp.getView(env, streamFactory)
|
||||
: streamFactory.Create("table", new (string?, int?, object?)[] { ("name", 0, id) });
|
||||
}
|
||||
@@ -109,7 +109,7 @@ public class DataStreamPublisherService : IHostedService, IDisposable {
|
||||
try {
|
||||
Log.Gen.info($"Building ViewStream: {streamExpression}");
|
||||
var exp = DataStreamExpressionsInterpreter.Evaluate(streamExpression);
|
||||
return exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
||||
return exp.Evaluate(streamFactory) as ViewStream;
|
||||
} catch (Exception ex) {
|
||||
Log.assert(false, $"Could not build stream. expression: {streamExpression}; ex: {ex}");
|
||||
return null;
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace BCG {
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
using Spark.Core.Strings;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
@@ -179,8 +180,13 @@ namespace BCG {
|
||||
// IDataPacket? pendingDataPacket = null;
|
||||
// bool isBusy = false;
|
||||
var version = Interlocked.Increment(ref pendingVersion);
|
||||
currStreamSub = myExp.getView(new Dictionary<string, StatementExpression>(0), streamFactory)
|
||||
?.AsObservable()
|
||||
var streamObj = myExp.Evaluate(streamFactory);
|
||||
if (streamObj is not ViewStream stream) {
|
||||
Log.Ui.error($$"""[dsqa_err.json]{ "err": "Failed to create stream", "query": "{{expStr}}", "ts": "{{DateTime.Now}}" }""");
|
||||
return;
|
||||
}
|
||||
currStreamSub = stream
|
||||
.AsObservable()
|
||||
.Sample(TimeSpan.FromMilliseconds(333))
|
||||
.Subscribe(dp => {
|
||||
lock (locker) {
|
||||
@@ -391,7 +397,9 @@ namespace BCG {
|
||||
myDataGrid.HorizScrollBarValue = hscrollval;
|
||||
|
||||
var varNames = new List<string>(0);
|
||||
var code = myExp?.print(9, varNames) ?? string.Empty;
|
||||
var bd = StringBuilderFactory.Create();
|
||||
myExp?.ToCode(bd, varNames);
|
||||
var code = bd.ToString() ?? string.Empty;
|
||||
myPrintCode(dp.Schema, varNames, code);
|
||||
myConsoleRichTextBox.Clear();
|
||||
// this was was very very slow
|
||||
|
||||
@@ -52,9 +52,9 @@ class StreamReportWindow : SparkWindow<StreamReport>, ISharedWindow {
|
||||
return;
|
||||
}
|
||||
var exp = DataStreamExpressionsInterpreter.Evaluate(report.Query);
|
||||
var stream = exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory)?.AsObservable()
|
||||
?? new LiteralStream("InvalidStream", "id:1,error:'Invalid query'").AsObservable();
|
||||
vm = new(report, stream, r => persist.set(StreamReport.PersistTopic, r.Name ?? string.Empty, r));
|
||||
var stream = exp.Evaluate(streamFactory) as ViewStream
|
||||
?? new LiteralStream("InvalidStream", "id:1,error:'Invalid query'");
|
||||
vm = new(report, stream.AsObservable(), r => persist.set(StreamReport.PersistTopic, r.Name ?? string.Empty, r));
|
||||
commands = new() {
|
||||
new SparkCommandCommand("Settings", Icons.Gear, vm.LaunchSettingsEditorCmd),
|
||||
};
|
||||
|
||||
@@ -308,6 +308,25 @@ namespace System.Collections.Generic {
|
||||
return source.All(hs.Add);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> MergeSorted<T>(this IEnumerable<T> items1, IEnumerable<T> items2) =>
|
||||
MergeSorted(items1, items2, Comparer<T>.Default);
|
||||
|
||||
public static IEnumerable<T> MergeSorted<T>(this IEnumerable<T> items1, IEnumerable<T> items2, IComparer<T> comparer) {
|
||||
using var en1 = items1.GetEnumerator();
|
||||
using var en2 = items2.GetEnumerator();
|
||||
bool hasItem1 = en1.MoveNext();
|
||||
bool hasItem2 = en2.MoveNext();
|
||||
while (hasItem1 || hasItem2) {
|
||||
if (!hasItem1 || (hasItem2 && comparer.Compare(en1.Current, en2.Current) > 0)) {
|
||||
yield return en2.Current;
|
||||
hasItem2 = en2.MoveNext();
|
||||
} else {
|
||||
yield return en1.Current;
|
||||
hasItem1 = en1.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="source"/> implements <see cref="ICollection{T}"/> or <see cref="IReadOnlyCollection{T}"/>. If so, the resultant Count is returned, otherwise null (will not enumerate through any items).
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class HashableTuple : IList<object?>, IReadOnlyList<object?>, IEquatable<HashableTuple> {
|
||||
readonly object?[] values;
|
||||
|
||||
public HashableTuple(object?[] values) =>
|
||||
this.values = values;
|
||||
|
||||
public int Count => values.Length;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public object? this[int index] {
|
||||
get => values[index];
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Add(object? item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public void Clear() =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Contains(object? item) =>
|
||||
IndexOf(item) != -1;
|
||||
|
||||
public void CopyTo(object?[] array, int arrayIndex) {
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
array[arrayIndex++] = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<object?> GetEnumerator() {
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
yield return values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(object? item) {
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
if (Equals(item, values[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void Insert(int index, object? item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Remove(object? item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public void RemoveAt(int index) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Equals(HashableTuple? other) {
|
||||
if (other is null) {
|
||||
return false;
|
||||
}
|
||||
if (values.Length != other.values.Length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
if (!Equals(values[i], other.values[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool Equals(object? other) {
|
||||
if (other is HashableTuple ht) {
|
||||
return Equals(ht);
|
||||
}
|
||||
if (other is not IEnumerable otherValues) {
|
||||
return false;
|
||||
}
|
||||
var otherEn = otherValues.GetEnumerator();
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
if (!otherEn.MoveNext() || !Equals(values[i], otherEn.Current)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return !otherEn.MoveNext();
|
||||
}
|
||||
|
||||
public override int GetHashCode() {
|
||||
var hash = new HashCode();
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
hash.Add(values[i]);
|
||||
}
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections;
|
||||
|
||||
using bts.collections;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
|
||||
public class PropertyInfoDictionary : IDictionary<string, object?> {
|
||||
readonly object source;
|
||||
readonly PropertyInfo[] properties;
|
||||
|
||||
public PropertyInfoDictionary(object source) {
|
||||
this.source = source;
|
||||
properties = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
||||
Array.Sort(properties, new ComparerBuilder<PropertyInfo>() { p => p.Name });
|
||||
}
|
||||
|
||||
public object? this[string key] {
|
||||
get {
|
||||
var prop = getProperty(key) ?? throw new KeyNotFoundException();
|
||||
return prop.GetValue(source);
|
||||
}
|
||||
set {
|
||||
var prop = getProperty(key) ?? throw new KeyNotFoundException();
|
||||
prop.SetValue(source, value);
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<string> Keys =>
|
||||
propertyNames;
|
||||
|
||||
public ICollection<object?> Values =>
|
||||
properties.WrapSelector(p => p.GetValue(source), null);
|
||||
|
||||
public int Count => properties.Length;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
IList<string> propertyNames => properties.WrapSelector(p => p.Name, null);
|
||||
|
||||
public void Add(string key, object? value) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public void Add(KeyValuePair<string, object?> item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public void Clear() =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Contains(KeyValuePair<string, object?> item) {
|
||||
var prop = getProperty(item.Key);
|
||||
return prop is not null && Equals(prop.GetValue(source), item.Value);
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key) =>
|
||||
getProperty(key) is not null;
|
||||
|
||||
public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) {
|
||||
for (int i = 0; i < properties.Length; i++) {
|
||||
array[arrayIndex++] = new(properties[i].Name, properties[i].GetValue(source));
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() {
|
||||
for (int i = 0; i < properties.Length; i++) {
|
||||
yield return new(properties[i].Name, properties[i].GetValue(source));
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(string key) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool Remove(KeyValuePair<string, object?> item) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public bool TryGetValue(string key, [MaybeNullWhen(false)] out object? value) {
|
||||
var prop = getProperty(key);
|
||||
if (prop is null) {
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
value = prop.GetValue(source);
|
||||
return true;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
|
||||
PropertyInfo? getProperty(string key) {
|
||||
var idx = propertyNames.BinarySearch(key);
|
||||
if (idx < 0) {
|
||||
return null;
|
||||
}
|
||||
return properties[idx];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
namespace Spark.Core.Strings;
|
||||
|
||||
using System;
|
||||
|
||||
public interface IStringBuilder {
|
||||
void Append(bool value);
|
||||
void Append(char value);
|
||||
void Append(sbyte value);
|
||||
void Append(byte value);
|
||||
void Append(short value);
|
||||
void Append(int value);
|
||||
void Append(long value);
|
||||
void Append(float value);
|
||||
void Append(double value);
|
||||
void Append(decimal value);
|
||||
void Append(ushort value);
|
||||
void Append(uint value);
|
||||
void Append(ulong value);
|
||||
void Append(string value);
|
||||
void AppendLine();
|
||||
void AppendLine(string s);
|
||||
}
|
||||
|
||||
public static class StringBuilderExtensions {
|
||||
public static IStringBuilder SetIndentation(this IStringBuilder stringBuilder, int indentation) {
|
||||
if (stringBuilder is IndentedStringBuilder isb) {
|
||||
isb.SetIndentation(indentation);
|
||||
return isb;
|
||||
}
|
||||
return new IndentedStringBuilder(stringBuilder, indentation);
|
||||
}
|
||||
|
||||
public static ScopedIndentation PushIndentation(this IStringBuilder stringBuilder, int indentation) {
|
||||
int origIndentation;
|
||||
var isb = stringBuilder as IndentedStringBuilder;
|
||||
if (isb is not null) {
|
||||
origIndentation = isb.Indentation;
|
||||
isb.SetIndentation(indentation);
|
||||
} else {
|
||||
origIndentation = 0;
|
||||
isb = new IndentedStringBuilder(stringBuilder, indentation);
|
||||
}
|
||||
return new(isb, origIndentation);
|
||||
}
|
||||
|
||||
public class ScopedIndentation : IStringBuilder, IDisposable {
|
||||
readonly IndentedStringBuilder bd;
|
||||
readonly int origIndentation;
|
||||
|
||||
public ScopedIndentation(IndentedStringBuilder bd, int origIndentation) {
|
||||
this.bd = bd;
|
||||
this.origIndentation = origIndentation;
|
||||
}
|
||||
|
||||
public void Append(bool value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(char value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(sbyte value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(byte value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(short value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(int value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(long value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(float value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(double value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(decimal value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(ushort value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(uint value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(ulong value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void Append(string value) {
|
||||
bd.Append(value);
|
||||
}
|
||||
|
||||
public void AppendLine() {
|
||||
bd.AppendLine();
|
||||
}
|
||||
|
||||
public void AppendLine(string s) {
|
||||
bd.AppendLine(s);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
bd.SetIndentation(origIndentation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Strings;
|
||||
|
||||
using System.IO;
|
||||
|
||||
public class IndentedStringBuilder : IStringBuilder {
|
||||
readonly IStringBuilder bd;
|
||||
string linePadding;
|
||||
bool pendingIndent;
|
||||
|
||||
public IndentedStringBuilder(int indentation)
|
||||
: this(StringBuilderFactory.Create(), indentation) { }
|
||||
|
||||
public IndentedStringBuilder(IStringBuilder stringBuilder, int indentation) {
|
||||
bd = stringBuilder;
|
||||
linePadding = new(' ', indentation);
|
||||
}
|
||||
|
||||
public int Indentation => linePadding.Length;
|
||||
|
||||
public void SetIndentation(int indentation) =>
|
||||
linePadding = new(' ', indentation);
|
||||
|
||||
public IndentedStringBuilder Append(bool value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(char value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(sbyte value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(byte value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(short value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(int value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(long value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(float value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(double value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(decimal value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(ushort value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(uint value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(ulong value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
bd.Append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder Append(string? value, bool indentLines = true) {
|
||||
if (value is null) {
|
||||
return this;
|
||||
}
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
pendingIndent = false;
|
||||
}
|
||||
if (indentLines) {
|
||||
using var reader = new StringReader(value);
|
||||
bool isFirst = true;
|
||||
for (var line = reader.ReadLine(); line is not null; line = reader.ReadLine()) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
bd.Append(line);
|
||||
} else {
|
||||
AppendLine(line);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bd.Append(value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public IndentedStringBuilder AppendLine() =>
|
||||
AppendLine(null);
|
||||
|
||||
public IndentedStringBuilder AppendLine(string? value) {
|
||||
if (pendingIndent) {
|
||||
bd.Append(linePadding);
|
||||
}
|
||||
bd.AppendLine(value);
|
||||
pendingIndent = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public override string ToString() =>
|
||||
bd.ToString() ?? string.Empty;
|
||||
|
||||
void IStringBuilder.Append(bool value) => Append(value);
|
||||
void IStringBuilder.Append(char value) => Append(value);
|
||||
void IStringBuilder.Append(sbyte value) => Append(value);
|
||||
void IStringBuilder.Append(byte value) => Append(value);
|
||||
void IStringBuilder.Append(short value) => Append(value);
|
||||
void IStringBuilder.Append(int value) => Append(value);
|
||||
void IStringBuilder.Append(long value) => Append(value);
|
||||
void IStringBuilder.Append(float value) => Append(value);
|
||||
void IStringBuilder.Append(double value) => Append(value);
|
||||
void IStringBuilder.Append(decimal value) => Append(value);
|
||||
void IStringBuilder.Append(ushort value) => Append(value);
|
||||
void IStringBuilder.Append(uint value) => Append(value);
|
||||
void IStringBuilder.Append(ulong value) => Append(value);
|
||||
void IStringBuilder.Append(string value) => Append(value);
|
||||
void IStringBuilder.AppendLine() => AppendLine();
|
||||
void IStringBuilder.AppendLine(string value) => AppendLine(value);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Strings;
|
||||
|
||||
using System.Text;
|
||||
|
||||
public static class StringBuilderFactory {
|
||||
public static IStringBuilder Create() =>
|
||||
new DefaultStringBuilder();
|
||||
|
||||
public static IStringBuilder Create(StringBuilder stringBuilder) =>
|
||||
new DefaultStringBuilder(stringBuilder);
|
||||
|
||||
class DefaultStringBuilder : IStringBuilder {
|
||||
readonly StringBuilder bd;
|
||||
|
||||
public DefaultStringBuilder(StringBuilder? bd = null) =>
|
||||
this.bd = bd ?? new();
|
||||
|
||||
public void Append(bool value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(char value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(sbyte value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(byte value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(short value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(int value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(long value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(float value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(double value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(decimal value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(ushort value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(uint value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(ulong value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void Append(string value) =>
|
||||
bd.Append(value);
|
||||
|
||||
public void AppendLine() =>
|
||||
bd.AppendLine();
|
||||
|
||||
public void AppendLine(string s) =>
|
||||
bd.AppendLine(s);
|
||||
|
||||
public override string ToString() =>
|
||||
bd.ToString();
|
||||
}
|
||||
}
|
||||
@@ -253,7 +253,7 @@ namespace bts.websocket_core {
|
||||
}
|
||||
Exception? exception = null;
|
||||
try {
|
||||
stream = exp.getView(new Dictionary<string, StatementExpression>(0), streamFactory);
|
||||
stream = exp.Evaluate(streamFactory) as ViewStream;
|
||||
} catch (Exception ex) {
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user