Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird variable scope #13

Closed
Kjarrigan opened this issue Dec 2, 2021 · 2 comments
Closed

Weird variable scope #13

Kjarrigan opened this issue Dec 2, 2021 · 2 comments
Milestone

Comments

@Kjarrigan
Copy link
Contributor

The title is a bit weird because I'm not sure how to describe it properly in brief BUT for the current AoC I tried RL once again and had some issues with the missing string functions so I tried building some myself, but due to how the variables work it's currently not possible. See my sample:

"// Last Updated: 2021.12.02"
"// RocketLang Version: 0.9.7"
"------------------------------------"

let x = 0;
let depth = 0;

let each = fn(enumerable, yield, idx) {
  if (idx == len(enumerable)) {
    return len(enumerable);
  }
  yield(enumerable[idx]);
  each(enumerable, yield, idx + 1);
};

"Does not work because you have to do push with let a = push(a, b)"
"which makes a a 'local' variable and thus breaks everything -.-"
let split = fn(string, separator) {
  let list = [];
  let current_word = "";

  each(string, fn(char) {
    puts(char == separator);
    if (char == separator) {
      puts "Split found!"
      let list = push(list, current_word);
      let current_word = "";
    } else {
      puts(current_word);
      puts("Another letter to the word: " + char);
      let current_word = current_word + char;
      puts(current_word);
    }
  }, 0);
  let list = push(list, current_word);

  return list;
};

split("Hello World", " ");

"Simplified sample of split. super sad that this does not work :-("
let char_array = [];
let counter = 0;
each("Hello World", fn(c) {
  let char_array = push(char_array, c);
  let counter = counter + 1;
}, 0);
puts(char_array);
puts(counter);

In theory it should "fill" the outer variables with characters and strings because they are defined outside of the inner block but I can't do current_word = current_word + "Hello" because I have to write a let before which then makes it a local variable thus the house of cards crumble.

Not sure if this is solvable but a workaround might be to make push change the array itself rather than returning a new one, but I think you tried this already when I requested the push/pop functions....

@Kjarrigan
Copy link
Contributor Author

if this could be solved a lot of (helper) functions could be implemented like:

"Mississippi".count("i") # => 4
"Hello World".split(" ") # => ["Hello", "World"]
"Hello World".tr("e", "a") # => "Hallo World"

@Flipez Flipez added this to the v0.10.0 milestone Dec 20, 2021
@Flipez
Copy link
Owner

Flipez commented Dec 20, 2021

All the issues mentioned should be fixed in version 0.10.0 as it introduces a lot new features such as

  • in-place operations
  • foreach loop
  • let-free variable usage

Feel free to test with the next branch until release.

@Flipez Flipez closed this as completed Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants