Skip to content

Commit 72a1146

Browse files
with tasks
1 parent b8d818d commit 72a1146

6 files changed

Lines changed: 248 additions & 112 deletions

File tree

EZCode/Debug.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using static EZCodeLanguage.Parser;
2+
3+
namespace EZCodeLanguage
4+
{
5+
public static class Debug
6+
{
7+
public class Breakpoint
8+
{
9+
public Line Line { get; set; }
10+
public bool Enabled { get; set; }
11+
public bool DisableWhenHit { get; set; }
12+
public string? EZCodeConditionToHit { get; set; }
13+
public string? EZCodeActionWhenHit { get; set; }
14+
public Breakpoint? EnabledWhenBreakpointIsHit { get; set; }
15+
public bool Hit { get; set; }
16+
public Breakpoint(Line line, bool enabled = true, bool disableWhenHit = false,
17+
string? ezcodeConditionToHit = null, string? ezcodeActionWhenHit = null,
18+
Breakpoint? enabledWhenBreakpointIsHit = null)
19+
{
20+
Line = line;
21+
Enabled = enabled;
22+
DisableWhenHit = disableWhenHit;
23+
EZCodeConditionToHit = ezcodeConditionToHit;
24+
EZCodeActionWhenHit = ezcodeActionWhenHit;
25+
EnabledWhenBreakpointIsHit = enabledWhenBreakpointIsHit;
26+
}
27+
}
28+
public static bool IsHit(Line line, Breakpoint[] breakpoints, Interpreter interpreter)
29+
{
30+
var point = breakpoints.FirstOrDefault(x => x.Line.FilePath == line.FilePath && x.Line.CodeLine == line.CodeLine);
31+
bool hit = point != null && point.Enabled;
32+
33+
if (hit)
34+
{
35+
if (point.EZCodeConditionToHit != null)
36+
{
37+
Parser parser = new Parser(point.EZCodeConditionToHit, "Debugger");
38+
var lines = parser.Parse();
39+
object result = interpreter.GetValue(lines);
40+
if (result != null)
41+
{
42+
if (bool.TryParse(result.ToString(), out bool res))
43+
{
44+
hit = res;
45+
}
46+
else
47+
{
48+
throw new Exception($"Error From Debugger in line \"{line.CodeLine}\" The condition does not return boolean");
49+
}
50+
}
51+
else
52+
{
53+
throw new Exception($"Error From Debugger in line \"{line.CodeLine}\" The condition returns null instead of boolean");
54+
}
55+
}
56+
if (hit && point.EZCodeActionWhenHit != null)
57+
{
58+
Parser parser = new Parser(point.EZCodeActionWhenHit, "Debugger");
59+
var lines = parser.Parse();
60+
foreach (var l in lines)
61+
{
62+
interpreter.SingleLine(l);
63+
}
64+
}
65+
if (point.EnabledWhenBreakpointIsHit != null)
66+
{
67+
point.EnabledWhenBreakpointIsHit.Enabled = true;
68+
}
69+
if (point.DisableWhenHit)
70+
{
71+
point.Enabled = false;
72+
}
73+
point.Hit = hit;
74+
}
75+
76+
return hit;
77+
}
78+
}
79+
}

