Skip to content

Commit 7b8ea9f

Browse files
Added Time Package
1 parent dc5d27c commit 7b8ea9f

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

EZCode/EZHelp.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using static EZCodeLanguage.Interpreter;
33
using System.Data;
44
using System.Diagnostics;
5+
using System.Timers;
6+
using Timer = System.Timers.Timer;
57

68
namespace EZCodeLanguage
79
{
@@ -251,7 +253,7 @@ public bool Expression(string expression)
251253
}
252254
continue;
253255
}
254-
if ((new[] { "&", "|", "and", "or", "&&", "||" }).Any(x => x == e))
256+
if ((new[] { "&", "|", "and", "not", "or", "&&", "||" }).Any(x => x == e))
255257
{
256258
allIsText = false;
257259
continue;
@@ -667,6 +669,11 @@ public int RandomNumber(object _min, object _max)
667669
int max = IntParse(_max);
668670
return random.Next(min, max);
669671
}
672+
public int RoundToInt(object _num)
673+
{
674+
var num = FloatParse(_num);
675+
return (int)Math.Round(num);
676+
}
670677
public string Trim(object text) => StringParse(text).Trim();
671678
public string ToLower(object text) => StringParse(text).ToLower();
672679
public string ToUpper(object text) => StringParse(text).ToUpper();
@@ -1139,15 +1146,34 @@ public void StopwatchEnd(object _stopwatch)
11391146
Stopwatch stopwatch = (Stopwatch)ObjectParse(_stopwatch, "stopwatch");
11401147
stopwatch.Stop();
11411148
}
1142-
public float StopwatchElapsedSeconds(object _stopwatch)
1143-
{
1144-
Stopwatch stopwatch = (Stopwatch)ObjectParse(_stopwatch, "stopwatch");
1145-
return (float)stopwatch.Elapsed.TotalSeconds;
1149+
public float StopwatchElapsedNanoseconds(object _stopwatch) => (float)(ObjectParse(_stopwatch, "stopwatch") as Stopwatch).Elapsed.TotalNanoseconds;
1150+
public float StopwatchElapsedMiliseconds(object _stopwatch) => (float)(ObjectParse(_stopwatch, "stopwatch") as Stopwatch).Elapsed.TotalMilliseconds;
1151+
public float StopwatchElapsedSeconds(object _stopwatch) => (float)(ObjectParse(_stopwatch, "stopwatch") as Stopwatch).Elapsed.TotalSeconds;
1152+
public float StopwatchElapsedMinutes(object _stopwatch) => (float)(ObjectParse(_stopwatch, "stopwatch") as Stopwatch).Elapsed.TotalMinutes;
1153+
public float StopwatchElapsedHours(object _stopwatch) => (float)(ObjectParse(_stopwatch, "stopwatch") as Stopwatch).Elapsed.TotalHours;
1154+
public Timer TimerNewInstance(object _hours, object _minutes, object _seconds, object _milliseconds)
1155+
{
1156+
var hours = IntParse(_hours);
1157+
var minutes = IntParse(_minutes);
1158+
var seconds = IntParse(_seconds);
1159+
var miliseconds = IntParse(_milliseconds);
1160+
1161+
var timespan = new TimeSpan(0, hours, minutes, seconds, miliseconds);
1162+
var timer = new Timer(timespan);
1163+
1164+
timer.Elapsed += Timer_Elapsed;
1165+
_Timers.Add(timer, false);
1166+
1167+
return timer;
11461168
}
1147-
public float StopwatchElapsedMiliseconds(object _stopwatch)
1169+
public void TimerStart(object _timer) => (ObjectParse(_timer, "timer") as Timer).Start();
1170+
public void TimerStop(object _timer) => (ObjectParse(_timer, "timer") as Timer).Stop();
1171+
public bool TimerIsDone(object _timer) => _Timers[ObjectParse(_timer, "timer") as Timer];
1172+
private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
11481173
{
1149-
Stopwatch stopwatch = (Stopwatch)ObjectParse(_stopwatch, "stopwatch");
1150-
return (float)stopwatch.Elapsed.TotalMilliseconds;
1174+
var timer = (Timer)sender;
1175+
_Timers[timer] = true;
11511176
}
1177+
public Dictionary<Timer, bool> _Timers = [];
11521178
}
11531179
}

EZCode/Interpreter.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,9 @@ public void Interperate(LineWithTokens[] LineTokens)
474474
string[] all_parts = all.Split(',');
475475
if (all_parts.Length > method.Parameters.Select(x => x.Required).ToArray().Length && !method.Parameters.Any(x => x.IsParams))
476476
throw new Exception($"Expects {(method.Parameters.Any(x => x.Required) ? "at least" : "")} {(method.Parameters.Any(x => x.Required) ? method.Parameters.Select(x => x.Required).ToArray().Length : method.Parameters.Length)} parameter for method \"{method.Name}\" but {all_parts.Length} were given");
477-
if (method.Parameters.Any(x => x.IsParams))
478-
{
479-
string[] new_parts = [];
480-
for (int j = 0; j < method.Parameters.Length; j++)
481-
{
482-
if (method.Parameters[j].IsParams)
483-
{
484-
new_parts = [.. new_parts, string.Join(",", all_parts.Skip(j)).Replace(" ,", ",")];
485-
break;
486-
}
487-
new_parts = [.. new_parts, all_parts[j]];
488-
}
489-
all_parts = new_parts;
490-
}
477+
478+
all_parts = GetIsParamParameters(all_parts, method);
479+
491480
vals = all_parts.Select(x => x.Trim()).Select( (selectValue, selectIndex) =>
492481
selectValue.StartsWith("@:|") && selectValue.EndsWith("{}") ?
493482
line.Tokens[int.Parse( selectValue.Substring(3, selectValue.Length - 5)) ].Value :
@@ -849,6 +838,24 @@ public void Interperate(LineWithTokens[] LineTokens)
849838
throw new Exception(e.Message, e);
850839
}
851840
}
841+
private string[] GetIsParamParameters(string[] all_parts, Method method)
842+
{
843+
if (method.Parameters.Any(x => x.IsParams))
844+
{
845+
string[] new_parts = [];
846+
for (int j = 0; j < method.Parameters.Length; j++)
847+
{
848+
if (method.Parameters[j].IsParams)
849+
{
850+
new_parts = [.. new_parts, string.Join(",", all_parts.Skip(j)).Replace(" ,", ",")];
851+
break;
852+
}
853+
new_parts = [.. new_parts, all_parts[j]];
854+
}
855+
all_parts = new_parts;
856+
}
857+
return all_parts;
858+
}
852859
private object? RunStatement(Statement statement, out bool broke)
853860
{
854861
statement = new Statement(statement.Type, statement.Line, statement.InBrackets.Select(x => new LineWithTokens(x.Tokens.Select(y => new Token(y.Type, y.Value, y.StringValue)).ToArray(), x.Line)).ToArray());

TestEnv/Code.ezcode

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
include main, time
22

33
method start {
4-
stopwatch clock new
5-
clock start
4+
print the timer started
65

7-
int i new : 0
8-
loop 10 {
9-
i + 1
10-
print 'i'
6+
timer time new : seconds:5
7+
time start
8+
9+
stopwatch stop new
10+
stop start
11+
12+
loop time is-not-done {
13+
float i new => stop elapsed-seconds
14+
print i
1115
}
12-
dispose i
1316

14-
clock end
15-
print 'clock elapsed-seconds'
17+
print the timer ended
1618
}

0 commit comments

Comments
 (0)