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

Mistake in code? #89

Open
chsasank opened this issue Jul 28, 2024 · 1 comment
Open

Mistake in code? #89

chsasank opened this issue Jul 28, 2024 · 1 comment

Comments

@chsasank
Copy link
Owner

if isinstance(stmt, list):

    def gen_stmt(self, stmt):
        try:
            if isinstance(stmt, list):
                if not stmt:
                    return []  #  Null statement
                elif self.is_compound_stmt(stmt):
                    return self.gen_compound_stmt(stmt)
                elif self.is_ret_stmt(stmt):
                    return self.gen_ret_stmt(stmt)
                elif self.is_decl_stmt(stmt):
                    return self.gen_decl_stmt(stmt)
                elif self.is_if_stmt(stmt):
                    return self.gen_if_stmt(stmt)
                elif self.is_for_stmt(stmt):
                    return self.gen_for_stmt(stmt)
                elif self.is_while_stmt(stmt):
                    return self.gen_while_stmt(stmt)
                else:
                    return self.gen_expr(stmt).instructions
            else:
                return self.gen_expr(stmt)[0]
        except Exception as e:
            print(f"Error in statement: {stmt}", file=sys.stderr)
            raise e

here, the if isinstance(stmt, list) and self.gen_expr(stmt)[0] are redundant. I think we can remove them.

@GlowingScrewdriver
Copy link
Contributor

Since we are borrowing C semantics, all expressions (including those that aren't formatted as lists) are valid statements.

Thus, the following while statement is valid:

(while #t ; Infinite loop
    0)    ; Placeholder statement

The if isinstance(stmt, list) is in place to handle the case of a non-list expression (e.g. literal, variable reference) being used as a statement. The alternative would be to have the same check in every is_*_stmt function, in which case it would fall through to return self.gen_expr(stmt).instructions.

Admittedly, though, none of our testcases use this structure.

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