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