From 82ceb7e5cde838cc81fd738da22a28de4dbb5bdb Mon Sep 17 00:00:00 2001 From: Jordan Wages Date: Wed, 20 Aug 2025 05:21:57 -0500 Subject: [PATCH] Refactored ValueType to FunctionValueType It was causing issues. --- src/core/FunctionValue.cs | 16 ++++++------ .../{ValueType.cs => FunctionValueType.cs} | 2 +- src/core/InputInterpreter.cs | 10 +++---- src/core/cocor/Interpreter.atg | 12 ++++----- src/stdlib/functions/AbsoluteValue.cs | 4 +-- src/stdlib/functions/FunctionBase.cs | 6 ++--- src/stdlib/functions/Sign.cs | 4 +-- src/tests/StdlibFunctionsTests.cs | 26 +++++++++---------- 8 files changed, 40 insertions(+), 40 deletions(-) rename src/core/{ValueType.cs => FunctionValueType.cs} (85%) diff --git a/src/core/FunctionValue.cs b/src/core/FunctionValue.cs index 9e84a9b..f5b396e 100644 --- a/src/core/FunctionValue.cs +++ b/src/core/FunctionValue.cs @@ -9,22 +9,22 @@ namespace csmic { public class FunctionValue { - public ValueType Type { get; set; } + public FunctionValueType Type { get; set; } public object? Value { get; set; } - public static readonly FunctionValue TRUE = new FunctionValue(ValueType.Numeric, 1m); - public static readonly FunctionValue FALSE = new FunctionValue(ValueType.Numeric, 0m); - public static readonly FunctionValue NONE = new FunctionValue(ValueType.None, null); - public static readonly FunctionValue NUMBER = new FunctionValue(ValueType.Numeric, 0m); - public static readonly FunctionValue STRING = new FunctionValue(ValueType.String, string.Empty); + public static readonly FunctionValue TRUE = new FunctionValue(FunctionValueType.Numeric, 1m); + public static readonly FunctionValue FALSE = new FunctionValue(FunctionValueType.Numeric, 0m); + public static readonly FunctionValue NONE = new FunctionValue(FunctionValueType.None, null); + public static readonly FunctionValue NUMBER = new FunctionValue(FunctionValueType.Numeric, 0m); + public static readonly FunctionValue STRING = new FunctionValue(FunctionValueType.String, string.Empty); public FunctionValue() { - this.Type = ValueType.None; + this.Type = FunctionValueType.None; this.Value = null; } - public FunctionValue(ValueType type, object? value) + public FunctionValue(FunctionValueType type, object? value) { this.Type = type; this.Value = value; diff --git a/src/core/ValueType.cs b/src/core/FunctionValueType.cs similarity index 85% rename from src/core/ValueType.cs rename to src/core/FunctionValueType.cs index f944849..5d3c9c0 100644 --- a/src/core/ValueType.cs +++ b/src/core/FunctionValueType.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace csmic { - public enum ValueType + public enum FunctionValueType { None, Numeric, diff --git a/src/core/InputInterpreter.cs b/src/core/InputInterpreter.cs index d631ccf..a38ca53 100644 --- a/src/core/InputInterpreter.cs +++ b/src/core/InputInterpreter.cs @@ -64,17 +64,17 @@ namespace csmic { switch (functionValue.Type) { - case ValueType.Numeric: + case FunctionValueType.Numeric: decimal numericValue = Convert.ToDecimal(functionValue.Value); ProduceOutput(numericValue, string.Empty); break; - case ValueType.String: + case FunctionValueType.String: if (functionValue.Value is string s) ProduceOutput(0, s); else ProduceOutput(0, string.Empty); break; - case ValueType.None: + case FunctionValueType.None: default: ProduceOutput(0, string.Empty); break; @@ -188,10 +188,10 @@ namespace csmic } 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 diff --git a/src/core/cocor/Interpreter.atg b/src/core/cocor/Interpreter.atg index d8c437b..67ba9c3 100644 --- a/src/core/cocor/Interpreter.atg +++ b/src/core/cocor/Interpreter.atg @@ -109,12 +109,12 @@ INTERPRETER (. | IF(IsAssignment()) Assignment - (. this.functionValue = new FunctionValue(ValueType.Numeric, r); + (. this.functionValue = new FunctionValue(FunctionValueType.Numeric, r); this.interpreter.ProduceOutput(this.functionValue); .) | Expression - (. this.functionValue = new FunctionValue(ValueType.Numeric, r); + (. this.functionValue = new FunctionValue(FunctionValueType.Numeric, r); this.interpreter.ProduceOutput(this.functionValue); .) . @@ -158,7 +158,7 @@ Value (. IF(IsFunctionCall()) Function (. - if (fvr.Type == ValueType.Numeric && fvr.Value != null) + if (fvr.Type == FunctionValueType.Numeric && fvr.Value != null) { try { r = signum * Convert.ToDecimal(fvr.Value); } catch { SemErr("function returned non-numeric"); r = 0; } @@ -198,7 +198,7 @@ Value (. if (this.interpreter.TryGetExpression(ident, out 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); } @@ -319,9 +319,9 @@ Arg (. 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 (. arg = new FunctionArgument(string.Empty, new FunctionValue(ValueType.Numeric, r)); .) + Expression (. arg = new FunctionArgument(string.Empty, new FunctionValue(FunctionValueType.Numeric, r)); .) ) . diff --git a/src/stdlib/functions/AbsoluteValue.cs b/src/stdlib/functions/AbsoluteValue.cs index af1b82b..14762fe 100644 --- a/src/stdlib/functions/AbsoluteValue.cs +++ b/src/stdlib/functions/AbsoluteValue.cs @@ -1,5 +1,5 @@ using csmic; -using ValueType = csmic.ValueType; +using FunctionValueType = csmic.FunctionValueType; namespace stdlib.functions { @@ -20,7 +20,7 @@ namespace stdlib.functions var input = _args[0].Value; decimal number = Convert.ToDecimal(input.Value); - return new FunctionValue(ValueType.Numeric, Math.Abs(number)); + return new FunctionValue(FunctionValueType.Numeric, Math.Abs(number)); }); } } diff --git a/src/stdlib/functions/FunctionBase.cs b/src/stdlib/functions/FunctionBase.cs index ee8ec14..338d2d7 100644 --- a/src/stdlib/functions/FunctionBase.cs +++ b/src/stdlib/functions/FunctionBase.cs @@ -1,5 +1,5 @@ using csmic; -using ValueType = csmic.ValueType; +using FunctionValueType = csmic.FunctionValueType; namespace stdlib.functions { @@ -35,12 +35,12 @@ namespace stdlib.functions 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; } - 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; } diff --git a/src/stdlib/functions/Sign.cs b/src/stdlib/functions/Sign.cs index 1f3bc5c..f6578d5 100644 --- a/src/stdlib/functions/Sign.cs +++ b/src/stdlib/functions/Sign.cs @@ -1,5 +1,5 @@ using csmic; -using ValueType = csmic.ValueType; +using FunctionValueType = csmic.FunctionValueType; namespace stdlib.functions { @@ -23,7 +23,7 @@ namespace stdlib.functions var input = _args[0].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); }); } } diff --git a/src/tests/StdlibFunctionsTests.cs b/src/tests/StdlibFunctionsTests.cs index cf01630..a8030c8 100644 --- a/src/tests/StdlibFunctionsTests.cs +++ b/src/tests/StdlibFunctionsTests.cs @@ -6,14 +6,14 @@ namespace tests; 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] public void AbsoluteValue_Positive_ReturnsSame() { var fn = new AbsoluteValue(); 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)); } @@ -22,7 +22,7 @@ public class StdlibFunctionsTests { var fn = new AbsoluteValue(); 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)); } @@ -31,7 +31,7 @@ public class StdlibFunctionsTests { var fn = new AbsoluteValue(); 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)); } @@ -41,7 +41,7 @@ public class StdlibFunctionsTests var fn = new AbsoluteValue(); var badArg = new FunctionArgument("value", FunctionValue.STRING); 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); } @@ -51,8 +51,8 @@ public class StdlibFunctionsTests var fn = new AbsoluteValue(); var result0 = fn.Execute(); var result2 = fn.Execute(NumArg(1m), NumArg(2m)); - Assert.That(result0.Type, Is.EqualTo(ValueType.None)); - Assert.That(result2.Type, Is.EqualTo(ValueType.None)); + Assert.That(result0.Type, Is.EqualTo(FunctionValueType.None)); + Assert.That(result2.Type, Is.EqualTo(FunctionValueType.None)); } [Test] @@ -60,7 +60,7 @@ public class StdlibFunctionsTests { var fn = new Sign(); 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)); } @@ -69,7 +69,7 @@ public class StdlibFunctionsTests { var fn = new Sign(); 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)); } @@ -78,7 +78,7 @@ public class StdlibFunctionsTests { var fn = new Sign(); 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)); } @@ -88,7 +88,7 @@ public class StdlibFunctionsTests var fn = new Sign(); var badArg = new FunctionArgument("value", FunctionValue.STRING); 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); } @@ -98,8 +98,8 @@ public class StdlibFunctionsTests var fn = new Sign(); var result0 = fn.Execute(); var result2 = fn.Execute(NumArg(1m), NumArg(2m)); - Assert.That(result0.Type, Is.EqualTo(ValueType.None)); - Assert.That(result2.Type, Is.EqualTo(ValueType.None)); + Assert.That(result0.Type, Is.EqualTo(FunctionValueType.None)); + Assert.That(result2.Type, Is.EqualTo(FunctionValueType.None)); } }