====== Objects, namespaces, dictionaries, and hashes ======
===== DRAFT IN PROGRESS =====
[[http://forum.lolcode.com/viewtopic.php?id=431|Discussion thread here]].
I hope to "finish" the proposal this weekend before we begin ripping it apart. Feel free to look at it and get a feel for what I've tried to compile.
===== Variables/Scope =====
==== Memory ====
The LOLCode language so far has either had only static variables, or required the use of a garbage collector. This proposal formally specifies a Garbage Collection Strategy. All variables are merely references to locations in memory. It is assumed that when a variable is no longer referenced, that variable's allocated space will be freed sometime in the future, or on program exit.
==== Declaration ====
Here's a few changes to how Declaring variables works.
I HAS A ITZ
This instantiates and initializes a variable.
If the value is a literal, the variable is initialized to the appropriate object type (YARN, TROOF, NOOB, NUMBR, NUMBAR).
If the value is an identifier or expression, the variable is initialized to the resulting expression.
I HAS A ITZ A
This instatiates a variable, and initializes it to a default value.
Built-In default values:
* YARN - ""
* TROOF - FAIL
* NUMBR - 0
* NUMBAR - 0.0
* NOOB - NOOB
I HAS A
This instatiates a variable and initializes it to NOOB. It is shorthand for I HAS A ITZ NOOB
Function types are declared/initialized using the HOW DUZ I / IF U SAY SO blocks, however they behave the same as variables. For example:
HOW DUZ I var YR stuff
BTW implement
IF U SAY SO
I HAS A var ITZ 0 BTW Throws an error AS var is already taken
var R 0 BTW FUNIKSHUN var no longer exists, it's now NUMBR-0
==== Deallocation ====
R NOOB
This above code ensures that a variable no longer references anything. The reference still exists on the current scope and still requires a small amount of memory. If this was the last reference to an object, it will be garbage collected in the future.
==== Primitive Types ====
All primitive types are considered Immutable. All built in operations return new objects instead of references to old objects. The exceptions to this rule are WIN, FAIL and NOOB. Every TROOF reference is either the WIN or FAIL object. Every NOOB reference is ot the NOOB instance.
==== SRS (serious) Cast ====
The SRS operation can be used to interpret a YARN (or something castable to a YARN) as an identifier. This operator may be used anywhere that a regular identifier is expected.
SRS
Examples:
I HAS A var ITZ 0
Is the same as:
I HAS A name ITZ "var"
I HAS A SRS name ITZ 0
The A becomes optional in variable declaration so:
I HAS A SRS name ITZ 0
is the same as
I HAS SRS name ITZ 0
===== FUNKSHUNS =====
Functions have a new function call syntax to ensure appropriate grammatical parsing (and proper english verb tense):
I IZ (YR (AN YR ) * ) ? MKAY
The I parameter is used to distingish a function call on the current namespace vs. a function call on a bukkit (defined below).
The MKAY has been added to help disambiguate function calls.
Creating functions is done with a new HOW IZ I operator instead of HOW DUZ I.
HOW IZ I (YR (AN YR )*)?
::code-block::
IF U SAY SO
===== BUKKITs =====
BUKKITs are the container type. They may hold NUMBRs, NUMBARs, TROOFs, YARNs, functions (FUNKSHUN), and other BUKKITS. Each entity within a BUKKIT may be indexed by a NUMBR or a YARN. These indices, whether NUMBRs or YARNs, referring to functions, variables, or other BUKKITs, are generically called "slots".
==== Introduction ====
To create an empty object within the current object's scope:
I HAS A
This object will have the default behavior of all bukkits.
=== Slot Creation / Assignment ===
One of the most obvious thing to do with a bukkit it place something in a slot.
A slot may be declared/initialized more than once, however doing so only changes the value the slot references.
This places the value returned from expression (could be another object) into the slot identified by slotname. The slot name may be any identifier (or SRS BIZNUS cast). Note: This identifier may be a function. Example:
HOW IZ I blogin YR stuff
VISIBLE stuff
IF U SAY SO
== Functions ==
The function definition syntax has changed as well. To declare a function inside a bukkit's slot
HOW IZ
So, the above code becomes
HOW IZ
== Me, myself and I ==
Functions operate differently in the context of bukkits. When a function is called from an object, some scope rules and variable resolution change.
When an identifier is used in a function, the variable is looked up in the following manner:
* The function namespace
* The calling object's namespace (if called from object)
* The "global" namespace
//IT is always looked up from global namespace//
The function namespace is made up of all arguments and any variable declared using the identifier "I" it resides within the function's namespace.
HOW IZ I fooin YR bar
BTW bar is on function namespace
I HAS A bar2
BTW bar2 is on the function namespace
IF U SAY SO
ME is an identifer used to access the calling object of a function. If there is no calling object, access to ME throws an exception. //Define This exception//
Declaring a variable on the calling objects namespace is done as follows:
HOW IZ I fooin YR bar
ME HAS A bar2
BTW bar2 is now a slot on calling object
IF U SAY SO
ME can also be used to explicitly use a slot variable vs. a function namespace variable.
HOW IZ I fooin YR bar
ME'Z bar R bar
BTW sets calling object's bar slot to bar value
IF U SAY SO
=== Alternate Syntax ===
There is an alternate way to define a new object/bukkit
O HAI IM
Anything "I" inside the codeblock actually refers to