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

Indentation in Python can cause unfriendly errors #2055

Open
edwardalee opened this issue Oct 14, 2023 · 1 comment
Open

Indentation in Python can cause unfriendly errors #2055

edwardalee opened this issue Oct 14, 2023 · 1 comment
Labels
enhancement Enhancement of existing feature good first issue Good for newcomers python Related to the Python target

Comments

@edwardalee
Copy link
Collaborator

It may not be worth much effort to fix this, but the following program generates Python that will not execute:

target Python
main reactor {
  reaction(startup) {=
    # comment
        print("Hello world");
  =}
}

The problem is that the comment is indented less than the code in the reaction body, which is apparently allowed in Python, but the code generator appends a return 0 statement to the generated reaction body, which looks like this:

    def reaction_function_0(self):
    
        # comment
            print("Hello world");
        return 0

It's not clear to me why a return value is needed for reactions in the Python target. This seems problematic anyway. The following code works correctly without any return value having been specified:

target Python
main reactor {
  state s = 0
  timer t(0, 1s)
  reaction(t) {=
    self.s += 1
    if (self.s % 2 == 0):
      print("odd")
      return
    print("even")
  =}
}

Maybe the return statement is not needed?
If this is the case, it could just be removed from generatePythonFunction in PythonReactionGenerator.java.

@edwardalee edwardalee added enhancement Enhancement of existing feature python Related to the Python target labels Oct 14, 2023
@edwardalee
Copy link
Collaborator Author

It seems the reason for this generated return statement is that Python does not tolerate empty reaction bodies. The generatePythonFunction method in PythonReactionGenerator.java could be changed to read something like this:

  public static String generatePythonFunction(
      String pythonFunctionName,
      String inits,
      String reactionBody,
      List<String> reactionParameters) {
    String params =
        reactionParameters.size() > 0 ? ", " + String.join(", ", reactionParameters) : "";
    CodeBuilder code = new CodeBuilder();
    code.pr("def " + pythonFunctionName + "(self" + params + "):");
    code.indent();
    code.pr(inits);
    code.pr(reactionBody);
    if (isEmptyOrCommentsOnly(initia) && isEmptyOrCommentsOnly(reactionBody)) {
      // Python does not tolerate an empty reaction body nor one with just comments.
      code.pr("return");
    }
    return code.toString();
  }

However, the isEmptyOrCommentsOnly method would have to be written and tests would need to be added.

@edwardalee edwardalee added the good first issue Good for newcomers label Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement of existing feature good first issue Good for newcomers python Related to the Python target
Projects
None yet
Development

No branches or pull requests

1 participant