Skip to content

Commit c63f498

Browse files
Fix the Time function
1 parent 2e904a8 commit c63f498

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

CritLang/Content/test.crit

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

22

33

4+
start = Time()
45

5-
6-
7-
ola = 34076580437543805743058347540383407658043754380574305834754038340765804375438057430583475403834076580437543805743058347540383407658043754380574305834754038 #BigInteger
8-
9-
num1 = ola + 69
10-
WriteLine(Convert(ola, "string");
11-
6+
WriteLine(start)
7+
Delay(1000)
8+
WriteLine(Time() - start)
129

1310

CritLang/CritVisitor.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public CritVisitor(string version)
3030

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

3535
//Gerenal Functions
3636
Variables["Convert"] = new Func<object?[], object?>(ConvertTo);
@@ -65,18 +65,16 @@ public CritVisitor(string version)
6565

6666
}
6767

68-
//TODO FIX THIS; UPDATE SHOULD NOW BE POSSIBLE WITH THE NEW BIG NUMBERS
69-
//public static object? Time(object?[] args)
70-
//{
71-
// if (args.Length != 0) throw new Exception("Time takes no arguments");
68+
69+
public static object? Time(object?[] args)
70+
{
71+
if (args.Length != 0) throw new Exception("Time takes no arguments");
72+
73+
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
74+
double timestamp = t.TotalMilliseconds;
75+
return Math.Round(timestamp / 1000, 2);
76+
}
7277

73-
// //TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
74-
// //double timestamp = t.TotalMilliseconds;
75-
// //return Math.Round(timestamp / 1000, 2);
76-
// //int m = DateTime.Now.Millisecond;
77-
// return DateTime.Now;
78-
//}
79-
8078
public static object? ReadText(object?[] args)
8179
{
8280
if (args.Length != 1) throw new Exception("ReadText expects 1 argument");
@@ -98,19 +96,19 @@ public CritVisitor(string version)
9896
throw new Exception("ConvertTo expects 2 arguments, first one the variable to convert to and the second one being the type.");
9997

10098

101-
string TypeToConvertTo = args[1]!.ToString()!;
99+
string typeToConvertTo = args[1]!.ToString()!;
102100

103101
string[] typeOptions = { "int", "float", "string", "bool" };
104-
if (!typeOptions.Contains(TypeToConvertTo))
102+
if (!typeOptions.Contains(typeToConvertTo))
105103
throw new Exception($"Invalid type...\nMust be one of the following types: {string.Join(", ", typeOptions)}");
106104

107-
object? ValueToConvert = args[0]!;
105+
object? valueToConvert = args[0]!;
108106

109-
return ValueToConvert switch
107+
return valueToConvert switch
110108
{
111-
string => Convert.ToString(ValueToConvert),
112-
bool => Convert.ToBoolean(ValueToConvert),
113-
_ => TypeDispatcher(ValueToConvert)
109+
string => Convert.ToString(valueToConvert),
110+
bool => Convert.ToBoolean(valueToConvert),
111+
_ => TypeDispatcher(valueToConvert)
114112
};
115113
}
116114

CritLang/sieve.crit

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
start = Time()
12
total = [];
23

34
rounds = 25000;
@@ -40,4 +41,6 @@ while k < rounds {
4041
k += 1;
4142
}
4243
composites = rounds - primes;
43-
WriteLine("Found " + primes + " and " + composites + " composites in " + rounds + " numbers!");
44+
end = Time()
45+
total_time = end - start
46+
WriteLine("Found " + primes + " and " + composites + " composites in " + rounds + " numbers! " + " Time: " + total_time + " seconds");

0 commit comments

Comments
 (0)