Bracket nesting level exceeded maximum of 256

Monkey Forums/Monkey Bug Reports/Bracket nesting level exceeded maximum of 256

JaviCervera(Posted February) [#1]
When compiling an application for the GLFW3 target on macOS, I get the following error:

"fatal error: bracket nesting level exceeded maximum of 256"

This seems to happen because I have a VERY long Select statement that is expanded into a set of indented if else blocks in the generated C++ code.

I tried adding this to the top of my program:

#CC_OPTS+="-fbracket-depth=1024"

But it didn't work because it seems that CC_OPTS is only taken into account in the C++ Tool target (which is a shame, it would be great to be able to specify additional compile options on any C++ targets).


Xaron(Posted February) [#2]
Sounds like a design problem in your app. ;)


dawlane(Posted February) [#3]
The latest version of MonkeyX has #GLFW_GCC_CC_OPTS and #GLFW_GCC_LD_OPTS for the GLFW target. But you should rethink the app design.


JaviCervera(Posted February) [#4]
I wouldn't go as far as to say that having a long Select statement is a design flaw :) I am using a custom scripting language where every function exported by the host has an assigned id number. When the script executes a function in the host, it uses this Select statement to run the correct function based on the id. The entire Select statement is autogenerated, and I can't think of a more efficient way to implement this (and I have seen this design in other interpreted languages).

Thanks for the info on #GLFW_GCC_CC_OPTS and #GLFW_GCC_LD_OPTS! I was looking for something like this, but for some reason I couldn't find these in the docs. I have upgraded my Monkey version to 87b, and added #GLFW_GCC_CC_OPTS+="-fbracket-depth=1024", but it seems to ignore this setting, so the bug is still there.


Goodlookinguy(Posted February) [#5]
Use a Map or HashMap with instances of classes if you're having this issue. Giant switch statements are maintenance nightmares. Alternatively, you could break it down into groups.

When 0-15: switch statement with 0-15
When 16-31: switch statement with 16-31
When 32-47: switch statement with 32-47
When 48-63: switch statement with 48-63
etc., etc., etc.


JaviCervera(Posted February) [#6]
Using maps is not an option in this case, since I can't store functions into them. Since the file with the Select statement is autogenerated, I am changing the generator right now to split the check into several Select statements as you suggested :)

Thanks!


Goodlookinguy(Posted February) [#7]
Edit: I had forgotten how Monkey generates Select statements. So yeah, just splitting it up will work just fine without needing to use the call stack. For every if/else statement the next group will increase the limitation. At 16 per group, by 240 groups, that's the limit.

Original Post: I honestly didn't explain what I meant by splitting them up very well and I'm sorry about that. What I said will still probably produce the brackets limit problem. If it doesn't, great, but if it does, this is what I actually meant...

I wrote this...
When 0-15: switch statement with 0-15
When 16-31: switch statement with 16-31
When 32-47: switch statement with 32-47
When 48-63: switch statement with 48-63
etc., etc., etc.


What I meant was this

If s >= 0 && s <= 15
    s0to15(s)
ElseIf s >= 16 && s <= 31
    s16to31(s)
...

Function s0to15:Void( opCode:Int )
    Select opCode
        Case 0:
             Do0()
         Case 1:
              Do1()
         ....
    End
End

...



JaviCervera(Posted March) [#8]
I did something similar to that and it works perfectly :) Thanks!