Skip to content

Commit a0ecf16

Browse files
v2.2.0 : new Math keyword
1 parent 10a557f commit a0ecf16

File tree

3 files changed

+233
-6
lines changed

3 files changed

+233
-6
lines changed

EZCode/EZCode.cs

Lines changed: 228 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class EzCode
2424
/// <summary>
2525
/// Directory of the script playing
2626
/// </summary>
27-
public static string Version { get; } = "2.1.8";
27+
public static string Version { get; } = "2.1.10";
2828
/// <summary>
2929
/// The Official EZCode Icon
3030
/// </summary>
@@ -651,6 +651,37 @@ async Task<string[]> PlaySwitch(string[]? _parts = null, string jumpsto = "", st
651651
ErrorText(parts, ErrorTypes.normal, keyword);
652652
}
653653
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;
654685
case "shape":
655686
case "label":
656687
case "textbox":
@@ -1958,6 +1989,202 @@ async Task<string[]> PlaySwitch(string[]? _parts = null, string jumpsto = "", st
19581989
return new string[] { returnOutput, "true" };
19591990
}
19601991
}
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+
}
19612188
Method? getMethod(string name)
19622189
{
19632190
return methods.FirstOrDefault(x => x.Name == name, null);

EZCode/EZCode.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<Title>EZCode WinForms Programming Language</Title>
8-
<Version>2.1.3</Version>
8+
<Version>2.1.10</Version>
99
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1010
<Description>EZCode is a easy to ue programming language for WinForms and can help speed up your development process. Go to https://ez-code.web.app</Description>
1111
<Copyright>Copyright © 2023</Copyright>
12-
<PackageProjectUrl>https://github.com/JBrosDevelopment/EZCode.git</PackageProjectUrl>
12+
<PackageProjectUrl>https://ez-code.web.app</PackageProjectUrl>
1313
<PackageIcon>EZCode_Logo.png</PackageIcon>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<RepositoryUrl>https://github.com/JBrosDevelopment/EZCode.git</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>EZCode;Code;Programming;Language;Windows;Forms;WinForms;WinForm</PackageTags>
18-
<PackageReleaseNotes>Version 2.0.1</PackageReleaseNotes>
18+
<PackageReleaseNotes></PackageReleaseNotes>
1919
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2020
<Company>JBros Development</Company>
2121
<Authors>Joseph H;</Authors>
22+
<WebPage>https://ez-code.web.app</WebPage>
2223
</PropertyGroup>
2324

2425
<ItemGroup>

EZCode/Variable.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public enum Types
3232
bool isFile();
3333
bool isFile(string value);
3434
bool isString();
35-
string value();
3635
bool? returnBool();
3736
}
3837
public class Var : Ivar
@@ -286,7 +285,7 @@ public bool isFile(string path)
286285

287286
return true;
288287
}
289-
public string value()
288+
private string value()
290289
{
291290
if (isNumber())
292291
{

0 commit comments

Comments
 (0)