Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from DemonExposer/requires
Browse files Browse the repository at this point in the history
Requires
  • Loading branch information
DemonExposer authored Aug 19, 2022
2 parents c16bb89 + f1988f2 commit 3b7c69f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
10 changes: 7 additions & 3 deletions Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public static Token Parse(Token[] line, int i, string[] lines, ref int lineNo, i
break;
case VariableToken vt: {
if (i + 1 < line.Length)
switch (line[i + 1]) {
switch (line[i+1]) {
case ParenthesesOperator:
vt.Args = Parse(line, i + 1, lines, ref lineNo, depth + 1);
break;
Expand All @@ -359,7 +359,7 @@ public static Token Parse(Token[] line, int i, string[] lines, ref int lineNo, i
statement.Left = po;

int numBrackets = 0;
int j = i + 1;
int j = i+1;
do {
if (Program.ClosingBrackets.Contains(line[j].Str))
numBrackets--;
Expand All @@ -372,6 +372,10 @@ public static Token Parse(Token[] line, int i, string[] lines, ref int lineNo, i
statement.Right = CurlyBracketsParse(line, lines, ref lineNo, statement, depth + 1);
break;
}
case RequireStatement reqStat:
reqStat.Child = Parse(line, i + 1, lines, ref lineNo, depth + 1);
reqStat.ParseImportFile();
break;
case ElseStatement or ClassStatement: {
if (t is ClassStatement classStat)
classStat.Name = line[i + 1].Str;
Expand All @@ -391,7 +395,7 @@ public static Token Parse(Token[] line, int i, string[] lines, ref int lineNo, i
unStat.Child = po;
break;
}
case MultilineStatementOperator mso: { // TODO: fix parsing of nested dictionaries
case MultilineStatementOperator mso: {
if (mso.Str == "}")
break;

Expand Down
21 changes: 11 additions & 10 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Program {
public static TrieDictionary<int> Priorities = new ();
public static List<string> OpeningBrackets = new ();
public static List<string> ClosingBrackets = new ();
private static TrieDictionary<Object> vars = new ();
public static TrieDictionary<Object> Vars = new ();

public static void Main(string[] args) {
// Arithmetic
Expand Down Expand Up @@ -72,6 +72,7 @@ public static void Main(string[] args) {
Bindings.Insert("function", typeof(FunctionStatement));
Bindings.Insert("return", typeof(ReturnStatement));
Bindings.Insert("class", typeof(ClassStatement));
Bindings.Insert("require", typeof(RequireStatement));

// Separators
Bindings.Insert(",", typeof(CommaSeparator));
Expand Down Expand Up @@ -108,13 +109,13 @@ public static void Main(string[] args) {
Priorities.Insert("decl", 11);

// Standard defined variables
vars.Insert("print", new Function(new [] {new FunctionArgument {ArgType = typeof(Object), Name = "args", IsUnlimited = true}}, new Print(null!)));
vars.Insert("read", new Function(new FunctionArgument[0], new Read(null!)));
vars.Insert("false", new Boolean(false));
vars.Insert("true", new Boolean(true));
vars.Insert("args", new Array(new ArraySegment<string>(args, 1, args.Length-1).Select(s => new String(s))));
vars.Insert("null", new Null());
vars.Insert("File", new Builtin.Classes.File());
Vars.Insert("print", new Function(new [] {new FunctionArgument {ArgType = typeof(Object), Name = "args", IsUnlimited = true}}, new Print(null!)));
Vars.Insert("read", new Function(new FunctionArgument[0], new Read(null!)));
Vars.Insert("false", new Boolean(false));
Vars.Insert("true", new Boolean(true));
Vars.Insert("args", new Array(new ArraySegment<string>(args, 1, args.Length-1).Select(s => new String(s))));
Vars.Insert("null", new Null());
Vars.Insert("File", new Builtin.Classes.File());

string[] lines = File.ReadAllLines(args[0]);
for (int i = 0; i < lines.Length; i++) {
Expand All @@ -136,7 +137,7 @@ public static void Main(string[] args) {
Token tree = Parser.Parse(tokenizedLine, Parser.GetTopElementIndex(tokenizedLine, 0, true), lines, ref i, 0);

// Console.WriteLine(tree.ToString(0));
tree.Evaluate(new List<TrieDictionary<Object>> {vars});
tree.Evaluate(new List<TrieDictionary<Object>> {Vars});
}
}
}
}
48 changes: 48 additions & 0 deletions Tokens/Statements/Unary/RequireStatement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Interpreter.Builtin.Functions;
using Interpreter.Types;
using Interpreter.Types.Function;
using TrieDictionary;
using Array = Interpreter.Types.Array;
using Object = Interpreter.Types.Object;
using Boolean = Interpreter.Types.Comparable.Boolean;

namespace Interpreter.Tokens.Statements.Unary;

public class RequireStatement : UnaryStatement {
private List<Token> importFile = new List<Token>();

public RequireStatement() {
Symbol = "require";
}

public override Object Evaluate(List<TrieDictionary<Object>> vars) {
TrieDictionary<Object> variables = new TrieDictionary<Object>();
variables["print"] = Program.Vars["print"];
variables["read"] = Program.Vars["read"];
variables["false"] = Program.Vars["false"];
variables["true"] = Program.Vars["true"];
variables["args"] = Program.Vars["args"];
variables["null"] = Program.Vars["null"];
variables["File"] = Program.Vars["File"];
variables["export"] = new Dictionary();

List<TrieDictionary<Object>> variableList = new List<TrieDictionary<Object>> {variables};
importFile.ForEach(t => t.Evaluate(variableList));
return variables["export"];
}

public void ParseImportFile() {
string[] lines = File.ReadAllLines(Child.Str + ".gsl");
for (int i = 0; i < lines.Length; i++) {
CheckedString[] lexedLine = Lexer.Lex(lines[i], i + 1);

lexedLine = Parser.CheckComment(lexedLine);
if (lexedLine.Length == 0)
continue;

Token[] tokenizedLine = Tokenizer.Tokenize(lexedLine);

importFile.Add(Parser.Parse(tokenizedLine, Parser.GetTopElementIndex(tokenizedLine, 0, true), lines, ref i, 0));
}
}
}
3 changes: 3 additions & 0 deletions examples/importing/main.gsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
decl Math = require math

print(Math.abs(5) : " " : Math.abs(-5))
9 changes: 9 additions & 0 deletions examples/importing/math.gsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function abs(num) {
on (num < 0) {
return (-num)
}

return (num)
}

export = {"abs": abs}

0 comments on commit 3b7c69f

Please sign in to comment.