Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
29c9805
Initial Commit of CodeBlocks
dustin-wojciechowski Feb 8, 2024
b2f59f8
Code Block Tests added
dustin-wojciechowski Feb 9, 2024
0c96dc2
Renaming, small changes
dustin-wojciechowski Feb 12, 2024
f69d653
Auto-format source code
Feb 12, 2024
117601f
Added Print(StreamWriter) method to ICodeBlocks
dustin-wojciechowski Feb 15, 2024
9a34606
Removed old Print method, created utility method for tests
dustin-wojciechowski Feb 15, 2024
7175e97
Added support for multiple arguments in method signature, constructor…
dustin-wojciechowski Feb 20, 2024
e80f685
Added IfElse and Else to IfBlock, added more tests including a Compre…
dustin-wojciechowski Feb 20, 2024
0c83dec
Auto-format source code
Feb 20, 2024
bc804ab
Removed unused print method
dustin-wojciechowski Feb 21, 2024
05d39ff
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski Feb 21, 2024
65cf116
Removed currentIndent from constructor arguments
dustin-wojciechowski Feb 22, 2024
730f06f
Removed unnecessary SetIndent method
dustin-wojciechowski Feb 22, 2024
228740f
Added test for MethodBlock
dustin-wojciechowski Feb 22, 2024
380fa57
Removed unnecessary headings
dustin-wojciechowski Feb 22, 2024
ef1d36d
Auto-format source code
Feb 22, 2024
6549373
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski Feb 22, 2024
6312b41
Changed namespace, changed newLine to NewLine
dustin-wojciechowski Feb 23, 2024
cfa59b3
Changed StringWriter to TextWriter
dustin-wojciechowski Feb 23, 2024
034e6f3
Changed single character strings to chars, got rid of some string con…
dustin-wojciechowski Feb 23, 2024
0ee0b6f
Added Codeblock constructors that take in param string[], modified te…
dustin-wojciechowski Feb 26, 2024
d122b11
Use of StringBuilder in MethodBlock
dustin-wojciechowski Feb 26, 2024
3ffc2ff
Changed to use params
dustin-wojciechowski Feb 27, 2024
f9b5a73
Formatting of expected text
dustin-wojciechowski Feb 27, 2024
4a90b3f
Auto-format source code
Feb 27, 2024
dfcb2d1
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski Feb 27, 2024
f240249
Changed CodeBlock constructors to add range to Blocks versus assignment
dustin-wojciechowski Feb 29, 2024
be5b261
Changed one more instance of Blocks.AddRange, some IDE suggestions
dustin-wojciechowski Feb 29, 2024
e452a8e
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski Feb 29, 2024
8cef401
Easier to read API for MethodBlock
dustin-wojciechowski Mar 1, 2024
3d76352
Replace AddBlock with Add
dustin-wojciechowski Mar 1, 2024
257b8ef
Easier API usage
dustin-wojciechowski Mar 4, 2024
6b7b141
Enable method chaining in the IfBlock, modified tests to use them
dustin-wojciechowski Mar 4, 2024
c60f3f9
Enabled chaining for IfBlock constructors that take in blocks
dustin-wojciechowski Mar 5, 2024
9cd1a67
Rewrote ComprehensiveTest to use new api design
dustin-wojciechowski Mar 5, 2024
a5df70c
IfBlock now writes directly to TextWriter instead of using string con…
dustin-wojciechowski Mar 5, 2024
52a06fd
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski Mar 5, 2024
f77ade5
Auto-format source code
Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/bgen/CodeBlocks/BlockContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.IO;

