Skip to content

Commit 216bbb3

Browse files
Added some functions for arrays and sieve example
1 parent dcdc165 commit 216bbb3

File tree

5 files changed

+139
-70
lines changed

5 files changed

+139
-70
lines changed

CritLang/Content/test.crit

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
ola = [1, "dois", 3.0];
1+
ola = [1, "dois", 3, "quatro"];
22

3-
num = 0;
4-
while num < 2 {
5-
WriteLine(ola[num]);
6-
num = num + 1;
7-
}
83

4+
WriteLine(ola);
95

6+
Remove(ola, 1);
7+
8+
WriteLine("----------------");
9+
WriteLine(ola);
10+
11+
12+
Add(ola, 69.420);
13+
14+
WriteLine("----------------");
15+
WriteLine(ola);

CritLang/CritVisitor.cs

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,58 @@ public CritVisitor()
1414
//GLOBAL FUNCTIONs / VARIABLES
1515
Variables["PI"] = Math.PI;
1616
Variables["Sqrt"] = new Func<object?[], object?>(Sqrt);
17+
Variables["Pow"] = new Func<object?[], object?>(Pow);
1718

1819
Variables["Write"] = new Func<object?[], object?>(Write);
1920
Variables["WriteLine"] = new Func<object?[], object?>(WriteLine);
2021
Variables["Sum"] = new Func<object?[], object?>(SumArr);
2122
Variables["Add"] = new Func<object?[], object?>(AddArr);
23+
Variables["Remove"] = new Func<object?[], object?>(RemoveArr);
24+
Variables["Len"] = new Func<object?[], object?>(LenArr);
2225
}
2326

2427

28+
private object? RemoveArr(object?[] args)
29+
{
30+
if (args.Length != 2)
31+
{
32+
throw new Exception("Remove expects 2 arguments, first one the array and the second ono the index.");
33+
}
34+
35+
if (args[0] is List<object> objArr)
36+
{
37+
objArr.RemoveAt((int)args[1]!);
38+
}
39+
40+
return null;
41+
}
2542

2643

44+
private static object? Pow(object?[] args) => args.Length is not 2
45+
? throw new Exception("Pow expects 2 arguments.")
46+
: (float)(Math.Pow(Convert.ToSingle(args[0]!), Convert.ToSingle(args[1]!)));
47+
48+
private static object? LenArr(object?[] args)
49+
{
50+
if (args.Length is not 1)
51+
throw new Exception("Len expects 1 argument");
52+
53+
if (args[0] is List<object> arr)
54+
return arr.Count;
55+
56+
return new Exception("Len expects an array");
57+
58+
}
59+
2760

2861
private static object? AddArr(object?[] args)
2962
{
3063
if (args.Length is not 2)
31-
throw new Exception("Add expects 2 argument");
64+
throw new Exception("Add expects 2 argument, the first one being the array and the second one being the index.");
3265

33-
if (args[0] is object[] objArr)
66+
if (args[0] is List<object> objArr)
3467
{
35-
Console.WriteLine(objArr);
68+
objArr.Add(args[1]!);
3669
}
3770

3871
return null;
@@ -44,7 +77,7 @@ public CritVisitor()
4477
if (args.Length is not 1)
4578
throw new Exception("Sum expects 1 argument");
4679

47-
if (args[0] is object[] objArr)
80+
if (args[0] is List<object> objArr)
4881
return (float)objArr.Sum(Convert.ToDouble);
4982

5083
throw new Exception("Sum: Argument is not a valid array.");
@@ -60,8 +93,8 @@ public CritVisitor()
6093

6194
return arg[0] switch
6295
{
63-
int d => Math.Sqrt(Convert.ToDouble(d)),
64-
float f => Math.Sqrt(f),
96+
int d => Convert.ToInt32(Math.Sqrt(Convert.ToDouble(d))),
97+
float f => Convert.ToSingle(Math.Sqrt(f)),
6598
_ => throw new Exception("Sqrt takes one integer ot float argument")
6699
};
67100
}
@@ -72,7 +105,7 @@ public CritVisitor()
72105
{
73106
foreach (var arg in args)
74107
{
75-
if (arg is object[] objArr)
108+
if (arg is List<object> objArr)
76109
{
77110
foreach (var obj in objArr)
78111
Console.Write(obj);
@@ -87,7 +120,7 @@ public CritVisitor()
87120
{
88121
foreach (var arg in args)
89122
{
90-
if (arg is object[] objArr)
123+
if (arg is List<object> objArr)
91124
{
92125
foreach (var obj in objArr)
93126
Console.WriteLine(obj);
@@ -141,17 +174,34 @@ public CritVisitor()
141174

142175
if (varName.Contains('[') && varName.Contains(']'))
143176
{
144-
string varWithoutIndex = varName.Replace("[", string.Empty).Replace("]", string.Empty);
145-
char index = varWithoutIndex[^1];
177+
string[] variableHelper = varName.Replace("]", string.Empty).Split('[');
178+
string varWithoutIndex = variableHelper[0];
179+
string index = variableHelper[1];
146180
//Variables[varWithoutIndex[..^1]]?[int.Parse(index.ToString())] = value;
147181
//Console.WriteLine(Variables[varWithoutIndex[..^1]]?[int.Parse(index.ToString())]);
148-
var variable = Variables[varWithoutIndex[..^1]];
149-
if (variable is not object[] vO) return null;
150-
foreach (var ola in vO)
182+
var variable = Variables[varWithoutIndex];
183+
if (variable is not List<object> vO) return null;
184+
//foreach (var ola in vO)
185+
//{
186+
// Console.WriteLine(ola);
187+
//}
188+
try
189+
{
190+
if (int.TryParse(index, out int intIndex))
191+
{
192+
vO[intIndex] = value!;
193+
}
194+
else if (Variables.ContainsKey(varWithoutIndex))
195+
{
196+
var varValue = Variables[index];
197+
return varValue is not null ? vO[(int)Math.Round(Convert.ToSingle(varValue), 0)] = varValue : throw new Exception("Index not valid.");
198+
}
199+
}
200+
catch (ArgumentOutOfRangeException)
151201
{
152-
Console.WriteLine(ola);
202+
vO.Add(value!);
153203
}
154-
vO[int.Parse(index.ToString())] = value!;
204+
155205
}
156206
else
157207
{
@@ -178,13 +228,13 @@ public CritVisitor()
178228
var variable = Variables[varWithoutIndex];
179229
if (int.TryParse(index, out _))
180230
{
181-
if (variable is object[] vO)
231+
if (variable is List<object> vO)
182232
return vO[int.Parse(index)];
183233
}
184234
else if (Variables.ContainsKey(varWithoutIndex))
185235
{
186236
var value = Variables[index];
187-
if (variable is object[] vO)
237+
if (variable is List<object> vO)
188238
return vO[int.Parse(value!.ToString() ?? throw new Exception("Index is not a number"))];
189239
}
190240
else
@@ -204,6 +254,7 @@ public CritVisitor()
204254

205255
public override object? VisitConstant(CritParser.ConstantContext context)
206256
{
257+
207258
if (context.INTEGER() is { } i)
208259
return int.Parse((i.GetText()));
209260

@@ -220,25 +271,22 @@ public CritVisitor()
220271
if (context.array() is { } a)
221272
{
222273
string[] strArr = a.GetText()[1..^1].Split(',');
223-
object[] anyArr = new object[strArr.Length];
224-
//var anyLst = new List<object>
225-
//{
226-
// Capacity = strArr.Length
227-
//};
228-
int index = 0;
274+
var anyLst = new List<object>();
275+
if (strArr.Length <= 1)
276+
return anyLst;
277+
278+
279+
280+
229281
foreach (string element in strArr)
230282
{
231283
if (element.StartsWith('"') && element.EndsWith('"'))
232-
anyArr[index] = element[1..^1];
233-
//anyLst.Add(element[1..^1]);
284+
anyLst.Add(element[1..^1]);
234285

235-
//else if (element.StartsWith('[') && element.EndsWith(']'))
236-
// throw new Exception("Array indexing is not implemented");
237286
else
238-
anyArr[index] = int.TryParse(element, out int outi) ? outi : float.Parse(element, CultureInfo.InvariantCulture);
239-
index++;
287+
anyLst.Add(int.TryParse(element, out int outi) ? outi : float.Parse(element, CultureInfo.InvariantCulture));
240288
}
241-
return anyArr;
289+
return anyLst;
242290
}
243291

244292
if (context.NULL() is { })

CritLang/primes.crit

Lines changed: 0 additions & 31 deletions
This file was deleted.

CritLang/sieve.crit

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
total = [];
2+
3+
rounds = 25000;
4+
5+
6+
i = 0;
7+
8+
9+
while i < rounds {
10+
total[i] = true;
11+
i = i + 1;
12+
}
13+
14+
#start of sieve
15+
16+
17+
num = 2;
18+
19+
20+
while num < Sqrt(rounds) {
21+
22+
if total[num] == true {
23+
j = Pow(num, 2);
24+
25+
while j < rounds {
26+
total[j] = false;
27+
j = j + num;
28+
}
29+
}
30+
num = num + 1;
31+
}
32+
33+
34+
primes = 0;
35+
k = 2;
36+
while k < rounds {
37+
if total[k] == true {
38+
primes = primes + 1;
39+
}
40+
k = k + 1;
41+
}
42+
composites = rounds - primes;
43+
WriteLine("Found " + primes + " and " + composites + " composites in " + rounds + " numbers!");
44+

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ else {
1818
WriteLine(num + " is smaller than 2.");
1919
}
2020

21-
while num < 10 {
21+
while num < 10 {
2222
WriteLine(num);
2323
num = num + 1;
2424
}
2525
else {
2626
WriteLine("num was already bigger than 10.");
2727
}
2828
```
29-
You can look [here](CritLang/primes.crit) for more "real world" example.
29+
You can look [here](CritLang/sieve.crit) for more an implemantion of the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) in crit!
3030

3131
## Tips and Tricks
3232
You can have a else block after a while loop declaration to avoid a if statement.
@@ -42,8 +42,10 @@ until num > 10 {
4242
## TODOs
4343

4444
- [ ] Add arrays, python-like dictionaries and some more basic data structures.
45-
- [ ] Add a special block of code to query with [Nanql](https://github.com/lucascompython/NANQL).
45+
- [ ] Add a special block of code to query with [NANQL](https://github.com/lucascompython/NANQL).
4646
- [ ] Add the hability of making functions.
47+
- [ ] Documentation.
48+
- [ ] Add the hability of importing other files.
4749
- [ ] Make a proper std lib.
4850
- [ ] Add for loops.
4951
- [ ] Add local variables.

0 commit comments

Comments
 (0)