From 757c79e7b503643d88efd6118a22694a8bad8a09 Mon Sep 17 00:00:00 2001 From: Dylan Beattie Date: Sun, 14 Jul 2024 01:51:40 +0100 Subject: [PATCH] gaaah. --- Starship/Rockstar.Engine/Values/Value.cs | 8 ++++ Starship/Rockstar.Engine/rockstar.peg | 2 +- .../Rockstar.Test/ParserTests/ArrayTests.cs | 42 +++++++++++++++++++ .../Rockstar.Test/ParserTests/LoopTests.cs | 19 +++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/Starship/Rockstar.Engine/Values/Value.cs b/Starship/Rockstar.Engine/Values/Value.cs index cac3f98..7f1e665 100644 --- a/Starship/Rockstar.Engine/Values/Value.cs +++ b/Starship/Rockstar.Engine/Values/Value.cs @@ -6,6 +6,13 @@ namespace Rockstar.Engine.Values; public abstract class Value(Source source) : Expression(source) { + + public static bool operator ==(Value? lhs, Value? rhs) + => lhs?.Equals(rhs) ?? rhs is null; + + public static bool operator !=(Value? lhs, Value? rhs) + => !(lhs == rhs); + public abstract bool Truthy { get; } public bool Falsy => !Truthy; @@ -15,6 +22,7 @@ public Value Plus(IEnumerable that) public Value Plus(Value that) => (this, that) switch { (Strïng a, _) => a.Concat(that), (_, Strïng b) => this.ToStrïng().Concat(b), + (Array a, IHaveANumber b) => new Number(a.Length.NumericValue + b.NumericValue), (IHaveANumber a, IHaveANumber b) => new Number(a.NumericValue + b.NumericValue), (IHaveANumber a, _) diff --git a/Starship/Rockstar.Engine/rockstar.peg b/Starship/Rockstar.Engine/rockstar.peg index c21b599..911595a 100644 --- a/Starship/Rockstar.Engine/rockstar.peg +++ b/Starship/Rockstar.Engine/rockstar.peg @@ -111,7 +111,7 @@ conditional loopable = _ s:statement { new Block(s) } - / EOL b:block EOL { b } + / EOL _? b:block EOL { b } loop = while _ e:expression s:loopable diff --git a/Starship/Rockstar.Test/ParserTests/ArrayTests.cs b/Starship/Rockstar.Test/ParserTests/ArrayTests.cs index 2e8dbc1..8366163 100644 --- a/Starship/Rockstar.Test/ParserTests/ArrayTests.cs +++ b/Starship/Rockstar.Test/ParserTests/ArrayTests.cs @@ -1,6 +1,48 @@ namespace Rockstar.Test.ParserTests; public class ArrayTests(ITestOutputHelper output) : ParserTestBase(output) { + [Fact] + public void ParseBigChunk() { + var source = """ + rock first with 0, 1, 2 + rock second with 3, 4, 5 + + if first ain't second + say "arrays of the same length but different contents are not equal" + + ArrayCopy takes source + rock dest + let i be 0 + let len be source + 0 + while i is less than len + let dest at i be source at i + build i up + + return dest + + let First Copy be ArrayCopy taking first + if First Copy is first + say "element-wise-copied arrays are equal" + + let Second Copy be second + if Second Copy is second + say "assignment-copied arrays are equal" + + rock First Nested with first, second + rock Second Nested with first, second + if First Nested is Second Nested + say "nested arrays with the same contents are equal" + + rock Third Nested with first, second + rock Fourth Nested with second, first + if Third Nested ain't Fourth Nested + say "nested arrays with different contents are not equal" + + """; + var parsed = Parse(source); + parsed.Statements.Count.ShouldBe(14); + } + [Fact] public void ParseRock() { var source = "rock first with 0, 1, 2"; diff --git a/Starship/Rockstar.Test/ParserTests/LoopTests.cs b/Starship/Rockstar.Test/ParserTests/LoopTests.cs index 3ccb048..0a84128 100644 --- a/Starship/Rockstar.Test/ParserTests/LoopTests.cs +++ b/Starship/Rockstar.Test/ParserTests/LoopTests.cs @@ -13,6 +13,25 @@ say done """)] public void ParserParsesLoop(string source) => Parse(source); + [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 + Shout "No" + Else + Shout "Yes" + + + """; + var parsed = Parse(source); + } + [Theory] [InlineData(""" Say "begin"