Skip to content

Commit b8d818d

Browse files
taken a break and come back
1 parent ab72392 commit b8d818d

4 files changed

Lines changed: 108 additions & 18 deletions

File tree

EZCode/EZHelp.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using static EZCodeLanguage.Parser;
2+
using static EZCodeLanguage.Interpreter;
23
using System.Data;
34

45
namespace EZCodeLanguage
@@ -547,7 +548,7 @@ public int ArrayLength(object array)
547548
{
548549
try
549550
{
550-
string sep = "|\\@@@@@__~>=>=//\\:@@@@@@#:#####{}}{sd\\___gpgdfpsg14702580690, ";
551+
string sep = "|\\@@@@@__~>=>=//\\@@@@@@######{}}{sd\\___gpgdfpsg14702580690, ";
551552
return ObjectParse(array, "list", to_string: false, arraySeperator: sep).ToString().Split(sep).Length;
552553
}
553554
catch (Exception e)
@@ -585,5 +586,42 @@ public object ArrayIndex(object array, object index)
585586
throw;
586587
}
587588
}
589+
public float MathFunc(object obj, object op)
590+
{
591+
try
592+
{
593+
obj = ObjectParse(obj, "str");
594+
bool obj2_is_null = !obj.ToString().Contains(",");
595+
object obj1 = obj;
596+
if (!obj2_is_null) obj1 = obj.ToString().Split(",")[0];
597+
object? obj2 = null;
598+
obj1 = ObjectParse(obj1, "float");
599+
if (!obj2_is_null) obj2 = ObjectParse(obj.ToString().Split(",")[1], "float");
600+
op = StringParse(op);
601+
602+
if (!float.TryParse(obj1.ToString(), out float val1))
603+
{
604+
throw new Exception("Could not parse 'obj' to type 'float'");
605+
}
606+
if (!float.TryParse(!obj2_is_null ? obj2.ToString() : "", out float val2))
607+
{
608+
if (!obj2_is_null)
609+
{
610+
throw new Exception("Could not parse 'obj' to type 'float'");
611+
}
612+
}
613+
string operation = string.Join("", op.ToString().ToLower().ToCharArray().Select((x, y) => { if (y == 0) return char.Parse(x.ToString().ToUpper()); else return x; }));
614+
615+
object[] parameters = [val1];
616+
if (!obj2_is_null) parameters = [..parameters, val2];
617+
618+
return float.Parse(InvokeMethod($"System.MathF.{operation}", parameters, this).ToString());
619+
}
620+
catch (Exception ex)
621+
{
622+
Error = ex.Message;
623+
throw;
624+
}
625+
}
588626
}
589627
}

