I gave chat gpt the interface and suggested to use Boost LibContext and he did the rest, i added some little things and adjustements, documentation is AI generated in libcor.h.
The project:
https://github.com/lukkahub/libcor
The realease contains the compiled libs:
https://github.com/lukkahub/libcor/releases/tag/v0.1.0
The PB imclude:
Code: Select all
;Adjudt #LIBCOR_LIB_FOLDER to your needs
#LIBCOR_LIB_FOLDER = "libcor-release-v0.1.0/lib/"
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86 Or #PB_Compiler_Processor = #PB_Processor_Arm32
Debug "32 bit not supported yet"
End
CompilerEndIf
;- Enum #COR_VARTYPE_
Enumeration
#COR_VARTYPE_NONE ;No value assigned (uninitialized Or cleared).
#COR_VARTYPE_I64 ;Signed 64-bit integer (`int64_t`) — `i64`.
#COR_VARTYPE_I32 ;Signed 32-bit integer (`int32_t`) — `i32`.
#COR_VARTYPE_I16 ;Signed 16-bit integer (`int16_t`) — `i16`.
#COR_VARTYPE_I8 ;Signed 8-bit integer (`int8_t`) — `i8`.
#COR_VARTYPE_U64 ;Unsigned 64-bit integer (`uint64_t`) — `u64`.
#COR_VARTYPE_U32 ;Unsigned 32-bit integer (`uint32_t`) — `u32`.
#COR_VARTYPE_U16 ;Unsigned 16-bit integer (`uint16_t`) — `u16`.
#COR_VARTYPE_U8 ;Unsigned 8-bit integer (`uint8_t`) — `u8`.
#COR_VARTYPE_F32 ;32-bit floating-point number (`float`) — `f32`.
#COR_VARTYPE_F64 ;64-bit floating-point number (`double`) — `f64`.
#COR_VARTYPE_PTR ;Generic pointer (`void*`) — `ptr`.
EndEnumeration
;- Enum #COR_STATE_
Enumeration
#COR_STATE_READY ;< The coroutine has been created And is ready To run.
#COR_STATE_RUNNING ;< The coroutine is currently executing.
#COR_STATE_DONE ;< The coroutine has finished execution.
#COR_STATE_INVALID ;< The coroutine is invalid Or uninitialized.
EndEnumeration
;- cor_var
Structure cor_var Align #PB_Structure_AlignC
type.l ;Indicates the currently active type stored in the union.
StructureUnion
i64Val.q ;Signed 64-bit integer.
i32Val.l ;Signed 32-bit integer.
i16Val.w ;Signed 16-bit integer.
i8Val.b ;Signed 8-bit integer.
u64Val.q ;Unsigned 64-bit integer.
u32Val.l ;Unsigned 32-bit integer.
u16Val.u ;Unsigned 16-bit integer.
u8Val.a ;Unsigned 8-bit integer.
f32Val.f ;32-bit floating-point number.
f64Val.d ;64-bit floating-point number.
ptrVal.i ;Generic pointer.
EndStructureUnion
EndStructure
;- IMPORT
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#LIBCOR_OS_FOLDER = "windows/x64/"
#LIBCOR_PATH = #LIBCOR_LIB_FOLDER + #LIBCOR_OS_FOLDER + "cor.lib"
#LIBCOR_LIBBOOST_PATH = #LIBCOR_LIB_FOLDER + #LIBCOR_OS_FOLDER + "libboost_context-vc143-mt-s-x64-1_88.lib"
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
#LIBCOR_OS_FOLDER = "linux/x64/"
#LIBCOR_PATH = #LIBCOR_LIB_FOLDER + #LIBCOR_OS_FOLDER + "libcor.a"
#LIBCOR_LIBBOOST_PATH = #LIBCOR_LIB_FOLDER + #LIBCOR_OS_FOLDER + "libboost_context.a"
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
#LIBCOR_OS_FOLDER = "macos/x64/"
CompilerElseIf #PB_Compiler_Processor = #PB_Processor_Arm64
#LIBCOR_OS_FOLDER = "macos/arm64/"
CompilerEndIf
#LIBCOR_PATH = #LIBCOR_LIB_FOLDER + #LIBCOR_OS_FOLDER + "libcor.a"
#LIBCOR_LIBBOOST_PATH = #LIBCOR_LIB_FOLDER + #LIBCOR_OS_FOLDER + "libboost_context.a"
CompilerEndIf
Import #LIBCOR_PATH
cor_create.i(func.i, arg.i)
cor_resume.l(co.i, input.i, output.i)
cor_yield.l(output.i, input.i)
cor_get_state.l(co)
cor_destroy.b(co.i)
cor_var_copy(*src.cor_var, *dest.cor_var)
cor_var_clear(*var.cor_var)
cor_var_init(*var.cor_var, type.l)
cor_version.i()
cor_version_major.l()
cor_version_minor.l()
cor_version_patch.l()
EndImport
Import #LIBCOR_LIBBOOST_PATH
EndImport
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
Import "-lstdc++" ;Dynamic linking
EndImport
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
CompilerEndIf
Code: Select all
XIncludeFile "libcor.pbi"
;Coroutine function that yields Fibonacci numbers
Procedure.l fib_coroutine(arg.i)
Protected.l a, b, i, _next
Protected.cor_var output, input
a = 0
b = 1
output\type = #COR_VARTYPE_I32
For i = 0 To 9
output\type = #COR_VARTYPE_I32
output\i32Val = a
;Yield the current Fibonacci number And wait For Next input
cor_var_clear(@input)
cor_yield(@output, @input)
_next = a + b
a = b
b = _next
Next
EndProcedure
Procedure main()
Protected.i co
Protected.cor_var input, output
co = cor_create(@fib_coroutine(), 0)
input\type = #COR_VARTYPE_NONE
output\type = #COR_VARTYPE_NONE
While cor_resume(co, @input, @output)
If output\type = #COR_VARTYPE_I32
Debug "Fib: " + Str(output\i32Val)
EndIf
;Optionally prepare Next Input (Not used here)
Wend
cor_destroy(co)
EndProcedure
main()