11using System . Globalization ;
2+ using System . Reflection . Metadata ;
23using CritLang . Content ;
34
45namespace CritLang ;
@@ -11,20 +12,65 @@ public class CritVisitor: CritBaseVisitor<object?>
1112
1213 public CritVisitor ( )
1314 {
14- //GLOBAL FUNCTIONs / VARIABLES
15+ //Math Functions
1516 Variables [ "PI" ] = Math . PI ;
1617 Variables [ "Sqrt" ] = new Func < object ? [ ] , object ? > ( Sqrt ) ;
1718 Variables [ "Pow" ] = new Func < object ? [ ] , object ? > ( Pow ) ;
1819
20+ //Console Functions
1921 Variables [ "Write" ] = new Func < object ? [ ] , object ? > ( Write ) ;
2022 Variables [ "WriteLine" ] = new Func < object ? [ ] , object ? > ( WriteLine ) ;
23+ Variables [ "ReadLine" ] = new Func < object ? [ ] , object ? > ( ReadLine ) ;
24+
25+ //Array Functions
2126 Variables [ "Sum" ] = new Func < object ? [ ] , object ? > ( SumArr ) ;
2227 Variables [ "Add" ] = new Func < object ? [ ] , object ? > ( AddArr ) ;
2328 Variables [ "Remove" ] = new Func < object ? [ ] , object ? > ( RemoveArr ) ;
2429 Variables [ "Len" ] = new Func < object ? [ ] , object ? > ( LenArr ) ;
30+
31+
32+ //Gerenal Functions
33+ Variables [ "Convert" ] = new Func < object ? [ ] , object ? > ( ConvertTo ) ;
34+
2535 }
2636
2737
38+ public static object ? ConvertTo ( object ? [ ] args )
39+ {
40+ if ( args . Length != 2 )
41+ {
42+ throw new Exception ( "ConvertTo expects 2 arguments, first one the variable to convert to and the second one being the type." ) ;
43+ }
44+
45+ string [ ] typeOptions = new [ ] { "int" , "float" , "string" , "bool" } ;
46+ if ( ! typeOptions . Contains ( args [ 1 ] ! . ToString ( ) ! ) )
47+ throw new Exception ( $ "Invalid type...\n Must be one of the following types: { string . Join ( ", " , typeOptions ) } ") ;
48+
49+ return args [ 1 ] ! . ToString ( ) switch
50+ {
51+ "int" => Convert . ToInt32 ( args [ 0 ] ) ,
52+ "float" => Convert . ToSingle ( args [ 0 ] ) ,
53+ "string" => Convert . ToString ( args [ 0 ] ) ,
54+ "bool" => Convert . ToBoolean ( args [ 0 ] ) ,
55+ _ => throw new Exception ( "Invalid type..." )
56+ } ;
57+ }
58+
59+
60+ private static object ? ReadLine ( object ? [ ] args )
61+ {
62+ if ( args . Length != 1 )
63+ {
64+ throw new Exception ( "ReadLine expects 1 arguments, being the text to prompt to the use." ) ;
65+ }
66+
67+ string text = args [ 0 ] ! . ToString ( ) ! ;
68+ Console . Write ( text ) ;
69+ return Console . ReadLine ( ) ;
70+
71+ }
72+
73+
2874 private static object ? RemoveArr ( object ? [ ] args )
2975 {
3076 if ( args . Length != 2 )
@@ -251,10 +297,8 @@ public CritVisitor()
251297 public override object ? VisitIdentifierExpression ( CritParser . IdentifierExpressionContext context )
252298 {
253299 var varName = context . IDENTIFIER ( ) . GetText ( ) ;
254-
255300 if ( varName . Contains ( '[' ) && varName . Contains ( ']' ) )
256301 {
257-
258302 string [ ] variableHelper = varName . Replace ( "]" , string . Empty ) . Split ( '[' ) ;
259303 string varWithoutIndex = variableHelper [ 0 ] ;
260304 string index = variableHelper [ 1 ] ;
@@ -277,6 +321,25 @@ public CritVisitor()
277321 throw new Exception ( $ "Variable { varWithoutIndex } not found") ;
278322 }
279323 }
324+
325+
326+ //TODO ADD SOMETHING LIKE THIS
327+ //if (varName.Contains('.'))
328+ //{az
329+ // string[] variableHelper = varName.Split('.');
330+ // string varWithoutIndex = variableHelper[0];
331+ // string index = variableHelper[1];
332+ // var variable = Variables[varWithoutIndex];
333+ // if (variable is not null)
334+ // {
335+ // var value = variable.GetType().GetProperty(index).GetValue(variable);
336+ // return value;
337+ // }
338+ // else
339+ // {
340+ // throw new Exception($"Variable {varWithoutIndex} not found");
341+ // }
342+ //}
280343
281344 if ( ! Variables . ContainsKey ( varName ) )
282345 throw new Exception ( $ "Variable '{ varName } ' is not defined") ;
0 commit comments