Closed towerofnix closed 8 years ago
Looks really cool!
This seems fancy.
Your documentation in the commit message doesn't say which frame gets a newly created variable. I'm guessing it's the innermost one, but you have to make that clear.
Yep, that's the idea. I'll need to change the documentation a little.
And this allows for global variables. My understanding of this is essentially "oh so it's like redefining window in javascript" - is that right? :8ball:
Well, yes, you can access the "window" or global environment by using environment
in the top scope:
global => environment;
fn() {
# do things #
global.foo > 42;
}();
print(foo); # 42 #
But you can also access any scope.. well, that you have access to:
global => environment;
fn() {
# haha, you can't access this environment! #
bar => "No looking at me";
}();
fn() {
fn_env => environment;
fn() {
inner_fn_env => environment;
}();
}();
There's no "function environment".. if there was, though, you could do this:
bar_fn => fn() {
bar => "You can't get me!";
};
print(invoke_and_get_environment(bar_fn).bar); # You can't get me! #
Probably going to go with these names.
environment
Variable.from(env, name)
Variable.value(var)
Variable.change(var, new_value)
Variable.make(var, name, new value)
Variable.exists(env, name)
Not finished yet.
TODO:
variable_(something)
functions into methods of a new global object (moved toVariable.(something)
)Environments
Environments are used to dynamically create, modify, and do various other things with variables.
You can get started with environments by getting the current environment the interpreter is using. Just use the
environment
keyword to do so:env
now stores the interpreter's environment -- basically it's a container for all the variables within the (implicitly) given scope.You can access the value of the variable "foo" like so:
And you can set it to 42 like so:
Setting a variable using the environment will dynamically create it within the scope:
Using this method of setting a variable is equivalent to assigning a variable:
env.var > val
is the same asvar => val
.You can also make a variable with
variable_make
:Raw variables
You can access the raw variable objects using
variable_raw
:Variables can be changed with
variable_change
:The value of a variable can be gotten with
variable_value
:You can't get a non-existant variable:
You can use
variable_exists
to check if a variable exists within a given environment: