Skip to content

Commit 6533e9c

Browse files
authored
feat: introduce the interface IContext and variables as Func<object> (#231)
feat: introduce the interface IContext
1 parent 10fc87a commit 6533e9c

File tree

10 files changed

+42
-57
lines changed

10 files changed

+42
-57
lines changed

Expressif/Context.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
namespace Expressif;
99

10-
public class Context
10+
public class Context : IContext
1111
{
1212
public ContextVariables Variables { get; } = new ();
1313
public ContextObject CurrentObject { get; } = new ();
1414
}
15+
16+
public interface IContext
17+
{
18+
ContextVariables Variables { get; }
19+
ContextObject CurrentObject { get; }
20+
}

Expressif/Expression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class Expression : IFunction
1212
private readonly IFunction expression;
1313
public Expression(string code)
1414
: this(code, new Context()) { }
15-
public Expression(string code, Context context)
15+
public Expression(string code, IContext context)
1616
: this(code, context, new ExpressionFactory()) { }
17-
public Expression(string code, Context context, ExpressionFactory factory)
17+
public Expression(string code, IContext context, ExpressionFactory factory)
1818
=> expression = factory.Instantiate(code, context);
1919

2020
public object? Evaluate(object? value) => expression.Evaluate(value);

Expressif/ExpressionBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ namespace Expressif;
1515
public class ExpressionBuilder
1616
{
1717

18-
private Context Context { get; }
18+
private IContext Context { get; }
1919
private ExpressionFactory Factory { get; }
2020
private ExpressionSerializer Serializer { get; }
2121

2222
public ExpressionBuilder()
2323
: this(new Context()) { }
24-
public ExpressionBuilder(Context? context = null, ExpressionFactory? factory = null, ExpressionSerializer? serializer = null)
24+
public ExpressionBuilder(IContext? context = null, ExpressionFactory? factory = null, ExpressionSerializer? serializer = null)
2525
=> (Context, Factory, Serializer) = (context ?? new Context(), factory ?? new ExpressionFactory(), serializer ?? new ExpressionSerializer());
2626

2727
private Queue<IExpression> Pile { get; } = new();

Expressif/Functions/BaseExpressionFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public abstract class BaseExpressionFactory
1818
protected BaseExpressionFactory(BaseTypeMapper typeSetter)
1919
=> TypeMapper = typeSetter;
2020

21-
protected internal T Instantiate<T>(string functionName, IParameter[] parameters, Context context)
21+
protected internal T Instantiate<T>(string functionName, IParameter[] parameters, IContext context)
2222
=> Instantiate<T>(TypeMapper.Execute(functionName), parameters, context);
2323

24-
protected T Instantiate<T>(Type type, IParameter[] parameters, Context context)
24+
protected T Instantiate<T>(Type type, IParameter[] parameters, IContext context)
2525
{
2626
var ctor = GetMatchingConstructor(type, parameters.Length);
2727

@@ -53,9 +53,9 @@ protected internal ConstructorInfo GetMatchingConstructor(Type type, int paramCo
5353
=> type.GetConstructors().SingleOrDefault(x => x.GetParameters().Length == paramCount)
5454
?? throw new MissingOrUnexpectedParametersFunctionException(type.Name, paramCount);
5555

56-
protected Delegate InstantiateScalarDelegate(IParameter parameter, Type scalarType, Context context)
56+
protected Delegate InstantiateScalarDelegate(IParameter parameter, Type scalarType, IContext context)
5757
{
58-
var instantiate = typeof(BaseExpressionFactory).GetMethod(nameof(InstantiateScalarResolver), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IParameter), typeof(Context)])
58+
var instantiate = typeof(BaseExpressionFactory).GetMethod(nameof(InstantiateScalarResolver), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IParameter), typeof(IContext)])
5959
?? throw new InvalidProgramException(nameof(InstantiateScalarResolver));
6060
var instantiateGeneric = instantiate.MakeGenericMethod(scalarType);
6161
var resolver = instantiateGeneric.Invoke(null, new object[] { parameter, context })!;
@@ -66,7 +66,7 @@ protected Delegate InstantiateScalarDelegate(IParameter parameter, Type scalarTy
6666
return Delegate.CreateDelegate(funcType, resolver, execute);
6767
}
6868

69-
private static IScalarResolver<T> InstantiateScalarResolver<T>(IParameter parameter, Context context)
69+
private static IScalarResolver<T> InstantiateScalarResolver<T>(IParameter parameter, IContext context)
7070
=> parameter switch
7171
{
7272
LiteralParameter l => InstantiateScalarResolver<T>(typeof(LiteralScalarResolver<T>), [l.Value]),
@@ -79,7 +79,7 @@ private static IScalarResolver<T> InstantiateScalarResolver<T>(IParameter parame
7979
private static IScalarResolver<T> InstantiateScalarResolver<T>(Type generic, object[] parameters)
8080
=> (Activator.CreateInstance(generic, parameters) as IScalarResolver<T>)!;
8181

82-
protected Delegate InstantiateIntervalDelegate(IParameter parameter, Type type, Context context)
82+
protected Delegate InstantiateIntervalDelegate(IParameter parameter, Type type, IContext context)
8383
{
8484
if (parameter is not IntervalParameter i)
8585
throw new ArgumentOutOfRangeException(nameof(parameter));
@@ -99,7 +99,7 @@ protected Delegate InstantiateIntervalDelegate(IParameter parameter, Type type,
9999
return Delegate.CreateDelegate(funcType, resolver, execute);
100100
}
101101

102-
protected Delegate InstantiateInputExpressionDelegate(InputExpressionParameter exp, Type type, Context context)
102+
protected Delegate InstantiateInputExpressionDelegate(InputExpressionParameter exp, Type type, IContext context)
103103
{
104104
var functions = new List<IFunction>();
105105
foreach (var member in exp.Expression.Members)

Expressif/Functions/ExpressionFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ExpressionFactory : BaseExpressionFactory
2020
public ExpressionFactory()
2121
: base(new FunctionTypeMapper()) { }
2222

23-
public IFunction Instantiate(string code, Context context)
23+
public IFunction Instantiate(string code, IContext context)
2424
{
2525
var expression = Parser.Parse(code);
2626

@@ -30,9 +30,9 @@ public IFunction Instantiate(string code, Context context)
3030
return new ChainFunction(functions);
3131
}
3232

33-
public IFunction Instantiate(string name, IParameter[] parameters, Context context)
33+
public IFunction Instantiate(string name, IParameter[] parameters, IContext context)
3434
=> Instantiate<IFunction>(name, parameters, context);
3535

36-
public IFunction Instantiate(Type type, IParameter[] parameters, Context context)
36+
public IFunction Instantiate(Type type, IParameter[] parameters, IContext context)
3737
=> Instantiate<IFunction>(type, parameters, context);
3838
}

Expressif/Functions/ExpressionMember.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Expressif/Predicates/PredicationFactory.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ protected internal PredicationFactory(PredicateTypeMapper mapper, UnaryOperatorF
2626
public PredicationFactory()
2727
: this(new PredicateTypeMapper(), new UnaryOperatorFactory(), new BinaryOperatorFactory()) { }
2828

29-
public virtual IPredicate Instantiate(string code, Context context)
29+
public virtual IPredicate Instantiate(string code, IContext context)
3030
{
3131
var predication = Parser.Parse(code);
3232
var predicate = Instantiate(predication, context);
3333
return predicate;
3434
}
3535

36-
public IPredicate Instantiate(IPredication predication, Context context)
36+
public IPredicate Instantiate(IPredication predication, IContext context)
3737
=> predication switch
3838
{
3939
SinglePredication single => Instantiate(single, context),
@@ -42,21 +42,21 @@ public IPredicate Instantiate(IPredication predication, Context context)
4242
_ => throw new NotImplementedException()
4343
};
4444

45-
internal IPredicate Instantiate(SinglePredication basic, Context context)
45+
internal IPredicate Instantiate(SinglePredication basic, IContext context)
4646
{
4747
var predicates = new List<IPredicate>();
4848
foreach (var predicate in basic.Members)
4949
predicates.Add(Instantiate<IPredicate>(predicate.Name, predicate.Parameters, context));
5050
return predicates[0];
5151
}
5252

53-
internal IPredicate Instantiate(UnaryPredication unary, Context context)
53+
internal IPredicate Instantiate(UnaryPredication unary, IContext context)
5454
{
5555
var predicate = Instantiate(unary.Member, context);
5656
return UnaryOperatorFactory.Instantiate(unary.Operator.Name, predicate);
5757
}
5858

59-
internal IPredicate Instantiate(BinaryPredication binary, Context context)
59+
internal IPredicate Instantiate(BinaryPredication binary, IContext context)
6060
{
6161
var left = Instantiate(binary.LeftMember, context);
6262
var right = Instantiate(binary.RightMember, context);

Expressif/Predication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class Predication : IPredicate
1414

1515
public Predication(string code)
1616
: this(code, new Context()) { }
17-
public Predication(string code, Context context)
17+
public Predication(string code, IContext context)
1818
: this(code, context, new PredicationFactory()) { }
19-
public Predication(string code, Context context, PredicationFactory factory)
19+
public Predication(string code, IContext context, PredicationFactory factory)
2020
=> predicate = factory.Instantiate(code, context);
2121

2222
public virtual bool Evaluate(object? value) => predicate.Evaluate(value)!;

Expressif/PredicationBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ namespace Expressif;
1313

1414
public class PredicationBuilder
1515
{
16-
private Context Context { get; }
16+
private IContext Context { get; }
1717
private PredicationFactory Factory { get; }
1818
private PredicationSerializer Serializer { get; }
1919

2020
public PredicationBuilder()
2121
: this(new Context()) { }
22-
public PredicationBuilder(Context? context = null, PredicationFactory? factory = null, PredicationSerializer? serializer = null)
22+
public PredicationBuilder(IContext? context = null, PredicationFactory? factory = null, PredicationSerializer? serializer = null)
2323
=> (Context, Factory, Serializer) = (context ?? new Context(), factory ?? new(), serializer ?? new());
2424

2525
private IPredication? Pile { get; set; }

Expressif/Values/ContextVariables.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Expressif.Values;
99

1010
public class ContextVariables
1111
{
12-
private IDictionary<string, IScalarResolver> Variables { get; } = new Dictionary<string, IScalarResolver>();
12+
private IDictionary<string, Func<object?>> Variables { get; } = new Dictionary<string, Func<object?>>();
1313

14-
internal void Add(string name, IScalarResolver value)
14+
public void Add(string name, Func<object?> value)
1515
{
1616
name = name.StartsWith('@') ? name[1..] : name;
1717
if (Variables.ContainsKey(name))
@@ -20,9 +20,12 @@ internal void Add(string name, IScalarResolver value)
2020
}
2121

2222
public void Add<T>(string name, object value)
23-
=> Add(name, new LiteralScalarResolver<T>(value));
23+
{
24+
var resolver = new LiteralScalarResolver<T>(value);
25+
Add(name, () => resolver.Execute());
26+
}
2427

25-
internal void Set(string name, IScalarResolver value)
28+
public void Set(string name, Func<object?> value)
2629
{
2730
name = name.StartsWith('@') ? name[1..] : name;
2831
if (Variables.ContainsKey(name))
@@ -32,7 +35,10 @@ internal void Set(string name, IScalarResolver value)
3235
}
3336

3437
public void Set<T>(string name, object value)
35-
=> Set(name, new LiteralScalarResolver<T>(value));
38+
{
39+
var resolver = new LiteralScalarResolver<T>(value);
40+
Set(name, () => resolver.Execute());
41+
}
3642

3743
public void Remove(string name)
3844
{
@@ -49,7 +55,7 @@ public object? this[string name]
4955
{
5056
name = name.StartsWith('@') ? name[1..] : name;
5157
if (Variables.TryGetValue(name, out var value))
52-
return value.Execute();
58+
return value.Invoke();
5359
throw new UnexpectedVariableException(name);
5460
}
5561
}

0 commit comments

Comments
 (0)