Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>1.0.12</Version>
<Version>1.0.13</Version>
<Authors>Lucas Breedlove</Authors>
<Company>Queuebal</Company>
<Copyright>Copyright © 2025 Queuebal</Copyright>
Expand Down
39 changes: 39 additions & 0 deletions Queuebal.Expressions/AssignExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Queuebal.Json;

namespace Queuebal.Expressions;


/// <summary>
/// Assigns a value to an existing variable in the VariableProvider.
/// </summary>
public class AssignExpression : Expression
{
public static string ExpressionType { get; } = "Assign";

/// <summary>
/// The name of the variable to assign a value to.
/// </summary>
public required string VariableName { get; set; }

/// <summary>
/// The expression that evaluates to the value to assign to the variable.
/// </summary>
public required IExpression Value { get; set; }

/// <summary>
/// Executes the assignment statement.
/// </summary>
/// <param name="context">The context in which the statement is executed.</param>
protected override JSONValue EvaluateExpression(ExpressionContext context, JSONValue inputValue)
{
var value = Value.Evaluate(context, inputValue);
try
{
return context.VariableProvider.SetValue(VariableName, value);
}
catch (KeyNotFoundException)
{
throw new InvalidOperationException($"Variable '{VariableName}' is not declared.");
}
}
}
6 changes: 3 additions & 3 deletions Queuebal.Expressions/DeclareAndAssignExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Queuebal.Expressions;


/// <summary>
/// Declares a variable in the current DataProviderScope and assigns it a value.
/// Declares a variable in the current VariableProviderScope and assigns it a value.
/// This expression is used to create a new variable and initialize it with a value.
/// If the variable already exists, an exception is thrown.
/// </summary>
Expand All @@ -28,13 +28,13 @@ public class DeclareAndAssignExpression : Expression
/// <param name="context">The context in which the statement is executed.</param>
protected override JSONValue EvaluateExpression(ExpressionContext context, JSONValue inputValue)
{
if (context.DataProvider.GetValueInCurrentScope(VariableName) != null)
if (context.VariableProvider.GetValueInCurrentScope(VariableName) != null)
{
throw new InvalidOperationException($"Variable '{VariableName}' is already declared.");
}

var newValue = Value.Evaluate(context, inputValue);
context.DataProvider.AddValue(VariableName, newValue);
context.VariableProvider.AddValue(VariableName, newValue);
return newValue;
}
}
2 changes: 1 addition & 1 deletion Queuebal.Expressions/DictExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override JSONValue EvaluateExpression(ExpressionContext context, JSONV
var output = new Dictionary<string, JSONValue>();
foreach (var kv in Value)
{
var key = Tokenizer.Evaluate(kv.Key, context.DataProvider);
var key = Tokenizer.Evaluate(kv.Key, context.VariableProvider);
if (!key.IsString)
{
throw new InvalidOperationException("Dict keys must evaluate to a string.");
Expand Down
10 changes: 5 additions & 5 deletions Queuebal.Expressions/ExpressionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class ExpressionContext
/// <summary>
/// Initializes a new instance of the <see cref="ExpressionContext"/> class.
/// </summary>
/// <param name="dataProvider">The dataprovider used to resolve variable values when evaluating an expression.</param>
public ExpressionContext(DataProvider dataProvider)
/// <param name="variableProvider">The dataprovider used to resolve variable values when evaluating an expression.</param>
public ExpressionContext(VariableProvider variableProvider)
{
DataProvider = dataProvider;
VariableProvider = variableProvider;
}

/// <summary>
/// The DataProvider used when evaluating expressions.
/// The VariableProvider used when evaluating expressions.
/// </summary>
public DataProvider DataProvider { get; }
public VariableProvider VariableProvider { get; }
}
4 changes: 2 additions & 2 deletions Queuebal.Expressions/IExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public JSONValue Evaluate(ExpressionContext context, JSONValue inputValue)
// represents a variable value in the context's data provider
if (inputValue.IsString)
{
inputValue = Tokenizer.Evaluate(inputValue.StringValue, context.DataProvider);
inputValue = Tokenizer.Evaluate(inputValue.StringValue, context.VariableProvider);
}

// Evaluate the expression with the provided context and input value
var result = EvaluateExpression(context, inputValue);
if (result.IsString)
{
result = Tokenizer.Evaluate(result.StringValue, context.DataProvider);
result = Tokenizer.Evaluate(result.StringValue, context.VariableProvider);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion Queuebal.Expressions/IMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public JSONValue Evaluate(ExpressionContext context, JSONValue inputValue)
if (result.IsString)
{
// If the result is a string, evaluate it as a token
result = Tokenizer.Evaluate(result.StringValue, context.DataProvider);
result = Tokenizer.Evaluate(result.StringValue, context.VariableProvider);
}
return result;
}
Expand Down
Loading