Refactored ValueType to FunctionValueType

It was causing issues.
This commit is contained in:
Jordan Wages 2025-08-20 05:21:57 -05:00
commit 82ceb7e5cd
8 changed files with 40 additions and 40 deletions

View file

@ -9,22 +9,22 @@ namespace csmic
{ {
public class FunctionValue public class FunctionValue
{ {
public ValueType Type { get; set; } public FunctionValueType Type { get; set; }
public object? Value { get; set; } public object? Value { get; set; }
public static readonly FunctionValue TRUE = new FunctionValue(ValueType.Numeric, 1m); public static readonly FunctionValue TRUE = new FunctionValue(FunctionValueType.Numeric, 1m);
public static readonly FunctionValue FALSE = new FunctionValue(ValueType.Numeric, 0m); public static readonly FunctionValue FALSE = new FunctionValue(FunctionValueType.Numeric, 0m);
public static readonly FunctionValue NONE = new FunctionValue(ValueType.None, null); public static readonly FunctionValue NONE = new FunctionValue(FunctionValueType.None, null);
public static readonly FunctionValue NUMBER = new FunctionValue(ValueType.Numeric, 0m); public static readonly FunctionValue NUMBER = new FunctionValue(FunctionValueType.Numeric, 0m);
public static readonly FunctionValue STRING = new FunctionValue(ValueType.String, string.Empty); public static readonly FunctionValue STRING = new FunctionValue(FunctionValueType.String, string.Empty);
public FunctionValue() public FunctionValue()
{ {
this.Type = ValueType.None; this.Type = FunctionValueType.None;
this.Value = null; this.Value = null;
} }
public FunctionValue(ValueType type, object? value) public FunctionValue(FunctionValueType type, object? value)
{ {
this.Type = type; this.Type = type;
this.Value = value; this.Value = value;

View file

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace csmic namespace csmic
{ {
public enum ValueType public enum FunctionValueType
{ {
None, None,
Numeric, Numeric,

View file

@ -64,17 +64,17 @@ namespace csmic
{ {
switch (functionValue.Type) switch (functionValue.Type)
{ {
case ValueType.Numeric: case FunctionValueType.Numeric:
decimal numericValue = Convert.ToDecimal(functionValue.Value); decimal numericValue = Convert.ToDecimal(functionValue.Value);
ProduceOutput(numericValue, string.Empty); ProduceOutput(numericValue, string.Empty);
break; break;
case ValueType.String: case FunctionValueType.String:
if (functionValue.Value is string s) if (functionValue.Value is string s)
ProduceOutput(0, s); ProduceOutput(0, s);
else else
ProduceOutput(0, string.Empty); ProduceOutput(0, string.Empty);
break; break;
case ValueType.None: case FunctionValueType.None:
default: default:
ProduceOutput(0, string.Empty); ProduceOutput(0, string.Empty);
break; break;
@ -188,10 +188,10 @@ namespace csmic
} }
catch catch
{ {
return new FunctionValue(ValueType.None, null); return new FunctionValue(FunctionValueType.None, null);
} }
} }
return new FunctionValue(ValueType.None, null); return new FunctionValue(FunctionValueType.None, null);
} }
#endregion #endregion

View file

@ -109,12 +109,12 @@ INTERPRETER (.
| |
IF(IsAssignment()) IF(IsAssignment())
Assignment<out r> Assignment<out r>
(. this.functionValue = new FunctionValue(ValueType.Numeric, r); (. this.functionValue = new FunctionValue(FunctionValueType.Numeric, r);
this.interpreter.ProduceOutput(this.functionValue); this.interpreter.ProduceOutput(this.functionValue);
.) .)
| |
Expression<out r> Expression<out r>
(. this.functionValue = new FunctionValue(ValueType.Numeric, r); (. this.functionValue = new FunctionValue(FunctionValueType.Numeric, r);
this.interpreter.ProduceOutput(this.functionValue); this.interpreter.ProduceOutput(this.functionValue);
.) .)
. .
@ -158,7 +158,7 @@ Value<out decimal r> (.
IF(IsFunctionCall()) IF(IsFunctionCall())
Function<out fvr> Function<out fvr>
(. (.
if (fvr.Type == ValueType.Numeric && fvr.Value != null) if (fvr.Type == FunctionValueType.Numeric && fvr.Value != null)
{ {
try { r = signum * Convert.ToDecimal(fvr.Value); } try { r = signum * Convert.ToDecimal(fvr.Value); }
catch { SemErr("function returned non-numeric"); r = 0; } catch { SemErr("function returned non-numeric"); r = 0; }
@ -198,7 +198,7 @@ Value<out decimal r> (.
if (this.interpreter.TryGetExpression(ident, out expr)) if (this.interpreter.TryGetExpression(ident, out expr))
{ {
FunctionValue eval = this.interpreter.EvaluateExpression(expr); FunctionValue eval = this.interpreter.EvaluateExpression(expr);
if (eval.Type == ValueType.Numeric && eval.Value != null) if (eval.Type == FunctionValueType.Numeric && eval.Value != null)
{ {
r = signum * Convert.ToDecimal(eval.Value); r = signum * Convert.ToDecimal(eval.Value);
} }
@ -319,9 +319,9 @@ Arg<out FunctionArgument arg>
(. arg = new FunctionArgument(string.Empty, new FunctionValue()); decimal r = 0; string s = string.Empty; .) (. arg = new FunctionArgument(string.Empty, new FunctionValue()); decimal r = 0; string s = string.Empty; .)
= =
( (
string (. s = t.val.Substring(1, t.val.Length - 2); arg = new FunctionArgument(string.Empty, new FunctionValue(ValueType.String, s)); .) string (. s = t.val.Substring(1, t.val.Length - 2); arg = new FunctionArgument(string.Empty, new FunctionValue(FunctionValueType.String, s)); .)
| |
Expression<out r> (. arg = new FunctionArgument(string.Empty, new FunctionValue(ValueType.Numeric, r)); .) Expression<out r> (. arg = new FunctionArgument(string.Empty, new FunctionValue(FunctionValueType.Numeric, r)); .)
) )
. .

View file

@ -1,5 +1,5 @@
using csmic; using csmic;
using ValueType = csmic.ValueType; using FunctionValueType = csmic.FunctionValueType;
namespace stdlib.functions namespace stdlib.functions
{ {
@ -20,7 +20,7 @@ namespace stdlib.functions
var input = _args[0].Value; var input = _args[0].Value;
decimal number = Convert.ToDecimal(input.Value); decimal number = Convert.ToDecimal(input.Value);
return new FunctionValue(ValueType.Numeric, Math.Abs(number)); return new FunctionValue(FunctionValueType.Numeric, Math.Abs(number));
}); });
} }
} }

View file

@ -1,5 +1,5 @@
using csmic; using csmic;
using ValueType = csmic.ValueType; using FunctionValueType = csmic.FunctionValueType;
namespace stdlib.functions namespace stdlib.functions
{ {
@ -35,12 +35,12 @@ namespace stdlib.functions
return false; return false;
} }
if (argument.Value.Type == ValueType.Numeric && argument.Value.Value is not decimal) if (argument.Value.Type == FunctionValueType.Numeric && argument.Value.Value is not decimal)
{ {
return false; return false;
} }
if (argument.Value.Type == ValueType.String && argument.Value.Value is not string) if (argument.Value.Type == FunctionValueType.String && argument.Value.Value is not string)
{ {
return false; return false;
} }

View file

@ -1,5 +1,5 @@
using csmic; using csmic;
using ValueType = csmic.ValueType; using FunctionValueType = csmic.FunctionValueType;
namespace stdlib.functions namespace stdlib.functions
{ {
@ -23,7 +23,7 @@ namespace stdlib.functions
var input = _args[0].Value; var input = _args[0].Value;
decimal number = Convert.ToDecimal(input.Value); decimal number = Convert.ToDecimal(input.Value);
return new FunctionValue(ValueType.Numeric, number >= 0 ? POSITIVE : NEGATIVE); return new FunctionValue(FunctionValueType.Numeric, number >= 0 ? POSITIVE : NEGATIVE);
}); });
} }
} }

View file

@ -6,14 +6,14 @@ namespace tests;
public class StdlibFunctionsTests public class StdlibFunctionsTests
{ {
private static FunctionArgument NumArg(decimal d) => new FunctionArgument("value", new FunctionValue(ValueType.Numeric, d)); private static FunctionArgument NumArg(decimal d) => new FunctionArgument("value", new FunctionValue(FunctionValueType.Numeric, d));
[Test] [Test]
public void AbsoluteValue_Positive_ReturnsSame() public void AbsoluteValue_Positive_ReturnsSame()
{ {
var fn = new AbsoluteValue(); var fn = new AbsoluteValue();
var result = fn.Execute(NumArg(5m)); var result = fn.Execute(NumArg(5m));
Assert.That(result.Type, Is.EqualTo(ValueType.Numeric)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
Assert.That(result.Value, Is.EqualTo(5m)); Assert.That(result.Value, Is.EqualTo(5m));
} }
@ -22,7 +22,7 @@ public class StdlibFunctionsTests
{ {
var fn = new AbsoluteValue(); var fn = new AbsoluteValue();
var result = fn.Execute(NumArg(-12.5m)); var result = fn.Execute(NumArg(-12.5m));
Assert.That(result.Type, Is.EqualTo(ValueType.Numeric)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
Assert.That(result.Value, Is.EqualTo(12.5m)); Assert.That(result.Value, Is.EqualTo(12.5m));
} }
@ -31,7 +31,7 @@ public class StdlibFunctionsTests
{ {
var fn = new AbsoluteValue(); var fn = new AbsoluteValue();
var result = fn.Execute(NumArg(0m)); var result = fn.Execute(NumArg(0m));
Assert.That(result.Type, Is.EqualTo(ValueType.Numeric)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
Assert.That(result.Value, Is.EqualTo(0m)); Assert.That(result.Value, Is.EqualTo(0m));
} }
@ -41,7 +41,7 @@ public class StdlibFunctionsTests
var fn = new AbsoluteValue(); var fn = new AbsoluteValue();
var badArg = new FunctionArgument("value", FunctionValue.STRING); var badArg = new FunctionArgument("value", FunctionValue.STRING);
var result = fn.Execute(badArg); var result = fn.Execute(badArg);
Assert.That(result.Type, Is.EqualTo(ValueType.None)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.None));
Assert.That(result.Value, Is.Null); Assert.That(result.Value, Is.Null);
} }
@ -51,8 +51,8 @@ public class StdlibFunctionsTests
var fn = new AbsoluteValue(); var fn = new AbsoluteValue();
var result0 = fn.Execute(); var result0 = fn.Execute();
var result2 = fn.Execute(NumArg(1m), NumArg(2m)); var result2 = fn.Execute(NumArg(1m), NumArg(2m));
Assert.That(result0.Type, Is.EqualTo(ValueType.None)); Assert.That(result0.Type, Is.EqualTo(FunctionValueType.None));
Assert.That(result2.Type, Is.EqualTo(ValueType.None)); Assert.That(result2.Type, Is.EqualTo(FunctionValueType.None));
} }
[Test] [Test]
@ -60,7 +60,7 @@ public class StdlibFunctionsTests
{ {
var fn = new Sign(); var fn = new Sign();
var result = fn.Execute(NumArg(-1m)); var result = fn.Execute(NumArg(-1m));
Assert.That(result.Type, Is.EqualTo(ValueType.Numeric)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
Assert.That(result.Value, Is.EqualTo(Sign.NEGATIVE)); Assert.That(result.Value, Is.EqualTo(Sign.NEGATIVE));
} }
@ -69,7 +69,7 @@ public class StdlibFunctionsTests
{ {
var fn = new Sign(); var fn = new Sign();
var result = fn.Execute(NumArg(0m)); var result = fn.Execute(NumArg(0m));
Assert.That(result.Type, Is.EqualTo(ValueType.Numeric)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
Assert.That(result.Value, Is.EqualTo(Sign.POSITIVE)); Assert.That(result.Value, Is.EqualTo(Sign.POSITIVE));
} }
@ -78,7 +78,7 @@ public class StdlibFunctionsTests
{ {
var fn = new Sign(); var fn = new Sign();
var result = fn.Execute(NumArg(99.99m)); var result = fn.Execute(NumArg(99.99m));
Assert.That(result.Type, Is.EqualTo(ValueType.Numeric)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
Assert.That(result.Value, Is.EqualTo(Sign.POSITIVE)); Assert.That(result.Value, Is.EqualTo(Sign.POSITIVE));
} }
@ -88,7 +88,7 @@ public class StdlibFunctionsTests
var fn = new Sign(); var fn = new Sign();
var badArg = new FunctionArgument("value", FunctionValue.STRING); var badArg = new FunctionArgument("value", FunctionValue.STRING);
var result = fn.Execute(badArg); var result = fn.Execute(badArg);
Assert.That(result.Type, Is.EqualTo(ValueType.None)); Assert.That(result.Type, Is.EqualTo(FunctionValueType.None));
Assert.That(result.Value, Is.Null); Assert.That(result.Value, Is.Null);
} }
@ -98,8 +98,8 @@ public class StdlibFunctionsTests
var fn = new Sign(); var fn = new Sign();
var result0 = fn.Execute(); var result0 = fn.Execute();
var result2 = fn.Execute(NumArg(1m), NumArg(2m)); var result2 = fn.Execute(NumArg(1m), NumArg(2m));
Assert.That(result0.Type, Is.EqualTo(ValueType.None)); Assert.That(result0.Type, Is.EqualTo(FunctionValueType.None));
Assert.That(result2.Type, Is.EqualTo(ValueType.None)); Assert.That(result2.Type, Is.EqualTo(FunctionValueType.None));
} }
} }