From 5a429dc1ba0f5a21752c60ddf0a9947998c9cf63 Mon Sep 17 00:00:00 2001 From: Dylan Beattie Date: Sun, 14 Jul 2024 15:36:15 +0100 Subject: [PATCH] Build one to throw away. --- Starship/Rockstar.Engine/rockstar.peg | 12 ++++++------ Starship/Rockstar.Test/FixtureBase.cs | 12 ++++++++++++ Starship/Rockstar.Test/FixtureTests.cs | 10 +++++++--- .../Rockstar.Test/ParserTests/FunctionTests.cs | 17 +++++++++++++++++ Starship/Rockstar.Test/ParserTests/LoopTests.cs | 11 ++--------- .../Rockstar.Test.v3.ncrunchproject | 2 +- 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Starship/Rockstar.Engine/rockstar.peg b/Starship/Rockstar.Engine/rockstar.peg index 911595a..2093029 100644 --- a/Starship/Rockstar.Engine/rockstar.peg +++ b/Starship/Rockstar.Engine/rockstar.peg @@ -35,7 +35,7 @@ statement / return / loop / conditional - / operation + / operation / e:expression { new ExpressionStatement(e, state.Source()) } break_stmt @@ -77,7 +77,6 @@ return operation = listen_stmt / output_stmt / crement / mutation / assign_stmt / rounding - listen_stmt = listen _ to _ v:variable { new Listen(v, state.Source()) } @@ -94,11 +93,11 @@ block consequent = _ s:statement { new Block(s) } - / EOL b:block { b } + / EOL _? b:block { b } alternate - = _ else EOL b:block { b } - / _ else _ s:statement { new Block(s) } + = _ else _ s:statement { new Block(s) } + / _ else EOL b:block { b } / EOL+ _? else s:statement { new Block(s) } / EOL+ _? else EOL b:block { b } / EOL { new Block() } @@ -111,7 +110,8 @@ conditional loopable = _ s:statement { new Block(s) } - / EOL _? b:block EOL { b } + / EOL _? b:block noise* EOF { b } + / EOL _? b:block noise* EOL { b } loop = while _ e:expression s:loopable diff --git a/Starship/Rockstar.Test/FixtureBase.cs b/Starship/Rockstar.Test/FixtureBase.cs index 7fa648b..9e05eb6 100644 --- a/Starship/Rockstar.Test/FixtureBase.cs +++ b/Starship/Rockstar.Test/FixtureBase.cs @@ -79,6 +79,18 @@ private string RunProgram(Block program, Queue? inputs = null) { return env.Output; } + public void ParseFile(string filePath, string directory) { + var relativePath = Path.Combine(directory, filePath); + filePath = QualifyRelativePath(relativePath); + var source = File.ReadAllText(filePath, Encoding.UTF8); + try { + Parser.Parse(source); + } catch (FormatException ex) { + PrettyPrint(source, filePath, ex); + throw; + } + } + public void RunFile(string filePath, string directory) { var relativePath = Path.Combine(directory, filePath); filePath = QualifyRelativePath(relativePath); diff --git a/Starship/Rockstar.Test/FixtureTests.cs b/Starship/Rockstar.Test/FixtureTests.cs index 33c0e49..89b4c76 100644 --- a/Starship/Rockstar.Test/FixtureTests.cs +++ b/Starship/Rockstar.Test/FixtureTests.cs @@ -1,9 +1,13 @@ -using Rockstar.Test.ParserTests; - namespace Rockstar.Test; -public class FixtureTests(ITestOutputHelper testOutput) : FixtureBase(testOutput) { +public class RunFixtureTests(ITestOutputHelper testOutput) : FixtureBase(testOutput) { [Theory] [MemberData(nameof(AllFixtureFiles))] public void Fixture(string filePath) => RunFile(filePath, FIXTURES_DIRECTORY); +} + +public class ParseFixtureTests(ITestOutputHelper testOutput) : FixtureBase(testOutput) { + [Theory] + [MemberData(nameof(AllFixtureFiles))] + public void ParseFixture(string filePath) => ParseFile(filePath, FIXTURES_DIRECTORY); } \ No newline at end of file diff --git a/Starship/Rockstar.Test/ParserTests/FunctionTests.cs b/Starship/Rockstar.Test/ParserTests/FunctionTests.cs index cf83918..a53d9c1 100644 --- a/Starship/Rockstar.Test/ParserTests/FunctionTests.cs +++ b/Starship/Rockstar.Test/ParserTests/FunctionTests.cs @@ -73,6 +73,23 @@ say Function taking 2 Run(source).ShouldBe("4\n".ReplaceLineEndings()); } + [Fact] + public void ParseHelloWorldWorks() { + var source = """ + Eternity takes the pain. + The prize is silence + Until the pain is nothing, + Roll the pain into violence, + Cast violence into your lies, + Let the prize be with your lies. + + Give back the prize + + + """; + Parse(source).Statements.Count.ShouldBe(1); + } + [Fact] public void ParseFunctionWorks() { var source = """ diff --git a/Starship/Rockstar.Test/ParserTests/LoopTests.cs b/Starship/Rockstar.Test/ParserTests/LoopTests.cs index 0a84128..0d902cb 100644 --- a/Starship/Rockstar.Test/ParserTests/LoopTests.cs +++ b/Starship/Rockstar.Test/ParserTests/LoopTests.cs @@ -16,18 +16,11 @@ say done [Fact] public void ControlFlowIndentedElse() { var source = """ - my hope is less than my dream - My dream is a thought - My hope is a dream - While my hope is less than my dream - Knock my dream down - Shout my dream - If my dream is my hope + While true + If false Shout "No" Else Shout "Yes" - - """; var parsed = Parse(source); } diff --git a/Starship/Rockstar.Test/Rockstar.Test.v3.ncrunchproject b/Starship/Rockstar.Test/Rockstar.Test.v3.ncrunchproject index e0aae56..bf55c31 100644 --- a/Starship/Rockstar.Test/Rockstar.Test.v3.ncrunchproject +++ b/Starship/Rockstar.Test/Rockstar.Test.v3.ncrunchproject @@ -1,5 +1,5 @@  - 5000 + 0 \ No newline at end of file