{"action":"created","issue":{"url":"https://api.github.com/repos/KSP-KOS/KOS/issues/750","labels_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/750/labels{/name}","comments_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/750/comments","events_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/750/events","html_url":"https://github.com/KSP-KOS/KOS/issues/750","id":67046183,"number":750,"title":"Better keywords for global/local control (for locks but also vars)","user":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/KSP-KOS/KOS/labels/documentation","name":"documentation","color":"5319e7"},{"url":"https://api.github.com/repos/KSP-KOS/KOS/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/KSP-KOS/KOS/milestones/9","html_url":"https://github.com/KSP-KOS/KOS/milestones/v0.17.0%20Punchlist","labels_url":"https://api.github.com/repos/KSP-KOS/KOS/milestones/9/labels","id":1050633,"number":9,"title":"v0.17.0 Punchlist","description":"These are the tasks we need to do to get 17 out the door. no feature work.","creator":{"login":"erendrake","id":486486,"avatar_url":"https://avatars.githubusercontent.com/u/486486?v=3","gravatar_id":"","url":"https://api.github.com/users/erendrake","html_url":"https://github.com/erendrake","followers_url":"https://api.github.com/users/erendrake/followers","following_url":"https://api.github.com/users/erendrake/following{/other_user}","gists_url":"https://api.github.com/users/erendrake/gists{/gist_id}","starred_url":"https://api.github.com/users/erendrake/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erendrake/subscriptions","organizations_url":"https://api.github.com/users/erendrake/orgs","repos_url":"https://api.github.com/users/erendrake/repos","events_url":"https://api.github.com/users/erendrake/events{/privacy}","received_events_url":"https://api.github.com/users/erendrake/received_events","type":"User","site_admin":false},"open_issues":26,"closed_issues":34,"state":"open","created_at":"2015-04-02T16:44:37Z","updated_at":"2015-04-13T21:50:48Z","due_on":null,"closed_at":null},"comments":12,"created_at":"2015-04-08T04:28:59Z","updated_at":"2015-04-13T22:04:57Z","closed_at":null,"body":"Proposal: The syntax is getting clunky for differentiating scope, and as I wish to add scoping to LOCK as well, it's getting needlessly verbose.\r\n\r\nI propose this instead:\r\n\r\nTO and IS synonyms:\r\n----------------------------\r\n\r\nWe allow \"TO\" and \"IS\" to be the same: i.e. *declare x is 10* is the same as *declare x to 10*  This is needed for some of the changes below to keep things making gramattical sense:\r\n\r\nWe keep using \"declare\" only in a backward-compatibility sense.  We make the grammar allow it as an alternative extra keyword but prefer the use of \"local\".\r\n\r\nDECLARE, LOCAL, GLOBAL:\r\n----------------------------------------\r\n\r\nSaying *declare local* : means you are making a variable local to current scope.  It's the same exact thing as saying just \"declare\" now does.\r\n\r\nSaying *declare global* : means you are explicitly asking for a variable that lives at global scope *instead* of the current local scope.\r\n\r\nSaying *local* only : means the same as *declare local*.  The word *declare* is entirely optional when \"local\" or \"global\" is used.\r\n\r\nSaying *global* only : means the same as *declare global*.  The word *declare* is entirely optional when \"local\" or \"global\" is used.\r\n\r\nSaying *declare* only : means the same as *declare local*.  The \"local\" is presumed when left off.\r\n\r\n*set* : same as before - it stores in the first variable it can find when starting from localmost and going outward to globalmost, and if it finds none, then and only then does it make a variable, and it's global.\r\n\r\n@LAZYGLOBAL:\r\n-----------------------\r\n\r\nUsing *@LAZYGLOBAL OFF* now has a new meaning:  It still disallows *set* from implicitly making lazy globals, but it also disallows the use of saying *declare* only and having the \"local\" be presumed.  It now requires the use of either \"global\" or \"local\".  You can, however, leave the word \"declare\" off and just say \"local\" or just say \"global\".\r\n\r\nPARAMETER:\r\n-------------------\r\n\r\nThe word \"parameter\" can be left as-is now, dropping the \"declare\" if you like, but then like the cases above, an optional \"declare\" won't break anything.  You can say \"declare parameter\", \"declare local parameter\", or \"local parameter\", or just \"parameter\" and they are all the same, but the compiler will call it an error to say \"global parameter\" - parameters cannot be anything but local to where they are declared and saying otherwise is confusing.\r\n\r\nFUNCTION:\r\n----------------\r\n\r\nThe word \"function\" can be left as-is now, dropping the \"declare\" if you like, but an optional 'declare' is still allowed and won't break anything. You can say:\r\n```\r\ndeclare function foo {stuff}\r\n```\r\nor just\r\n```\r\nfunction foo {stuff}\r\n```\r\nand it means the same thing.\r\n\r\nFILE SCOPING:\r\n----------------------\r\n\r\nAt the moment, file scope and global scope are identical to each other.  With the new GLOBAL keyword, we could make file scope be its own indent level, and thus when declared in file scope, *local x is 1.* would be different from *global x is 1*.\r\n\r\nExamples of valid lines of script using the above rules:\r\n----------------------------------------------------------------------\r\n\r\nThe following would be equivalent and all valid:\r\n```\r\nlocal x is 0. // this is the version we'd encourage and people would probably prefer anyway.\r\nlocal x to 0.\r\ndeclare x to 0.\r\ndeclare x is 0.\r\ndeclare local x to 0.\r\ndeclare local x is 0.\r\n```\r\n\r\nThe following would be equivalent and all valid:\r\n```\r\nglobal x is 0. // this is the preferred way and the one people are more likely to use anyway.\r\nglobal x to 0.\r\ndeclare global x is 0.\r\ndeclare global x to 0.\r\n```\r\n\r\nThe following would be equivalent and all valid:\r\n```\r\nglobal lock foo to some_expression_here.\r\ndeclare global lock foo to some_expression_here.\r\nlock foo to some_expression_here. // only equivalent if there is not already a foo lock at some local scope.  Like set, it only makes a global one if there isn't one it finds to override already.\r\n```\r\n\r\nThe following would be equivalent and all valid:\r\n```\r\nlocal lock foo to some_expression_here.\r\ndeclare local lock foo to some_expression_here.\r\ndeclare lock foo to some_expression_here.\r\n```\r\n\r\nThe following would be equivalent and all valid:\r\n```\r\ndeclare parameter foo.\r\nparameter foo.\r\nlocal parameter foo.\r\ndeclare local parameter foo.\r\n```\r\n\r\nThe following would be disallowed:\r\n```\r\nglobal parameter foo.\r\ndeclare global parameter foo.\r\n```\r\n\r\n### Not as complex to implement as it looks.\r\n\r\nDespite how long it takes to explain all the possibilities in every detail, to *implement* this is really just a matter of changing the grammar file to allow the keywords to be optional, and making the compiler set some flags for the statement based on which keywords were actually present.\r\n\r\n\r\nWhy?\r\n------\r\n\r\nIssue #741 dealt with giving locks closures but didn't deal with the fact that all locks are global.  The effect is that when you make a lock, it's a lock forever.  Since they now have closures, that means they keep around the entire set of stacked variables in memory somewhere forever.\r\nConsider the following:\r\n```\r\n// make g_list a list of 100 empty vectors:\r\nset g_list to list().\r\nset i to 0. until i >= 100 { g_list:add(v(0,0,0)). set i to i +1}.\r\n\r\ndeclare function foo {\r\n  declare a to 10.\r\n\r\n  // make foo_list a local list of 100 empty vectors:\r\n  declare foo_list to list().\r\n  set i to 0. until i >= 100 { foo_list:add(v(0,0,0)). set i to i +1}.\r\n\r\n  declare function bar {\r\n    declare b to 20.\r\n\r\n    // create a lock indented to be used in bar:\r\n    lock l_bar to a+b.\r\n\r\n    // make bar_list a local list of 100 empty vectors:\r\n    declare bar_list to list().\r\n    set i to 0. until i >= 100 { bar_list:add(v(0,0,0)). set i to i +1}.\r\n\r\n    // ...Assume there's other code here, that uses the lock bar, and the bar_list and the foo_list .....\r\n  }.\r\n}.\r\n\r\nfoo().\r\n\r\n// assume more code here for the rest of the program...\r\n\r\n```\r\nThe problem with the\r\n```\r\n    lock l_bar to a+b.\r\n```\r\nbeing global is that even though it only uses A and B, it still keeps the entire scope alive in memory for both function bar and its containing function, function foo, and because it's global, it keeps that scope alive forever even after the function call foo() is long done.  So the two big lists of vectors (and whatever other locals may exist in foo() and bar() never really go away.  You can't reach them anymore - only l_bar can, but they still take up room in memory that cannot go away.\r\n\r\nThat's correct behavior for when you *need* that, like when doing a LOCK STEERING and it has to keep being capable of seeing those closure variables indefinitely until you LOCK STEERING to something else.\r\n\r\nBut there will be cases where you don't need it, and for them we need to provide users with the ability to make their locks local if they like.  Thus the impetus for this issue.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/KSP-KOS/KOS/issues/comments/92513789","html_url":"https://github.com/KSP-KOS/KOS/issues/750#issuecomment-92513789","issue_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/750","id":92513789,"user":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"created_at":"2015-04-13T22:04:57Z","updated_at":"2015-04-13T22:04:57Z","body":"I'm splitting off the docs for this into a separate issue (sort of violating my own rule I know) just so that I can fix up the bugs first, get them in for others to play with, and make the docs a separate issue I can work on after this is merged in.\r\n"}}