@@ -156,6 +156,14 @@ public static void Main(string[] args)
156156 Console . WriteLine ( "└──────────────────────────────────────────┘\n " ) ;
157157 RunExecutionTest ( converter , reverseConverter , parser , sp , GetRawNestedScript ( ) , helpers , "Test K" ) ;
158158 }
159+
160+ if ( ShouldRunTest ( "L" ) )
161+ {
162+ Console . WriteLine ( "\n ┌──────────────────────────────────────────┐" ) ;
163+ Console . WriteLine ( "│ Test L: Assembly Compilation │" ) ;
164+ Console . WriteLine ( "└──────────────────────────────────────────┘\n " ) ;
165+ RunAssemblyCompilationTest ( reverseConverter , parser , sp ) ;
166+ }
159167 }
160168
161169 private static void ParseArgs ( string [ ] args )
@@ -366,6 +374,106 @@ private static void RunExecutionTest(
366374 }
367375 }
368376
377+ // Test L: Assembly Compilation
378+ // ──────────────────────────────────────────────
379+ private static void RunAssemblyCompilationTest (
380+ IBlueprintToBlockScriptConverter reverseConverter ,
381+ IBlockScriptParser parser ,
382+ System . IServiceProvider sp )
383+ {
384+ try
385+ {
386+ var sourceCode = @"#ConstBlock
387+ int count = 0;
388+ int max = 3;
389+
390+ #MainBlock
391+ Set(""count"", 0);
392+ NextBlock = ""LoopBlock"";
393+
394+ #Block LoopBlock
395+ NextBlock = Loop(HelperFuncCompare(""BLT"", Get(""count""), max), ""PrintBlock"", ""EndBlock"");
396+
397+ #Block PrintBlock
398+ Print(Get(""count""));
399+ Set(""count"", HelperFuncAdd(Get(""count""), 1));
400+ NextBlock = ToLoopCond(""LoopBlock"");
401+
402+ #Block EndBlock
403+ Print(""Done"");
404+ " ;
405+
406+ var parseResult = parser . Parse ( sourceCode ) ;
407+ if ( ! parseResult . IsSuccess || parseResult . Script == null )
408+ {
409+ Console . WriteLine ( "[Test L] FAILED: Parse error: " + parseResult . ErrorMessage ) ;
410+ return ;
411+ }
412+
413+ parseResult . Script . HelperFunctions = GetExecutionHelpers ( ) ;
414+
415+ // Test 1: Direct ScriptAssemblyCompiler test
416+ var compiler = new ScriptAssemblyCompiler ( ) ;
417+ Console . WriteLine ( "[Test L] Compiling..." ) ;
418+ var compiled = compiler . CompileScript ( parseResult . Script ) ;
419+ Console . WriteLine ( $ "[Test L] Assembly compilation: { ( compiled != null ? "SUCCESS" : "FAILED (null)" ) } ") ;
420+
421+ if ( compiled != null )
422+ {
423+ // Test 2: Execute via compiled assembly
424+ var output = new List < string > ( ) ;
425+ var globals = new BlockScriptExecutionGlobals (
426+ new BlockScopeManager ( ) , output ) ;
427+ globals . ResetRunState ( ) ;
428+
429+ Console . WriteLine ( "[Test L] About to call Run()..." ) ;
430+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 2 ) ) ;
431+ try
432+ {
433+ compiled . Run ( globals , cts . Token ) ;
434+ Console . WriteLine ( $ "[Test L] Assembly execution: SUCCESS") ;
435+ Console . WriteLine ( $ "[Test L] ExecutedBlockCount: { globals . ExecutedBlockCount } ") ;
436+ Console . WriteLine ( $ "[Test L] Output: [{ string . Join ( ", " , output ) } ]") ;
437+ }
438+ catch ( OperationCanceledException )
439+ {
440+ Console . WriteLine ( "[Test L] Assembly execution: TIMEOUT (infinite loop?)" ) ;
441+ }
442+ catch ( Exception ex )
443+ {
444+ Console . WriteLine ( $ "[Test L] Assembly execution: EXCEPTION - { ex . GetType ( ) . Name } : { ex . Message } ") ;
445+ if ( ex . InnerException != null )
446+ Console . WriteLine ( $ " Inner: { ex . InnerException . GetType ( ) . Name } : { ex . InnerException . Message } ") ;
447+ }
448+ }
449+
450+ // Test 3: Compare via IBlockScriptExecutor (should use assembly path)
451+ var executor = sp . GetRequiredService < IBlockScriptExecutor > ( ) ;
452+ using var cts2 = new CancellationTokenSource ( TimeSpan . FromSeconds ( 5 ) ) ;
453+ var result = executor . ExecuteAsync ( parseResult . Script , cancellationToken : cts2 . Token )
454+ . GetAwaiter ( ) . GetResult ( ) ;
455+ Console . WriteLine ( $ "[Test L] IBlockScriptExecutor: IsSuccess={ result . IsSuccess } , BlockCount={ result . ExecutedBlockCount } ") ;
456+ Console . WriteLine ( $ "[Test L] Executor output ({ result . Output . Count } lines): [{ string . Join ( ", " , result . Output ) } ]") ;
457+
458+ // Verify output matches expected: 0, 1, 2, Done
459+ var expected = new List < string > { "0" , "1" , "2" , "Done" } ;
460+ bool match = result . IsSuccess && result . Output . Count == expected . Count ;
461+ if ( match )
462+ {
463+ for ( int i = 0 ; i < expected . Count ; i ++ )
464+ {
465+ if ( result . Output [ i ] != expected [ i ] ) { match = false ; break ; }
466+ }
467+ }
468+
469+ Console . WriteLine ( $ "[Test L] { ( match ? "PASS - Output correct!" : ( result . IsSuccess ? "DIFF" : "FAILED" ) ) } ") ;
470+ }
471+ catch ( Exception ex )
472+ {
473+ Console . WriteLine ( $ "[Test L] FAILED: { ex . Message } \n { ex . StackTrace } ") ;
474+ }
475+ }
476+
369477 /// <summary>
370478 /// Helper functions with actual execution bodies (for execution test).
371479 /// </summary>
0 commit comments