Skip to content

Commit 4221e49

Browse files
assembly and loading dlls to program and other
1 parent 883f0fc commit 4221e49

9 files changed

Lines changed: 230 additions & 126 deletions

File tree

EZCode.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EZCode", "EZCode\EZCode.csp
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestEnv", "TestEnv\TestEnv.csproj", "{ECC3A0D8-B300-4258-804A-96B4288FD74C}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ez", "ez\ez.csproj", "{A9062E15-5201-47CC-917D-C210E41A1AEC}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ez", "ez\ez.csproj", "{A9062E15-5201-47CC-917D-C210E41A1AEC}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormsEZCodeLibrary", "..\Packages\WinForms\WinFormsEZCodeLibrary\WinFormsEZCodeLibrary.csproj", "{E76392D5-986E-4434-9D50-9B24D1D148F5}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
2729
{A9062E15-5201-47CC-917D-C210E41A1AEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{A9062E15-5201-47CC-917D-C210E41A1AEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{A9062E15-5201-47CC-917D-C210E41A1AEC}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{E76392D5-986E-4434-9D50-9B24D1D148F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{E76392D5-986E-4434-9D50-9B24D1D148F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{E76392D5-986E-4434-9D50-9B24D1D148F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{E76392D5-986E-4434-9D50-9B24D1D148F5}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

EZCode/App.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<runtime>
4+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
5+
<probing privatePath="Debug/Libraries;Release/Libraries" />
6+
</assemblyBinding>
7+
</runtime>
8+
</configuration>

EZCode/EZCode.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReadmeFile>README.md</PackageReadmeFile>
1414
<RepositoryUrl>https://github.com/JBrosDevelopment/EZCode.git</RepositoryUrl>
1515
<RepositoryType>git</RepositoryType>
16-
<PackageTags>EZCode;Code;Programming;Language;Windows;Forms;WinForms;WinForm</PackageTags>
16+
<PackageTags>EZCode;Code;Programming;Language;ProgrammingLanguage;Windows;C#;CSharp</PackageTags>
1717
<PackageReleaseNotes></PackageReleaseNotes>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1919
<Company>JBros Development</Company>
@@ -58,5 +58,5 @@
5858
<PackagePath>\</PackagePath>
5959
</None>
6060
</ItemGroup>
61-
61+
6262
</Project>

EZCode/EZHelp.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace EZCodeLanguage
77
public class EZHelp
88
{
99
public Interpreter Interpreter { get; set; }
10-
public string? Error = null;
10+
public static string? Error = null;
1111
public EZHelp(Interpreter interpreter)
1212
{
1313
Interpreter = interpreter;
@@ -179,6 +179,12 @@ public object ObjectParse(object obj, object type, bool to_string, string arrayS
179179
throw;
180180
}
181181
}
182+
public static object GetParameter(object obj, string type)
183+
{
184+
EZHelp e = new EZHelp(Instance);
185+
186+
return e.ObjectParse(obj, type);
187+
}
182188
public bool Evaluate(string expression)
183189
{
184190
try
@@ -462,15 +468,15 @@ public bool SameType(object a, object b)
462468
}
463469
}
464470
public int StringLength(object str) => StringParse(str).Length;
465-
public int RunEZCode(string code)
471+
public void RunEZCode(string code)
466472
{
467473
try
468474
{
469475
code = ObjectParse(code, "str").ToString();
470476
Parser parser = new Parser(string.Join(" ", code), "(instance running from inside file)");
471477
parser.Parse();
472478
Interpreter interpreter = new Interpreter(parser);
473-
return interpreter.Interperate();
479+
interpreter.Interperate();
474480
}
475481
catch (Exception e)
476482
{
@@ -623,5 +629,12 @@ public float MathFunc(object obj, object op)
623629
throw;
624630
}
625631
}
632+
public float Average(object obj)
633+
{
634+
object[] array = ArrayParse(obj, ",") as object[];
635+
float[] numbers = array.Select((x, y) => float.Parse(ObjectParse(array[y], "float").ToString())).ToArray();
636+
float all = numbers.Sum();
637+
return all / numbers.Length;
638+
}
626639
}
627640
}

EZCode/Interpreter.cs

Lines changed: 137 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)