Re: [RFC] Generators
| From: | Ángel González | Date: | Mon, 20 Aug 2012 20:07:14 +0000 |
| Subject: | Re: [RFC] Generators | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62309@lists.php.net to get a copy of this message | ||
On 20/08/12 15:12, Nikita Popov wrote:
>> You could not decorate it and rely instead on the presence of the yield
>> keyword, but parsers will thank knowing about it from the start rather
>> than realising at mid-parsing that the function is a completely
>> different beast.
> No, parsers don't care about this. It's trivial to detect in both cases.
>
> Nikita
Yes, it's trivial to detect, but how much work is for it to do so?
Suppose the parser reads php code and outputs machine instructions. When
it encouters a function(), it adds the function prologue, goes reserving
stack
space for the variables it encounters, and so on. Then you find a yield
keyword.
Options:
- Discard the generated code and go back to the function begin, using
this time
an storage into an object instead of the stack.
- Copy all the state to an object and return it.
- Add an initial pass checking which functions are generators.
PHP being a dynamic language, it may be able to move the function variables
to a class. But it's not trivial for any parser.