|
16 | 16 | namespace EZCode |
17 | 17 | { |
18 | 18 | /// <summary> |
19 | | - /// This is the Official EZCode Source Code. See Version '<seealso cref="Version"/>' |
| 19 | + /// This is the Official EZCode Source Code. See <seealso cref="Version"/> |
20 | 20 | /// </summary> |
21 | 21 | public class EzCode |
22 | 22 | { |
23 | 23 | #region Variables_and_Initializers |
24 | 24 | /// <summary> |
25 | 25 | /// Directory of the script playing |
26 | 26 | /// </summary> |
27 | | - public static string Version { get; } = "2.1.8"; |
| 27 | + public static string Version { get; } = "2.2.0"; |
28 | 28 | /// <summary> |
29 | 29 | /// The Official EZCode Icon |
30 | 30 | /// </summary> |
@@ -651,6 +651,37 @@ async Task<string[]> PlaySwitch(string[]? _parts = null, string jumpsto = "", st |
651 | 651 | ErrorText(parts, ErrorTypes.normal, keyword); |
652 | 652 | } |
653 | 653 | break; |
| 654 | + case "math": |
| 655 | + try |
| 656 | + { |
| 657 | + string[] equationparts = parts.Skip(1).TakeWhile(x => !(x == "=>" || x == ":")).ToArray(); |
| 658 | + for (int i = 0; i < equationparts.Length; i++) |
| 659 | + { |
| 660 | + Var var = getVar(equationparts[i]); |
| 661 | + if (var.isSet) |
| 662 | + { |
| 663 | + equationparts[i] = var.Value; |
| 664 | + } |
| 665 | + else |
| 666 | + { |
| 667 | + equationparts[i] = MathFunc(equationparts[i], parts); |
| 668 | + } |
| 669 | + } |
| 670 | + string equation = string.Join(" ", equationparts); |
| 671 | + string? result = SolveEquation(equation); |
| 672 | + if (result == null) ErrorText(parts, ErrorTypes.errorEquation); |
| 673 | + |
| 674 | + if (jumpTo) |
| 675 | + { |
| 676 | + return new string[] { result, stillInFile.ToString() }; |
| 677 | + } |
| 678 | + returnOutput += SetVKeyword(parts, 3, keyword, result, Types.Float); |
| 679 | + } |
| 680 | + catch |
| 681 | + { |
| 682 | + ErrorText(parts, ErrorTypes.normal, keyword); |
| 683 | + } |
| 684 | + break; |
654 | 685 | case "shape": |
655 | 686 | case "label": |
656 | 687 | case "textbox": |
@@ -1958,6 +1989,202 @@ async Task<string[]> PlaySwitch(string[]? _parts = null, string jumpsto = "", st |
1958 | 1989 | return new string[] { returnOutput, "true" }; |
1959 | 1990 | } |
1960 | 1991 | } |
| 1992 | + string MathFunc(string value, string[] parts) |
| 1993 | + { |
| 1994 | + if (!value.EndsWith(")") && Regex.Matches(value, @"\)").Count == 1 && Regex.Matches(value, @"\(").Count == 1) |
| 1995 | + return value; |
| 1996 | + |
| 1997 | + if (value.StartsWith("abs(")) |
| 1998 | + { |
| 1999 | + string eq = value.Replace("abs(", "").Replace(")", ""); |
| 2000 | + try |
| 2001 | + { |
| 2002 | + eq = getVar(eq).isSet ? getVar(eq).Value : eq; |
| 2003 | + return Math.Abs(decimal.Parse(eq)).ToString(); |
| 2004 | + } |
| 2005 | + catch |
| 2006 | + { |
| 2007 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'"); |
| 2008 | + } |
| 2009 | + } |
| 2010 | + else if (value.StartsWith("neg(")) |
| 2011 | + { |
| 2012 | + string eq = value.Replace("neg(", "").Replace(")", ""); |
| 2013 | + try |
| 2014 | + { |
| 2015 | + eq = getVar(eq).isSet ? getVar(eq).Value : eq; |
| 2016 | + return (-decimal.Parse(eq)).ToString(); |
| 2017 | + } |
| 2018 | + catch |
| 2019 | + { |
| 2020 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'"); |
| 2021 | + } |
| 2022 | + } |
| 2023 | + else if (value.StartsWith("sq(")) |
| 2024 | + { |
| 2025 | + string eq = value.Replace("sq(", "").Replace(")", ""); |
| 2026 | + try |
| 2027 | + { |
| 2028 | + eq = getVar(eq).isSet ? getVar(eq).Value : eq; |
| 2029 | + return (decimal.Parse(eq) * decimal.Parse(eq)).ToString(); |
| 2030 | + } |
| 2031 | + catch |
| 2032 | + { |
| 2033 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'"); |
| 2034 | + } |
| 2035 | + } |
| 2036 | + else if (value.StartsWith("sqr(")) |
| 2037 | + { |
| 2038 | + string eq = value.Replace("sqr(", "").Replace(")", ""); |
| 2039 | + try |
| 2040 | + { |
| 2041 | + eq = getVar(eq).isSet ? getVar(eq).Value : eq; |
| 2042 | + return Math.Sqrt(double.Parse(eq)).ToString(); |
| 2043 | + } |
| 2044 | + catch |
| 2045 | + { |
| 2046 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'"); |
| 2047 | + } |
| 2048 | + } |
| 2049 | + else if (value.StartsWith("round(")) |
| 2050 | + { |
| 2051 | + string eq = value.Replace("round(", "").Replace(")", ""); |
| 2052 | + try |
| 2053 | + { |
| 2054 | + eq = getVar(eq).isSet ? getVar(eq).Value : eq; |
| 2055 | + return Math.Round(decimal.Parse(eq)).ToString(); |
| 2056 | + } |
| 2057 | + catch |
| 2058 | + { |
| 2059 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'"); |
| 2060 | + } |
| 2061 | + } |
| 2062 | + else if (value.StartsWith("pow(")) |
| 2063 | + { |
| 2064 | + string eq = value.Replace("pow(", "").Replace(")", ""); |
| 2065 | + string[] eqboth = eq.Split(","); |
| 2066 | + if (eqboth.Length != 2) |
| 2067 | + { |
| 2068 | + ErrorText(parts, ErrorTypes.custom, custom: $"Expected 2 parts for power function with '{parts[0]}'. Correct Syntax, 'pow(value,exponent)'. This is"); |
| 2069 | + return value; |
| 2070 | + } |
| 2071 | + try |
| 2072 | + { |
| 2073 | + string ineq1 = getVar(eqboth[0].Trim()).isSet ? getVar(eqboth[0].Trim()).Value : eqboth[0].Trim(); |
| 2074 | + string ineq2 = getVar(eqboth[1].Trim()).isSet ? getVar(eqboth[1].Trim()).Value : eqboth[1].Trim(); |
| 2075 | + return Math.Pow(double.Parse(ineq1), double.Parse(ineq2)).ToString(); |
| 2076 | + } |
| 2077 | + catch |
| 2078 | + { |
| 2079 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the values '{eq}'. Try removing spaces between values, 'pow(value,exponent)'. This is with '{parts[0]}'"); |
| 2080 | + } |
| 2081 | + } |
| 2082 | + else if (value.StartsWith("clamp(")) |
| 2083 | + { |
| 2084 | + string eq = value.Replace("clamp(", "").Replace(")", ""); |
| 2085 | + string[] eqboth = eq.Split(","); |
| 2086 | + if (eqboth.Length != 3) |
| 2087 | + { |
| 2088 | + ErrorText(parts, ErrorTypes.custom, custom: $"Expected 3 parts for clamp function with '{parts[0]}'. Correct Syntax, 'clamp(value,min,max)'. This is"); |
| 2089 | + return value; |
| 2090 | + } |
| 2091 | + try |
| 2092 | + { |
| 2093 | + float ineq1 = getVar(eqboth[0].Trim()).isSet ? float.Parse(getVar(eqboth[0].Trim()).Value) : float.Parse(eqboth[0].Trim()); |
| 2094 | + float ineq2 = getVar(eqboth[1].Trim()).isSet ? float.Parse(getVar(eqboth[1].Trim()).Value) : float.Parse(eqboth[1].Trim()); |
| 2095 | + float ineq3 = getVar(eqboth[2].Trim()).isSet ? float.Parse(getVar(eqboth[2].Trim()).Value) : float.Parse(eqboth[2].Trim()); |
| 2096 | + |
| 2097 | + if (ineq1.CompareTo(ineq2) < 0) return ineq2.ToString(); |
| 2098 | + else if (ineq1.CompareTo(ineq3) > 0) return ineq3.ToString(); |
| 2099 | + else return ineq1.ToString(); |
| 2100 | + } |
| 2101 | + catch |
| 2102 | + { |
| 2103 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the values '{eq}'. Try removing spaces between values, 'clamp(value,min,max)'. This is with '{parts[0]}'"); |
| 2104 | + } |
| 2105 | + } |
| 2106 | + else if (value.StartsWith("sum(")) |
| 2107 | + { |
| 2108 | + string eq = value.Replace("sum(", "").Replace(")", ""); |
| 2109 | + string[] eqboth = eq.Split(","); |
| 2110 | + float result = 0; |
| 2111 | + try |
| 2112 | + { |
| 2113 | + for (int i = 0; i < eqboth.Length; i++) |
| 2114 | + { |
| 2115 | + result += getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim()); |
| 2116 | + } |
| 2117 | + return result.ToString(); |
| 2118 | + } |
| 2119 | + catch |
| 2120 | + { |
| 2121 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the values '{eq}'. Try removing spaces between values, 'sum(value1,value2,etc)'. This is with '{parts[0]}'"); |
| 2122 | + } |
| 2123 | + } |
| 2124 | + else if (value.StartsWith("avg(")) |
| 2125 | + { |
| 2126 | + string eq = value.Replace("avg(", "").Replace(")", ""); |
| 2127 | + string[] eqboth = eq.Split(","); |
| 2128 | + float[] result = new float[0]; |
| 2129 | + try |
| 2130 | + { |
| 2131 | + for (int i = 0; i < eqboth.Length; i++) |
| 2132 | + { |
| 2133 | + result = result.Append(getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim())).ToArray(); |
| 2134 | + } |
| 2135 | + return result.Average().ToString(); |
| 2136 | + } |
| 2137 | + catch |
| 2138 | + { |
| 2139 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the values '{eq}'. Try removing spaces between values, 'avg(value1,value2,etc)'. This is with '{parts[0]}'"); |
| 2140 | + } |
| 2141 | + } |
| 2142 | + else if (value.StartsWith("min(")) |
| 2143 | + { |
| 2144 | + string eq = value.Replace("min(", "").Replace(")", ""); |
| 2145 | + string[] eqboth = eq.Split(","); |
| 2146 | + float? result = null; |
| 2147 | + try |
| 2148 | + { |
| 2149 | + for (int i = 0; i < eqboth.Length; i++) |
| 2150 | + { |
| 2151 | + float fl = getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim()); |
| 2152 | + result ??= fl; |
| 2153 | + if (fl < result) result = fl; |
| 2154 | + } |
| 2155 | + return result.ToString()!; |
| 2156 | + } |
| 2157 | + catch |
| 2158 | + { |
| 2159 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the values '{eq}'. Try removing spaces between values, 'min(value1,value2)'. This is with '{parts[0]}'"); |
| 2160 | + } |
| 2161 | + } |
| 2162 | + else if (value.StartsWith("max(")) |
| 2163 | + { |
| 2164 | + string eq = value.Replace("max(", "").Replace(")", ""); |
| 2165 | + string[] eqboth = eq.Split(","); |
| 2166 | + float? result = null; |
| 2167 | + try |
| 2168 | + { |
| 2169 | + for (int i = 0; i < eqboth.Length; i++) |
| 2170 | + { |
| 2171 | + float fl = getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim()); |
| 2172 | + result ??= fl; |
| 2173 | + if (fl > result) result = fl; |
| 2174 | + } |
| 2175 | + return result.ToString()!; |
| 2176 | + } |
| 2177 | + catch |
| 2178 | + { |
| 2179 | + ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the values '{eq}'. Try removing spaces between values, 'max(value1,value2)'. This is with '{parts[0]}'"); |
| 2180 | + } |
| 2181 | + } |
| 2182 | + else if (value.Equals("pi()")) |
| 2183 | + { |
| 2184 | + return Math.PI.ToString(); |
| 2185 | + } |
| 2186 | + return value; |
| 2187 | + } |
1961 | 2188 | Method? getMethod(string name) |
1962 | 2189 | { |
1963 | 2190 | return methods.FirstOrDefault(x => x.Name == name, null); |
|
0 commit comments