chsasank / llama.lisp

Lisp dialect designed for HPC and AI
GNU Lesser General Public License v2.1
14 stars 7 forks source link

Mistake in code? #89

Open chsasank opened 1 month ago

chsasank commented 1 month ago

https://github.com/chsasank/llama.lisp/blob/c79daf7a267461f5ebf8fef57d61321192a6127c/src/backend/c-lisp.py#L137

    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 commented 1 month ago

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.