EZCode/EZHelp.cs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,39 @@ public EZHelp(Interpreter interpreter)
1313
Interpreter = interpreter;
1414
}
1515
public EZHelp() { }
16-
public void Print(string text)
16+
public async Task Await(object val)
1717
{
18-
text = Format(text.ToString());
18+
try
19+
{
20+
string wait = StringParse(val);
21+
if (bool.TryParse(wait, out bool res))
22+
{
23+
while (res)
24+
{
25+
res = bool.Parse(ObjectParse(val, "bool").ToString());
26+
}
27+
}
28+
else if (int.TryParse(wait, out int num))
29+
{
30+
await WaitForMiliseconds(num);
31+
}
32+
}
33+
catch (Exception e)
34+
{
35+
Error = e.ToString();
36+
throw;
37+
}
38+
}
39+
internal static async Task WaitForMiliseconds(int mililseconds)
40+
{
41+
for (int i = 0; i < mililseconds; i++)
42+
{
43+
await Task.Delay(1);
44+
}
45+
}
46+
public async void Print(string text)
47+
{
48+
text = await Format(text.ToString());
1949
Interpreter.Output = [.. Interpreter.Output, text.ToString()];
2050
Console.WriteLine(text);
2151
}
@@ -29,13 +59,13 @@ public string Input()
2959
string input = Interpreter.ConsoleInput();
3060
return input;
3161
}
32-
public string Format(object _text) => Format(_text, "'");
33-
public string Format(object _text, object _char)
62+
public async Task<string> Format(object _text) => await Format(_text, "'");
63+
public async Task<string> Format(object _text, object _char)
3464
{
3565
try
3666
{
3767
char c = char.Parse(_char.ToString());
38-
string text = ObjectParse(_text.ToString(), "str", true).ToString();
68+
string text = (await ObjectParse(_text.ToString(), "str", true)).ToString();
3969
string format = "";
4070
char[] chars = text.ToCharArray();
4171
bool open = true, backslash = false;
@@ -114,14 +144,14 @@ public string Format(object _text, object _char)
114144
}
115145
if (Interpreter.Vars.Any(x => x.Name == instance_name))
116146
{
117-
format = format.Remove(range.Start, range.Count).Insert(range.Start, Interpreter.GetValue(name, DataType.GetType("str", Interpreter.Classes)).ToString());
147+
format = format.Remove(range.Start, range.Count).Insert(range.Start, (await Interpreter.GetValue(name, DataType.GetType("str", Interpreter.Classes))).ToString());
118148
}
119149
else
120150
{
121151
Interpreter.parser.WatchIsFound([name], 0, out ExplicitWatch watch, out _);
122152

123-
object val = Interpreter.GetValue(watch != null ? watch.Runs : name, DataType.GetType("str", Interpreter.Classes));
124-
format = format.Remove(range.Start, range.Count).Insert(range.Start, Interpreter.GetValue(val, new DataType(DataType.Types._string, null)).ToString());
153+
object val = await Interpreter.GetValue(watch != null ? watch.Runs : name, DataType.GetType("str", Interpreter.Classes));
154+
format = format.Remove(range.Start, range.Count).Insert(range.Start, (await Interpreter.GetValue(val, new DataType(DataType.Types._string, null))).ToString());
125155
}
126156
}
127157

@@ -142,8 +172,8 @@ public string StringEmpty()
142172
{
143173
return string.Empty;
144174
}
145-
public object ObjectParse(object obj, object type) => ObjectParse(obj, type, false);
146-
public object ObjectParse(object obj, object type, bool to_string, string arraySeperator = " ", bool returnNull = false)
175+
public async Task<object> ObjectParse(object obj, object type) => await ObjectParse(obj, type, false);
176+
public async Task<object> ObjectParse(object obj, object type, bool to_string, string arraySeperator = " ", bool returnNull = false)
147177
{
148178
try
149179
{
@@ -160,7 +190,7 @@ public object ObjectParse(object obj, object type, bool to_string, string arrayS
160190
o = obj;
161191
DataType data = DataType.GetType(type.ToString(), Interpreter.Classes);
162192
if (Interpreter.Vars.Any(x => x.Name == n)) Interpreter.Vars.FirstOrDefault(x => x.Name == n).DataType = data;
163-
obj = Interpreter.GetValue(n, data, arraySeperator);
193+
obj = await Interpreter.GetValue(n, data, arraySeperator);
164194
} while (obj != o);
165195
}
166196
catch when(returnNull) { return null; }
@@ -462,15 +492,15 @@ public bool SameType(object a, object b)
462492
}
463493
}
464494
public int StringLength(object str) => StringParse(str).Length;
465-
public int RunEZCode(string code)
495+
public void RunEZCode(string code)
466496
{
467497
try
468498
{
469499
code = ObjectParse(code, "str").ToString();
470500
Parser parser = new Parser(string.Join(" ", code), "(instance running from inside file)");
471501
parser.Parse();
472502
Interpreter interpreter = new Interpreter(parser);
473-
return interpreter.Interperate();
503+
interpreter.Interperate();
474504
}
475505
catch (Exception e)
476506
{

0 commit comments

Comments
 (0)