1- using System . Reflection ;
1+ using System . Linq ;
2+ using System . Reflection ;
23using static EZCodeLanguage . Parser ;
34
45namespace EZCodeLanguage
@@ -35,7 +36,7 @@ public void SetInput(string input, EZInputType inputType = EZInputType.InputMeth
3536 }
3637 public string ConsoleInput ( )
3738 {
38- string input = "" ;
39+ string input ;
3940 if ( InputType == EZInputType . Console ) input = Console . ReadLine ( ) ;
4041 else
4142 {
@@ -49,15 +50,14 @@ public string ConsoleInput()
4950 return input ;
5051 }
5152 public Parser parser { get ; set ; }
52- public string WorkingFile { get ; set ; }
5353 public EZHelp EZHelp { get ; private set ; }
54- public Interpreter ( string file , Parser parser )
54+ public Interpreter ( Parser parser )
5555 {
5656 StackTrace = new Stack < string > ( ) ;
57- WorkingFile = file ;
5857 this . parser = parser ;
5958 EZHelp = new EZHelp ( this ) ;
6059 Methods = parser . Methods . ToArray ( ) ;
60+ Classes = parser . Classes . ToArray ( ) ;
6161
6262 for ( int i = 0 ; i < Classes . Length ; i ++ )
6363 {
@@ -75,8 +75,8 @@ public Interpreter(string file, Parser parser)
7575 }
7676 if ( var . Value == null )
7777 {
78- Parser p = new Parser ( ) ;
79- Token [ ] parsertokens = p . Parse ( var . Line . Value ) [ 0 ] . Tokens ;
78+ Parser p = new Parser ( var . Line . Value , var . Line . FilePath ) ;
79+ Token [ ] parsertokens = p . Parse ( ) [ 0 ] . Tokens ;
8080 LineWithTokens lineWithTokens = new LineWithTokens ( parsertokens . Skip ( 0 ) . ToArray ( ) , var . Line ) ;
8181 object obj = null ;
8282 try { obj = GetValue ( lineWithTokens . Tokens . Skip ( var . Line . Value . Contains ( "new :" ) ? 4 : 3 ) . ToArray ( ) ) ; } catch { }
@@ -103,7 +103,7 @@ public Interpreter(string file, Parser parser)
103103 public Exception [ ] Errors { get ; private set ; } = [ ] ;
104104 public Var [ ] Vars { get ; set ; } = [ ] ;
105105 public Method [ ] Methods { get ; set ; } = [ ] ;
106- public Class [ ] Classes { get => parser . Classes . ToArray ( ) ; }
106+ public Class [ ] Classes { get ; set ; }
107107 public Line CurrentLine { get ; private set ; }
108108 private bool StartMethodEntry = false ;
109109 public int Interperate ( ) => Interperate ( parser . LinesWithTokens ) ;
@@ -155,7 +155,7 @@ public int Interperate(LineWithTokens[] LineTokens)
155155 StackNumber ++ ;
156156 try
157157 {
158- string message = $ "codeline: \" { line . Line . Value } \" , file: \" { WorkingFile } \" , line: { line . Line . CodeLine } ";
158+ string message = $ "codeline: \" { line . Line . Value } \" , file: \" { line . Line . FilePath } \" , line: { line . Line . CodeLine } ";
159159 bool duplicate_stack = false ;
160160 try { duplicate_stack = StackTrace . First ( ) != message ; } catch { }
161161 if ( ! duplicate_stack ) StackTrace . Push ( message ) ;
@@ -164,6 +164,20 @@ public int Interperate(LineWithTokens[] LineTokens)
164164 object ? Return = null ;
165165 switch ( FirstToken . Type )
166166 {
167+ case TokenType . Include :
168+ string [ ] packages = [ "main" ] ;
169+ parser = Package . ReturnParserWithPackages ( parser , packages ) ;
170+ Methods = [ .. Methods , .. parser . Methods ] ;
171+ Classes = [ .. Classes , .. parser . Classes ] ;
172+ break ;
173+ case TokenType . Exclude :
174+ packages = [ "main" ] ;
175+ Parser except = Package . ReturnParserWithPackages ( new Parser ( "" , "" ) , packages ) ;
176+ parser = Package . RemovePackageFromParser ( parser , except ) ;
177+ Methods = parser . Methods . ToArray ( ) ;
178+ Classes = parser . Classes . ToArray ( ) ;
179+ break ;
180+
167181 case TokenType . Identifier :
168182 try
169183 {
@@ -466,7 +480,7 @@ public int Interperate(LineWithTokens[] LineTokens)
466480 try
467481 {
468482 CSharpMethod method = FirstToken . Value as CSharpMethod ;
469- StackTrace . Push ( $ "csharp-method: \" { ( method . Path != "" ? method . Path : "Null" ) } \" , file: \" { WorkingFile } \" , line: { line . Line . CodeLine } ") ;
483+ StackTrace . Push ( $ "csharp-method: \" { ( method . Path != "" ? method . Path : "Null" ) } \" , file: \" { line . Line . FilePath } \" , line: { line . Line . CodeLine } ") ;
470484 object obj = Reflect ( method ! ) ;
471485 StackTrace . TryPop ( out _ ) ;
472486 Return = obj ;
@@ -515,7 +529,16 @@ public int Interperate(LineWithTokens[] LineTokens)
515529 }
516530 catch ( Exception ex )
517531 {
518- throw new Exception ( $ "Error with \" return\" , Error Message:\" { ex . Message } \" ") ;
532+ string stringReturns = string . Join ( " " , line . Tokens . Select ( x => x . StringValue ) ) ;
533+ if ( stringReturns != string . Empty && ex . Message . Contains ( $ "Error Message:\" The identifier \" { line . Tokens . First ( ) . StringValue } \" does not exist in this current context\" ") )
534+ {
535+ Return = stringReturns ;
536+ returned = Return ;
537+ }
538+ else
539+ {
540+ throw new Exception ( $ "Error with \" return\" , Error Message:\" { ex . Message } \" ") ;
541+ }
519542 }
520543 break ;
521544 case TokenType . Global :
@@ -794,7 +817,7 @@ private IdentType IsType(string token, out object? type)
794817 }
795818 public object ? MethodRun ( Method method , Var [ ] ? parameters )
796819 {
797- StackTrace . Push ( $ "method: { method . Name } , file: { WorkingFile } , line: { method . Line . CodeLine } ") ;
820+ StackTrace . Push ( $ "method: { method . Name } , file: { method . Line . FilePath } , line: { method . Line . CodeLine } ") ;
798821 parameters ??= [ ] ;
799822 LineWithTokens [ ] lines = method . Lines ;
800823 Var [ ] vvars = method . Parameters ?? [ ] ;
@@ -872,12 +895,7 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
872895 object [ ] a = ( object [ ] ) obj ;
873896 string all = "" ;
874897 if ( a . Length > 0 )
875- { /*
876- if (Classes.Any(x => x.Name == GetValue(a[0]).ToString()))
877- {
878- all = GetValue(string.Join(" ", a), type).ToString();
879- if (all != null) return all;
880- }*/
898+ {
881899 for ( int i = 0 ; i < a . Length ; i ++ )
882900 {
883901 if ( a [ i ] . GetType ( ) . IsArray )
@@ -1033,7 +1051,7 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
10331051 throw new Exception ( $ "Could not find the property \" { property_name } \" in the class instance \" { firstpart } \" ") ;
10341052 }
10351053 }
1036- if ( Vars . FirstOrDefault ( x => x . Name == firstpart ) . Value is RunMethod m )
1054+ else if ( Vars . FirstOrDefault ( x => x . Name == firstpart ) . Value is RunMethod m )
10371055 {
10381056 if ( m . Parameters . FirstOrDefault ( x => x . Name == property_name ) is Var v )
10391057 {
@@ -1049,9 +1067,24 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
10491067 throw new Exception ( $ "Variable \" { firstpart } \" is not a class instance") ;
10501068 }
10511069 }
1052- else if ( Classes . Any ( x => x . Name == firstpart ) )
1070+ else if ( Classes . FirstOrDefault ( x => x . Name == firstpart ) is Class c )
10531071 {
1054- throw new NotImplementedException ( ) ; // ////////////////////////////////////////////////////////////////////////////////////////////////////////NEXT
1072+ string property_name = string . Join ( ":" , obj . ToString ( ) . Split ( ':' ) . Skip ( 1 ) ) . Trim ( ) ;
1073+ if ( c . Properties . FirstOrDefault ( x => x . Name == property_name ) is Var v )
1074+ {
1075+ return v . Value ;
1076+ }
1077+ if ( c . Methods . FirstOrDefault ( x => x . Name == property_name ) is Method m )
1078+ {
1079+ if ( m . Parameters . Select ( x => x . Required ) . ToArray ( ) . Length != 0 )
1080+ throw new Exception ( $ "Method \" { property_name } \" can not have any required parameters if being called like a property") ;
1081+
1082+ return MethodRun ( m , [ ] ) ;
1083+ }
1084+ else
1085+ {
1086+ throw new Exception ( $ "Class path \" { obj } \" is not correct") ;
1087+ }
10551088 }
10561089 }
10571090 if ( obj . ToString ( ) . Split ( ' ' ) . Length > 0 )
@@ -1093,17 +1126,17 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
10931126 }
10941127 void DoMethod ( Method m , int skip = 1 )
10951128 {
1096- Parser parser = new Parser ( ) ;
1097- Token [ ] parsertokens = parser . Parse ( input ) [ 0 ] . Tokens ;
1129+ Parser parser = new Parser ( input , m . Line . FilePath ) ;
1130+ Token [ ] parsertokens = parser . Parse ( ) [ 0 ] . Tokens ;
10981131 LineWithTokens lineWithTokens = new LineWithTokens ( parsertokens . Skip ( skip ) . ToArray ( ) , CurrentLine ) ;
10991132 obj = SingleLine ( lineWithTokens ) ;
11001133 }
11011134 void DoClass ( Class c , int skip = 1 )
11021135 {
11031136 Methods = [ .. Methods , .. c . Methods ] ;
11041137 Vars = [ .. Vars , .. c . Properties ] ;
1105- Parser parser = new Parser ( ) ;
1106- Token [ ] parsertokens = parser . Parse ( input ) [ 0 ] . Tokens ;
1138+ Parser parser = new Parser ( input , c . Line . FilePath ) ;
1139+ Token [ ] parsertokens = parser . Parse ( ) [ 0 ] . Tokens ;
11071140 LineWithTokens lineWithTokens = new LineWithTokens ( parsertokens . Skip ( skip ) . ToArray ( ) , CurrentLine ) ;
11081141 obj = SingleLine ( lineWithTokens ) ;
11091142 Methods = Methods . Except ( c . Methods ) . ToArray ( ) ;
@@ -1156,7 +1189,7 @@ public object Reflect(CSharpMethod method)
11561189 if ( method . IsVar )
11571190 {
11581191 string value = Vars . FirstOrDefault ( x => x . Name == method . Path ) . Value . ToString ( ) ;
1159- Line [ ] l = [ new Line ( value , 0 ) ] ;
1192+ Line [ ] l = [ new Line ( value , 0 , method . Line . FilePath ) ] ;
11601193 string [ ] r = { } ;
11611194 object [ ] o = parser . SplitParts ( ref l , 0 , 0 , ref r , out _ , out _ ) ;
11621195 if ( o . Length > 1 ) throw new Exception ( "Error with reflection properties" ) ;
0 commit comments