Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Eforen committed Sep 8, 2023
1 parent b14040d commit 1b78fb3
Show file tree
Hide file tree
Showing 23 changed files with 736 additions and 120 deletions.
19 changes: 18 additions & 1 deletion src/Kore.AST/AstNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace Kore.AST {
using System.Text;

namespace Kore.AST {
public abstract class AstNode {
public const int DEBUG_INDENT_COUNT = 4;
public const int DEBUG_LINE_NUMBER_LEN = 5;
public int lineNumber = -1;
public abstract AstNode CallProcessor(ASTProcessor processor);

Expand All @@ -14,5 +18,18 @@ public abstract class AstNode {
public override int GetHashCode() {
return lineNumber.GetHashCode();
}

public string getDebugText() {
return getDebugText(0, new StringBuilder()).ToString();
}
protected StringBuilder addDebugTextHeader(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(lineNumber, indentLevel, builder);
}
protected StringBuilder addDebugTextHeader(int lineNumberOverride, int indentLevel, StringBuilder builder) {
if(lineNumberOverride < 0) return builder.Append(' ', DEBUG_LINE_NUMBER_LEN+1).Append(' ', indentLevel * DEBUG_INDENT_COUNT);
return builder.Append(lineNumberOverride.ToString().PadLeft(DEBUG_LINE_NUMBER_LEN, '0')).Append(':').Append(' ', indentLevel * DEBUG_INDENT_COUNT);
}

public abstract StringBuilder getDebugText(int indentLevel, StringBuilder builder);
}
}
8 changes: 7 additions & 1 deletion src/Kore.AST/CommentNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Kore.AST {
using System.Text;

namespace Kore.AST {

/// <summary>
/// Node representing a comment in the assembly code.
Expand Down Expand Up @@ -29,5 +31,9 @@ public class CommentNode : AstNode {
public override int GetHashCode() {
return Text.GetHashCode();
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"COMMENT {Text}");
}
}
}
4 changes: 4 additions & 0 deletions src/Kore.AST/DirectiveNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ public class DirectiveNode : AstNode {
public override int GetHashCode() {
return Name.GetHashCode();
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"DIRECTIVE {Name}");
}
}
}
4 changes: 4 additions & 0 deletions src/Kore.AST/InlineDirectiveNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@ public enum InlineDirectiveType {
public override int GetHashCode() {
return Name.GetHashCode();
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"INLINE DIRECTIVE {Name}");
}
}
}
10 changes: 10 additions & 0 deletions src/Kore.AST/InstructionNodeTypeB.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kore.RiscMeta;
using System.Text;

namespace Kore.AST {
/// <summary>
Expand Down Expand Up @@ -48,9 +49,14 @@ public InstructionNodeTypeBImmediate(Kore.RiscMeta.Instructions.TypeB op, Regist
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).Append($"TypeB {op} RS1:{rs1} RS2:{rs2} IMM:{imm}");
}
}
/// <summary>
/// Represents a B-Type RISC-V instruction.
/// TODO: This should likely be changed to the wrapper type of instruction so that we don't have this redundency
/// </summary>
public class InstructionNodeTypeBLabel : InstructionNode<Kore.RiscMeta.Instructions.TypeB> {
/// <summary>
Expand Down Expand Up @@ -96,5 +102,9 @@ public InstructionNodeTypeBLabel(Kore.RiscMeta.Instructions.TypeB op, Register r
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeB {op} RS1:{rs1} RS2:{rs2} LABEL:{label}");
}
}
}
4 changes: 4 additions & 0 deletions src/Kore.AST/InstructionNodeTypeI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ public InstructionNodeTypeI(Kore.RiscMeta.Instructions.TypeI op, Register rd, Re
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeI {op} RD:{rd} RS:{rs} IMM:{immediate}");
}
}
}
10 changes: 10 additions & 0 deletions src/Kore.AST/InstructionNodeTypeJ.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kore.RiscMeta;
using System.Text;

namespace Kore.AST {
/// <summary>
Expand Down Expand Up @@ -40,9 +41,14 @@ public class InstructionNodeTypeJImmediate : InstructionNode<Kore.RiscMeta.Instr
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).Append($"TypeJ {op} RD:{rd} IMM:{imm}");
}
}
/// <summary>
/// Represents a J-type instruction in the RISC-V assembly language.
/// TODO: This should likely be changed to the wrapper type of instruction so that we don't have this redundency
/// </summary>
public class InstructionNodeTypeJLabel : InstructionNode<Kore.RiscMeta.Instructions.TypeJ> {
/// <summary>
Expand Down Expand Up @@ -80,6 +86,10 @@ public class InstructionNodeTypeJLabel : InstructionNode<Kore.RiscMeta.Instructi
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeJ {op} RD:{rd} LABEL:{label}");
}
}
}

