Skip to content

Commit 78c076b

Browse files
I've done a lot of stuff. I forgot to commit a while ago so I don't exactly what I've done
1 parent 6e6e8ab commit 78c076b

7 files changed

Lines changed: 144 additions & 115 deletions

File tree

EZCode/EZHelp.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public string Format(object _text, object _char)
3737
chars[i] =
3838
chars[i] == '\\' ? '\\' :
3939
chars[i] == 'n' ? '\n' :
40+
chars[i] == 'r' ? '\r' :
4041
chars[i] == 'c' ? ',' :
4142
chars[i] == 'p' ? '.' :
4243
chars[i] == '"' ? '\'' :
@@ -354,5 +355,23 @@ public bool Compare(object v1)
354355
public bool BoolParse(object v) => bool.Parse(ObjectParse(v, "bool").ToString());
355356
public float FloatParse(object v) => float.Parse(ObjectParse(v, "float").ToString());
356357
public int IntParse(object v) => int.Parse(ObjectParse(v, "int").ToString());
358+
public int RunEZCodeWithPackage(string code, string package)
359+
{
360+
code = ObjectParse(code, "str").ToString() + Environment.NewLine;
361+
package = ObjectParse(package, "str").ToString();
362+
Parser parser = new Parser(string.Join(" ", code));
363+
parser = Package.ReturnParserWithPackages(parser, [package]);
364+
parser.Parse();
365+
Interpreter interpreter = new Interpreter($"{Interpreter.WorkingFile}(instance running from inside file)", parser);
366+
return interpreter.Interperate();
367+
}
368+
public int RunEZCode(string code)
369+
{
370+
code = ObjectParse(code, "str").ToString();
371+
Parser parser = new Parser(string.Join(" ", code));
372+
parser.Parse();
373+
Interpreter interpreter = new Interpreter($"{Interpreter.WorkingFile}(instance running from inside file)", parser);
374+
return interpreter.Interperate();
375+
}
357376
}
358377
}

EZCode/Interpreter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Interpreter(string file, Parser parser)
5757
public Container[] Containers { get => parser.Containers.ToArray(); }
5858
public Line CurrentLine { get; private set; }
5959
private bool StartMethodEntry = false;
60-
public int Interperate() => Interperate(parser.Tokens);
60+
public int Interperate() => Interperate(parser.LinesWithTokens);
6161
public int Interperate(LineWithTokens[] LineTokens)
6262
{
6363
int endcode = 0;
@@ -572,6 +572,10 @@ public int Interperate(LineWithTokens[] LineTokens)
572572
broke = true;
573573
break;
574574
}
575+
576+
if (line.Tokens.Length == 0)
577+
continue;
578+
575579
if (line.Tokens[0].Type == TokenType.Yield)
576580
{
577581
yielded = true;
@@ -603,7 +607,10 @@ public int Interperate(LineWithTokens[] LineTokens)
603607
LineWithTokens line = new LineWithTokens(argument.Tokens, argument.Line);
604608
object? result = null;
605609
try { result = SingleLine(line); } catch { }
606-
if (result == null) result = argument.Value;
610+
if (result is Class c)
611+
result = Argument.EvaluateTerm(c.Properties.FirstOrDefault(x => x.Name.ToLower() == "value").Value.ToString());
612+
if (result == null)
613+
result = argument.Value;
607614
bool? term = Argument.EvaluateTerm(result.ToString());
608615
if (term == null) throw new Exception($"Expected the argument section's method \"{argument.Value}\" to return boolean");
609616
return term;
@@ -763,6 +770,7 @@ public object GetValue(object obj, DataType? type = null)
763770
}
764771
try
765772
{
773+
if (obj == null) throw new Exception();
766774
switch (var.DataType.Type)
767775
{
768776
case DataType.Types._string: return obj.ToString();

EZCode/Package.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static Parser GetPackageAsParser(string package_name)
2929

3030
// CACHE SYSTEM NOT WORKING PROPERLY
3131

32-
if (parser.Classes.Count == 0 && parser.Methods.Count == 0 && parser.Containers.Count == 0 && parser.Tokens.Length == 0)
33-
parser.Tokenize(string.Join("\n\n// End of File\n\n", project.Files.Select(x => File.ReadAllText(Path.Combine(pack_dir, x)))));
32+
if (parser.Classes.Count == 0 && parser.Methods.Count == 0 && parser.Containers.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)))));
3434

