Closed GoogleCodeExporter closed 9 years ago
Here some preliminary idea about the syntax. One new keyword - "callback"
introduced and one new operator &<funcname>
void funcArguFunc(int a, int b, callback fparam)
{
// the idea is to get the function prototype from the first call instead of
// using long and convoluted declarations
// i.e. the function prototype checks are delayed and will be clear after
// parsing the line below
int result = fparam(a,b);
string buffer = sprintf ("%d fparam %d is %d", a, b, result);
printf ("[%s] \n",buffer);
}
int simpleAdd(int a, int b)
{
return (a + b);
}
int simpleSub(int a, int b)
{
return (a - b);
}
funcArguFunc(2,3,&simpleAdd);
funcArguFunc(2,3,&simpleSub);
// similar thing can be done in a regular variables (not only a function
argument)
callback varfunction;
varfunction = &simpleAdd;
int c = varfunction(2,3);
varfunction = &simpleSub;
int d = varfunction(2,3);
Seems to be a good idea to have a default function prototype - for example:
void protoFunc();
The syntax is different from C, but I think we have to make an exception here -
just for reference - the C syntax is something like:
int Cfunc(void(*f)(int))
{
int ivar = 0;
...
f(ivar)
return ivar;
}
void tada(int a)
{
printf("Value is %d",a);
}
return Cfunc(tada);
Original comment by krustev....@gmail.com
on 16 Aug 2011 at 11:28
The implementation slightly differs from the initial idea described in the
previous post (all good reasons). The recent syntax is described here
https://code.google.com/p/toped/source/browse/trunk/tll/callbackTest.tll
It also works as expected after r1936
Original comment by krustev....@gmail.com
on 3 Sep 2011 at 8:47
Updates introduced over a number of commits starting from r1921 and ending with
r1939. All planned callback functionality is implemented now. As an addition to
the previous post describing the features - here is a short list of
restrictions :
- callback type as a parameter to another callback (will hit assert)
- Functions with a variable number of parameters (printf, sprintf) called
trough a callback
- assignment between callback variables with different types even if the types
unroll to identical definitions. This implies that a variable of anonymous
callback type can receive its value only from a function address operator
(currently @).
Can't think of more cases - if any - they shall be reported here.
Original comment by krustev....@gmail.com
on 4 Sep 2011 at 3:17
Further updates are in place to make sure that the new type works in
structures, lists etc. Also the function address operator @ is replaced with
more natural (C-like) & in r1947. The test file updated accordingly.
Original comment by krustev....@gmail.com
on 6 Sep 2011 at 10:35
Original comment by krustev....@gmail.com
on 1 Apr 2012 at 12:21
Original issue reported on code.google.com by
analogc...@gmx.net
on 19 Jul 2011 at 8:47