Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
gaaah.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeattie committed Jul 14, 2024
1 parent 1d8671f commit 757c79e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Starship/Rockstar.Engine/Values/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,6 +22,7 @@ public Value Plus(IEnumerable<Value> 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, _)
Expand Down
2 changes: 1 addition & 1 deletion Starship/Rockstar.Engine/rockstar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ conditional <Statement>

loopable <Block>
= _ s:statement { new Block(s) }
/ EOL b:block EOL { b }
/ EOL _? b:block EOL { b }

loop <Loop>
= while _ e:expression s:loopable
Expand Down
42 changes: 42 additions & 0 deletions Starship/Rockstar.Test/ParserTests/ArrayTests.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
19 changes: 19 additions & 0 deletions Starship/Rockstar.Test/ParserTests/LoopTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 757c79e

Please sign in to comment.