3535
return parser;
3636
}
@@ -45,17 +45,17 @@ public static Parser ReturnParserWithPackages(Parser parser, string[] package_na
4545
}
4646
public static string SaveCache(string file, Parser parser)
4747
{
48-
if (parser.Tokens == null || parser.Tokens.Length == 0)
49-
parser.Tokenize();
48+
if (parser.LinesWithTokens == null || parser.LinesWithTokens.Length == 0)
49+
parser.Parse();
5050

5151
FileInfo fileInfo = new FileInfo(file);
5252
if (fileInfo.Name == "package.json")
5353
{
5454
Project project = JsonConvert.DeserializeObject<Project>(File.ReadAllText(fileInfo.FullName));
5555
Parser parse = OpenCache(project.Files.Select(x => Path.Combine(fileInfo.DirectoryName, x)).ToArray());
5656

57-
if (parse.Classes.Count == 0 && parse.Methods.Count == 0 && parse.Containers.Count == 0 && parse.Tokens.Length == 0)
58-
parse.Tokenize(string.Join("\n\n// End of File\n\n", project.Files.Select(x => File.ReadAllText(Path.Combine(fileInfo.DirectoryName, x)))));
57+
if (parse.Classes.Count == 0 && parse.Methods.Count == 0 && parse.Containers.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)))));
5959

6060
parser = parse;
6161
}
@@ -84,8 +84,8 @@ public static Parser OpenCache(string[] files)
8484
Project project = JsonConvert.DeserializeObject<Project>(File.ReadAllText(fileInfo.FullName));
8585
cache = OpenCache(project.Files.Select(x=> Path.Combine(fileInfo.DirectoryName, x)).ToArray());
8686

87-
if (cache.Classes.Count == 0 && cache.Methods.Count == 0 && cache.Containers.Count == 0 && cache.Tokens.Length == 0)
88-
cache.Tokenize(string.Join("\n\n// End of File\n\n", project.Files.Select(x => File.ReadAllText(Path.Combine(fileInfo.DirectoryName, x)))));
87+
if (cache.Classes.Count == 0 && cache.Methods.Count == 0 && cache.Containers.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)))));
8989

