towerofnix / tlnccuwagnf

The Language Nobody Could Come Up With A Good Name For
MIT License
18 stars 3 forks source link

New environment feature #10

Closed towerofnix closed 8 years ago

towerofnix commented 8 years ago

Not finished yet.

TODO:

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 => environment

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:

env.foo

And you can set it to 42 like so:

env.foo > 42;

Setting a variable using the environment will dynamically create it within the scope:

print(foo); # 42 #

Using this method of setting a variable is equivalent to assigning a variable: env.var > val is the same as var => val.

You can also make a variable with variable_make:

variable_make(env, 'foo', 42);

Raw variables

You can access the raw variable objects using variable_raw:

foo_var => variable_raw(env, 'foo'); # <Variable> #

Variables can be changed with variable_change:

variable_change(foo_var, 'new foo value');

The value of a variable can be gotten with variable_value:

print(variable_value(foo_var)); # new foo value #

You can't get a non-existant variable:

variable_raw(env, 'fake_variable') # error #

You can use variable_exists to check if a variable exists within a given environment:

variable_exists(env, 'fake_variable') # false #
variable_exists(env, 'foo') # true #
BookOwl commented 8 years ago

Looks really cool!

bates64 commented 8 years ago

This seems fancy.

brianharvey commented 8 years ago

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.

towerofnix commented 8 years ago

Yep, that's the idea. I'll need to change the documentation a little.

bates64 commented 8 years ago

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:

towerofnix commented 8 years ago

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! #
towerofnix commented 8 years ago

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)