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

Add iterators #82

Open
adam-mcdaniel opened this issue Aug 27, 2020 · 2 comments
Open

Add iterators #82

adam-mcdaniel opened this issue Aug 27, 2020 · 2 comments

Comments

@adam-mcdaniel
Copy link
Owner

Right now, for loops look like the following:

for (let i=0; i<10; i+=1) { ... }
// or
for i in 0..10 { ... }

Although these look different, they are identical after TIR. I think the first for loop should stay the same, but the second should change. Instead of being expanded to for (let i=0; i<10; i+=1) {...}, it should be expanded to something like

// Replace 1 with however many for loops have preceded this one, so that the variables don't clobber each other in nested loops
let %FOR_ITER_1% = Range::new(0, 10);
for (let i = %FOR_ITER_1%.next(); !(itr.is_done()); i=%FOR_ITER_1%.next()) { ... }

Additionally, this compile time expansion would allow any type that has a next and is_done method to be an iterator for a loop.

#[std]
const EOF = 0;
struct Input {
    let last: char;
    fn stdin() -> Input { return ' ' as Input }
    fn next(self: &Input) -> char {
        self->last = get_char();
        return self->last;
    }

    fn is_done(self: &Input) -> bool { return self->last == EOF as char }
}

fn main() {
    for ch in Input::stdin() {
        putstr("Got char '");
        putchar(ch);
        putcharln('\'');
    }
}
@kevinramharak
Copy link
Contributor

I like this idea. Would Range be an internal type or would you actually implement it in oak?

@adam-mcdaniel
Copy link
Owner Author

@kevinramharak I would probably implement it in Oak in core.ok, I think. This way its easier for us to hack at it

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