s1s0 / toped

Cross platform, open source IC layout editor
http://www.toped.org.uk/
GNU General Public License v2.0
15 stars 8 forks source link

TELL: allow functions as an argument of functions #105

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Could be helpful in developing libraries.

e.g. writing a general function like "calc_area (function object)". 
The object could be anything like the calculation rules 
for NMOST, R, .. what ever

Original issue reported on code.google.com by analogc...@gmx.net on 19 Jul 2011 at 8:47

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by krustev....@gmail.com on 1 Apr 2012 at 12:21