From d427c9bfed5a88311cd4fac7a4b35298b063a4e2 Mon Sep 17 00:00:00 2001 From: Marcus Aurelius Date: Tue, 19 Dec 2017 19:00:09 +0100 Subject: [PATCH] #3 Remove Parser We removed the parser and added a scan method instead of the parser in the scanner. --- Arrow.Core/Arrow.Core.csproj | 1 - Arrow.Core/ArrowCompiler.cs | 17 +++++++++-------- Arrow.Core/Parsing/Parser.cs | 24 ------------------------ Arrow.Core/Parsing/Scanner.cs | 9 +++++++++ Arrow.Core/Parsing/Syntax.cs | 2 ++ Arrow.Core/Parsing/SyntaxStream.cs | 4 +++- 6 files changed, 23 insertions(+), 34 deletions(-) delete mode 100644 Arrow.Core/Parsing/Parser.cs diff --git a/Arrow.Core/Arrow.Core.csproj b/Arrow.Core/Arrow.Core.csproj index 09953ca..832e722 100644 --- a/Arrow.Core/Arrow.Core.csproj +++ b/Arrow.Core/Arrow.Core.csproj @@ -63,7 +63,6 @@ - diff --git a/Arrow.Core/ArrowCompiler.cs b/Arrow.Core/ArrowCompiler.cs index 331c956..7d58b2c 100644 --- a/Arrow.Core/ArrowCompiler.cs +++ b/Arrow.Core/ArrowCompiler.cs @@ -1,4 +1,5 @@ using Arrow.Core.Parsing; +using Arrow.Core.Parsing.Definition; using Arrow.Core.Scanning; using Arrow.Core.Visitors; using System; @@ -18,8 +19,8 @@ public Func RunFunc(string input) var tokenDefinitions = TokenDefinitionCollection.LoadFromIntern(); var tokenizer = new Tokenizer(tokenDefinitions); var tokenResult = tokenizer.Parse(input); - var parser = new Parser(); - var synatxTree = parser.Parse(tokenResult); + var scanner = new Scanner(); + var synatxTree = new SyntaxTree(scanner.Scan(new SyntaxStream(tokenResult))); return synatxTree.Visit(); } @@ -28,8 +29,8 @@ public Action RunVoid(string input) var tokenDefinitions = TokenDefinitionCollection.LoadFromIntern(); var tokenizer = new Tokenizer(tokenDefinitions); var tokenResult = tokenizer.Parse(input); - var parser = new Parser(); - var synatxTree = parser.Parse(tokenResult); + var scanner = new Scanner(); + var synatxTree = new SyntaxTree(scanner.Scan(new SyntaxStream(tokenResult))); return synatxTree.Visit(); } @@ -43,10 +44,10 @@ public Assembly GetAssembly(string input) var tokenDefinitions = TokenDefinitionCollection.LoadFromIntern(); var tokenizer = new Tokenizer(tokenDefinitions); var tokenResult = tokenizer.Parse(input); - var parser = new Parser(); - var synatxTree = parser.Parse(tokenResult); + var scanner = new Scanner(); + var expression = scanner.Scan(new SyntaxStream(tokenResult)); - visitor.Visit(synatxTree.Expression, new ProgramScope(moduleBuilder)); + visitor.Visit(expression, new ProgramScope(moduleBuilder)); moduleBuilder.CreateGlobalFunctions(); @@ -55,4 +56,4 @@ public Assembly GetAssembly(string input) return assemblyBuilder; } } -} +} \ No newline at end of file diff --git a/Arrow.Core/Parsing/Parser.cs b/Arrow.Core/Parsing/Parser.cs deleted file mode 100644 index 20926dc..0000000 --- a/Arrow.Core/Parsing/Parser.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Arrow.Core.Parsing.Definition; -using Arrow.Core.Scanning; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Arrow.Core.Parsing -{ - public class Parser - { - Scanner scanner; - - public Parser() - { - scanner = new Scanner(); - } - - public SyntaxTree Parse(List tokens) => new SyntaxTree(scanner.Scan(new SyntaxStream(tokens))); - - - } -} diff --git a/Arrow.Core/Parsing/Scanner.cs b/Arrow.Core/Parsing/Scanner.cs index 8ff1347..55fcc0d 100644 --- a/Arrow.Core/Parsing/Scanner.cs +++ b/Arrow.Core/Parsing/Scanner.cs @@ -49,6 +49,15 @@ internal Syntax Scan(SyntaxStream syntaxStream) throw new Exception($"Syntax {syntaxStream.SyntaxList.FirstOrDefault()} not found!"); } + internal Syntax Scan(SyntaxStream syntaxStream) + where T : Syntax,new() + { + if (TryScan(syntaxStream, out var syntax)) + return syntax; + + throw new Exception($"Syntax {syntaxStream.SyntaxList.FirstOrDefault()} not found!"); + } + internal void Collect() { diff --git a/Arrow.Core/Parsing/Syntax.cs b/Arrow.Core/Parsing/Syntax.cs index 5df83a2..8a6b801 100644 --- a/Arrow.Core/Parsing/Syntax.cs +++ b/Arrow.Core/Parsing/Syntax.cs @@ -10,6 +10,8 @@ namespace Arrow.Core.Parsing { public abstract class Syntax : IScanable { + public int Position { get; set; } + public int Length { get; set; } public string Name { get; protected set; } diff --git a/Arrow.Core/Parsing/SyntaxStream.cs b/Arrow.Core/Parsing/SyntaxStream.cs index 8084204..6b0c313 100644 --- a/Arrow.Core/Parsing/SyntaxStream.cs +++ b/Arrow.Core/Parsing/SyntaxStream.cs @@ -11,7 +11,9 @@ namespace Arrow.Core.Parsing public class SyntaxStream { public int Count => baseStream != null ? length : syntaxList.Count; - + public int GlobalPosition => baseStream == null ? 0 : baseStream.GlobalPosition + index; + + private List syntaxList; private SyntaxStream baseStream; private int index;