-
Notifications
You must be signed in to change notification settings - Fork 5
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
Support statements and expressions in the same cell #3
Comments
I don't think that this is a good idea. Think about existing repls, typically you type one expression/statement/function at a time. Given that creating new jupyter boxes is easy, I don't see much point in allowing to merge statements and expressions. As for " loosen the distinction between statements and expressions", I don't see how this is possible. Again, think existing repls, Python does distinghuish expressions and statements: >>> 2+2
4
>>> if True:
... print(1)
... Syntactically you don't need to put |
I think it's nice to use cells to organize the notebook code. Many notebooks I've encountered only split out a new cell when you want to output some expression. It's common that you want to execute some statements together and see the result, and this is more difficult if they are split across multiple cells. In python expressions are valid statements. That has been the case in all languages I'm familiar with (even C accepts expressions and empty statements). However I'll accept that probably changing the language syntax isn't possible. Could the rigid semicolon requirement be used to do a quick regex-based separation of statements and expressions without parsing the full syntax? |
Well, multiple statements are supported, right? You can write things like:
This is fine, it is just you can't add
This is an interesting one. Expressions are statements, in the same way as they are statements in C. Indeed you can write something like: def f():
5
return 10 However, these statements won't have any effect. Definitely not the effect that you are after. On the other hand, there is a difference between repl and the interpreter. The following code: if True:
5
else:
6 prints As for compiled language like SaC that has types, I don't see a consistent way of doing this. Unless we change the jupyter validator to accept a list of statements and one expression at the end... As for using regexps for parsing --- this is bad idea. With-loop is an expression but it may contain code blocks, and this can be all nested, so there is no way you can come up with a proper regexp. |
I think this would solve the issue if it can be done. I now see that you're already parsing with sac2c, so if jupyter_parse_from_string can be modified to accept a single trailing expression that would be perfect. I only mentioned regex because I assumed parsing would have to be done in python and you wouldn't want to write a complete parser again. |
Currently a cell can either contain statements or a single expression. At a minimum this should be relaxed to allow some statements followed by an expression.
Better would be to loosen the distinction between statements and expressions so that they can be used interchangably. This would make semicolon use more predictable (eg no need to distinguish void function calls (statements) from non-void calls (expressions)).
For a final twist, I can't figure out how use statements fit in (they require semicolons but are one-per-cell like expressions), but they should be harmonized as well.
The text was updated successfully, but these errors were encountered: