Skip to content

Commit

Permalink
Core parsing of groups - functions not yet included.
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS-1 committed Oct 9, 2024
1 parent d0de21c commit 222a334
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
24 changes: 14 additions & 10 deletions Library/Core/Model/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ public class Workspace : IFragment
return returnValue;
}

private void InspectBlockChain(Block? block)
private void InspectBlockChain(Block? block, List<GroupInfo> groups)
{
for (; block != null; block = block.Next)
{
System.Diagnostics.Debug.WriteLine(block.Type);
GroupInfo? info = null;

if (block is ExecutionGroup group)
{
System.Diagnostics.Debug.WriteLine(group.Fields["NAME"]);
groups.Add(info = new GroupInfo { Id = block.Id, Name = group.Fields["NAME"] });

InspectBlockChain(group.Values.Get("RESULT")?.Block);
}
var list = info?.Children ?? groups;

foreach (var value in block.Values)
InspectBlockChain(value.Block, list);

foreach (var statement in block.Statements)
InspectBlockChain(statement.Block);
InspectBlockChain(statement.Block, list);
}
}

Expand All @@ -73,8 +74,11 @@ private void InspectBlockChain(Block? block)
/// the script.
/// </summary>
/// <returns>The group information tree.</returns>
public async Task<int> GetGroupTreeAsync()
public async Task<List<GroupInfo>> GetGroupTreeAsync()
{
/* Resulting list. */
var groups = new List<GroupInfo>();

/* Use a dummy site. */
var context = new Context((IScriptSite)null!);

Expand All @@ -85,9 +89,9 @@ public async Task<int> GetGroupTreeAsync()
/* Inspect all blocks. */
foreach (var block in Blocks)
if (block is not ProceduresDef)
InspectBlockChain(block);
InspectBlockChain(block, groups);

return 0;
return groups;
}
}

2 changes: 1 addition & 1 deletion Library/Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private class ParsedScript(Workspace workspace) : IParsedScript
}

/// <inheritdoc/>
public Task<int> GetGroupTreeAsync() => _workspace.GetGroupTreeAsync();
public Task<List<GroupInfo>> GetGroupTreeAsync() => _workspace.GetGroupTreeAsync();
}

public IParsedScript Parse(string scriptAsText)
Expand Down
28 changes: 28 additions & 0 deletions Library/Scripting/Engine/GroupInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace BlocklyNet.Scripting.Engine;

/// <summary>
///
/// </summary>
public class GroupInfo
{
/// <summary>
///
/// </summary>
[NotNull, Required]
public string Id { get; set; } = null!;

/// <summary>
///
/// </summary>
[NotNull, Required]
public string? Name { get; set; }

/// <summary>
///
/// </summary>
[NotNull, Required]
public List<GroupInfo> Children { get; set; } = [];
}
2 changes: 1 addition & 1 deletion Library/Scripting/Parsing/IParsedScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface IParsedScript
/// <summary>
///
/// </summary>
Task<int> GetGroupTreeAsync();
Task<List<GroupInfo>> GetGroupTreeAsync();
}
2 changes: 1 addition & 1 deletion Tests/Engine/GroupAnalyserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ public async Task Can_Retrieve_Group_Structure_With_Functions_Async()

var tree = await script.GetGroupTreeAsync();

Assert.That(tree, Is.EqualTo(0));
Assert.That(tree, Has.Count.EqualTo(3));
}
}

0 comments on commit 222a334

Please sign in to comment.