Skip to content

Commit 627f6b7

Browse files
Added delay and split function + update len func
1 parent abe3694 commit 627f6b7

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

CritLang/Content/test.crit

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-

1+
texto = ReadText("./Content/ola.txt");
22

3-
path = "./Content/ola.txt";
3+
textoArr = Split(texto, " ");
44

5+
num = 0;
56

6-
text = ReadText(path);
7-
8-
9-
WriteLine(text + CritVersion);
10-
7+
while num < Len(textoArr) {
8+
9+
WriteLine(textoArr[num]);
10+
if textoArr[num] == "version:" {
11+
Write(CritVersion);
12+
}
1113

14+
num += 1;
15+
}

CritLang/CritVisitor.cs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,49 @@ public CritVisitor(string version)
2727
Variables["Sum"] = new Func<object?[], object?>(SumArr);
2828
Variables["Add"] = new Func<object?[], object?>(AddArr);
2929
Variables["Remove"] = new Func<object?[], object?>(RemoveArr);
30-
Variables["Len"] = new Func<object?[], object?>(LenArr);
3130

3231
//OS Functions
3332
Variables["ReadText"] = new Func<object?[], object?>(ReadText);
33+
//Variables["Time"] = new Func<object?[], object?>(Time);
3434

3535
//Gerenal Functions
3636
Variables["Convert"] = new Func<object?[], object?>(ConvertTo);
37+
Variables["Delay"] = new Func<object?[], object?>(Delay);
3738
Variables["CritVersion"] = _version;
39+
Variables["Split"] = new Func<object?[], object?>(Split);
40+
Variables["Len"] = new Func<object?[], object?>(Len);
3841

3942
}
4043

4144

45+
46+
public static object? Split(object?[] args)
47+
{
48+
if (args.Length != 2) throw new Exception("Delay takes 2 arguments.");
49+
object[]? ol = args[0]?.ToString()?.Split(args[1]!.ToString()!.ToCharArray());
50+
return ol?.ToList();
51+
}
52+
53+
public static object? Delay(object?[] args)
54+
{
55+
if (args.Length != 1) throw new Exception("Delay takes 1 argument");
56+
Task.Delay((int)args[0]!).Wait();
57+
return null;
58+
59+
}
60+
61+
//TODO FIX THIS
62+
//public static object? Time(object?[] args)
63+
//{
64+
// if (args.Length != 0) throw new Exception("Time takes no arguments");
65+
66+
// //TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
67+
// //double timestamp = t.TotalMilliseconds;
68+
// //return Math.Round(timestamp / 1000, 2);
69+
// //int m = DateTime.Now.Millisecond;
70+
// return DateTime.Now;
71+
//}
72+
4273
public static object? ReadText(object?[] args)
4374
{
4475
if (args.Length != 1) throw new Exception("ReadText expects 1 argument");
@@ -51,8 +82,6 @@ public CritVisitor(string version)
5182
{
5283
throw new Exception("ReadText failed: " + e.Message);
5384
}
54-
55-
5685
}
5786

5887

@@ -112,16 +141,17 @@ public CritVisitor(string version)
112141
? throw new Exception("Pow expects 2 arguments.")
113142
: (float)(Math.Pow(Convert.ToSingle(args[0]!), Convert.ToSingle(args[1]!)));
114143

115-
private static object? LenArr(object?[] args)
144+
private static object? Len(object?[] args)
116145
{
117146
if (args.Length is not 1)
118147
throw new Exception("Len expects 1 argument");
119148

120-
if (args[0] is List<object> arr)
121-
return arr.Count;
122-
123-
return new Exception("Len expects an array");
124-
149+
return args[0] switch
150+
{
151+
List<object> arr => arr.Count,
152+
string str => str.Length,
153+
_ => new Exception("Len expects an array or string")
154+
};
125155
}
126156

127157

CritLang/sieve.crit

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ while k < rounds {
4040
k += 1;
4141
}
4242
composites = rounds - primes;
43-
WriteLine("Found " + primes + " and " + composites + " composites in " + rounds + " numbers!");
44-
43+
WriteLine("Found " + primes + " and " + composites + " composites in " + rounds + " numbers!");

0 commit comments

Comments
 (0)