3 ms·
This would probably ban "4[5]" but not "$x = 4; $x[5]". PHP enforces a lot of things in the parser (as opposed to making bytecode and having a simple type chec
by pbiggar 14y ago
This would probably ban "4[5]" but not "$x = 4; $x[5]".
PHP enforces a lot of things in the parser (as opposed to making bytecode and having a simple type checker). Instead of having a general "expression" type which can be dereferenced, they have different rules for scalars, strings, variables, function calls, etc. For a long time, you couldn't dereference the result of a function call, because the parser didn't allow it, so this is probably the same.
- mikeash 14y agoEnforcing it in the parser seems so horribly wrong. If you want to disallow stuff like 4[5] at compile time then you should do that in a separate pass on the AST, not in the parser itself. That's ought to be a semantic error, not a syntax error. Treating that separately would require all manner of special cases. But, I guess that must be what they do.
- pbiggar 14y agoYeah, there's tons of special cases in the parser. I worked on phc (http://phpcompiler.org http://phpcompiler.org), so I've read the PHP parser and helped write some of our own. We took the AST approach and it worked out pretty well. But PHP was written before its authors knew good interpreter design, and its currently very hard to refactor it out to a better design.
- mikeash 14y agoPainful. Thanks for sharing your experience. Cool to ask such a question and get an answer from someone who's actually built something related.