Adding variables

This commit is contained in:
Jordan Wages 2025-08-26 23:24:50 -05:00
commit 0def6434d6
3 changed files with 15 additions and 12 deletions

View file

@ -19,6 +19,6 @@
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" /> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" />
<PackageReference Include="CSMic.StandardLibrary" Version="2.0.0-beta-03" /> <PackageReference Include="CSMic.StandardLibrary" Version="2.0.0-beta-04" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,6 +1,7 @@
using System; using System;
using Avalonia.Data.Converters; using Avalonia.Data.Converters;
using Avalonia; using Avalonia;
using Avalonia.Controls;
namespace AdvancedCalculator.Converters; namespace AdvancedCalculator.Converters;

View file

@ -12,10 +12,12 @@ public class CalculatorService : ICalculatorService
{ {
private readonly object _lock = new(); private readonly object _lock = new();
// We attempt to use the legacy namespace to maximize compatibility. private readonly CSMic.InputInterpreter _interpreter = new();
// If using the newer CSMic.StandardLibrary with different namespaces,
// update the using directives and type names here. public CalculatorService()
private readonly csmic.InputInterpreter _interpreter = new(); {
CSMic.StandardLibrary.Initializer.InitializeAllFunctions(_interpreter);
}
public Task<InterpretResult> InterpretAsync(string input) public Task<InterpretResult> InterpretAsync(string input)
{ {
@ -24,17 +26,17 @@ public class CalculatorService : ICalculatorService
lock (_lock) lock (_lock)
{ {
_interpreter.Interpret(input); _interpreter.Interpret(input);
var output = _interpreter.Output ?? string.Empty; var output = string.IsNullOrWhiteSpace(_interpreter.StringValue) ? _interpreter.NumericValue.ToString() : _interpreter.StringValue;
// Build variables list // Build variables list
var variables = new List<VariableItem>(); var variables = new List<VariableItem>();
foreach (var kvp in _interpreter.Variables) foreach (var csmicVariable in _interpreter.Variables)
{ {
var name = kvp.Key; var name = csmicVariable.Name;
var variable = kvp.Value; var variable = csmicVariable.Value;
var valueString = variable?.Value?.ToString() ?? string.Empty; var valueString = variable?.ToString() ?? string.Empty;
var isExpression = variable?.Type == csmic.VariableType.Equation; var isExpression = csmicVariable.Type == CSMic.VariableType.Expression;
var item = new VariableItem var item = new VariableItem
{ {
VariableName = name, VariableName = name,
@ -48,7 +50,7 @@ public class CalculatorService : ICalculatorService
{ {
// Compute the expression-based variable's current value // Compute the expression-based variable's current value
_interpreter.Interpret(valueString); _interpreter.Interpret(valueString);
item.ExpressionComputation = _interpreter.Output ?? string.Empty; item.ExpressionComputation = _interpreter.StringValue ?? string.Empty;
item.Icon = IconFont.Function; item.Icon = IconFont.Function;
} }