11/*
22 * COPYRIGHT: See COPYING in the top level directory
3- * PROJECT: Weaver.Core
3+ * PROJECT: Weaver.Core.Commands
44 * FILE: EvaluateCommand.cs
55 * PURPOSE: Does double duty as internal command and extension to evaluate expressions. It can be either used as calculator or expression evaluator.
66 * PROGRAMMER: Peter Geinitz (Wayfarer)
1010using Weaver . Interfaces ;
1111using Weaver . Messages ;
1212
13- namespace Weaver . Core
13+ namespace Weaver . Core . Commands
1414{
1515 /// <summary>
1616 /// Simple Commmand to evaluate expressions or do simple calculations.
1717 /// </summary>
18- /// <seealso cref="Weaver.Interfaces. ICommand" />
18+ /// <seealso cref="ICommand" />
1919 internal class EvaluateCommand : ICommand
2020 {
2121 /// <inheritdoc />
@@ -64,7 +64,6 @@ public CommandResult Execute(string[] args)
6464 // If no expression, maybe store previous pipeline value or return null
6565 if ( string . IsNullOrWhiteSpace ( expression ) )
6666 {
67- // Allow empty input if .store() will handle it
6867 return CommandResult . Ok ( null ) ;
6968 }
7069
@@ -73,30 +72,40 @@ public CommandResult Execute(string[] args)
7372 {
7473 foreach ( var variable in _registry . GetAll ( ) )
7574 {
76- if ( variable . Value . Value == null )
75+ var key = variable . Key ;
76+ var ( valueObj , valueType ) = variable . Value ;
77+
78+ if ( valueObj == null )
7779 continue ;
7880
79- var key = variable . Key ;
80- var val = variable . Value . Value . ToString ( ) ?? "0" ;
81- expression = expression . Replace ( key , val , StringComparison . OrdinalIgnoreCase ) ;
81+ string replacement = valueType switch
82+ {
83+ EnumTypes . Wint => Convert . ToInt64 ( valueObj ) . ToString ( ) ,
84+ EnumTypes . Wdouble => Convert . ToDouble ( valueObj ) . ToString ( CultureInfo . InvariantCulture ) ,
85+ EnumTypes . Wbool => Convert . ToBoolean ( valueObj ) ? "1" : "0" ,
86+ EnumTypes . Wstring => valueObj . ToString ( ) ?? "" ,
87+ _ => valueObj . ToString ( ) ?? ""
88+ } ;
89+
90+ expression = expression . Replace ( key , replacement , StringComparison . OrdinalIgnoreCase ) ;
8291 }
8392 }
8493
8594 object ? result ;
8695 EnumTypes type ;
8796
88- var isBooleanExpr = _evaluator . IsBooleanExpression ( expression ) ;
97+ bool isBooleanExpr = _evaluator . IsBooleanExpression ( expression ) ;
8998
9099 try
91100 {
92101 if ( isBooleanExpr )
93102 {
94- result = _evaluator . Evaluate ( expression ) ;
103+ result = _evaluator . Evaluate ( expression ) ; // returns bool
95104 type = EnumTypes . Wbool ;
96105 }
97106 else
98107 {
99- result = _evaluator . EvaluateNumeric ( expression ) ;
108+ result = _evaluator . EvaluateNumeric ( expression ) ; // returns double
100109 type = EnumTypes . Wdouble ;
101110 }
102111 }
@@ -120,6 +129,7 @@ public CommandResult Execute(string[] args)
120129 }
121130
122131 _registry . Set ( targetVar , result ! , type ) ;
132+
123133 return CommandResult . Ok (
124134 $ "Stored '{ result } ' in '{ targetVar } '.",
125135 result ,
@@ -128,7 +138,7 @@ public CommandResult Execute(string[] args)
128138 }
129139
130140 // Return computed result
131- var message = type switch
141+ string message = type switch
132142 {
133143 EnumTypes . Wbool => result ? . ToString ( ) ?? "false" ,
134144 EnumTypes . Wdouble => Convert . ToDouble ( result ) . ToString ( CultureInfo . InvariantCulture ) ,
@@ -138,10 +148,5 @@ public CommandResult Execute(string[] args)
138148 return CommandResult . Ok ( message , result , type ) ;
139149 }
140150
141- /// <inheritdoc />
142- public CommandResult InvokeExtension ( string extensionName , string [ ] args )
143- {
144- return CommandResult . Fail ( $ "'{ Name } ' has no extensions.") ;
145- }
146151 }
147- }
152+ }
0 commit comments