Skip to content

Commit 11f86a1

Browse files
💾 Feat(BlockScript): 增强内置函数定义,支持语句类型映射与字段提取,彻底清理散弹式修改的旧方法
1 parent 2f7ef6d commit 11f86a1

16 files changed

Lines changed: 175 additions & 318 deletions

File tree

KitX Clients/KitX Core/KitX.Core.BluePrint.Test/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using KitX.Core.DI;
66
using KitX.Core.Contract.Workflow;
77
using KitX.Core.Workflow.Blueprint;
8+
using KitX.Core.Workflow.BlockScripting;
89

910
namespace KitX.Core.BluePrint.Test;
1011

@@ -52,7 +53,8 @@ public static void Main(string[] args)
5253
}
5354
};
5455

55-
var converter = new BlockScriptToBlueprintConverter(parser, nodeRegistry, layoutService);
56+
var funcRegistry = sp.GetRequiredService<BuiltinFunctionRegistry>();
57+
var converter = new BlockScriptToBlueprintConverter(parser, nodeRegistry, layoutService, funcRegistry);
5658

5759
// ── Test A: Pre-expanded format (already in test script) ──
5860
Console.WriteLine("┌──────────────────────────────────────────┐");

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BlockStatementExtractor.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,8 @@ private void ExtractStatements(SyntaxNode root, BlockDefinition block, BlockType
212212
{
213213
var methodName = ExprUtils.GetMethodName(invoke);
214214

215-
if (methodName == Branch)
216-
{
217-
block.Statements.Add(CreateFlowControlStatement(invoke, FlowControlType.Branch, exprStmt.GetLineNumber(), exprText));
218-
}
219-
else if (methodName == Loop)
220-
{
221-
block.Statements.Add(CreateFlowControlStatement(invoke, FlowControlType.Loop, exprStmt.GetLineNumber(), exprText));
222-
}
223-
else if (methodName == LoopBodyEnd)
224-
{
225-
block.Statements.Add(CreateFlowControlStatement(invoke, FlowControlType.LoopBodyEnd, exprStmt.GetLineNumber(), exprText));
226-
}
227-
// Try BuiltinFunctionRegistry for new/future functions (e.g. Flip)
228-
else if (_functionRegistry != null && _functionRegistry.Get(methodName) is { } funcDef)
215+
// Try BuiltinFunctionRegistry for all registered functions
216+
if (_functionRegistry != null && _functionRegistry.Get(methodName) is { } funcDef)
229217
{
230218
var stmt = funcDef.ExtractStatement(invoke, exprStmt.GetLineNumber(), exprText);
231219
if (stmt != null)
@@ -256,20 +244,8 @@ private void ExtractStatements(SyntaxNode root, BlockDefinition block, BlockType
256244
var methodName = ExprUtils.GetMethodName(assignInvoke);
257245
Log.Debug("[BlockStatementExtractor] assignment.Right is InvocationExpressionSyntax, methodName = {MethodName}", methodName);
258246

259-
if (methodName == Branch)
260-
{
261-
block.Statements.Add(CreateFlowControlStatement(assignInvoke, FlowControlType.Branch, exprStmt.GetLineNumber(), exprText));
262-
}
263-
else if (methodName == Loop)
264-
{
265-
block.Statements.Add(CreateFlowControlStatement(assignInvoke, FlowControlType.Loop, exprStmt.GetLineNumber(), exprText));
266-
}
267-
else if (methodName == LoopBodyEnd)
268-
{
269-
block.Statements.Add(CreateFlowControlStatement(assignInvoke, FlowControlType.LoopBodyEnd, exprStmt.GetLineNumber(), exprText));
270-
}
271-
// Try BuiltinFunctionRegistry for new/future functions (e.g. Flip)
272-
else if (_functionRegistry != null && _functionRegistry.Get(methodName) is { } funcDef)
247+
// Try BuiltinFunctionRegistry for all registered functions
248+
if (_functionRegistry != null && _functionRegistry.Get(methodName) is { } funcDef)
273249
{
274250
var stmt = funcDef.ExtractStatement(assignInvoke, exprStmt.GetLineNumber(), exprText);
275251
if (stmt != null)

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/BranchFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class BranchFunction : IBuiltinFunctionDefinition
1919
public bool IsNonExtractable => false;
2020
public BlueprintNodeType? LegacyNodeType => BlueprintNodeType.Branch;
2121
public bool IsBlockTerminator => true;
22+
public FormattedStatementKind StatementKind => FormattedStatementKind.Branch;
2223
public double NodeWidth => 120;
2324
public double NodeHeight => 80;
2425

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/BreakFunction.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public class BreakFunction : IBuiltinFunctionDefinition
1414
public string FunctionName => "Break";
1515
public string DisplayName => "Break";
1616
public bool IsFlowControl => true;
17+
public bool IsBlockTerminator => true;
1718
public bool IsNonExtractable => true;
1819
public BlueprintNodeType? LegacyNodeType => BlueprintNodeType.Break;
20+
public FormattedStatementKind StatementKind => FormattedStatementKind.Break;
1921
public double NodeWidth => 100;
2022
public double NodeHeight => 40;
2123

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/FlipFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class FlipFunction : IBuiltinFunctionDefinition
2020
public bool IsFlowControl => true;
2121
public bool IsNonExtractable => true;
2222
public bool IsBlockTerminator => true;
23+
public FormattedStatementKind StatementKind => FormattedStatementKind.Branch;
2324
public double NodeWidth => 120;
2425
public double NodeHeight => 80;
2526

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/GetFunction.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public class GetFunction : IBuiltinFunctionDefinition
1414
public string FunctionName => "Get";
1515
public string DisplayName => "Get";
1616
public bool IsFlowControl => false;
17-
public bool IsNonExtractable => true;
17+
public bool IsNonExtractable => false;
1818
public BlueprintNodeType? LegacyNodeType => BlueprintNodeType.Get;
19+
public FormattedStatementKind StatementKind => FormattedStatementKind.Assignment;
1920
public double NodeWidth => 120;
2021
public double NodeHeight => 60;
2122

@@ -74,11 +75,40 @@ public List<FormattedStatement> FormatInvocation(
7475

7576
public BlueprintNode ConfigureNode(BlueprintNode node, FormattedStatement stmt)
7677
{
77-
if (node is BuiltinFunctionNode bfn)
78-
bfn.Properties["VarName"] = stmt.GetVarName ?? "";
78+
var varName = stmt.GetVarName ?? (stmt.Arguments?.Count > 0 ? stmt.Arguments[0].Trim('"') : "");
79+
if (node is GetNode gn)
80+
gn.VarName = varName;
81+
else if (node is BuiltinFunctionNode bfn)
82+
bfn.Properties["VarName"] = varName;
7983
return node;
8084
}
8185

86+
public (string?, string?, string?) ExtractStatementFields(
87+
InvocationExpressionSyntax invoke, List<string> expandedArgs,
88+
string? assignedVar, PipelineContext context)
89+
{
90+
string? getVarName = null;
91+
if (expandedArgs.Count > 0)
92+
{
93+
var firstArgExpr = invoke.ArgumentList.Arguments[0].Expression;
94+
getVarName = ExprUtils.GetStringLiteralValue(firstArgExpr) ?? expandedArgs[0];
95+
}
96+
97+
string? pubVarTarget;
98+
if (string.IsNullOrEmpty(assignedVar) || !context.PubVarNames.Contains(assignedVar))
99+
{
100+
pubVarTarget = ExprUtils.GeneratePubVarName(context.NextPubVarCounter++);
101+
if (!context.PubVarNames.Contains(pubVarTarget))
102+
context.PubVarNames.Add(pubVarTarget);
103+
}
104+
else
105+
{
106+
pubVarTarget = assignedVar;
107+
}
108+
109+
return (null, getVarName, pubVarTarget);
110+
}
111+
82112
public BlockStatement? ToStatement(BlueprintNode node, INodeExportHelper helper)
83113
{
84114
var varName = node switch

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/LoopBodyEndFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class LoopBodyEndFunction : IBuiltinFunctionDefinition
1717
public bool IsFlowControl => true;
1818
public bool IsNonExtractable => false;
1919
public bool IsBlockTerminator => true;
20+
public FormattedStatementKind StatementKind => FormattedStatementKind.LoopBodyEnd;
2021
public double NodeWidth => 80;
2122
public double NodeHeight => 60;
2223

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/LoopFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class LoopFunction : IBuiltinFunctionDefinition
2020
public bool IsNonExtractable => false;
2121
public BlueprintNodeType? LegacyNodeType => BlueprintNodeType.Loop;
2222
public bool IsBlockTerminator => true;
23+
public FormattedStatementKind StatementKind => FormattedStatementKind.Loop;
2324
public double NodeWidth => 120;
2425
public double NodeHeight => 80;
2526

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/PauseFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class PauseFunction : IBuiltinFunctionDefinition
1616
public bool IsFlowControl => false;
1717
public bool IsNonExtractable => true;
1818
public BlueprintNodeType? LegacyNodeType => BlueprintNodeType.Pause;
19+
public FormattedStatementKind StatementKind => FormattedStatementKind.Pause;
1920
public double NodeWidth => 100;
2021
public double NodeHeight => 50;
2122

KitX Clients/KitX Core/KitX.Core/Workflow/BlockScripting/BuiltinFunctions/PrintFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class PrintFunction : IBuiltinFunctionDefinition
1616
public bool IsFlowControl => false;
1717
public bool IsNonExtractable => true;
1818
public BlueprintNodeType? LegacyNodeType => BlueprintNodeType.Print;
19+
public FormattedStatementKind StatementKind => FormattedStatementKind.Print;
1920
public double NodeWidth => 100;
2021
public double NodeHeight => 50;
2122

0 commit comments

Comments
 (0)