Proposed method for "recursion" without pointers:
Code: Select all
/*
Really useless recursion function. I'm pretty sure it's not infinite recursion (which would cause a "stack overflow")
*/
type funcName(unsigned int foo)
{
// ... code 1 ...
int bar = foo;
foo *= foo;
if(foo > 25)
return(foo);
foo++;
bar = funcName(foo);
// ... code 2 ...
bar /= 2;
bar = funcName(bar);
// ... code 3 ...
return(bar);
}
Code: Select all
// The following prototypes will be implemented in the firmware
void pop(variant arr[], variant& val);
void push(variant& arr[], variant val);
#define DONE 4
struct funcName_Stack
{
struct args
{
// function parameters
unsigned int foo;
};
unsigned long pos;
struct stack
{
// local function variables
int bar;
};
type ret; // the return value of the next recursive function call
}
void funcName_code(funcName_Stack &fns[])
{
funcName_stack fnsTemp;
unsigned long i = ArrayLen(fns) - 1;
switch(pos)
{
case 1:
// fns[i].pos++;
// ... code 1 ...
fns[i].stack.bar = fns[i].args.foo;
fns[i].args.foo *= fns[i].args.foo;
if(fns[i].args.foo > 25)
{
// return(foo);
fns[i].ret = fns[i].args.foo;
fns[i].pos = DONE;
break;
}
// "Call" recursive function
fnsTemp.pos = 1;
fnsTemp.args.foo = fns[i].args.foo;
push(fns, fnsTemp);
break;
case 2:
// fns[i].pos++;
fns[i].stack.bar = fns[i].ret;
// ... code 2 ...
fns[i].stack.bar /= 2;
// "Call" recursive function
fnsTemp.pos = 1;
fnsTemp.args.foo = fns[i].stack.bar;
push(fns, fnsTemp);
break;
case 3:
// fns[i].pos++;
fns[i].stack.bar = fns[i].ret;
// ... code 3 ...
// return(bar);
fns[i].ret = fns[i].stack.bar;
fns[i].pos = DONE;
break;
case DONE:
default:
pop(fns, fnsTemp);
i--;
fns[i].ret = fnsTemp.ret;
break;
}
fns[i].pos++;
}
inline type funcName(unsigned int foo)
{
funcName_stack fnsTemp;
funcName_stack fns[];
// Set initial function arguments
fnsTemp.args.foo = fns[i].args.foo;
// Set "position"
fnsTemp.pos = 1;
// Push initial argument onto stack
push(fns, fnsTemp);
while(fns[0].pos < DONE)
{
funcName_code(fns);
}
return(fns[0].ret);
}
-----
Up to John to decide if it's worth his time. (Or he could tell me how the SVN is organized for me to look at the NBC.exe compiler code. ... Especially if the code's in C/C++)
-----
EDIT: Maybe I'll write a simple parser that will convert recursive functions to the above. It'll be even better if John implements pop()/push() within the EF.