@@ -142,15 +142,31 @@ func (i *InterpreterImpl) ExecuteInstructionBlock(source string, input interface
142142 lastResult := input
143143 for idx , op := range instructions {
144144 if op != nil {
145+
145146 i .LogDebug ("[%s:%d]: ExecuteInstructionBlock: %v <- %s %v" , source , idx , op .Destination , op .Opcode , op .Arguments )
147+
146148 result , err := i .ExecuteInstruction (op .Opcode , op .Destination , op .Arguments )
149+
147150 if err != nil {
148151 if returnValue , ok := primitive .GetReturnValue (err ); ok {
149152 i .LogDebug ("[%s:%d]: ExecuteInstructionBlock: return by ReturnValue: %v" , source , idx , returnValue .Value )
150153 return returnValue .Value , nil
151154 }
152155 return nil , fmt .Errorf ("\n %s:%d: %v" , source , idx , err )
153156 }
157+
158+ if codeResult , ok := primitive .GetCodeResult (result ); ok {
159+ codeInstructions , err := i .HandleCodeResult (source , codeResult , instructions )
160+ if err != nil {
161+ return nil , err
162+ }
163+
164+ // Store the codeInstructions in the destination slot
165+ i .LogDebug ("[%s]: ExecutePrimitive: storing %d codeInstructions in %s" , source , len (codeInstructions ), op .Destination .Name )
166+ i .Slots [op .Destination .Name ] = codeInstructions
167+ return codeInstructions , nil
168+ }
169+
154170 lastResult = result
155171 }
156172 }
@@ -257,13 +273,74 @@ func (i *InterpreterImpl) LoadArguments(source string, arguments []interface{})
257273 return resolvedArgs , nil
258274}
259275
276+ // HandleCodeResult processes a CodeResult and returns the concatenated instructions
277+ func (i * InterpreterImpl ) HandleCodeResult (source string , codeResult * primitive.CodeResult , block []* parsers.Instruction ) ([]* parsers.Instruction , error ) {
278+ i .LogDebug ("[%s]: HandleCodeResult: processing targets: %v" , source , codeResult .Targets )
279+ var allInstructions []* parsers.Instruction
280+
281+ // Process each target in order
282+ for _ , target := range codeResult .Targets {
283+ var instructions []* parsers.Instruction
284+
285+ switch v := target .(type ) {
286+ case string :
287+ if v == "@" {
288+ if block == nil {
289+ return nil , fmt .Errorf ("[%s]: HandleCodeResult: no instructions provided for @" , source )
290+ }
291+ instructions = append (instructions , block ... )
292+ } else {
293+
294+ // Get instructions using existing subroutine logic
295+ pwd := i .GetScriptDir ()
296+ i .LogDebug ("[%s]: HandleCodeResult: pwd = %s" , source , pwd )
297+
298+ // Check if the subroutine is already loaded
299+ subPath := SubroutinePath (v , pwd )
300+ i .LogDebug ("[%s]: HandleCodeResult: subPath = %v" , source , subPath )
301+
302+ var err error
303+ instructions , err = i .GetSubroutineInstructions (subPath )
304+ i .LogDebug ("[%s]: HandleCodeResult: instructions = %v" , source , instructions )
305+ if err != nil {
306+ return nil , fmt .Errorf ("[%s]: HandleCodeResult: failed to get instructions for %v: %v" , source , target , err )
307+ }
308+ if instructions == nil {
309+ return nil , fmt .Errorf ("[%s]: HandleCodeResult: no instructions found for %v" , source , target )
310+ }
311+
312+ }
313+ case []* parsers.Instruction :
314+ // Use the instructions directly
315+ instructions = v
316+ if instructions == nil {
317+ return nil , fmt .Errorf ("[%s]: HandleCodeResult: no instructions found for %v" , source , target )
318+ }
319+ case * parsers.Instruction :
320+ // Use the instructions directly
321+ instructions = []* parsers.Instruction {v }
322+ if instructions == nil {
323+ return nil , fmt .Errorf ("[%s]: HandleCodeResult: no instructions found for %v" , source , target )
324+ }
325+ default :
326+ return nil , fmt .Errorf ("[%s]: HandleCodeResult: invalid target type: %T" , source , target )
327+ }
328+
329+ // Append instructions to the result
330+ allInstructions = append (allInstructions , instructions ... )
331+ }
332+
333+ return allInstructions , nil
334+ }
335+
260336// ExecutePrimitive executes a single GND primitive and returns its result
261337func (i * InterpreterImpl ) ExecutePrimitive (prim primitive.Primitive , destination * parsers.PropertyRef , arguments []interface {}) (interface {}, error ) {
262338
263339 // Log regular instruction
264340 i .LogDebug ("[%s]: ExecutePrimitive: %v <- %s %v" , prim .Name (), destination , prim .Name (), arguments )
265341
266342 result , err := prim .Execute (arguments )
343+
267344 if err != nil {
268345
269346 // Check if this is a ReturnValue
@@ -289,6 +366,12 @@ func (i *InterpreterImpl) ExecutePrimitive(prim primitive.Primitive, destination
289366 return nil , fmt .Errorf ("[%s]: ExecutePrimitive: error: %v" , prim .Name (), err )
290367 }
291368
369+ // Check if this is a CodeResult
370+ if codeResult , ok := primitive .GetCodeResult (result ); ok {
371+ i .LogDebug ("[%s]: ExecutePrimitive: code result detected: %v" , prim .Name (), codeResult )
372+ return codeResult , nil
373+ }
374+
292375 // Store the result in the destination slot
293376 i .LogDebug ("[%s]: ExecutePrimitive: %v <- %v" , prim .Name (), destination , log .StringifyValue (result ))
294377 i .Slots [destination .Name ] = result
0 commit comments