Skip to content

Commit dcdc165

Browse files
Add array indexing
1 parent aeeab87 commit dcdc165

File tree

4 files changed

+88
-14
lines changed

4 files changed

+88
-14
lines changed

CritLang/Content/Crit.g4

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ assignment: IDENTIFIER '=' expression;
2020
functionCall: IDENTIFIER '(' (expression (',' expression)*)? ')';
2121

2222
expression
23-
: constant (index)* #constantExpression
23+
: constant #constantExpression
2424
| IDENTIFIER #identifierExpression
2525
| functionCall #functionCallExpression
2626
| '(' expression ')' #parenthesizedExpression
@@ -41,20 +41,20 @@ boolOp: BOOL_OPERATOR;
4141
BOOL_OPERATOR: 'and' | 'or' | 'xor';
4242

4343
constant: INTEGER | FLOAT | STRING | BOOL | array | NULL;
44-
44+
//arrayIndex: constant '[' INTEGER ']';
4545
INTEGER: [0-9]+;
4646
FLOAT: [0-9]+ '.' [0-9]+;
4747
STRING: ('"' ~'"'* '"') | ('\'' ~'\''* '\'');
4848
BOOL: 'true' | 'false';
4949
array: '[' (constant (',' constant)*)? ']';
50-
index: '[' INTEGER ']';
50+
//index: '[' INTEGER ']';
5151
NULL: 'null';
5252

5353

5454
block: '{' line* '}';
5555

5656
WS: [ \t\r\n]+ -> skip;
57-
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]*;
57+
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]* ('[' IDENTIFIER ']' | '[' INTEGER ']')* ;
5858

5959
Comment
6060
: '#' ~( '\r' | '\n' )*

CritLang/Content/test.crit

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
1+
ola = [1, "dois", 3.0];
2+
3+
num = 0;
4+
while num < 2 {
5+
WriteLine(ola[num]);
6+
num = num + 1;
7+
}
28

3-
WriteLine(Sqrt(Sum(arr))) # 7.41619
49

CritLang/CritVisitor.cs

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,30 @@ public CritVisitor()
1818
Variables["Write"] = new Func<object?[], object?>(Write);
1919
Variables["WriteLine"] = new Func<object?[], object?>(WriteLine);
2020
Variables["Sum"] = new Func<object?[], object?>(SumArr);
21+
Variables["Add"] = new Func<object?[], object?>(AddArr);
2122
}
2223

2324

2425

2526

2627

28+
private static object? AddArr(object?[] args)
29+
{
30+
if (args.Length is not 2)
31+
throw new Exception("Add expects 2 argument");
32+
33+
if (args[0] is object[] objArr)
34+
{
35+
Console.WriteLine(objArr);
36+
}
37+
38+
return null;
39+
}
40+
2741

2842
private static object? SumArr(object?[] args)
2943
{
30-
if (args.Length is 0 or not 1)
44+
if (args.Length is not 1)
3145
throw new Exception("Sum expects 1 argument");
3246

3347
if (args[0] is object[] objArr)
@@ -36,7 +50,7 @@ public CritVisitor()
3650
throw new Exception("Sum: Argument is not a valid array.");
3751
}
3852

39-
53+
4054

4155
private static object? Sqrt(object?[] arg)
4256
{
@@ -92,9 +106,11 @@ public CritVisitor()
92106

93107
//public override object? VisitConstantExpression(CritParser.ConstantExpressionContext context)
94108
//{
95-
// throw new NotImplementedException("Array indexing is not implemented");
109+
// return base.VisitConstantExpression(context);
96110
//}
97111

112+
113+
98114

99115
public override object? VisitFunctionCall(CritParser.FunctionCallContext context)
100116
{
@@ -106,7 +122,7 @@ public CritVisitor()
106122

107123

108124

109-
if (!(Variables[name] is Func<object?[], object?> func))
125+
if (Variables[name] is not Func<object?[], object?> func)
110126
throw new Exception($"Function {name} is not a function");
111127

112128

@@ -123,8 +139,24 @@ public CritVisitor()
123139

124140
var value = Visit(context.expression());
125141

126-
127-
Variables[varName] = value;
142+
if (varName.Contains('[') && varName.Contains(']'))
143+
{
144+
string varWithoutIndex = varName.Replace("[", string.Empty).Replace("]", string.Empty);
145+
char index = varWithoutIndex[^1];
146+
//Variables[varWithoutIndex[..^1]]?[int.Parse(index.ToString())] = value;
147+
//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)
151+
{
152+
Console.WriteLine(ola);
153+
}
154+
vO[int.Parse(index.ToString())] = value!;
155+
}
156+
else
157+
{
158+
Variables[varName] = value;
159+
}
128160

129161

130162
return null;
@@ -134,11 +166,36 @@ public CritVisitor()
134166
public override object? VisitIdentifierExpression(CritParser.IdentifierExpressionContext context)
135167
{
136168
var varName = context.IDENTIFIER().GetText();
169+
170+
if (varName.Contains('[') && varName.Contains(']'))
171+
{
172+
173+
string[] variableHelper = varName.Replace("]", string.Empty).Split('[');
174+
string varWithoutIndex = variableHelper[0];
175+
string index = variableHelper[1];
176+
177+
178+
var variable = Variables[varWithoutIndex];
179+
if (int.TryParse(index, out _))
180+
{
181+
if (variable is object[] vO)
182+
return vO[int.Parse(index)];
183+
}
184+
else if (Variables.ContainsKey(varWithoutIndex))
185+
{
186+
var value = Variables[index];
187+
if (variable is object[] vO)
188+
return vO[int.Parse(value!.ToString() ?? throw new Exception("Index is not a number"))];
189+
}
190+
else
191+
{
192+
throw new Exception($"Variable {varWithoutIndex} not found");
193+
}
194+
}
137195

138196
if (!Variables.ContainsKey(varName))
139-
{
140197
throw new Exception($"Variable '{varName}' is not defined");
141-
}
198+
142199

143200
return Variables[varName];
144201

@@ -159,15 +216,22 @@ public CritVisitor()
159216
if (context.BOOL() is { } b)
160217
return b.GetText() == "true";
161218

219+
162220
if (context.array() is { } a)
163221
{
164222
string[] strArr = a.GetText()[1..^1].Split(',');
165223
object[] anyArr = new object[strArr.Length];
224+
//var anyLst = new List<object>
225+
//{
226+
// Capacity = strArr.Length
227+
//};
166228
int index = 0;
167229
foreach (string element in strArr)
168230
{
169231
if (element.StartsWith('"') && element.EndsWith('"'))
170232
anyArr[index] = element[1..^1];
233+
//anyLst.Add(element[1..^1]);
234+
171235
//else if (element.StartsWith('[') && element.EndsWith(']'))
172236
// throw new Exception("Array indexing is not implemented");
173237
else

CritLang/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
Console.WriteLine("Usage: critlang [file]");
1111
Environment.Exit(0);
1212
}
13+
else if (new[] { "-v", "--version" }.Contains(arg))
14+
{
15+
Console.WriteLine("CritLang v0.1.2-beta");
16+
Environment.Exit(0);
17+
}
1318
else if (arg.StartsWith("-"))
1419
{
1520
Console.WriteLine("Unknown option: " + arg);

0 commit comments

Comments
 (0)