EZCode/Interpreter.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,15 @@ public int Interperate(LineWithTokens[] LineTokens)
545545
string stringReturns = string.Join(" ", line.Tokens.Select(x => x.StringValue));
546546
if (stringReturns != string.Empty && ex.Message.Contains($"Error Message:\"The identifier \"{line.Tokens.First().StringValue}\" does not exist in this current context\""))
547547
{
548-
Return = stringReturns;
549-
returned = Return;
548+
try
549+
{
550+
Return = GetValue(stringReturns);
551+
returned = Return;
552+
}
553+
catch
554+
{
555+
throw new Exception($"Error with \"return\", Error Message:\"{ex.Message}\"");
556+
}
550557
}
551558
else
552559
{
@@ -793,7 +800,21 @@ public int Interperate(LineWithTokens[] LineTokens)
793800
LineWithTokens line = new LineWithTokens(argument.Tokens, argument.Line);
794801
object? result = null;
795802
string exc = "";
796-
try { result = SingleLine(line) ?? throw new Exception(); } catch (Exception e) { exc = EZHelp.Error; EZHelp.Error = null; result = GetValue(argument.Tokens, DataType.GetType("bool", Classes)); }
803+
try
804+
{
805+
result = SingleLine(line) ?? throw new Exception();
806+
}
807+
catch (Exception e)
808+
{
809+
exc = EZHelp.Error;
810+
EZHelp.Error = null;
811+
string[] before = line.Tokens.Select(x => x.Value.ToString()).ToArray();
812+
result = GetValue(argument.Tokens, DataType.GetType("bool", Classes));
813+
if (before.SequenceEqual(result.ToString().Split(' ')))
814+
{
815+
try { result = EZHelp.Expression(result.ToString()); } catch { }
816+
}
817+
}
797818
if (result is Class c)
798819
result = Argument.EvaluateTerm(c.Properties.FirstOrDefault(x => x.Name.ToLower() == "value").Value.ToString());
799820
if (result == null)
@@ -1059,6 +1080,18 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
10591080
{
10601081
return v.Value;
10611082
}
1083+
else if (c.Methods.FirstOrDefault(x => x.Name == property_name) is Method m)
1084+
{
1085+
Methods = [.. Methods, .. c.Methods];
1086+
Vars = [.. Vars, .. c.Properties];
1087+
Parser parser = new Parser(property_name, m.Line.FilePath);
1088+
Token[] parsertokens = parser.Parse()[0].Tokens;
1089+
LineWithTokens lineWithTokens = new LineWithTokens(parsertokens, CurrentLine);
1090+
obj = SingleLine(lineWithTokens);
1091+
Methods = Methods.Except(c.Methods).ToArray();
1092+
Vars = Vars.Except(c.Properties).ToArray();
1093+
return obj;
1094+
}
10621095
else
10631096
{
10641097
throw new Exception($"Could not find the property \"{property_name}\" in the class instance \"{firstpart}\"");
@@ -1165,6 +1198,7 @@ void DoClass(Class c, int skip = 1)
11651198
Class cl = new Class(Classes.FirstOrDefault(x => x.Name == run.ClassName));
11661199
Method[] backupMethods = Methods;
11671200
Var[] backupVars = Vars;
1201+
Var[] backupRunParameters = run.Parameters.Select(x => new Var(x.Name, x.Value, x.Line, x.StackNumber, x.DataType, x.Required, x.Global, x.IsParams)).ToArray();
11681202
Vars = [.. Vars, .. cl.Properties];
11691203
Methods = [.. Methods, .. cl.Methods];
11701204

@@ -1184,6 +1218,7 @@ void DoClass(Class c, int skip = 1)
11841218

11851219
Methods = backupMethods;
11861220
Vars = backupVars;
1221+
run.Parameters = backupRunParameters;
11871222
return o;
11881223
}
11891224
else

EZCode/Parser.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,25 @@ private LineWithTokens[] TokenArray(string code, string file, bool insideClass =
464464
stringParts = SplitParts(ref Lines, i, arrow_input_index, ref stringParts, out _, out _, insideClass, true).Select(x => x.ToString()).ToArray();
465465
// gets the token's values
466466
object[] parts = SplitParts(ref Lines, i, arrow_input_index, ref stringParts, out continues, out arrow_output_index, insideClass);
467+
468+
// check if the line is an include or exclude
469+
if (parts.Length > 1 && parts[0].ToString() == "include")
470+
{
471+
// grab the packages and return the parser with those packages
472+
string combined_packages = string.Join(" ", parts.Skip(1).Select(x => x.ToString()));
473+
string[] packages = combined_packages.Split(",").Select(x => x.Trim()).ToArray();
474+
Parser parser = Package.ReturnParserWithPackages(this, packages);
475+
}
476+
if (parts.Length > 1 && parts[0].ToString() == "exclude")
477+
{
478+
// grab the packages to except and get it as a parser instance
479+
string combined_packages = string.Join(" ", parts.Skip(1).Select(x => x.ToString()));
480+
string[] packages = combined_packages.Split(",").Select(x => x.Trim()).ToArray();
481+
Parser except = Package.ReturnParserWithPackages(new Parser("", ""), packages);
482+
// remove the except parser from the current parser instance
483+
Parser parser = Package.RemovePackageFromParser(this, except);
484+
}
485+
467486
// loops through each part and creates the token from it
468487
for (int j = 0; j < parts.Length; j++)
469488
{

TestEnv/Code.ezcode

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
include main, math
2-
3-
class aaa {
4-
undefined val => hi
5-
method yes => @var {
6-
return defrs
7-
}
1+
include main
2+
3+
list numbers new : 1;2;3;4
4+
int length new => numbers length
5+
print 'length'
6+
int i new : 0
7+
loop (length > i) {
8+
print 'length' > 'i' = '(length > i)'
9+
print 'numbers index : i'
10+
i + 1
811
}
9-
str val new => str format : aaa:yes
10-
print val
11-
12-
print 'add 5, 6'
13-
14-
exclude main
1512

16-
print hu
13+
print 'operate max, 60, 88'
14+
print 'avg 50, 80, 90'

0 commit comments

Comments
 (0)