@@ -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
0 commit comments