# this is a test for how to implement coroutinues in C++ / brace # what would be a good "test" program? # how about these five coroutines: # 1. reads blocks, and splits / accumulates into lines (C++ string) # 2. reads lines, splits into TSV (returns a vector) # 3. reads two tsv vectors, does a join based on first column # 4. converts TSV vectors into TSV strings # 5. accumulates strings into blocks # we need to use objects, to hold the variables # let's not use any for or while loops, but use tail recursion instead # apparently the C compiler doesn't do tail recursion properly # - could return from every function to the scheduler, and get it to call the # next function # - could try to use goto # How about I implement each _process_ as a single function in a class, with # the different states being labels, and use "computed" gotos to go between # them? I am going to hope that I _can_ use it to jump to code in a different # function if I don't use the stack at all during that code. # how to implement channels? # remember to refer to newsqueak doc! # it would be good to be able to do this in C, not just C++, I'll try that later. #class blocks_to_lines # int block_size # blocks_to_lines(int block_size) : block_size(block_size) # I'll try a simpler test for a start. # ok, using goto between functions really isn't working, not quite sure why. # Let's try passing the address to the function as its argument. use stdio.h struct pass_5_eat_5 void *start pass_5_eat_5() go(NULL) void go(void *to) if to goto *to else start = &&start return start printf("Hello World!\n") return int main() pass_5_eat_5 p p.go(p.start) return 0 # This unfortunately has to do _two_ jumps for each call. # I hate C! # hey, could the linker cope linking non-C-function stuff? # maybe it could # Perhaps I could implement an assembly munger - use the C compiler to generate # code for functions, and munge the assembly to make coroutines possible... # apparently the ghc project uses or used such an "evil mangler" perl script! # I could use labels to delimit the actual computational assembly code in a # function, independent of architecture, and extract it with a script, I could # then append such independent assembly code fragments together. # the minimal inter-process switch would be: # save entry point for this process, if it has changed # update current process pointer, checking for wrap # set global data (register) for new process (this) # jump to entry point for new process # tail calls - should be able to use tail calls to functions without arguments # reliably, on most architectures at least - these would qualify as sibling calls.