Skip to content

Commit

Permalink
v2.2.0 : new Math keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
JBrosDevelopment committed Jan 6, 2024
1 parent 10a557f commit a0ecf16
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 6 deletions.
229 changes: 228 additions & 1 deletion EZCode/EZCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class EzCode
/// <summary>
/// Directory of the script playing
/// </summary>
public static string Version { get; } = "2.1.8";
public static string Version { get; } = "2.1.10";
/// <summary>
/// The Official EZCode Icon
/// </summary>
Expand Down Expand Up @@ -651,6 +651,37 @@ async Task<string[]> PlaySwitch(string[]? _parts = null, string jumpsto = "", st
ErrorText(parts, ErrorTypes.normal, keyword);
}
break;
case "math":
try
{
string[] equationparts = parts.Skip(1).TakeWhile(x => !(x == "=>" || x == ":")).ToArray();
for (int i = 0; i < equationparts.Length; i++)
{
Var var = getVar(equationparts[i]);
if (var.isSet)
{
equationparts[i] = var.Value;
}
else
{
equationparts[i] = MathFunc(equationparts[i], parts);
}
}
string equation = string.Join(" ", equationparts);
string? result = SolveEquation(equation);
if (result == null) ErrorText(parts, ErrorTypes.errorEquation);

if (jumpTo)
{
return new string[] { result, stillInFile.ToString() };
}
returnOutput += SetVKeyword(parts, 3, keyword, result, Types.Float);
}
catch
{
ErrorText(parts, ErrorTypes.normal, keyword);
}
break;
case "shape":
case "label":
case "textbox":
Expand Down Expand Up @@ -1958,6 +1989,202 @@ async Task<string[]> PlaySwitch(string[]? _parts = null, string jumpsto = "", st
return new string[] { returnOutput, "true" };
}
}
string MathFunc(string value, string[] parts)
{
if (!value.EndsWith(")") && Regex.Matches(value, @"\)").Count == 1 && Regex.Matches(value, @"\(").Count == 1)
return value;

if (value.StartsWith("abs("))
{
string eq = value.Replace("abs(", "").Replace(")", "");
try
{
eq = getVar(eq).isSet ? getVar(eq).Value : eq;
return Math.Abs(decimal.Parse(eq)).ToString();
}
catch
{
ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'");
}
}
else if (value.StartsWith("neg("))
{
string eq = value.Replace("neg(", "").Replace(")", "");
try
{
eq = getVar(eq).isSet ? getVar(eq).Value : eq;
return (-decimal.Parse(eq)).ToString();
}
catch
{
ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'");
}
}
else if (value.StartsWith("sq("))
{
string eq = value.Replace("sq(", "").Replace(")", "");
try
{
eq = getVar(eq).isSet ? getVar(eq).Value : eq;
return (decimal.Parse(eq) * decimal.Parse(eq)).ToString();
}
catch
{
ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'");
}
}
else if (value.StartsWith("sqr("))
{
string eq = value.Replace("sqr(", "").Replace(")", "");
try
{
eq = getVar(eq).isSet ? getVar(eq).Value : eq;
return Math.Sqrt(double.Parse(eq)).ToString();
}
catch
{
ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'");
}
}
else if (value.StartsWith("round("))
{
string eq = value.Replace("round(", "").Replace(")", "");
try
{
eq = getVar(eq).isSet ? getVar(eq).Value : eq;
return Math.Round(decimal.Parse(eq)).ToString();
}
catch
{
ErrorText(parts, ErrorTypes.custom, custom: $"Error solving '{value}' with the value '{eq}' with '{parts[0]}'");
}
}
else if (value.StartsWith("pow("))
{
string eq = value.Replace("pow(", "").Replace(")", "");
string[] eqboth = eq.Split(",");
if (eqboth.Length != 2)
{
ErrorText(parts, ErrorTypes.custom, custom: $"Expected 2 parts for power function with '{parts[0]}'. Correct Syntax, 'pow(value,exponent)'. This is");
return value;
}
try
{
string ineq1 = getVar(eqboth[0].Trim()).isSet ? getVar(eqboth[0].Trim()).Value : eqboth[0].Trim();
string ineq2 = getVar(eqboth[1].Trim()).isSet ? getVar(eqboth[1].Trim()).Value : eqboth[1].Trim();
return Math.Pow(double.Parse(ineq1), double.Parse(ineq2)).ToString();
}
catch
{
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]}'");
}
}
else if (value.StartsWith("clamp("))
{
string eq = value.Replace("clamp(", "").Replace(")", "");
string[] eqboth = eq.Split(",");
if (eqboth.Length != 3)
{
ErrorText(parts, ErrorTypes.custom, custom: $"Expected 3 parts for clamp function with '{parts[0]}'. Correct Syntax, 'clamp(value,min,max)'. This is");
return value;
}
try
{
float ineq1 = getVar(eqboth[0].Trim()).isSet ? float.Parse(getVar(eqboth[0].Trim()).Value) : float.Parse(eqboth[0].Trim());
float ineq2 = getVar(eqboth[1].Trim()).isSet ? float.Parse(getVar(eqboth[1].Trim()).Value) : float.Parse(eqboth[1].Trim());
float ineq3 = getVar(eqboth[2].Trim()).isSet ? float.Parse(getVar(eqboth[2].Trim()).Value) : float.Parse(eqboth[2].Trim());

if (ineq1.CompareTo(ineq2) < 0) return ineq2.ToString();
else if (ineq1.CompareTo(ineq3) > 0) return ineq3.ToString();
else return ineq1.ToString();
}
catch
{
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]}'");
}
}
else if (value.StartsWith("sum("))
{
string eq = value.Replace("sum(", "").Replace(")", "");
string[] eqboth = eq.Split(",");
float result = 0;
try
{
for (int i = 0; i < eqboth.Length; i++)
{
result += getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim());
}
return result.ToString();
}
catch
{
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]}'");
}
}
else if (value.StartsWith("avg("))
{
string eq = value.Replace("avg(", "").Replace(")", "");
string[] eqboth = eq.Split(",");
float[] result = new float[0];
try
{
for (int i = 0; i < eqboth.Length; i++)
{
result = result.Append(getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim())).ToArray();
}
return result.Average().ToString();
}
catch
{
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]}'");
}
}
else if (value.StartsWith("min("))
{
string eq = value.Replace("min(", "").Replace(")", "");
string[] eqboth = eq.Split(",");
float? result = null;
try
{
for (int i = 0; i < eqboth.Length; i++)
{
float fl = getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim());
result ??= fl;
if (fl < result) result = fl;
}
return result.ToString()!;
}
catch
{
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]}'");
}
}
else if (value.StartsWith("max("))
{
string eq = value.Replace("max(", "").Replace(")", "");
string[] eqboth = eq.Split(",");
float? result = null;
try
{
for (int i = 0; i < eqboth.Length; i++)
{
float fl = getVar(eqboth[i].Trim()).isSet ? float.Parse(getVar(eqboth[i].Trim()).Value) : float.Parse(eqboth[i].Trim());
result ??= fl;
if (fl > result) result = fl;
}
return result.ToString()!;
}
catch
{
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]}'");
}
}
else if (value.Equals("pi()"))
{
return Math.PI.ToString();
}
return value;
}
Method? getMethod(string name)
{
return methods.FirstOrDefault(x => x.Name == name, null);
Expand Down
7 changes: 4 additions & 3 deletions EZCode/EZCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Title>EZCode WinForms Programming Language</Title>
<Version>2.1.3</Version>
<Version>2.1.10</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<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>
<Copyright>Copyright © 2023</Copyright>
<PackageProjectUrl>https://github.com/JBrosDevelopment/EZCode.git</PackageProjectUrl>
<PackageProjectUrl>https://ez-code.web.app</PackageProjectUrl>
<PackageIcon>EZCode_Logo.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/JBrosDevelopment/EZCode.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>EZCode;Code;Programming;Language;Windows;Forms;WinForms;WinForm</PackageTags>
<PackageReleaseNotes>Version 2.0.1</PackageReleaseNotes>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Company>JBros Development</Company>
<Authors>Joseph H;</Authors>
<WebPage>https://ez-code.web.app</WebPage>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions EZCode/Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public enum Types
bool isFile();
bool isFile(string value);
bool isString();
string value();
bool? returnBool();
}
public class Var : Ivar
Expand Down Expand Up @@ -286,7 +285,7 @@ public bool isFile(string path)

return true;
}
public string value()
private string value()
{
if (isNumber())
{
Expand Down

0 comments on commit a0ecf16

Please sign in to comment.