@@ -6,6 +6,7 @@ namespace EZCodeLanguage
66{
77 public class Interpreter
88 {
9+ public static Interpreter Instance { get ; private set ; }
910 public static string Version = "3.0.0-beta" ;
1011 public event EventHandler OutputWrote ;
1112 public event EventHandler OutputCleared ;
@@ -54,6 +55,7 @@ public string ConsoleInput()
5455 public EZHelp EZHelp { get ; private set ; }
5556 public Interpreter ( Parser parser , Debug . Breakpoint [ ] ? breakpoints = null )
5657 {
58+ Instance = this ;
5759 StackTrace = new Stack < string > ( ) ;
5860 this . parser = parser ;
5961 EZHelp = new EZHelp ( this ) ;
@@ -115,6 +117,7 @@ public void Interperate(LineWithTokens[] LineTokens)
115117 int endcode = 0 ;
116118 var temp_stack = new Stack < string > ( StackTrace ) ;
117119
120+ Package . RemoveAllPackagesFromExecutionDirectory ( AppDomain . CurrentDomain . BaseDirectory ) ;
118121
119122 if ( Methods . Any ( x => x . Name . ToLower ( ) == "start" ) && ! StartMethodEntry )
120123 {
@@ -153,6 +156,8 @@ public void Interperate(LineWithTokens[] LineTokens)
153156 }
154157 }
155158 }
159+
160+ Package . RemoveAllPackagesFromExecutionDirectory ( AppDomain . CurrentDomain . BaseDirectory ) ;
156161 }
157162 internal object ? SingleLine ( LineWithTokens line )
158163 {
@@ -180,6 +185,22 @@ public void Interperate(LineWithTokens[] LineTokens)
180185 {
181186 string combined_packages = string . Join ( " " , line . Tokens . Skip ( 1 ) . Select ( x => x . StringValue ) ) ;
182187 string [ ] packages = combined_packages . Split ( "," ) . Select ( x=> x . Trim ( ) ) . ToArray ( ) ;
188+ Project [ ] projects = new Project [ packages . Length ] ;
189+
190+ for ( int i = 0 ; i < packages . Length ; i ++ )
191+ projects [ i ] = Package . GetPackageAsProject ( packages [ i ] ) ;
192+
193+ if ( projects . Any ( x => ! string . IsNullOrEmpty ( x . LibraryDirectory ) ) )
194+ {
195+ string destination = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "Libraries" ) ;
196+ foreach ( var project in projects )
197+ {
198+ if ( FirstToken . StringValue == "include" )
199+ Package . AddPackageToExecutionDirectory ( project , destination ) ;
200+ else
201+ Package . RemovePackageFromExecutionDirectory ( project , destination ) ;
202+ }
203+ }
183204
184205 if ( FirstToken . StringValue == "include" )
185206 {
@@ -902,7 +923,7 @@ private IdentType IsType(string token, out object? type)
902923 {
903924 result = SingleLine ( line ) ;
904925
905- if ( line . Tokens [ 0 ] . Type == TokenType . Return || returned != null )
926+ if ( line . Tokens . Length > 0 && line . Tokens [ 0 ] . Type == TokenType . Return || returned != null )
906927 break ;
907928 }
908929 result ??= returned ;
@@ -932,6 +953,7 @@ internal bool ParameterNotEqual(Var v1, Var v2)
932953
933954 return t1 != t2 ;
934955 }
956+ internal int getValueLoopIndex = 0 ;
935957 public object GetValue ( object obj , DataType ? type = null , string arraySeperator = " " )
936958 {
937959 if ( obj . GetType ( ) . IsArray )
@@ -1005,11 +1027,20 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
10051027 }
10061028 catch
10071029 {
1008- try
1030+ if ( getValueLoopIndex < 50 )
10091031 {
1010- return GetValue ( var . Value , type , arraySeperator ) ;
1032+ try
1033+ {
1034+ getValueLoopIndex ++ ;
1035+ return GetValue ( var . Value , type , arraySeperator ) ;
1036+ }
1037+ catch { }
1038+ }
1039+ else
1040+ {
1041+ getValueLoopIndex = 0 ;
1042+ return obj ;
10111043 }
1012- catch { }
10131044 throw new Exception ( "Error returning correct value" ) ;
10141045 }
10151046 }
@@ -1038,26 +1069,20 @@ public object GetValue(object obj, DataType? type = null, string arraySeperator
10381069 return result ;
10391070 else throw new Exception ( $ "The Class of the instance does not contain a \" get\" method for the expected datatype \" { type . Type . ToString ( ) . Remove ( 0 , 1 ) } \" ") ;
10401071 }
1041- else
1072+ }
1073+ if ( var . DataType . Type == DataType . Types . _object && type . Type == DataType . Types . _null )
1074+ {
1075+ obj = var . Value ;
1076+ if ( obj is Class _c )
10421077 {
1043- if ( var . DataType . Type == DataType . Types . _object && type . Type == DataType . Types . _null )
1044- {
1045- obj = var . Value ;
1046- if ( obj is Class _c )
1047- {
1048- obj = _c . Properties . FirstOrDefault ( x => x . Name . Equals ( "value" , StringComparison . CurrentCultureIgnoreCase ) ) . Value ?? var . Value ;
1049- }
1050- return obj ;
1051- }
1052- else if ( var . DataType == type || var . DataType . Type == type . Type )
1053- {
1054- return var . Value ;
1055- }
1056- else
1057- {
1058- throw new Exception ( $ "The Class of the instance does not contain a \" get\" method for the expected datatype \" { type . Type . ToString ( ) . Remove ( 0 , 1 ) } \" ") ;
1059- }
1078+ Var ? v = _c . Properties . FirstOrDefault ( x => x . Name . Equals ( "value" , StringComparison . CurrentCultureIgnoreCase ) ) ;
1079+ obj = v != null ? v . Value : var . Value ;
10601080 }
1081+ return obj ;
1082+ }
1083+ else if ( var . DataType == type || var . DataType . Type == type . Type )
1084+ {
1085+ return var . Value ;
10611086 }
10621087 else
10631088 {
@@ -1224,6 +1249,13 @@ void DoClass(Class c, int skip = 1)
12241249 Vars = Vars . Except ( backupVars ) . ToArray ( ) ;
12251250 Methods = Methods . Except ( backupMethods ) . ToArray ( ) ;
12261251 object o = MethodRun ( run . Runs , run . Parameters ) ;
1252+ cl . Properties = cl . Properties . Select ( x =>
1253+ {
1254+ if ( x . Value == null && Vars . FirstOrDefault ( y => y . Name == x . Name ) is Var v )
1255+ return new Var ( x . Name , v . Value , x . Line , x . StackNumber , x . DataType , x . Required , x . Global , x . IsParams ) ;
1256+ else return x ;
1257+ } ) . ToArray ( ) ;
1258+
12271259 try { o = GetValue ( cl , type , arraySeperator ) ; } catch { }
12281260
12291261 Methods = backupMethods ;
@@ -1269,54 +1301,102 @@ public object Reflect(CSharpMethod method)
12691301 throw new ArgumentException ( "Invalid method path" ) ;
12701302 }
12711303
1272- // Get the type from the full type name
1273- string typeName = string . Join ( "." , pathParts . Take ( pathParts . Length - 1 ) ) ;
1274- // Include the assembly information for types in the System namespace
1275- if ( typeName . StartsWith ( "System." ) )
1276- {
1277- typeName += ", mscorlib" ;
1278- }
1279- Type type = Type . GetType ( typeName ) ;
1280- if ( type == null )
1304+ if ( pathParts . Length >= 2 && pathParts [ 1 ] . Equals ( "dll" ) )
12811305 {
1282- throw new ArgumentException ( $ "Type \" { typeName } \" not found") ;
1283- }
1306+ // If the method path contains an assembly name, load the assembly
1307+ string assemblyPath = pathParts [ 0 ] + "." + pathParts [ 1 ] ;
1308+ string subdirectory = pathParts [ 0 ] ; // Name of the subdirectory is first part of namespace
1309+ string fullAssemblyPath = Path . Combine ( subdirectory , assemblyPath ) ; // Combine subdirectory path with assembly name
1310+ Assembly assembly = Assembly . LoadFrom ( fullAssemblyPath ) ;
12841311
1285- // Get the method name from the path
1286- string methodName = pathParts . Last ( ) ;
1312+ // Get the type name
1313+ string typeName = string . Join ( "." , pathParts . Skip ( 2 ) . Take ( pathParts . Length - 3 ) ) ;
12871314
1288- // Find the method in the type
1289- MethodInfo methodInfo = type . GetMethod ( methodName , parameters . Select ( p => p . GetType ( ) ) . ToArray ( ) ) ;
1290- if ( methodInfo == null )
1291- {
1292- throw new ArgumentException ( $ "Method \" { methodName } \" not found in type \" { typeName } \" ") ;
1293- }
1315+ // Get the method name
1316+ string methodName = pathParts . Last ( ) ;
12941317
1295- // If the method is static, no need to create an instance
1296- if ( methodInfo . IsStatic )
1297- {
1298- // Invoke the method with the specified parameters
1299- object result = methodInfo . Invoke ( null , parameters ) ;
1300- return result ;
1318+ // Get the type from the assembly
1319+ Type type = assembly . GetType ( typeName ) ;
1320+ if ( type == null )
1321+ {
1322+ throw new ArgumentException ( $ "Type \" { typeName } \" not found in assembly \" { fullAssemblyPath } \" ") ;
1323+ }
1324+
1325+ // Get the method from the type
1326+ MethodInfo methodInfo = type . GetMethod ( methodName ) ;
1327+ if ( methodInfo == null )
1328+ {
1329+ throw new ArgumentException ( $ "Method \" { methodName } \" not found in type \" { typeName } \" ") ;
1330+ }
1331+
1332+ // If the method is static, no need to create an instance
1333+ if ( methodInfo . IsStatic )
1334+ {
1335+ // Invoke the method with the specified parameters
1336+ object result = methodInfo . Invoke ( null , parameters ) ;
1337+ return result ;
1338+ }
1339+ else
1340+ {
1341+ // Create an instance of the type
1342+ object instance = Activator . CreateInstance ( type ) ;
1343+
1344+ // Invoke the non-static method with the specified parameters
1345+ object result = methodInfo . Invoke ( instance , parameters ) ;
1346+ return result ;
1347+ }
13011348 }
13021349 else
13031350 {
1304- // Create an instance of the type
1305- object instance = Activator . CreateInstance ( type ) ;
1306- if ( typeName == "EZCodeLanguage.EZHelp" ) instance = e ;
1351+ // Get the type from the full type name
1352+ string typeName = string . Join ( "." , pathParts . Take ( pathParts . Length - 1 ) ) ;
1353+ // Include the assembly information for types in the System namespace
1354+ if ( typeName . StartsWith ( "System." ) )
1355+ {
1356+ typeName += ", mscorlib" ;
1357+ }
1358+ Type type = Type . GetType ( typeName ) ;
1359+ if ( type == null )
1360+ {
1361+ throw new ArgumentException ( $ "Type \" { typeName } \" not found") ;
1362+ }
13071363
1308- // Invoke the non-static method with the specified parameters
1309- try
1364+ // Get the method name from the path
1365+ string methodName = pathParts . Last ( ) ;
1366+
1367+ // Find the method in the type
1368+ MethodInfo methodInfo = type . GetMethod ( methodName , parameters . Select ( p => p . GetType ( ) ) . ToArray ( ) ) ;
1369+ if ( methodInfo == null )
13101370 {
1311- object result = methodInfo . Invoke ( instance , parameters ) ;
1371+ throw new ArgumentException ( $ "Method \" { methodName } \" not found in type \" { typeName } \" ") ;
1372+ }
1373+
1374+ // If the method is static, no need to create an instance
1375+ if ( methodInfo . IsStatic )
1376+ {
1377+ // Invoke the method with the specified parameters
1378+ object result = methodInfo . Invoke ( null , parameters ) ;
13121379 return result ;
13131380 }
1314- catch
1381+ else
13151382 {
1316- string ? message = e . Error ;
1317- throw new Exception ( message ?? $ "Error occured in \" { methodPath } \" ") ;
1383+ // Create an instance of the type
1384+ object instance = Activator . CreateInstance ( type ) ;
1385+ if ( typeName == "EZCodeLanguage.EZHelp" ) instance = e ;
1386+
1387+ // Invoke the non-static method with the specified parameters
1388+ try
1389+ {
1390+ object result = methodInfo . Invoke ( instance , parameters ) ;
1391+ return result ;
1392+ }
1393+ catch
1394+ {
1395+ string ? message = EZHelp . Error ;
1396+ throw new Exception ( message ?? $ "Error occured in \" { methodPath } \" ") ;
1397+ }
13181398 }
13191399 }
13201400 }
13211401 }
1322- }
1402+ }
0 commit comments