diff --git a/Parser.cs b/Parser.cs index 3c5698d..13405e8 100644 --- a/Parser.cs +++ b/Parser.cs @@ -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; @@ -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--; @@ -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; @@ -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; diff --git a/Program.cs b/Program.cs index fa997c0..9fb65ce 100644 --- a/Program.cs +++ b/Program.cs @@ -23,7 +23,7 @@ public class Program { public static TrieDictionary Priorities = new (); public static List OpeningBrackets = new (); public static List ClosingBrackets = new (); - private static TrieDictionary vars = new (); + public static TrieDictionary Vars = new (); public static void Main(string[] args) { // Arithmetic @@ -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)); @@ -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(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(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++) { @@ -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> {vars}); + tree.Evaluate(new List> {Vars}); } } -} +} \ No newline at end of file diff --git a/Tokens/Statements/Unary/RequireStatement.cs b/Tokens/Statements/Unary/RequireStatement.cs new file mode 100644 index 0000000..8a0221d --- /dev/null +++ b/Tokens/Statements/Unary/RequireStatement.cs @@ -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 importFile = new List(); + + public RequireStatement() { + Symbol = "require"; + } + + public override Object Evaluate(List> vars) { + TrieDictionary variables = new TrieDictionary(); + 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> variableList = new List> {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)); + } + } +} \ No newline at end of file diff --git a/examples/importing/main.gsl b/examples/importing/main.gsl new file mode 100644 index 0000000..ca1bde3 --- /dev/null +++ b/examples/importing/main.gsl @@ -0,0 +1,3 @@ +decl Math = require math + +print(Math.abs(5) : " " : Math.abs(-5)) diff --git a/examples/importing/math.gsl b/examples/importing/math.gsl new file mode 100644 index 0000000..052a601 --- /dev/null +++ b/examples/importing/math.gsl @@ -0,0 +1,9 @@ +function abs(num) { + on (num < 0) { + return (-num) + } + + return (num) +} + +export = {"abs": abs} \ No newline at end of file