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

Make it possible to define array comprehensions #12

Open
masak opened this issue Jan 29, 2015 · 1 comment
Open

Make it possible to define array comprehensions #12

masak opened this issue Jan 29, 2015 · 1 comment

Comments

@masak
Copy link
Owner

masak commented Jan 29, 2015

The idea is to be able to define a macro that makes this syntax legal.

my numbers = [1, 2, 3];
my squares = [ x <- numbers | x * x ];

Roughly, it'd happen something like this:

  • We define a macro term:<[>
  • It parses enough to find out if the first things after the [ are an identifier and an arrow <- (modulo whitespace)
  • If it isn't, then it defers control to the usual (right now hypothetical) term:<[> which knows how to parse normal arrays.

Assuming it succeeds, it parses the rest, and generates this code:

my res = [];
for numbers -> x {
    res.push(x * x);
}
res;

What's necessary for this to work?

  • Macro terms
  • Built-in terms being (conceptually) macros too, so that we can defer to them
  • Some is parsed functionality
  • A defer mechanism (which, in a given sub or macro, physically substitutes the current call with a call to the sub or macro that was shadowed)

And, crucially,

  • A way to avoid infinite macro recursion when the [] is parsed in the code to be generated.

I can see two ways to do that last bit. Either we don't write quasi code at all for that (because it's too complicated anyway), or we simply pull out [] into a constant (EMPTY_ARRAY, say) before the macro, and use that.

@masak
Copy link
Owner Author

masak commented Nov 4, 2018

#421 would be a good mechanism to achieve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant