diff --git a/tests/cases/checker/05_control_flow.py b/tests/cases/checker/05_control_flow.py new file mode 100644 index 0000000..486818b --- /dev/null +++ b/tests/cases/checker/05_control_flow.py @@ -0,0 +1,25 @@ +def valid(a: int, b: int) -> int: + return a + b + +def with_if(a: int, b: int) -> int: + if a < b: + return b - a + else: + return a - b + +def unreachable1(): + return + a = 0 + +def unreachable2(a: int) -> int: + if a > 10: + return a - 10 + else: + return a + b = 0 + +def mixed(a: int, b: int): + if a < b: + return b - a + else: + return "oops" diff --git a/tests/cases/checker/05_control_flow.py.ref.json b/tests/cases/checker/05_control_flow.py.ref.json new file mode 100644 index 0000000..a68a7b9 --- /dev/null +++ b/tests/cases/checker/05_control_flow.py.ref.json @@ -0,0 +1,46 @@ +{ + "diagnostics": [ + { + "type": "Warning", + "location": { + "start": [ + 12, + 4 + ], + "end": [ + 12, + 9 + ] + }, + "message": "Unreachable statement" + }, + { + "type": "Warning", + "location": { + "start": [ + 19, + 4 + ], + "end": [ + 19, + 9 + ] + }, + "message": "Unreachable statement" + }, + { + "type": "Error", + "location": { + "start": [ + 21, + 0 + ], + "end": [ + 25, + 21 + ] + }, + "message": "Mixed return types: [BaseType(name='int'), BaseType(name='str')]" + } + ] +} \ No newline at end of file diff --git a/tests/serializer/python.py b/tests/serializer/python.py index e14dc1b..8dc43f4 100644 --- a/tests/serializer/python.py +++ b/tests/serializer/python.py @@ -15,6 +15,7 @@ from midas.ast.python import ( FrameType, Function, GetExpr, + IfStmt, LiteralExpr, LogicalExpr, MidasType, @@ -164,6 +165,14 @@ class PythonAstJsonSerializer( "value": self._serialize_optional(stmt.value), } + def visit_if_stmt(self, stmt: IfStmt) -> dict: + return { + "_type": "IfStmt", + "test": stmt.test.accept(self), + "body": self._serialize_list(stmt.body), + "orelse": self._serialize_list(stmt.orelse), + } + def visit_binary_expr(self, expr: BinaryExpr) -> dict: return { "_type": "BinaryExpr",