{"action":"created","issue":{"url":"https://api.github.com/repos/bmx-ng/bcc/issues/86","labels_url":"https://api.github.com/repos/bmx-ng/bcc/issues/86/labels{/name}","comments_url":"https://api.github.com/repos/bmx-ng/bcc/issues/86/comments","events_url":"https://api.github.com/repos/bmx-ng/bcc/issues/86/events","html_url":"https://github.com/bmx-ng/bcc/issues/86","id":58867117,"number":86,"title":"Incorrect behaviour: Usage of a global before it was defined","user":{"login":"GWRon","id":2625226,"avatar_url":"https://avatars.githubusercontent.com/u/2625226?v=3","gravatar_id":"","url":"https://api.github.com/users/GWRon","html_url":"https://github.com/GWRon","followers_url":"https://api.github.com/users/GWRon/followers","following_url":"https://api.github.com/users/GWRon/following{/other_user}","gists_url":"https://api.github.com/users/GWRon/gists{/gist_id}","starred_url":"https://api.github.com/users/GWRon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GWRon/subscriptions","organizations_url":"https://api.github.com/users/GWRon/orgs","repos_url":"https://api.github.com/users/GWRon/repos","events_url":"https://api.github.com/users/GWRon/events{/privacy}","received_events_url":"https://api.github.com/users/GWRon/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-02-25T08:29:44Z","updated_at":"2015-02-25T22:15:26Z","closed_at":"2015-02-25T22:10:18Z","body":"## What\r\nCompilers (vanilla and NG) behave differently when using __globals__ before they got defined in the code.\r\nSo vanilla fails compilation with an error when the global was not defined. Exception is when it is some kind of deferred call (eg. a function definition __after__ the global).\r\nNG instead compiles fine and allows accessing globals before they even got defined.\r\n\r\n__But:__ it only works for single dimension variables. As soon as you have a global array things get different. NG compilates segfault when accessing them and vanilla blames access beyond array length during compilation.\r\n\r\n\r\n## Example Codes \r\n\r\nThe following code does not compile with vanilla but with NG\r\n```BlitzMax\r\nSuperStrict\r\nFramework Brl.StandardIO\r\n\r\nPrint Test\r\nGlobal Test:String = \"A\"\r\n```\r\nVanilla output: ``Compile Error: Identifier 'Test' not found``\r\nNG output: ``A``\r\n\r\n\r\nWhen using a function call it works as long as the global is defined BEFORE the function definition:\r\n\r\nFailing with vanilla. Working with NG:\r\n```BlitzMax\r\nSuperStrict\r\nFramework Brl.StandardIO\r\n\r\nPrint TestFunc()\r\n\r\nFunction TestFunc:String()\r\n\tReturn test\r\nEnd Function\r\n\r\nGlobal Test:String = \"A\"\r\n```\r\n\r\nWorking in vanilla. Working with NG:\r\n```BlitzMax\r\nSuperStrict\r\nFramework Brl.StandardIO\r\n\r\nPrint TestFunc()\r\n\r\nGlobal Test:String = \"A\"\r\n\r\nFunction TestFunc:String()\r\n\tReturn test\r\nEnd Function\r\n```\r\n\r\n\r\n\r\n__Now to the important thing__\r\nIt seems it only works for NG when it is a single dimension type.\r\n\r\nAccessing a global array leads to a segfault:\r\n(when calling the TestArrayFunc(), but would segfault by a direct global access too)\r\n```BlitzMax\r\nSuperStrict\r\nFramework Brl.StandardIO\r\n\r\nPrint TestArrayFunc()\r\nPrint TestArray[1]\r\nGlobal TestArray:String[] = [\"a\", \"b\", \"c\"]\r\n\r\nFunction TestArrayFunc:String()\r\n\tReturn TestArray[1]\r\nEnd Function\r\n```\r\n\r\nIn vanilla this code fails differently:\r\n(removed direct access as that leads to ``Compile Error: Identifier 'TestArray' not found``)\r\n```BlitzMax\r\nSuperStrict\r\nFramework Brl.StandardIO\r\n\r\nPrint TestArrayFunc()\r\nGlobal TestArray:String[] = [\"a\", \"b\", \"c\"]\r\n\r\nFunction TestArrayFunc:String()\r\n\tReturn TestArray[1]\r\nEnd Function\r\n```\r\nresult in vanilla: ``Unhandled Exception:Attempt to index array element beyond array length``\r\n\r\n## Conclusion\r\nInconsequent behaviour, either do not allow all variants before global is defined, or allow all.\r\n\r\nOf course even vanilla behaves differently depending on the global type (single dimension or array)"},"comment":{"url":"https://api.github.com/repos/bmx-ng/bcc/issues/comments/76072483","html_url":"https://github.com/bmx-ng/bcc/issues/86#issuecomment-76072483","issue_url":"https://api.github.com/repos/bmx-ng/bcc/issues/86","id":76072483,"user":{"login":"GWRon","id":2625226,"avatar_url":"https://avatars.githubusercontent.com/u/2625226?v=3","gravatar_id":"","url":"https://api.github.com/users/GWRon","html_url":"https://github.com/GWRon","followers_url":"https://api.github.com/users/GWRon/followers","following_url":"https://api.github.com/users/GWRon/following{/other_user}","gists_url":"https://api.github.com/users/GWRon/gists{/gist_id}","starred_url":"https://api.github.com/users/GWRon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GWRon/subscriptions","organizations_url":"https://api.github.com/users/GWRon/orgs","repos_url":"https://api.github.com/users/GWRon/repos","events_url":"https://api.github.com/users/GWRon/events{/privacy}","received_events_url":"https://api.github.com/users/GWRon/received_events","type":"User","site_admin":false},"created_at":"2015-02-25T22:15:26Z","updated_at":"2015-02-25T22:15:26Z","body":"I do not know if the behaviour of vanilla is correct at all.\n\nIn my opinion globals only exist if they are defined via `global bla:int`. if you leave out an initial value the default value is used (null, 0, \"\"). Every defaultvalue of a global could only be either another global or const ... so the compiler should know the value to use.\n\nAs a\"global\" by definition is accessible from everywhere I assume you are able to (initially) define it everywhere in the file/scope.\n\nOf course this is not backwards compatible with vanilla. But imho still a \"bug\" in vanilla."}}