Skip to content

Commit a70672c

Browse files
whole bunch of stuff
1 parent 65d8b1a commit a70672c

4 files changed

Lines changed: 485 additions & 258 deletions

File tree

EZCode/EZHelp.cs

Lines changed: 162 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public string Format(object _text, object _char)
6060
chars[i] == 'q' ? '?' :
6161
chars[i] == 'a' ? '@' :
6262
chars[i] == ';' ? ':' :
63+
chars[i] == 's' ? ';' :
6364
chars[i];
6465
if (chars[i] == '!')
6566
{
@@ -141,11 +142,10 @@ public string StringEmpty()
141142
return string.Empty;
142143
}
143144
public object ObjectParse(object obj, object type) => ObjectParse(obj, type, false);
144-
public object ObjectParse(object obj, object type, bool to_string)
145+
public object ObjectParse(object obj, object type, bool to_string, string arraySeperator = " ", bool returnNull = false)
145146
{
146147
try
147148
{
148-
149149
if (obj.ToString().StartsWith("{") && obj.ToString().EndsWith("}"))
150150
{
151151
obj = obj.ToString().Substring(1, obj.ToString().Length - 2).Trim();
@@ -159,10 +159,10 @@ public object ObjectParse(object obj, object type, bool to_string)
159159
o = obj;
160160
DataType data = DataType.GetType(type.ToString(), Interpreter.Classes, Interpreter.Containers);
161161
if (Interpreter.Vars.Any(x => x.Name == n)) Interpreter.Vars.FirstOrDefault(x => x.Name == n).DataType = data;
162-
obj = Interpreter.GetValue(n, data);
162+
obj = Interpreter.GetValue(n, data, arraySeperator);
163163
} while (obj != o);
164164
}
165-
catch { }
165+
catch when(returnNull) { return null; }
166166
if (!to_string)
167167
{
168168
if (int.TryParse(obj.ToString(), out int i)) return i;
@@ -226,12 +226,31 @@ public float Operate(string expression, bool object_parse)
226226
}
227227
public bool Expression(string expression)
228228
{
229-
string[] parts = SplitWithDelimiters(ObjectParse(expression, "str").ToString(), ['-', '+', '=', '*', '/', '%', '&', '|', '!', ' ']).Where(x => x != "" && x != " ").ToArray();
230-
expression = "";
231-
foreach (string e in parts)
229+
char[] symbols = ['-', '+', '=', '*', '/', '%', '&', '|', '!', ' '];
230+
string[] parts = SplitWithDelimiters(ObjectParse(expression, "str", true).ToString(), symbols).Where(x => x != "" && x != " ").ToArray();
231+
bool allIsText = false;
232+
for (int i = 0; i < parts.Length; i++)
232233
{
233-
expression += ObjectParse(e, "str").ToString() + " ";
234+
string e = parts[i];
235+
if ((new[] { "=", ">", "<", "=", "!=", "!", ">=", "<=" }).Any(x => x == e))
236+
{
237+
if (!allIsText && !(float.TryParse(parts[i + 1], out _) || bool.TryParse(parts[i + 1], out _) || symbols.Any(x => x.ToString() == parts[i + 1])))
238+
{
239+
parts[i - 1] = $"'{StringParse(parts[i - 1])}'";
240+
}
241+
continue;
242+
}
243+
if ((new[] { "&", "|", "and", "or", "&&", "||" }).Any(x => x == e))
244+
{
245+
allIsText = false;
246+
continue;
247+
}
248+
bool isText = !(float.TryParse(e, out _) || bool.TryParse(e, out _) || symbols.Any(x => x.ToString() == e)) || allIsText;
249+
if (isText) allIsText = true;
250+
e = !isText ? StringParse(e) : $"'{StringParse(e)}'";
251+
parts[i] = e;
234252
}
253+
expression = string.Join(" ", parts);
235254
return Evaluate(expression.Trim());
236255
}
237256
public string StringExpression(string expression)
@@ -396,7 +415,14 @@ public bool Compare(object v1, object v2, object v3)
396415
values[i] = ObjectParse(values[i], "str");
397416
}
398417
string all = string.Join(" ", values.Select(x => x.ToString()));
399-
return Expression(all);
418+
try
419+
{
420+
return Expression(all);
421+
}
422+
catch
423+
{
424+
return false;
425+
}
400426
}
401427
catch (Exception e)
402428
{
@@ -412,10 +438,28 @@ public bool Compare(object v1)
412438
{
413439
return Compare(v1, "", "");
414440
}
415-
public string StringParse(object v) => ObjectParse(v, "str").ToString();
441+
public string StringParse(object v) => ObjectParse(v, "str", true).ToString();
416442
public bool BoolParse(object v) => bool.Parse(ObjectParse(v, "bool").ToString());
417443
public float FloatParse(object v) => float.Parse(ObjectParse(v, "float").ToString());
418444
public int IntParse(object v) => int.Parse(ObjectParse(v, "int").ToString());
445+
public bool SameType(object a, object b)
446+
{
447+
if (a is Var va && b is Var vb)
448+
{
449+
if (va.Value is Class ca && vb.Value is Class cb)
450+
{
451+
return ca.Name == cb.Name;
452+
}
453+
else
454+
{
455+
return va.Value.GetType() == vb.Value.GetType();
456+
}
457+
}
458+
else
459+
{
460+
return a.GetType() == b.GetType();
461+
}
462+
}
419463
public int StringLength(object str) => StringParse(str).Length;
420464
public int RunEZCode(string code)
421465
{
@@ -433,5 +477,113 @@ public int RunEZCode(string code)
433477
throw;
434478
}
435479
}
480+
public bool IsType(object obj, object type)
481+
{
482+
try
483+
{
484+
type = StringParse(type);
485+
if (obj.ToString().StartsWith("{") && obj.ToString().EndsWith("}"))
486+
{
487+
obj = obj.ToString().Substring(1, obj.ToString().Length - 2).Trim();
488+
if (Interpreter.Vars.FirstOrDefault(x => x.Name == obj.ToString()) is Var var) obj = var;
489+
else
490+
{
491+
throw new Exception($"Could not find variable \"{obj}\"");
492+
}
493+
if (!var.Value.ToString().StartsWith('#'))
494+
{
495+
throw new Exception($"For IsType method, use \"#varName\" with # sign");
496+
}
497+
else
498+
{
499+
if (var.Value is Class c)
500+
{
501+
c.Properties.First(x => x.Name.ToString() == "value").Value = var.Value.ToString().Remove(0, 1);
502+
}
503+
else
504+
{
505+
var.Value = var.Value.ToString().Remove(0, 1);
506+
}
507+
}
508+
if (Interpreter.Vars.FirstOrDefault(x => x.Name == var.Value.ToString()) is Var v2) obj = v2;
509+
else
510+
{
511+
throw new Exception($"Could not find variable \"{var.Value}\"");
512+
}
513+
}
514+
if (obj is Var v)
515+
{
516+
if (v.Value is Class c)
517+
{
518+
return c.Name == type.ToString();
519+
}
520+
else if (v.DataType.ObjectClass is Class cl)
521+
{
522+
return cl.Name == type.ToString();
523+
}
524+
else
525+
{
526+
throw new Exception($"Variable \"{v.Name}\" is not defined");
527+
}
528+
}
529+
else if (obj is Class c)
530+
{
531+
return c.Name == type.ToString();
532+
}
533+
else
534+
{
535+
throw new Exception($"Object \"{obj}\" is not a variable");
536+
}
537+
}
538+
catch (Exception e)
539+
{
540+
Error = e.Message;
541+
throw;
542+
}
543+
}
544+
public object ArrayParse(object array, object separator) => ObjectParse(array, "list").ToString().Split(StringParse(separator)).Select(x => x.Trim()).ToArray();
545+
public string ArrayStringParse(object array) => ObjectParse(array, "list", to_string:false, arraySeperator:", ").ToString();
546+
public int ArrayLength(object array)
547+
{
548+
try
549+
{
550+
string sep = "|\\@@@@@__~>=>=//\\:@@@@@@#:#####{}}{sd\\___gpgdfpsg14702580690, ";
551+
return ObjectParse(array, "list", to_string: false, arraySeperator: sep).ToString().Split(sep).Length;
552+
}
553+
catch (Exception e)
554+
{
555+
Error = e.Message;
556+
throw;
557+
}
558+
}
559+
public object ArrayAppend(object array, object appends, object separator)
560+
{
561+
try
562+
{
563+
string sep = "|\\@@@@@__~>=>=//\\:@@@@@@#:#####{}}{sd\\___gpgdfpsg14702580690, ";
564+
var tempA = ObjectParse(array, "list", to_string: false, arraySeperator: sep, returnNull: true);
565+
object[] a = (tempA != null) ? tempA.ToString().Split(sep) : [];
566+
object[] b = (object[])ArrayParse(appends, separator);
567+
return a.Concat(b).ToArray();
568+
}
569+
catch (Exception e)
570+
{
571+
Error = e.Message;
572+
throw;
573+
}
574+
}
575+
public object ArrayIndex(object array, object index)
576+
{
577+
try
578+
{
579+
string sep = "|\\@@@@@__~>=>=//\\:@@@@@@#:#####{}}{sd\\___gpgdfpsg14702580690, ";
580+
return ObjectParse(array, "list", to_string: false, arraySeperator: sep).ToString().Split(sep)[int.Parse(StringParse(index))];
581+
}
582+
catch (Exception e)
583+
{
584+
Error = e.Message;
585+
throw;
586+
}
587+
}
436588
}
437589
}

0 commit comments

Comments
 (0)