digitalmars.D - Lookhead for nested functions
- Amex (14/14) Jun 13 2019 fails:
- FeepingCreature (10/12) Jun 13 2019 Nope. Consider the following code:
- Amex (2/12) Jun 15 2019 There is nothing wrong with this.
- rikki cattermole (31/47) Jun 15 2019 Yes there is.
- Timon Gehr (6/29) Jun 14 2019 If you need mutual recursion for local functions, this hack works:
- Walter Bright (4/10) Jun 14 2019 Using function pointers also works.
fails: void foo() { bar(); void bar() { } } passes: void foo() { void bar() { } bar(); } I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?
Jun 13 2019
On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?Nope. Consider the following code: int foo() { return bar(); int i = 5; int bar() { return i; } } Nested functions are inextricably bound to function scope, and function scope is strictly sequential.
Jun 13 2019
On Friday, 14 June 2019 at 06:00:13 UTC, FeepingCreature wrote:On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:There is nothing wrong with this.I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?Nope. Consider the following code: int foo() { return bar(); int i = 5; int bar() { return i; } }
Jun 15 2019
On 15/06/2019 10:16 PM, Amex wrote:On Friday, 14 June 2019 at 06:00:13 UTC, FeepingCreature wrote:Yes there is. i never got initialized, it is effectively =void. Assuming its stored on the stack and not a register. E.g. Here is an example that shows what you're suggesting. int func() { int x; somethingElse(x); int y = 7; return y; } Assembled: int onlineapp.func(): push RBP mov RBP,RSP sub RSP,010h xor EAX,EAX mov -8[RBP],EAX mov EDI,EAX call void onlineapp.somethingElse(int) PLT32 mov ECX,7 mov -4[RBP],ECX mov EAX,ECX leave ret The instructions: mov ECX,7 mov -4[RBP],ECX Never ran and hence who knows what the return value is. If you really want this, it will require a DIP.On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:There is nothing wrong with this.I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?Nope. Consider the following code: int foo() { return bar(); int i = 5; int bar() { return i; } }
Jun 15 2019
On 14.06.19 07:35, Amex wrote:fails: void foo() { bar(); void bar() { } } passes: void foo() { void bar() { } bar(); } I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?If you need mutual recursion for local functions, this hack works: void foo(){ void bar()(){ baz(); } void baz(){ bar(); } }
Jun 14 2019
On 6/14/2019 5:50 AM, Timon Gehr wrote:If you need mutual recursion for local functions, this hack works: void foo(){ void bar()(){ baz(); } void baz(){ bar(); } }Using function pointers also works. The point being, the use cases are rare enough that such workarounds are reasonable given the problems of trying to compile forward dependencies.
Jun 14 2019