Skip to content

Commit 6e6e8ab

Browse files
Good version
1 parent d145086 commit 6e6e8ab

3 files changed

Lines changed: 43 additions & 31 deletions

File tree

EZCode/EZHelp.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public string Format(object _text, object _char)
100100
{
101101
if (Interpreter.parser.WatchIsFound([name], 0, out ExplicitWatch watch, out _))
102102
{
103-
object val = Interpreter.MethodRun(watch.Runs.Runs, watch.Runs.Parameters);
103+
object val = Interpreter.GetValue(watch.Runs, DataType.GetType("str", Interpreter.Classes, Interpreter.Containers));
104104
format = format.Remove(range.Start, range.Count).Insert(range.Start, Interpreter.GetValue(val, new DataType(DataType.Types._string, null)).ToString());
105105
}
106106
}
@@ -141,8 +141,8 @@ public object ObjectParse(object obj, object type, bool to_string)
141141
{
142142
if (int.TryParse(obj.ToString(), out int i)) return i;
143143
if (float.TryParse(obj.ToString(), out float f)) return f;
144-
try { obj = Operate(obj.ToString(), false); } catch { }
145-
try { obj = Evaluate(obj.ToString()); } catch { }
144+
try { obj = Operate(obj.ToString(), false); return obj; } catch { }
145+
try { obj = Evaluate(obj.ToString()); return obj; } catch { }
146146
}
147147
return obj;
148148
}
@@ -339,7 +339,7 @@ public bool Compare(object v1, object v2, object v3)
339339
{
340340
values[i] = ObjectParse(values[i], "str");
341341
}
342-
string all = string.Join(" ", values.Select(x => x.ToString()), true);
342+
string all = string.Join(" ", values.Select(x => x.ToString()));
343343
return Evaluate(all);
344344
}
345345
public bool Compare(object v1, object v2)

