@@ -74,6 +74,15 @@ public Interpreter(string file, Parser parser)
7474 var . DataType . ObjectClass = Classes [ i ] ;
7575 }
7676 }
77+ if ( var . Value == null )
78+ {
79+ Parser p = new Parser ( ) ;
80+ Token [ ] parsertokens = p . Parse ( var . Line . Value ) [ 0 ] . Tokens ;
81+ LineWithTokens lineWithTokens = new LineWithTokens ( parsertokens . Skip ( 0 ) . ToArray ( ) , var . Line ) ;
82+ object obj = null ;
83+ try { obj = GetValue ( lineWithTokens . Tokens . Skip ( var . Line . Value . Contains ( "new :" ) ? 4 : 3 ) . ToArray ( ) ) ; } catch { }
84+ var . Value = string . IsNullOrEmpty ( obj . ToString ( ) ) ? null : obj ;
85+ }
7786 }
7887 for ( int j = 0 ; j < Classes [ i ] . GetTypes . Length ; j ++ )
7988 {
@@ -308,12 +317,22 @@ public int Interperate(LineWithTokens[] LineTokens)
308317 throw new Exception ( $ "Class \" { cl . Name } \" does not have any properties") ;
309318 }
310319 }
311- return var ;
320+ Return = var ;
321+ }
322+ else if ( line . Tokens . Length > 1 )
323+ {
324+ Methods = [ .. Methods , .. cl . Methods ] ;
325+ Vars = [ .. Vars , .. cl . Properties ] ;
326+ line . Tokens = line . Tokens . Skip ( 1 ) . ToArray ( ) ;
327+ Return = SingleLine ( line ) ;
328+ Methods = Methods . Except ( cl . Methods ) . ToArray ( ) ;
329+ Vars = Vars . Except ( cl . Properties ) . ToArray ( ) ;
312330 }
313331 else
314332 {
315333 throw new Exception ( "Expected \" new\" keyword to declare instance of class" ) ;
316334 }
335+ break ;
317336
318337 case IdentType . Method :
319338 Method method = type as Method ;
@@ -330,7 +349,66 @@ public int Interperate(LineWithTokens[] LineTokens)
330349
331350 if ( method . Parameters != null && method . Parameters . Length > 0 )
332351 {
333- vars = GetMethodParameters ( line , method ) ;
352+ int next = nocol ? 0 : 1 ;
353+ string all ;
354+ object [ ] vals ;
355+
356+ try
357+ {
358+ all = string . Join ( " " , line . Tokens . Select ( ( x , y ) => x . Value is RunMethod r ? "@:|" + y . ToString ( ) + "{}" : x . StringValue ) . Skip ( next + 1 ) ) ;
359+ string [ ] all_parts = all . Split ( ',' ) ;
360+ if ( all_parts . Length > method . Parameters . Select ( x => x . Required ) . ToArray ( ) . Length && ! method . Parameters . Any ( x => x . IsParams ) )
361+ throw new Exception ( $ "Expects { ( method . Parameters . Any ( x => x . Required ) ? "at least" : "" ) } { ( method . Parameters . Any ( x => x . Required ) ? method . Parameters . Select ( x => x . Required ) . ToArray ( ) . Length : method . Parameters . Length ) } parameter for method \" { method . Name } \" but { all_parts . Length } were given") ;
362+ if ( method . Parameters . Any ( x => x . IsParams ) )
363+ {
364+ string [ ] new_parts = [ ] ;
365+ for ( int j = 0 ; j < method . Parameters . Length ; j ++ )
366+ {
367+ if ( method . Parameters [ j ] . IsParams )
368+ {
369+ new_parts = [ .. new_parts , string . Join ( "," , all_parts . Skip ( j ) ) . Replace ( " ," , "," ) ] ;
370+ break ;
371+ }
372+ new_parts = [ .. new_parts , all_parts [ j ] ] ;
373+ }
374+ all_parts = new_parts ;
375+ }
376+ vals = all_parts . Select ( x => x . Trim ( ) ) . Select ( ( x , y ) => x . StartsWith ( "@:|" ) && x . EndsWith ( "{}" ) ? line . Tokens [ int . Parse ( x . Substring ( 3 , x . Length - 5 ) ) ] . Value : GetValue ( x , method . Parameters [ y ] . DataType ) . ToString ( ) ) . Where ( x => x . ToString ( ) != "" ) . ToArray ( ) ;
377+ }
378+ catch ( Exception e )
379+ {
380+ if ( e . Message . StartsWith ( "Expects " ) )
381+ throw new Exception ( e . Message ) ;
382+ throw new Exception ( "Error getting values for method paramters" ) ;
383+ }
384+ if ( vals . FirstOrDefault ( x => x is RunMethod ) is RunMethod r )
385+ {
386+ for ( int i = 0 ; i < r . Parameters . Length ; i ++ )
387+ {
388+ r . Parameters [ i ] . Value = GetValue ( r . Parameters [ i ] , r . Parameters [ i ] . DataType ) ;
389+ }
390+ }
391+ if ( vals . Length > 0 )
392+ {
393+ for ( int i = 0 ; i < method . Parameters . Length ; i ++ )
394+ {
395+ if ( ! method . Parameters [ i ] . Required && vals . Length - 1 < i )
396+ continue ;
397+ object value = method . Parameters [ i ] . IsParams ? string . Join ( "," , vals . Skip ( i ) ) : vals [ i ] ;
398+ vars = [ .. vars , new Var ( method . Parameters [ i ] . Name , value , line . Line , stackNumber : StackNumber , type : method . Parameters [ i ] . DataType , required : method . Parameters [ i ] . Required ) ] ;
399+ }
400+ if ( vars . Where ( x => x . Required ) . ToArray ( ) . Length != method . Parameters . Where ( x => x . Required ) . ToArray ( ) . Length )
401+ {
402+ throw new Exception ( "Not all parameters are set" ) ;
403+ }
404+ }
405+ else
406+ {
407+ if ( line . Tokens . Length > 1 )
408+ {
409+ throw new Exception ( "Method does not require any parameters" ) ;
410+ }
411+ }
334412 }
335413 }
336414 else
@@ -598,72 +676,6 @@ public int Interperate(LineWithTokens[] LineTokens)
598676 throw new Exception ( e . Message , e ) ;
599677 }
600678 }
601- private Var [ ] GetMethodParameters ( LineWithTokens line , Method method )
602- {
603- bool nocol = ( method . Settings & Method . MethodSettings . NoCol ) != 0 ;
604- int next = nocol ? 0 : 1 ;
605- Var [ ] vars = [ ] ;
606- string all ;
607- object [ ] vals ;
608-
609- try
610- {
611- all = string . Join ( " " , line . Tokens . Select ( ( x , y ) => x . Value is RunMethod r ? "@:|" + y . ToString ( ) + "{}" : x . StringValue ) . Skip ( next + 1 ) ) ;
612- string [ ] all_parts = all . Split ( ',' ) ;
613- if ( all_parts . Length > method . Parameters . Select ( x => x . Required ) . ToArray ( ) . Length && ! method . Parameters . Any ( x => x . IsParams ) )
614- throw new Exception ( $ "Expects { ( method . Parameters . Any ( x => x . Required ) ? "at least" : "" ) } { ( method . Parameters . Any ( x => x . Required ) ? method . Parameters . Select ( x => x . Required ) . ToArray ( ) . Length : method . Parameters . Length ) } parameter for method \" { method . Name } \" but { all_parts . Length } were given") ;
615- if ( method . Parameters . Any ( x => x . IsParams ) )
616- {
617- string [ ] new_parts = [ ] ;
618- for ( int j = 0 ; j < method . Parameters . Length ; j ++ )
619- {
620- if ( method . Parameters [ j ] . IsParams )
621- {
622- new_parts = [ .. new_parts , string . Join ( "," , all_parts . Skip ( j ) ) . Replace ( " ," , "," ) ] ;
623- break ;
624- }
625- new_parts = [ .. new_parts , all_parts [ j ] ] ;
626- }
627- all_parts = new_parts ;
628- }
629- vals = all_parts . Select ( x => x . Trim ( ) ) . Select ( ( x , y ) => x . StartsWith ( "@:|" ) && x . EndsWith ( "{}" ) ? line . Tokens [ int . Parse ( x . Substring ( 3 , x . Length - 5 ) ) ] . Value : GetValue ( x , method . Parameters [ y ] . DataType ) . ToString ( ) ) . Where ( x => x . ToString ( ) != "" ) . ToArray ( ) ;
630- }
631- catch ( Exception e )
632- {
633- if ( e . Message . StartsWith ( "Expects " ) )
634- throw new Exception ( e . Message ) ;
635- throw new Exception ( "Error getting values for method paramters" ) ;
636- }
637- if ( vals . FirstOrDefault ( x => x is RunMethod ) is RunMethod r )
638- {
639- for ( int i = 0 ; i < r . Parameters . Length ; i ++ )
640- {
641- r . Parameters [ i ] . Value = GetValue ( r . Parameters [ i ] , r . Parameters [ i ] . DataType ) ;
642- }
643- }
644- if ( vals . Length > 0 )
645- {
646- for ( int i = 0 ; i < method . Parameters . Length ; i ++ )
647- {
648- if ( ! method . Parameters [ i ] . Required && vals . Length - 1 < i )
649- continue ;
650- object value = method . Parameters [ i ] . IsParams ? string . Join ( "," , vals . Skip ( i ) ) : vals [ i ] ;
651- vars = [ .. vars , new Var ( method . Parameters [ i ] . Name , value , line . Line , stackNumber : StackNumber , type : method . Parameters [ i ] . DataType , required : method . Parameters [ i ] . Required ) ] ;
652- }
653- if ( vars . Where ( x => x . Required ) . ToArray ( ) . Length != method . Parameters . Where ( x => x . Required ) . ToArray ( ) . Length )
654- {
655- throw new Exception ( "Not all parameters are set" ) ;
656- }
657- }
658- else
659- {
660- if ( line . Tokens . Length > 1 )
661- {
662- throw new Exception ( "Method does not require any parameters" ) ;
663- }
664- }
665- return vars ;
666- }
667679 private object ? RunStatement ( Statement statement , out bool broke )
668680 {
669681 broke = false ;
@@ -1026,47 +1038,39 @@ public object GetValue(object obj, DataType? type = null)
10261038 }
10271039 else if ( c . Methods != null && c . Methods . FirstOrDefault ( x => x . Name == second ) is Method m )
10281040 {
1029- Methods = [ .. Methods , .. c . Methods ] ;
1030- Vars = [ .. Vars , .. c . Properties ] ;
1031- DoMethod ( m ) ;
1032- Methods = Methods . Except ( c . Methods ) . ToArray ( ) ;
1033- Vars = Vars . Except ( c . Properties ) . ToArray ( ) ;
1041+ DoClass ( c ) ;
10341042 }
10351043 else if ( c . Classes != null && c . Classes . FirstOrDefault ( x => x . Name == second ) is Class _c )
10361044 {
1037- Methods = [ .. Methods , .. _c . Methods ] ;
1038- Vars = [ .. Vars , .. _c . Properties ] ;
10391045 DoClass ( _c ) ;
1040- Methods = Methods . Except ( _c . Methods ) . ToArray ( ) ;
1041- Vars = Vars . Except ( _c . Properties ) . ToArray ( ) ;
10421046 }
10431047 }
10441048 }
10451049 else if ( Methods . FirstOrDefault ( x => x . Name == first ) is Method m )
10461050 {
1047- DoMethod ( m ) ;
1051+ DoMethod ( m , 0 ) ;
10481052 }
10491053 else if ( Classes . FirstOrDefault ( x => x . Name == first ) is Class c )
10501054 {
1051- DoClass ( c ) ;
1055+ DoClass ( c , 0 ) ;
10521056 }
1053- void DoMethod ( Method m )
1057+ void DoMethod ( Method m , int skip = 1 )
10541058 {
1055- Var [ ] parameters = [ ] ;
1056- if ( m . Parameters != null && m . Parameters . Select ( x=> x . Required ) . ToArray ( ) . Length != 0 )
1057- {
1058- LineWithTokens lineWithTokens = new LineWithTokens ( ) ;
1059- parameters = GetMethodParameters ( lineWithTokens , m ) ;
1060- }
1061- else if ( parts [ 2 ] == ":" )
1062- {
1063- throw new Exception ( $ "Method \" { m . Name } \" does not expect parameters") ;
1064- }
1065- MethodRun ( m , parameters ) ;
1059+ Parser parser = new Parser ( ) ;
1060+ Token [ ] parsertokens = parser . Parse ( input ) [ 0 ] . Tokens ;
1061+ LineWithTokens lineWithTokens = new LineWithTokens ( parsertokens . Skip ( skip ) . ToArray ( ) , CurrentLine ) ;
1062+ obj = SingleLine ( lineWithTokens ) ;
10661063 }
1067- void DoClass ( Class c )
1064+ void DoClass ( Class c , int skip = 1 )
10681065 {
1069-
1066+ Methods = [ .. Methods , .. c . Methods ] ;
1067+ Vars = [ .. Vars , .. c . Properties ] ;
1068+ Parser parser = new Parser ( ) ;
1069+ Token [ ] parsertokens = parser . Parse ( input ) [ 0 ] . Tokens ;
1070+ LineWithTokens lineWithTokens = new LineWithTokens ( parsertokens . Skip ( skip ) . ToArray ( ) , CurrentLine ) ;
1071+ obj = SingleLine ( lineWithTokens ) ;
1072+ Methods = Methods . Except ( c . Methods ) . ToArray ( ) ;
1073+ Vars = Vars . Except ( c . Properties ) . ToArray ( ) ;
10701074 }
10711075 }
10721076 }
0 commit comments