GOTO examples in EUPHORIA´s source
- Posted by Shawn Pringle <shawn.pringle at ?mail.c?m> Jun 05, 2008
- 624 views
I see some examples in EUPHORIA'S source code using goto. There are some macros and I am glad we do not have those in euphoria containing goto. #define thread() goto loop_top #define thread2() {pc += 2; goto loop_top;} #define thread4() {pc += 4; goto loop_top;} #define thread5() {pc += 5; goto loop_top;} #define threadpc3() {pc = (int *)pc[3]; goto loop_top;} #define inc3pc() pc += 3 All of these goto examples can be replaced with 'continue'. I suppose the reason to eliminate goto is to encourage keywords like continue and exit. Then we see a place where goto is used a lot to jump into error handling code from multiple places:
case L_ASSIGN_OP_SUBS: /* var[subs] op= expr */ top = *(object_ptr)pc[1]; aos: if (!IS_SEQUENCE(top)) { //optimize better goto subsfail; } obj_ptr = (object_ptr)SEQ_PTR(top);/* the sequence */
later
top = *obj_ptr; if (!IS_SEQUENCE(top)) { goto asubsfail; } top = (object)SEQ_PTR(top);
... later..
*((object_ptr)top) = (object)obj_ptr; // storing a C pointer thread5(); } goto asubsfail;
Arguably you could use small function calls instead of gotos here. I am told there is a big cost in calling a function. Sometimes I put things into smaller functions just so that I can add convenience functions or I realise I can avoid retyping things over and over. If I had goto and I cared about so much about speed I might use it all over the place, saving labels in a simulated runtime stack and using globals for data passing. If I use functions for call backs and everything else with goto in the way mentioned would I gain speed? If so, wouldn't it make sense for EUPHORIA to compile the source in a way that simulates these gotos for recursive function calls? Isn't it all simulated anyway? So, the differences are, in functions variables are local to the routine's call. That is, The function can call itself without clobbering its own local variables. You can use call_back() to allow calls from C or any language that can call a C function. Suppose a procedure defined inside a routine would work exactly like gosub. There would be no parameter passing but you could modify the routine's variables. Wouldn't this be nearly as fast as goto? It seems that these kinds of procedures defined in routines would allow for gosub style programming. The code pointer is the only thing that gets saved. You can disallow recursive calls and allow only one call to a gosub at a time to avoid a stack structure. If the user needs those things he can use a file scope defined routine. Shawn Pringle