PerlPowerTools icon indicating copy to clipboard operation
PerlPowerTools copied to clipboard

bc: define is broken

Open briandfoy opened this issue 2 years ago • 1 comments

briandfoy avatar Jun 21 '23 12:06 briandfoy

  • "define" indicates a function declaration
  • When I declare a trivial function adding 1 to the input param, bc will only accept the syntax if the return expression is wrapped in parens
  • Every single return statement in the bc math library appears to wrap the expression in parens
  • Possibly the parens are required by the standard bc language[1]
  • GNU bc and OpenBSD bc allow return_expression without parens
  • Maybe this can only be fixed in the perl bc by generating a new parser from the yacc file of modern version of bc
$ cat fun.bc 
define yo(x)
{
 return (x + 1);
}
yo(1)
$ perl bc < fun.bc
2
$ cat fun2.bc
define yo(x)
{
 return x + 1;
}
yo(1)
$ perl bc < fun2.bc
line 3: syntax error
line 4: syntax error
No function yo() has been defined
  1. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html -- Return | Return '(' return_expression ')'

mknos avatar Oct 14 '23 12:10 mknos