8 changes: 7 additions & 1 deletion src/Kore.AST/InstructionNodeTypeMisc.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Kore.AST {
using System.Text;

namespace Kore.AST {
/// <summary>
/// Represents a miscellaneous instruction in the RISC-V assembly language.
/// </summary>
Expand All @@ -20,5 +22,9 @@ public class InstructionNodeTypeMisc : InstructionNode<string> {
public override int GetHashCode() {
return op.GetHashCode();
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeMisc {op}");
}
}
}
5 changes: 5 additions & 0 deletions src/Kore.AST/InstructionNodeTypeR.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Kore.RiscMeta;
using Kore.RiscMeta.Instructions;
using System.Text;

namespace Kore.AST {
/// <summary>
Expand Down Expand Up @@ -48,5 +49,9 @@ public class InstructionNodeTypeR : InstructionNode<Kore.RiscMeta.Instructions.T
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeR {op} RD:{rd} RS1:{rs1} RS2:{rs2}");
}
}
}
4 changes: 4 additions & 0 deletions src/Kore.AST/InstructionNodeTypeS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ public class InstructionNodeTypeS : InstructionNode<Kore.RiscMeta.Instructions.T
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeS {op} RS1:{rs1} RS2:{rs2} IMM:{imm}");
}
}
}
4 changes: 4 additions & 0 deletions src/Kore.AST/InstructionNodeTypeU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ public class InstructionNodeTypeU : InstructionNode<Kore.RiscMeta.Instructions.T
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"TypeB {op} RD:{rd} IMM:{imm}");
}
}
}
4 changes: 4 additions & 0 deletions src/Kore.AST/IntDirectiveNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ public class IntDirectiveNode : DirectiveNode {
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"DIRECTIVE {Name} INT:{Value}");
}
}
}
8 changes: 7 additions & 1 deletion src/Kore.AST/LabelNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Kore.AST {
using System.Text;

namespace Kore.AST {

/// <summary>
/// Node representing a label in the assembly code.
Expand Down Expand Up @@ -29,5 +31,9 @@ public class LabelNode : AstNode {
public override int GetHashCode() {
return Name.GetHashCode();
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"LABEL {Name}");
}
}
}
10 changes: 10 additions & 0 deletions src/Kore.AST/LabeledInlineDirectiveNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class LabeledInlineDirectiveNode : InlineDirectiveNode {
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).Append($"INLINE DIRECTIVE {Name} LABEL:{Label}");
}
}
public class LabeledInlineDirectiveNode<T> : LabeledInlineDirectiveNode where T : InstructionNode {
/// <summary>
Expand All @@ -54,5 +58,11 @@ public class LabeledInlineDirectiveNode<T> : LabeledInlineDirectiveNode where T
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
addDebugTextHeader(indentLevel, builder).AppendLine($"INLINE DIRECTIVE {Name} LABEL:{Label} {{");
WrappedInstruction.getDebugText(indentLevel + 1, builder);
return addDebugTextHeader(indentLevel, builder).AppendLine("}");
}
}
}
10 changes: 10 additions & 0 deletions src/Kore.AST/ProgramNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Text;

namespace Kore.AST {
/// <summary>
/// The root node of the AST, representing a RISC-V program.
Expand Down Expand Up @@ -40,6 +42,14 @@ public class ProgramNode : AstNode {
Sections.GetHashCode();
return base.GetHashCode();
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
addDebugTextHeader(indentLevel, builder).AppendLine($"PROGRAM [{Sections.Count}]{{");
foreach(var section in Sections) {
section.getDebugText(indentLevel+1, builder);
}
return addDebugTextHeader(indentLevel, builder).AppendLine("}");
}
}
}

9 changes: 9 additions & 0 deletions src/Kore.AST/SectionNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kore.AST {
/// <summary>
Expand Down Expand Up @@ -54,6 +55,14 @@ public class SectionNode : AstNode {

return hash;
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
addDebugTextHeader(indentLevel, builder).AppendLine($"SECTION {Name} [{Contents.Count}]{{");
foreach(var node in Contents) {
node.getDebugText(indentLevel + 1, builder);
}
return addDebugTextHeader(indentLevel, builder).AppendLine("}");
}
}
}

4 changes: 4 additions & 0 deletions src/Kore.AST/StringDirectiveNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ public class StringDirectiveNode : DirectiveNode {
return hash;
}
}

public override StringBuilder getDebugText(int indentLevel, StringBuilder builder) {
return addDebugTextHeader(indentLevel, builder).AppendLine($"DIRECTIVE {Name} STRING:{Value}");
}
}
}
9 changes: 5 additions & 4 deletions src/Kore.Kuick.Tests/Lexer/LexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,10 @@ public class LexerTests {
[TestCase("s9", Lexer.Token.REGISTER, "x25")]
[TestCase("s10", Lexer.Token.REGISTER, "x26")]
[TestCase("s11", Lexer.Token.REGISTER, "x27")]
[TestCase("s12", Lexer.Token.REGISTER, "x28")]
[TestCase("t04", Lexer.Token.REGISTER, "x29")]
[TestCase("t05", Lexer.Token.REGISTER, "x30")]
[TestCase("t06", Lexer.Token.REGISTER, "x31")]
[TestCase("t3", Lexer.Token.REGISTER, "x28")]
[TestCase("t4", Lexer.Token.REGISTER, "x29")]
[TestCase("t5", Lexer.Token.REGISTER, "x30")]
[TestCase("t6", Lexer.Token.REGISTER, "x31")]
[TestCase("s01", Lexer.Token.REGISTER, "x9")]
[TestCase("a00", Lexer.Token.REGISTER, "x10")]
[TestCase("a01", Lexer.Token.REGISTER, "x11")]
Expand All @@ -720,6 +720,7 @@ public class LexerTests {
[TestCase("s07", Lexer.Token.REGISTER, "x23")]
[TestCase("s08", Lexer.Token.REGISTER, "x24")]
[TestCase("s09", Lexer.Token.REGISTER, "x25")]
[TestCase("t03", Lexer.Token.REGISTER, "x28")]
[TestCase("t04", Lexer.Token.REGISTER, "x29")]
[TestCase("t05", Lexer.Token.REGISTER, "x30")]
[TestCase("t06", Lexer.Token.REGISTER, "x31")]
Expand Down
Loading

0 comments on commit 1b78fb3

Please sign in to comment.