@@ -17,33 +17,39 @@ public CritVisitor()
1717
1818 Variables [ "Write" ] = new Func < object ? [ ] , object ? > ( Write ) ;
1919 Variables [ "WriteLine" ] = new Func < object ? [ ] , object ? > ( WriteLine ) ;
20+ Variables [ "Sum" ] = new Func < object ? [ ] , object ? > ( SumArr ) ;
2021 }
2122
2223
2324
2425
2526
2627
28+ private static object ? SumArr ( object ? [ ] args )
29+ {
30+ if ( args . Length is 0 or not 1 )
31+ throw new Exception ( "Sum expects 1 argument" ) ;
32+
33+ if ( args [ 0 ] is object [ ] objArr )
34+ return ( float ) objArr . Sum ( Convert . ToDouble ) ;
35+
36+ throw new Exception ( "Sum: Argument is not a valid array." ) ;
37+ }
38+
39+
2740
2841 private static object ? Sqrt ( object ? [ ] arg )
2942 {
3043
3144 if ( arg . Length != 1 )
32- {
3345 throw new Exception ( "Sqrt takes one argument" ) ;
34- }
35-
3646
37- if ( arg [ 0 ] is int d )
47+ return arg [ 0 ] switch
3848 {
39- return Math . Sqrt ( Convert . ToDouble ( d ) ) ;
40- }
41- if ( arg [ 0 ] is float f )
42- {
43- return Math . Sqrt ( f ) ;
44- }
45-
46- throw new Exception ( "Sqrt takes one integer ot float argument" ) ;
49+ int d => Math . Sqrt ( Convert . ToDouble ( d ) ) ,
50+ float f => Math . Sqrt ( f ) ,
51+ _ => throw new Exception ( "Sqrt takes one integer ot float argument" )
52+ } ;
4753 }
4854
4955
@@ -52,19 +58,29 @@ public CritVisitor()
5258 {
5359 foreach ( var arg in args )
5460 {
61+ if ( arg is object [ ] objArr )
62+ {
63+ foreach ( var obj in objArr )
64+ Console . Write ( obj ) ;
65+ continue ;
66+ }
5567 Console . Write ( arg ) ;
5668 }
57-
5869 return null ;
5970 }
6071
6172 private static object ? WriteLine ( object ? [ ] args )
6273 {
6374 foreach ( var arg in args )
6475 {
76+ if ( arg is object [ ] objArr )
77+ {
78+ foreach ( var obj in objArr )
79+ Console . WriteLine ( obj ) ;
80+ continue ;
81+ }
6582 Console . WriteLine ( arg ) ;
6683 }
67-
6884 return null ;
6985 }
7086
@@ -135,18 +151,31 @@ public CritVisitor()
135151 return int . Parse ( ( i . GetText ( ) ) ) ;
136152
137153 if ( context . FLOAT ( ) is { } f )
138- {
139154 return float . Parse ( f . GetText ( ) , CultureInfo . InvariantCulture ) ;
140- }
141-
155+
142156 if ( context . STRING ( ) is { } s )
143157 return s . GetText ( ) [ 1 ..^ 1 ] ;
144158
145159 if ( context . BOOL ( ) is { } b )
146160 return b . GetText ( ) == "true" ;
147161
148162 if ( context . array ( ) is { } a )
149- return a . GetText ( ) [ 1 ..^ 1 ] . Split ( ',' ) ;
163+ {
164+ string [ ] strArr = a . GetText ( ) [ 1 ..^ 1 ] . Split ( ',' ) ;
165+ object [ ] anyArr = new object [ strArr . Length ] ;
166+ int index = 0 ;
167+ foreach ( string element in strArr )
168+ {
169+ if ( element . StartsWith ( '"' ) && element . EndsWith ( '"' ) )
170+ anyArr [ index ] = element [ 1 ..^ 1 ] ;
171+ //else if (element.StartsWith('[') && element.EndsWith(']'))
172+ // throw new Exception("Array indexing is not implemented");
173+ else
174+ anyArr [ index ] = int . TryParse ( element , out int outi ) ? outi : float . Parse ( element , CultureInfo . InvariantCulture ) ;
175+ index ++ ;
176+ }
177+ return anyArr ;
178+ }
150179
151180 if ( context . NULL ( ) is { } )
152181 return null ;
@@ -192,15 +221,15 @@ public CritVisitor()
192221
193222
194223 private static object ? Add ( object ? left , object ? right ) => ( left , right ) switch
195- {
196- ( int l , int r ) => l + r ,
197- ( float lf , float rf ) => lf + rf ,
198- ( int lInt , float rf ) => lInt + rf ,
199- ( float lf , int rInt ) => lf + rInt ,
200- ( string , _) => $ "{ left } { right } ",
201- ( _, string ) => $ "{ left } { right } ",
202- ( _, _) => throw new Exception ( $ "Cannot add values of type { left ? . GetType ( ) } and { right ? . GetType ( ) } .")
203- } ;
224+ {
225+ ( int l , int r ) => l + r ,
226+ ( float lf , float rf ) => lf + rf ,
227+ ( int lInt , float rf ) => lInt + rf ,
228+ ( float lf , int rInt ) => lf + rInt ,
229+ ( string , _) => $ "{ left } { right } ",
230+ ( _, string ) => $ "{ left } { right } ",
231+ ( _, _) => throw new Exception ( $ "Cannot add values of type { left ? . GetType ( ) } and { right ? . GetType ( ) } .")
232+ } ;
204233
205234
206235 private static object ? Subtract ( object ? left , object ? right ) => ( left , right ) switch
@@ -214,13 +243,13 @@ public CritVisitor()
214243
215244
216245 private static object ? Multiply ( object ? left , object ? right ) => ( left , right ) switch
217- {
218- ( int l , int r ) => l * r ,
219- ( float lf , float rf ) => lf * rf ,
220- ( int lInt , float rf ) => lInt * rf ,
221- ( float lf , int rInt ) => lf * rInt ,
222- ( _, _) => throw new Exception ( $ "Cannot multiply values of type { left ? . GetType ( ) } and { right ? . GetType ( ) } .")
223- } ;
246+ {
247+ ( int l , int r ) => l * r ,
248+ ( float lf , float rf ) => lf * rf ,
249+ ( int lInt , float rf ) => lInt * rf ,
250+ ( float lf , int rInt ) => lf * rInt ,
251+ ( _, _) => throw new Exception ( $ "Cannot multiply values of type { left ? . GetType ( ) } and { right ? . GetType ( ) } .")
252+ } ;
224253
225254 private static object ? Divide ( object ? left , object ? right ) => ( left , right ) switch
226255 {
0 commit comments