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

Support statements and expressions in the same cell #3

Open
sbliven opened this issue Mar 21, 2022 · 4 comments
Open

Support statements and expressions in the same cell #3

sbliven opened this issue Mar 21, 2022 · 4 comments

Comments

@sbliven
Copy link

sbliven commented Mar 21, 2022

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.

a = 1;
a
int nonvoidFunction() {return 0;}
nonvoidFunction()

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)).

nonvoidFunction();
a = [4];
void voidFuntion() {}
voidFunction()

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.

@ashinkarov
Copy link
Collaborator

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 ;, but this is something we can't change.

@sbliven
Copy link
Author

sbliven commented Mar 22, 2022

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?

@ashinkarov
Copy link
Collaborator

Well, multiple statements are supported, right? You can write things like:

a = 5;
b = 6;

This is fine, it is just you can't add a+b with the intent to print it out.

In python expressions are valid statements.

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 5 in repl, but prints nothing if I call python x.py. I don't understand how exactly they do this (probably tinkering the mode of interpretation, or something like that).

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.

@sbliven
Copy link
Author

sbliven commented Mar 25, 2022

Unless we change the jupyter validator to accept a list of statements and one expression at the end...

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.

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