9090
CombineParsers(parser, cache);
9191
}
@@ -102,7 +102,7 @@ public static Parser OpenCache(string[] files)
102102
public static Parser CombineParsers(Parser parser, Parser p2)
103103
{
104104
parser.Code += p2.Code + "\n\n// End of File";
105-
parser.Tokens = [.. parser.Tokens, .. p2.Tokens];
105+
parser.LinesWithTokens = [.. parser.LinesWithTokens, .. p2.LinesWithTokens];
106106
parser.Classes = [.. parser.Classes, .. p2.Classes];
107107
parser.Methods = [.. parser.Methods, .. p2.Methods];
108108
parser.Containers = [.. parser.Containers, .. p2.Containers];

EZCode/Parser.cs

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -431,52 +431,67 @@ public enum TokenType
431431
public List<Class> Classes = [];
432432
public List<Container> Containers = [];
433433
public List<Method> Methods = [];
434-
public LineWithTokens[] Tokens = Array.Empty<LineWithTokens>();
434+
public LineWithTokens[] LinesWithTokens = Array.Empty<LineWithTokens>();
435435
public Parser() { }
436436
public Parser(string code)
437437
{
438438
Code = code;
439439
}
440-
public LineWithTokens[] Tokenize() => Tokens = Tokenize(Code);
441-
public LineWithTokens[] Tokenize(string code) => Tokens = TokenArray(code).Where(x => x.Line.Value.ToString() != "").ToArray();
440+
public LineWithTokens[] Parse() => LinesWithTokens = Parse(Code);
441+
public LineWithTokens[] Parse(string code) => LinesWithTokens = TokenArray(code).Where(x => x.Line.Value.ToString() != "").ToArray();
442442
private LineWithTokens[] TokenArray(string code, bool insideClass = false)
443443
{
444+
// Set the Code property to the parameter if it isn't null
444445
Code ??= code;
445-
List<LineWithTokens> withTokens = new List<LineWithTokens>();
446-
Line[] Lines = SplitLine(code);
446+
// The LineWithTokens list that gets returned
447+
List<LineWithTokens> lineWithTokens = new List<LineWithTokens>();
448+
// Splits the code into lines
449+
Line[] Lines = SplitLine(Code);
447450

451+
// loops through each line
448452
for (int i = 0; i < Lines.Length; i++)
449453
{
450-
List<Token> tokens;
451-
Line line = Lines[i];
452-
int continues = 0, arrow, ar = 0;
453-
string[] stringParts = { };
454+
// Some empty variables before looping through tokens
455+
List<Token> tokens; // tokens in the line
456+
Line line = Lines[i]; // current line
457+
string[] stringParts = []; // Needed for the 'SplitParts' method. each token as a string value
458+
int continues = 0, // used with the '->' token to continue the line to the next line
459+
// These are used to check if the line contains the '->' and there is still code after it. 2 code lines in 1 line
460+
arrow_output_index, // The output of the index after the '->'
461+
arrow_input_index = 0; // The input index for after the '->'
454462
do
455463
{
456-
tokens = new List<Token>();
457-
arrow = 0;
458-
stringParts = SplitParts(ref Lines, i, ar, ref stringParts, out _, out _, insideClass, true).Select(x=>x.ToString()).ToArray();
459-
object[] parts = SplitParts(ref Lines, i, ar, ref stringParts, out continues, out arrow, insideClass);
464+
tokens = new List<Token>(); // sets tokens to empty
465+
arrow_output_index = 0; // sets the arrow output to zero
466+
// gets the parts split by spaces. Tokens as strings
467+
stringParts = SplitParts(ref Lines, i, arrow_input_index, ref stringParts, out _, out _, insideClass, true).Select(x => x.ToString()).ToArray();
468+
// gets the token's values
469+
object[] parts = SplitParts(ref Lines, i, arrow_input_index, ref stringParts, out continues, out arrow_output_index, insideClass);
470+
// loops through each part and creates the token from it
460471
for (int j = 0; j < parts.Length; j++)
461472
{
462-
Token token = SingleToken(parts, j, stringParts.Length > j ? stringParts[j] : "", out bool stops);
463-
if (token.Type != TokenType.None && token.Type != TokenType.Comment) tokens.Add(token);
464-
if (stops) continue;
473+
// Creates the appropriate token for the part
474+
Token token = SingleToken(parts, j, stringParts.Length > j ? stringParts[j] : "");
475+
476+
// if the token is a comment or invalid, don't append the token to 'tokens'
477+
if (token.Type != TokenType.None || token.Type != TokenType.Comment) continue;
478+
tokens.Add(token);
465479
}
466-
ar = arrow;
480+
arrow_input_index = arrow_output_index; // sets the arrow input to the arrow output
467481

468-
withTokens.Add(new(tokens.ToArray(), line));
482+
// adds the line with the 'tokens' to 'lineWithTokens' list
483+
lineWithTokens.Add(new(tokens.ToArray(), line));
469484
}
470-
while (arrow != 0);
471-
i += continues;
472-
line.CodeLine += 1;
485+
while (arrow_output_index != 0); // checks if there isn't anymore arrows spliting lines
486+
487+
i += continues; // skips any lines that are apart of the line before it with the '->' token
488+
line.CodeLine += 1; // increments the line number by 1 so the first line is not 0
473489
}
474490

475-
return withTokens.ToArray();
491+
return lineWithTokens.ToArray();
476492
}
477-
internal Token SingleToken(object[] parts, int partIndex, string stringPart, out bool stops)
493+
internal Token SingleToken(object[] parts, int partIndex, string stringPart)
478494
{
479-
stops = false;
480495
TokenType tokenType = TokenType.None;
481496
if (parts[partIndex] is string)
482497
{
@@ -487,7 +502,7 @@ internal Token SingleToken(object[] parts, int partIndex, string stringPart, out
487502
case "!": case "not": tokenType = TokenType.Not; break;
488503
case "&": case "&&": case "and": tokenType = TokenType.And; break;
489504
case "|": case "||": case "or": tokenType = TokenType.Or; break;
490-
case "//": tokenType = TokenType.Comment; stops = true; break;
505+
case "//": tokenType = TokenType.Comment; break;
491506
case "=>": tokenType = TokenType.Arrow; break;
492507
case ":": tokenType = TokenType.Colon; break;
493508
case "{": tokenType = TokenType.OpenCurlyBracket; break;
@@ -559,9 +574,12 @@ internal Token SingleToken(object[] parts, int partIndex, string stringPart, out
559574
}
560575
public object[] SplitParts(ref Line[] lines, int lineIndex, int partStart, ref string[] stringParts, out int continues, out int arrow, bool insideClass = false, bool tostring = false)
561576
{
577+
// Current Line
562578
string line = lines[lineIndex].Value;
579+
// Get each token from the line. 'parts' is split by the token delimeters. 'partSpaces' is split by spaces
563580
object[] parts = SplitWithDelimiters(line, Delimeters).Where(x => x != "" && x != " ").Select(x => (object)x).Skip(partStart).ToArray();
564581
string[] partsSpaces = line.Split(" ").Where(x => x != "" && x != " ").ToArray();
582+
// Sets the out parameters to default
565583
continues = 0; arrow = 0;
566584
for (int i = 0; i < parts.Length; i++)
567585
{
@@ -1292,7 +1310,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
12921310
brackets = true;
12931311
end = parts.Length - j;
12941312
}
1295-
argTokens = argTokens.Append(SingleToken(parts, j, parts[partIndex].ToString(), out _)).ToArray();
1313+
argTokens = argTokens.Append(SingleToken(parts, j, parts[partIndex].ToString())).ToArray();
12961314
}
12971315
argTokens = argTokens.Take(argTokens.Length - end).ToArray();
12981316
if (argTokens.FirstOrDefault(new Token(TokenType.None, "", "")).Value.ToString() == "runexec")
@@ -1303,7 +1321,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
13031321
object[] objects = SplitParts(ref l, 0, 0, ref strParts, out _, out _);
13041322
Token[] t = [];
13051323
for (int i = 0; i < objects.Length; i++)
1306-
t = [.. t, SingleToken(objects, i, parts[partIndex].ToString(), out _)];
1324+
t = [.. t, SingleToken(objects, i, parts[partIndex].ToString())];
13071325
argTokens = t;
13081326
}
13091327
if (Statement.ConditionalTypes.Contains(parts[partIndex]))
@@ -1351,7 +1369,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
13511369
break;
13521370
code += bracketLine.Value + Environment.NewLine;
13531371
}
1354-
lineWithTokens = Tokenize(code);
1372+
lineWithTokens = Parse(code);
13551373
lineWithTokens = lineWithTokens.Select(x => { x.Line.CodeLine++; return x; }).ToArray();
13561374
if (lineWithTokens.Last().Line.Value.ToString() == "}")
13571375
{
@@ -1369,6 +1387,7 @@ private Statement SetStatement(ref Line[] lines, ref string[] strParts, int line
13691387
private Method SetMethod(Line[] lines, ref string[] strParts, int index, out string returns) => SetMethod(ref lines, ref strParts, index, out returns);
13701388
private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out string returns)
13711389
{
1390+
// Get the current line
13721391
Line line = lines[index];
13731392
line.CodeLine += 1;
13741393
returns = "";
@@ -1442,13 +1461,12 @@ private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out
14421461
param = param.Append(new Var(pName, pVal, line, type:pType, required:req)).ToArray();
14431462
}
14441463
}
1445-
1464+
int start = index + 1;
14461465
LineWithTokens[] lineWithTokens = [];
1447-
Line nextLine = lines[index + 1];
1466+
Line nextLine = lines[start];
14481467
bool sameLineBracket = nextLine.Value.StartsWith('{');
14491468
int curleyBrackets = sameLineBracket ? 0 : 1;
1450-
int[] indexes = [];
1451-
for (int i = index + 1; i < lines.Length; i++)
1469+
for (int i = start; i < lines.Length; i++)
14521470
{
14531471
Line bracketLine = lines[i];
14541472
Token[] bracketLineTokens = TokenArray(bracketLine.Value)[0].Tokens;
@@ -1457,7 +1475,7 @@ private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out
14571475
if (Statement.Types.Contains(bracketLineTokens[0].Value))
14581476
{
14591477
Statement statement = SetStatement(ref lines, ref strParts, i, 0);
1460-
bracketLineTokens = [SingleToken([statement], 0, string.Join(" ", statement.Line.Value), out _)];
1478+
bracketLineTokens = [SingleToken([statement], 0, string.Join(" ", statement.Line.Value))];
14611479
if (bracketLine.Value.Contains("{") && !bracketLine.Value.Contains("}")) curleyBrackets--;
14621480
}
14631481
} catch { }
@@ -1466,12 +1484,11 @@ private Method SetMethod(ref Line[] lines, ref string[] strParts, int index, out
14661484
if (bracketLine.Value.Contains('}'))
14671485
curleyBrackets--;
14681486
lineWithTokens = [.. lineWithTokens, new LineWithTokens(bracketLineTokens, bracketLine)];
1469-
indexes = [.. indexes, i];
14701487
if (curleyBrackets == 0)
14711488
break;
14721489
}
1473-
for (int i = 0; i < indexes.Length; i++)
1474-
lines = lines.Where((x, y) => y != indexes[i]).ToArray();
1490+
for (int i = 0; i < lineWithTokens.Length; i++)
1491+
lines = lines.Where((x, y) => y != start).ToArray();
14751492

14761493
if (lineWithTokens.Last().Line.Value.ToString() == "}")
14771494
{
@@ -1535,13 +1552,17 @@ internal static string[] SplitWithDelimiters(string input, char[] delimiters)
15351552
}
15361553
private static Line[] SplitLine(string code)
15371554
{
1555+
// Generates an empty Line array to return
15381556
Line[] lines = Array.Empty<Line>();
1539-
int i = 0;
1540-
foreach (var item in code.Split(Environment.NewLine).Select(s => s.Trim()).ToArray())
1557+
1558+
int index = 0; // index of loop
1559+
string[] string_lines = code.Split('\n').Select(s => s.Trim()).ToArray(); // splits code by each line
1560+
foreach (var item in string_lines)
15411561
{
1542-
lines = lines.Append(new Line(item, i)).ToArray();
1543-
i++;
1544-
};
1562+
// Append line to the array, 'lines'
1563+
lines = lines.Append(new Line(item, index)).ToArray();
1564+
index++; // increment the index by one
1565+
}
15451566
return lines;
15461567
}
15471568
}

0 commit comments

Comments
 (0)