EZCode/Interpreter.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public Interpreter(string file, Parser parser)
4343
}
4444
}
4545
private bool LastIfWasTrue = true;
46-
Exception? LastTryNotFailed = null;
46+
private Exception? LastTryNotFailed = null;
4747
private bool yielded = false;
48+
private object? returned = null;
49+
private DataType? Returning = null;
50+
private int StackNumber = 0;
4851
public Stack<string> StackTrace { get; private set; }
4952
public string[] Output { get; internal set; } = [];
5053
public Exception[] Errors { get; private set; } = [];
@@ -84,7 +87,8 @@ public int Interperate(LineWithTokens[] LineTokens)
8487
}
8588
catch (Exception ex)
8689
{
87-
string message = ex.Message + ", StackTrace: \n\t" + string.Join("\n\t", StackTrace.Reverse());
90+
string stack = string.Join("\n\t", StackTrace.Reverse());
91+
string message = ex.Message + ", StackTrace: \n\t" + (stack != "" ? stack : "Stack Empty");
8892
Console.WriteLine(message);
8993
Errors = Errors.Append(ex).ToArray();
9094
StackTrace = temp_stack;
@@ -95,15 +99,15 @@ public int Interperate(LineWithTokens[] LineTokens)
9599

96100
return endcode;
97101
}
98-
internal int StackNumber = 0;
99-
private object? SingleLine(LineWithTokens line)
102+
internal object? SingleLine(LineWithTokens line)
100103
{
101104
StackNumber++;
102105
try
103106
{
104107
string message = $"codeline: \"{line.Line.Value}\", file: \"{WorkingFile}\", line: {line.Line.CodeLine}";
105-
try { if (StackTrace.First() != message) StackTrace.Push(message); }
106-
catch { StackTrace.Push(message); }
108+
bool duplicate_stack = false;
109+
try { duplicate_stack = StackTrace.First() != message; } catch { }
110+
if (!duplicate_stack) StackTrace.Push(message);
107111
CurrentLine = line.Line;
108112
Token FirstToken = line.Tokens.FirstOrDefault(new Token(TokenType.None, "", ""));
109113
object? Return = null;
@@ -336,12 +340,12 @@ public int Interperate(LineWithTokens[] LineTokens)
336340
CSharpMethod method = FirstToken.Value as CSharpMethod;
337341
StackTrace.Push($"csharp-method: \"{(method.Path != "" ? method.Path : "Null")}\", file: \"{WorkingFile}\", line: {line.Line.CodeLine}");
338342
object obj = Reflect(method!);
339-
StackTrace.Pop();
343+
StackTrace.TryPop(out _);
340344
Return = obj;
341345
}
342346
catch (Exception ex)
343347
{
344-
throw new Exception($"Error with \"runexec\" and with C# method. The reason may be because the method may not exist. Error Message:\"{ex.Message}\"");
348+
throw new Exception($"Error with \"runexec\" and with C# method. The reason may be because the method does not exist. Error Message:\"{ex.Message}\"");
345349
}
346350
break;
347351
case TokenType.Undefined:
@@ -379,6 +383,7 @@ public int Interperate(LineWithTokens[] LineTokens)
379383
{
380384
line.Tokens = line.Tokens.Skip(1).ToArray();
381385
Return = SingleLine(line) ?? GetValue(line.Tokens);
386+
returned = Return;
382387
}
383388
catch (Exception ex)
384389
{
@@ -490,20 +495,24 @@ public int Interperate(LineWithTokens[] LineTokens)
490495
}
491496
else
492497
{
493-
string[] parts = [];
494-
for (int i = 0; i < arguments.Length; i++)
498+
bool run;
499+
do
495500
{
496-
Argument arg = arguments[i];
497-
bool? istrue = ArgumentIsTrue(arg);
498-
parts = [.. parts, istrue.ToString()];
499-
if (arg.ArgAdd != Argument.ArgAdds.None) parts = [.. parts, arg.ArgAdd.ToString()];
500-
}
501-
bool run = EZHelp.Evaluate(string.Join(" ", parts));
502-
while (run)
503-
{
504-
RunStatement(statement, out bool broke);
505-
if (broke) break;
506-
}
501+
string[] parts = [];
502+
for (int i = 0; i < arguments.Length; i++)
503+
{
504+
Argument arg = arguments[i];
505+
bool? istrue = ArgumentIsTrue(arg);
506+
parts = [.. parts, istrue.ToString()];
507+
if (arg.ArgAdd != Argument.ArgAdds.None) parts = [.. parts, arg.ArgAdd.ToString()];
508+
}
509+
run = EZHelp.Evaluate(string.Join(" ", parts));
510+
if (run)
511+
{
512+
RunStatement(statement, out bool broke);
513+
if (broke) break;
514+
}
515+
} while (run);
507516
}
508517
}
509518
catch (Exception ex)
@@ -540,7 +549,8 @@ public int Interperate(LineWithTokens[] LineTokens)
540549
}
541550
break;
542551
}
543-
StackTrace.TryPop(out _);
552+
if (!duplicate_stack)
553+
StackTrace.TryPop(out _);
544554
return Return;
545555
}
546556
catch (Exception e)
@@ -596,7 +606,6 @@ public int Interperate(LineWithTokens[] LineTokens)
596606
if (result == null) result = argument.Value;
597607
bool? term = Argument.EvaluateTerm(result.ToString());
598608
if (term == null) throw new Exception($"Expected the argument section's method \"{argument.Value}\" to return boolean");
599-
StackTrace.TryPop(out _);
600609
return term;
601610
}
602611
private enum IdentType { Var, Class, Method, Other }
@@ -625,7 +634,6 @@ private IdentType IsType(string token, out object? type)
625634
type = null;
626635
return IdentType.Other;
627636
}
628-
internal DataType? Returning = null;
629637
public object? MethodRun(Method method, Var[]? parameters)
630638
{
631639
StackTrace.Push($"method: {method.Name}, file: {WorkingFile}, line: {method.Line.CodeLine}");
@@ -662,15 +670,19 @@ private IdentType IsType(string token, out object? type)
662670
Vars = [.. Vars, .. parameters];
663671
object? result = null;
664672
var _returning = Returning != null ? new DataType(Returning.Type, Returning.ObjectClass, Returning.ObjectContainer) : null;
673+
var _returned = returned;
665674
Returning = method.Returns;
675+
returned = null;
666676
foreach (LineWithTokens line in lines)
667677
{
668678
result = SingleLine(new LineWithTokens(line));
669679

670-
if (line.Tokens[0].Type == TokenType.Return)
680+
if (line.Tokens[0].Type == TokenType.Return || returned != null)
671681
break;
672682
}
683+
result ??= returned;
673684
Returning = _returning;
685+
returned = _returned;
674686
Vars = [.. Vars, .. overlap];
675687
Vars = Vars.Except(parameters).ToArray();
676688

@@ -794,7 +806,7 @@ public object GetValue(object obj, DataType? type = null)
794806
Methods = backup_methods;
795807
if (result != null)
796808
return result;
797-
else throw new Exception("The \"get\" method for this class instance does not return a value");
809+
else throw new Exception($"The Class of the instance does not contain a \"get\" method for the expected datatype \"{type.Type.ToString().Remove(0, 1)}\"");
798810
}
799811
else
800812
{

ez/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ help Writes all of the possible commands
2222
version Writes the current version of EZCode installed
2323
run [CODE] Runs a line of code. 'Main' Package is already imported
2424
start Starts an EZCode environment
25-
[FILEPATH] Runs file. 'Main' Package is already imported.
25+
[FILEPATH] Runs file
2626
""");
2727
else
2828
{

0 commit comments

Comments
 (0)