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

Commit

Permalink
Overhauled AST printer and added ASCII art with box drawing!
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeattie committed Jul 9, 2024
1 parent 67c28aa commit e35f5f1
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 52 deletions.
9 changes: 5 additions & 4 deletions Starship/Rockstar.Engine/Expressions/Binary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using Rockstar.Engine.Statements;
using Rockstar.Engine.Values;

namespace Rockstar.Engine.Expressions;
Expand Down Expand Up @@ -31,9 +32,9 @@ public Value Resolve(Func<Expression, Value> eval) {
};
}

public override void Print(StringBuilder sb, int depth) {
sb.Indent(depth).AppendLine($"{op}:".ToLowerInvariant());
lhs.Print(sb, depth + 1);
rhs.Print(sb, depth + 1);
public override void Print(StringBuilder sb, string prefix) {
lhs.Print(sb, prefix);
sb.Append(prefix).AppendLine($"{op}:".ToLowerInvariant());
rhs.Print(sb, prefix + Statement.INDENT);
}
}
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Expressions/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Rockstar.Engine.Expressions;

public abstract class Expression(Source source) {

public virtual void Print(StringBuilder sb, int depth)
=> sb.Indent(depth).AppendLine(this.GetType().Name.ToLowerInvariant());
public virtual void Print(StringBuilder sb, string prefix)
=> sb.Append(prefix).AppendLine(this.GetType().Name.ToLowerInvariant());

protected string Location => source.Location;
}
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Expressions/Looküp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class Looküp(Variable variable, Source source)
: Expression(source) {
public Variable Variable => variable;

public override void Print(StringBuilder sb, int depth)
=> sb.Indent(depth).AppendLine($"lookup: {variable.Name} ({variable.GetType().Name})");
public override void Print(StringBuilder sb, string prefix)
=> sb.Append(prefix).AppendLine($"lookup: {variable.Name} ({variable.GetType().Name})");
}
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Expressions/Pronoun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Pronoun(string name, Source source) : Variable(name, source) {
public Pronoun() : this(String.Empty) { }
public Pronoun(string name) : this(name, Source.None) { }
public override string Key => Name.ToLowerInvariant();
public override void Print(StringBuilder sb, int depth) {
sb.Indent(depth).AppendLine($"pronoun: {Name}");
public override void Print(StringBuilder sb, string prefix) {
sb.Append(prefix).AppendLine($"pronoun: {Name}");
}
}
7 changes: 4 additions & 3 deletions Starship/Rockstar.Engine/Expressions/Unary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Rockstar.Engine.Statements;
using System.Text;

namespace Rockstar.Engine.Expressions;
Expand All @@ -6,8 +7,8 @@ public class Unary(Operator op, Expression expr, Source source)
: Expression(source) {
public Operator Op => op;
public Expression Expr => expr;
public override void Print(StringBuilder sb, int depth) {
sb.Indent(depth).AppendLine("unary: {op}");
expr.Print(sb, depth+1);
public override void Print(StringBuilder sb, string prefix) {
sb.Append(prefix).AppendLine("unary: {op}");
expr.Print(sb, prefix + Statement.INDENT);
}
}
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Expressions/Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Rockstar.Engine.Expressions;

public abstract class Variable(string name, Source source) : Expression(source) {
public string Name => name;
public override void Print(StringBuilder sb, int depth) {
sb.Indent(depth).AppendLine($"variable: {name}");
public override void Print(StringBuilder sb, string prefix) {
sb.Append(prefix).AppendLine($"variable: {name}");
}

private static readonly Regex whitespace = new("\\s+", RegexOptions.Compiled);
Expand Down
8 changes: 4 additions & 4 deletions Starship/Rockstar.Engine/Statements/Assign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class Assign(Variable variable, Expression expr, Source source)
: Statement(source) {
public Variable Variable => variable;
public Expression Expr => expr;
public override void Print(StringBuilder sb, int depth) {
sb.Indent(depth).AppendLine($"assign:");
variable.Print(sb, depth + 1);
expr.Print(sb, depth + 1);
public override void Print(StringBuilder sb, string prefix) {
sb.Append(prefix).AppendLine($"assign:");
variable.Print(sb, prefix + INDENT);
expr.Print(sb, prefix + INDENT);
}
}
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Statements/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string ToString() {
return sb.ToString();
}

public void Print(StringBuilder sb, int depth = 0) {
foreach (var stmt in Statements) stmt.Print(sb, depth);
public void Print(StringBuilder sb, string prefix = "") {
foreach (var stmt in Statements) stmt.Print(sb, prefix);
}
}
16 changes: 8 additions & 8 deletions Starship/Rockstar.Engine/Statements/Conditional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public Conditional Else(Block alternate) {
this.Alternate = alternate;
return this;
}
public override void Print(StringBuilder sb, int depth = 0) {
sb.Indent(depth).AppendLine("if:");
sb.Indent(depth + 1).AppendLine("test:");
condition.Print(sb, depth + 2);
sb.Indent(depth + 1).AppendLine("then:");
consequent.Print(sb, depth + 2);
public override void Print(StringBuilder sb, string prefix = "") {
sb.Append(prefix).AppendLine("if:");
sb.Append(prefix + INDENT).AppendLine("test:");
condition.Print(sb, prefix + INDENT + INDENT);
sb.Append(prefix + INDENT).AppendLine("then:");
consequent.Print(sb, prefix + INDENT + INDENT);
if (Alternate.IsEmpty) return;
sb.Indent(depth + 1).AppendLine("else:");
Alternate.Print(sb, depth + 2);
sb.Append(prefix + INDENT).AppendLine("else:");
Alternate.Print(sb, prefix + INDENT + INDENT);
}
}
6 changes: 3 additions & 3 deletions Starship/Rockstar.Engine/Statements/Decrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Rockstar.Engine.Statements;
public class Decrement(Variable v, int multiple, Source source) : Statement(source) {
public Variable Variable => v;
public int Multiple => multiple;
public override void Print(StringBuilder sb, int depth = 0) {
sb.Indent(depth).AppendLine($"decrement x {multiple}");
v.Print(sb, depth + 1);
public override void Print(StringBuilder sb, string prefix = "") {
sb.Append(prefix).AppendLine($"decrement x {multiple}");
v.Print(sb, prefix + INDENT);
}
}
6 changes: 3 additions & 3 deletions Starship/Rockstar.Engine/Statements/Increment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Rockstar.Engine.Statements;
public class Increment(Variable v, int multiple, Source source) : Statement(source) {
public Variable Variable => v;
public int Multiple => multiple;
public override void Print(StringBuilder sb, int depth = 0) {
sb.Indent(depth).AppendLine($"increment x {multiple}");
v.Print(sb, depth + 1);
public override void Print(StringBuilder sb, string prefix = "") {
sb.Append(prefix).AppendLine($"increment x {multiple}");
v.Print(sb, prefix + INDENT);
}
}
13 changes: 7 additions & 6 deletions Starship/Rockstar.Engine/Statements/Loop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public abstract class Loop(Expression condition, bool compareTo, Block body, Sou
public Expression Condition => condition;
public Block Body => body;
protected abstract string LoopType { get; }
public override void Print(StringBuilder sb, int depth = 0) {
sb.Indent(depth).AppendLine($"{LoopType}:");
sb.Indent(depth + 1).AppendLine("test:");
condition.Print(sb, depth + 2);
sb.Indent(depth + 1).AppendLine("then:");
body.Print(sb, depth + 2);
public override void Print(StringBuilder sb, string prefix = "") {
sb.Append(prefix).AppendLine($"{LoopType}:");
sb.Append(prefix + " \u2502 " ).AppendLine("test:");
condition.Print(sb, prefix + " \u2502 " + INDENT);
sb.Append(prefix + " \u2502 ").AppendLine("then:");
body.Print(sb, prefix + " \u2502 " + INDENT);
sb.AppendLine(prefix + " \u2514".PadRight(40, '\u2500'));
}
}
6 changes: 3 additions & 3 deletions Starship/Rockstar.Engine/Statements/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Output(Expression expr, Source source)
public Expression Expr => expr;
public override string ToString() => $"output: {expr}";

public override void Print(StringBuilder sb, int depth = 0) {
sb.Indent(depth).AppendLine("output:");
expr.Print(sb, depth + 1);
public override void Print(StringBuilder sb, string prefix = "") {
sb.Append(prefix).AppendLine("output:");
expr.Print(sb, prefix + INDENT);
}
}
3 changes: 2 additions & 1 deletion Starship/Rockstar.Engine/Statements/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Rockstar.Engine.Statements;

public abstract class Statement(Source source) {
public abstract void Print(StringBuilder sb, int depth = 0);
public const string INDENT = " ";
public abstract void Print(StringBuilder sb, string prefix = "");
protected string Location => source.Location;
}

4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Values/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public Number(decimal value) : this(value, Source.None) { }
public override string ToString()
=> value.ToString(CultureInfo.InvariantCulture);

//public override void Print(StringBuilder sb, int depth)
// => sb.Indent(depth).AppendLine($"number: {value:G29} {Location}");
//public override void Print(StringBuilder sb, string prefix)
// => sb.Append(prefix).AppendLine($"number: {value:G29} {Location}");

public override bool Truthy => value != 0m;

Expand Down
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Values/Strïng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class Strïng(string value, Source source)
: Value(source) {
public Strïng(string value) : this(value, Source.None) { }
public string Value => value;
public override void Print(StringBuilder sb, int depth)
=> sb.Indent(depth).AppendLine($"string: \"{value}\"");
public override void Print(StringBuilder sb, string prefix)
=> sb.Append(prefix).AppendLine($"string: \"{value}\"");

public override bool Truthy => !String.IsNullOrEmpty(value);

Expand Down
4 changes: 2 additions & 2 deletions Starship/Rockstar.Engine/Values/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public abstract class Value(Source source)

public override string ToString() => ToStrïng().Value;

public override void Print(StringBuilder sb, int depth)
=> sb.Indent(depth).AppendLine($"{this.GetType().Name.ToLowerInvariant()}: {this.ToStrïng().Value}");
public override void Print(StringBuilder sb, string prefix)
=> sb.Append(prefix).AppendLine($"{this.GetType().Name.ToLowerInvariant()}: {this.ToStrïng().Value}");


public Strïng ToStrïng() => this switch {
Expand Down
1 change: 0 additions & 1 deletion Starship/Rockstar.Engine/rockstar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ statement <Statement>
/ increment
/ decrement


noise = (_ / [;,])

increment <Increment>
Expand Down
18 changes: 18 additions & 0 deletions Starship/Rockstar.Test/ConditionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ say done
""")]
public void ParserParsesLoop(string source) => Parse(source);

[Theory]
[InlineData("""
Say "begin"
X is 10
While X is greater than nothing
Y is 0
While Y is less than 3
Build Y up
Say Y
Knock X down
Say X
Say "end"
""")]
public void ParserParsesNestedLoop(string source) => Parse(source);

[Fact]
public void WhileLoopWorks() {
var source = """
Expand Down
5 changes: 5 additions & 0 deletions Starship/Rockstar.Test/Rockstar.Test.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<ProjectConfiguration>
<Settings>
<DefaultTestTimeout>5000</DefaultTestTimeout>
<IgnoredTests>
<FixtureTestSelector>
<FixtureName>Rockstar.Test.FixtureTests</FixtureName>
</FixtureTestSelector>
</IgnoredTests>
</Settings>
</ProjectConfiguration>

0 comments on commit e35f5f1

Please sign in to comment.