Skip to content

Commit b35ded5

Browse files
Refractoring... Stopping Point on Packages... Took out Caching... Added base implementation of 'include' and 'exclude'
1 parent 967f78f commit b35ded5

8 files changed

Lines changed: 410 additions & 336 deletions

File tree

EZCode/EZHelp.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ public int RunEZCode(string code)
466466
try
467467
{
468468
code = ObjectParse(code, "str").ToString();
469-
Parser parser = new Parser(string.Join(" ", code));
469+
Parser parser = new Parser(string.Join(" ", code), "(instance running from inside file)");
470470
parser.Parse();
471-
Interpreter interpreter = new Interpreter($"{Interpreter.WorkingFile}(instance running from inside file)", parser);
471+
Interpreter interpreter = new Interpreter(parser);
472472
return interpreter.Interperate();
473473
}
474474
catch (Exception e)

EZCode/Interpreter.cs

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Linq;
2+
using System.Reflection;
23
using static EZCodeLanguage.Parser;
34

45
namespace 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");

EZCode/Package.cs

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ namespace EZCodeLanguage
44
{
55
public static class Package
66
{
7-
internal static string Extension = ".cache.json";
8-
internal static string Folder = "cache";
97
public static string PackagesDirectory = "D:\\EZCodeLanguage\\Packages\\";
108
public static string GetPackageDirectory(string package_name)
119
{
@@ -24,13 +22,28 @@ public static Parser GetPackageAsParser(string package_name)
2422
string file = GetPackageFile(package_name);
2523
string pack_dir = GetPackageDirectory(package_name);
2624
Project project = JsonConvert.DeserializeObject<Project>(File.ReadAllText(file));
27-
Parser parser = new Parser();
28-
if (project.Configuration.Cache) parser = OpenCache(project.Files.Select(x => Path.Combine(pack_dir, x)).ToArray());
29-
30-
// CACHE SYSTEM NOT WORKING PROPERLY
25+
string[] global_packages = project.Configuration.GlobalPackages ?? [];
26+
Parser[] parsers = [];
27+
foreach (var pack in global_packages)
28+
{
29+
string path = GetPackageFile(pack);
30+
string contents = File.ReadAllText(path);
31+
Parser p = new Parser(contents, path);
32+
p.Parse();
33+
parsers = [.. parsers, p];
34+
}
35+
foreach (string f in project.Files)
36+
{
37+
string path = Path.Combine(pack_dir, f);
38+
string contents = File.ReadAllText(path);
39+
Parser p = new Parser(contents, path);
40+
p.Parse();
41+
parsers = [.. parsers, p];
42+
}
43+
Parser parser = CombineParsers(parsers);
3144

3245
if (parser.Classes.Count == 0 && parser.Methods.Count == 0 && parser.LinesWithTokens.Length == 0)
33-
parser.Parse(string.Join("\n\n// End of File\n\n", project.Files.Select(x => File.ReadAllText(Path.Combine(pack_dir, x)))));
46+
parser.Parse();
3447

3548
return parser;
3649
}
@@ -43,65 +56,24 @@ public static Parser ReturnParserWithPackages(Parser parser, string[] package_na
4356
}
4457
return parser;
4558
}
46-
public static string SaveCache(string file, Parser parser)
59+
public static Parser RemovePackageFromParser(Parser main_parser, Parser remove)
4760
{
48-
if (parser.LinesWithTokens == null || parser.LinesWithTokens.Length == 0)
49-
parser.Parse();
50-
51-
FileInfo fileInfo = new FileInfo(file);
52-
if (fileInfo.Name == "package.json")
53-
{
54-
Project project = JsonConvert.DeserializeObject<Project>(File.ReadAllText(fileInfo.FullName));
55-
Parser parse = OpenCache(project.Files.Select(x => Path.Combine(fileInfo.DirectoryName, x)).ToArray());
56-
57-
if (parse.Classes.Count == 0 && parse.Methods.Count == 0 && parse.LinesWithTokens.Length == 0)
58-
parse.Parse(string.Join("\n\n// End of File\n\n", project.Files.Select(x => File.ReadAllText(Path.Combine(fileInfo.DirectoryName, x)))));
59-
60-
parser = parse;
61-
}
62-
63-
string cache = JsonConvert.SerializeObject(parser, Formatting.Indented);
64-
string path = Path.Combine(fileInfo.DirectoryName, Folder, fileInfo.Directory.Name) + Extension;
65-
if (!Directory.Exists(Path.Combine(fileInfo.DirectoryName, Folder))) Directory.CreateDirectory(Path.Combine(fileInfo.DirectoryName, Folder));
66-
File.WriteAllText(path, cache);
67-
return cache;
61+
main_parser.Classes = main_parser.Classes.Where(x => !remove.Classes.Any(y => y.Name == x.Name)).ToList();
62+
main_parser.Methods = main_parser.Methods.Where(x => !remove.Methods.Any(y => y.Name == x.Name)).ToList();
63+
return main_parser;
6864
}
69-
public static Parser OpenCache(string file, params string[] files) =>
70-
OpenCache(files.Prepend(file).ToArray());
71-
public static Parser OpenCache(string[] files)
65+
public static Parser CombineParsers(Parser[] parsers)
7266
{
73-
Parser parser = new Parser();
74-
75-
for (int i = 0; i < files.Length; i++)
67+
Parser parser = parsers.First();
68+
foreach(var p in parsers.Skip(1))
7669
{
77-
FileInfo fileInfo = new FileInfo(files[i]);
78-
if (!fileInfo.Exists) continue;
79-
80-
Parser cache;
81-
82-
if (fileInfo.Name == "package.json")
83-
{
84-
Project project = JsonConvert.DeserializeObject<Project>(File.ReadAllText(fileInfo.FullName));
85-
cache = OpenCache(project.Files.Select(x=> Path.Combine(fileInfo.DirectoryName, x)).ToArray());
86-
87-
if (cache.Classes.Count == 0 && cache.Methods.Count == 0 && cache.LinesWithTokens.Length == 0)
88-
cache.Parse(string.Join("\n\n// End of File\n\n", project.Files.Select(x => File.ReadAllText(Path.Combine(fileInfo.DirectoryName, x)))));
89-
90-
CombineParsers(parser, cache);
91-
}
92-
93-
FileInfo Read = new FileInfo(Path.Combine(fileInfo.DirectoryName, Folder, Path.GetFileNameWithoutExtension(fileInfo.Name) + Extension));
94-
if (!Read.Exists) continue;
95-
96-
cache = JsonConvert.DeserializeObject<Parser>(File.ReadAllText(Read.FullName));
97-
98-
parser = CombineParsers(parser, cache);
70+
parser = CombineParsers(parser, p);
9971
}
10072
return parser;
10173
}
10274
public static Parser CombineParsers(Parser parser, Parser p2)
10375
{
104-
parser.Code += p2.Code + "\n\n// End of File";
76+
parser.Code += "\n\n//NextFile\n\n" + p2.Code + "\n\n";
10577
parser.LinesWithTokens = [.. parser.LinesWithTokens, .. p2.LinesWithTokens];
10678
parser.Classes = [.. parser.Classes, .. p2.Classes];
10779
parser.Methods = [.. parser.Methods, .. p2.Methods];

0 commit comments

Comments
 (0)