public class BlockContainer : CodeBlock {
public override void Print (TextWriter writer)
{
foreach (ICodeBlock block in Blocks) {
block.SetIndent (CurrentIndent);
block.Print (writer);
}
}
}
108 changes: 108 additions & 0 deletions src/bgen/CodeBlocks/CodeBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class CodeBlock : ICodeBlock, IEnumerable<ICodeBlock> {
protected int CurrentIndent = 0;
protected int Indent = 4;
protected string HeaderText = string.Empty;
protected List<ICodeBlock> Blocks = new ();
protected const char StartBrace = '{';
protected const char EndBrace = '}';
protected const char NewLine = '\n';

public CodeBlock () { }

public CodeBlock (string text)
{
HeaderText = text;
}

public CodeBlock (string text, IList<ICodeBlock> blocks)
{
Blocks.AddRange (blocks);
HeaderText = text;
}

public CodeBlock (IList<ICodeBlock> blocks)
{
Blocks.AddRange (blocks);
}

public CodeBlock (string headerText, params string [] lines)
{
HeaderText = headerText;
foreach (string line in lines) {
Blocks.Add (new LineBlock (line));
}
}

public CodeBlock (params string [] lines)
{
foreach (string line in lines) {
Blocks.Add (new LineBlock (line));
}
}

public void Add (ICodeBlock block)
{
block.SetIndent (CurrentIndent + Indent);
Blocks.Add (block);
}

public void Add (string text)
{
LineBlock line = new LineBlock (text);
line.SetIndent (CurrentIndent + Indent);
Blocks.Add (line);
}

public void AddLine (string text)
{
LineBlock line = new LineBlock (text);
line.SetIndent (CurrentIndent + Indent);
Blocks.Add (line);
}

protected void WriteIndent (TextWriter writer)
{
for (var i = 0; i < CurrentIndent; i++)
writer.Write (' ');
}

protected virtual void WriteHeaderText (TextWriter writer)
{
WriteIndent (writer);
writer.Write (HeaderText);
writer.Write (NewLine);
}

public virtual void Print (TextWriter writer)
{
if (HeaderText != string.Empty)
WriteHeaderText (writer);

WriteIndent (writer);
writer.Write (StartBrace);
writer.Write (NewLine);

SetIndent (CurrentIndent + Indent);
foreach (ICodeBlock block in Blocks) {
block.SetIndent (CurrentIndent);
block.Print (writer);
}
SetIndent (CurrentIndent - Indent);

WriteIndent (writer);
writer.Write (EndBrace);
writer.Write (NewLine);
}

public void SetIndent (int indent)
{
CurrentIndent = indent;
}

public IEnumerator<ICodeBlock> GetEnumerator () => Blocks.GetEnumerator ();
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
}
7 changes: 7 additions & 0 deletions src/bgen/CodeBlocks/ICodeBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.IO;

public interface ICodeBlock {
public void Print (TextWriter writer);
public void SetIndent (int indent);
}

102 changes: 102 additions & 0 deletions src/bgen/CodeBlocks/IfBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.IO;

