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

Commit

Permalink
Build one to throw away.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeattie committed Jul 14, 2024
1 parent 757c79e commit 5a429dc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
12 changes: 6 additions & 6 deletions Starship/Rockstar.Engine/rockstar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ statement <Statement>
/ return
/ loop
/ conditional
/ operation
/ operation
/ e:expression { new ExpressionStatement(e, state.Source()) }

break_stmt <Statement>
Expand Down Expand Up @@ -77,7 +77,6 @@ return <Return>
operation <Statement>
= listen_stmt / output_stmt / crement / mutation / assign_stmt / rounding


listen_stmt <Statement>
= listen _ to _ v:variable
{ new Listen(v, state.Source()) }
Expand All @@ -94,11 +93,11 @@ block <Block>

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

alternate <Block>
= _ 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() }
Expand All @@ -111,7 +110,8 @@ conditional <Statement>

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

loop <Loop>
= while _ e:expression s:loopable
Expand Down
12 changes: 12 additions & 0 deletions Starship/Rockstar.Test/FixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ private string RunProgram(Block program, Queue<string>? 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);
Expand Down
10 changes: 7 additions & 3 deletions Starship/Rockstar.Test/FixtureTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 17 additions & 0 deletions Starship/Rockstar.Test/ParserTests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down
11 changes: 2 additions & 9 deletions Starship/Rockstar.Test/ParserTests/LoopTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Starship/Rockstar.Test/Rockstar.Test.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ProjectConfiguration>
<Settings>
<DefaultTestTimeout>5000</DefaultTestTimeout>
<DefaultTestTimeout>0</DefaultTestTimeout>
</Settings>
</ProjectConfiguration>

0 comments on commit 5a429dc

Please sign in to comment.