public class IfBlock : CodeBlock {
List<IfBlock> ElseIfBlocks = new ();
CodeBlock? ElseBlock = null;
public IfBlock (string condition)
{
HeaderText = condition;
}

public IfBlock (string condition, List<ICodeBlock> blocks)
{
HeaderText = condition;
Blocks.AddRange (blocks);
}

public IfBlock (string condition, params string [] lines)
{
HeaderText = condition;
Blocks.AddRange (new CodeBlock (lines));
}

public IfBlock AddElseIf (string condition, List<ICodeBlock> blocks)
{
ElseIfBlocks.Add (new IfBlock (condition, blocks));
return this;
}

public IfBlock AddElseIf (string condition, params string [] lines)
{
ElseIfBlocks.Add (new IfBlock (condition, lines));
return this;
}

public IfBlock AddElse (List<ICodeBlock> blocks)
{
ElseBlock = new CodeBlock ("else", blocks);
return this;
}

public IfBlock AddElse (params string [] lines)
{
ElseBlock = new CodeBlock ("else", lines);
return this;
}

protected override void WriteHeaderText (TextWriter writer)
{
WriteIndent (writer);
writer.Write ("if (");
writer.Write (HeaderText);
writer.Write (')');
writer.Write (NewLine);
}

protected void WriteElseHeaderText (TextWriter writer)
{
WriteIndent (writer);
writer.Write ("else if (");
writer.Write (HeaderText);
writer.Write (')');
writer.Write (NewLine);
}

public override void Print (TextWriter writer)
{
if (HeaderText != string.Empty)
WriteHeaderText (writer);

WriteIndent (writer);
writer.Write (StartBrace);
writer.Write (NewLine);

SetIndent (CurrentIndent + Indent);
foreach (ICodeBlock block in Blocks) {
block.SetIndent (CurrentIndent);
block.Print (writer);
}
SetIndent (CurrentIndent - Indent);
WriteIndent (writer);
writer.Write (EndBrace);
writer.Write (NewLine);

foreach (IfBlock block in ElseIfBlocks) {
block.SetIndent (CurrentIndent);
block.WriteElseHeaderText (writer);
writer.Write (StartBrace);
writer.Write (NewLine);
foreach (ICodeBlock b in block.Blocks) {
b.SetIndent (CurrentIndent + Indent);
b.Print (writer);

}
writer.Write (EndBrace);
writer.Write (NewLine);
}

if (ElseBlock is not null)
ElseBlock.Print (writer);
}
}
30 changes: 30 additions & 0 deletions src/bgen/CodeBlocks/LineBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.IO;

public class LineBlock : ICodeBlock {
readonly string line;
int currentIndent = 0;
const char newLine = '\n';

public LineBlock (string line)
{
this.line = line;
}

public void SetIndent (int indent)
{
currentIndent = indent;
}

public void WriteIndent (TextWriter writer)
{
for (var i = 0; i < currentIndent; i++)
writer.Write (' ');
}

public void Print (TextWriter writer)
{
WriteIndent (writer);
writer.Write (line);
writer.Write (newLine);
}
}
29 changes: 29 additions & 0 deletions src/bgen/CodeBlocks/MethodBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Text;

public class MethodBlock : CodeBlock {
public MethodBlock (string methodSignature, params string [] parameters)
{
StringBuilder allParameters = new ();
for (int i = 0; i < parameters.Length; i++) {
allParameters.Append (parameters [i]);
if (i != parameters.Length - 1) {
allParameters.Append (", ");
}
}
HeaderText = methodSignature + "(" + allParameters + ")";
}

public MethodBlock (string methodSignature, List<ICodeBlock> blocks, params string [] parameters)
{
StringBuilder allParameters = new ();
for (int i = 0; i < parameters.Length; i++) {
allParameters.Append (parameters [i]);
if (i != parameters.Length - 1) {
allParameters.Append (", ");
}
}
HeaderText = methodSignature + "(" + allParameters + ")";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of string concatenation in this class.

this.Blocks = blocks;
}
}
6 changes: 6 additions & 0 deletions src/generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@
<Compile Include="bgen\Models\MemberInformation.cs" />
<Compile Include="bgen\Models\TrampolineInfo.cs" />
<Compile Include="bgen\Models\WrapPropMemberInformation.cs" />
<Compile Include="bgen\CodeBlocks\ICodeBlock.cs" />
<Compile Include="bgen\CodeBlocks\CodeBlock.cs" />
<Compile Include="bgen\CodeBlocks\MethodBlock.cs" />
<Compile Include="bgen\CodeBlocks\IfBlock.cs" />
<Compile Include="bgen\CodeBlocks\LineBlock.cs" />
<Compile Include="bgen\CodeBlocks\BlockContainer.cs" />
<Compile Include="Resources.Designer.cs">
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions tests/bgen/bgen-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<Compile Include="..\generator\BGenTool.cs">
<Link>BGenTool.cs</Link>
</Compile>
<Compile Include="..\generator\CodeBlockTests.cs">
<Link>CodeBlockTests.cs</Link>
</Compile>
<Compile Include="..\generator\AttributeFactoryTests.cs">
<Link>AttributeFactoryTests.cs</Link>
</Compile>
Expand Down
Loading