2022-11-14 09:56:40 -04:00
|
|
|
|
2024-04-24 06:46:17 -03:00
|
|
|
import textwrap
|
2022-11-14 09:56:40 -04:00
|
|
|
from test.support.bytecode_helper import CodegenTestCase
|
|
|
|
|
|
|
|
# Tests for the code-generation stage of the compiler.
|
|
|
|
# Examine the un-optimized code generated from the AST.
|
|
|
|
|
|
|
|
class IsolatedCodeGenTests(CodegenTestCase):
|
|
|
|
|
2024-04-24 06:46:17 -03:00
|
|
|
def assertInstructionsMatch_recursive(self, insts, expected_insts):
|
|
|
|
expected_nested = [i for i in expected_insts if isinstance(i, list)]
|
|
|
|
expected_insts = [i for i in expected_insts if not isinstance(i, list)]
|
|
|
|
self.assertInstructionsMatch(insts, expected_insts)
|
|
|
|
self.assertEqual(len(insts.get_nested()), len(expected_nested))
|
|
|
|
for n_insts, n_expected in zip(insts.get_nested(), expected_nested):
|
|
|
|
self.assertInstructionsMatch_recursive(n_insts, n_expected)
|
|
|
|
|
2022-11-14 09:56:40 -04:00
|
|
|
def codegen_test(self, snippet, expected_insts):
|
|
|
|
import ast
|
2024-02-15 10:32:21 -04:00
|
|
|
a = ast.parse(snippet, "my_file.py", "exec")
|
2022-11-14 09:56:40 -04:00
|
|
|
insts = self.generate_code(a)
|
2024-04-24 06:46:17 -03:00
|
|
|
self.assertInstructionsMatch_recursive(insts, expected_insts)
|
2022-11-14 09:56:40 -04:00
|
|
|
|
|
|
|
def test_if_expression(self):
|
|
|
|
snippet = "42 if True else 24"
|
|
|
|
false_lbl = self.Label()
|
|
|
|
expected = [
|
|
|
|
('RESUME', 0, 0),
|
|
|
|
('LOAD_CONST', 0, 1),
|
2023-06-29 17:49:54 -03:00
|
|
|
('TO_BOOL', 0, 1),
|
2022-11-14 09:56:40 -04:00
|
|
|
('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 42, 1),
|
2024-01-06 10:20:08 -04:00
|
|
|
('JUMP_NO_INTERRUPT', exit_lbl := self.Label()),
|
2022-11-14 09:56:40 -04:00
|
|
|
false_lbl,
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 24, 1),
|
2022-11-14 09:56:40 -04:00
|
|
|
exit_lbl,
|
|
|
|
('POP_TOP', None),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_CONST', 1),
|
2023-05-07 14:47:28 -03:00
|
|
|
('RETURN_VALUE', None),
|
2022-11-14 09:56:40 -04:00
|
|
|
]
|
|
|
|
self.codegen_test(snippet, expected)
|
|
|
|
|
|
|
|
def test_for_loop(self):
|
|
|
|
snippet = "for x in l:\n\tprint(x)"
|
|
|
|
false_lbl = self.Label()
|
|
|
|
expected = [
|
|
|
|
('RESUME', 0, 0),
|
|
|
|
('LOAD_NAME', 0, 1),
|
|
|
|
('GET_ITER', None, 1),
|
|
|
|
loop_lbl := self.Label(),
|
|
|
|
('FOR_ITER', exit_lbl := self.Label(), 1),
|
2024-06-12 08:53:19 -03:00
|
|
|
('NOP', None, 1, 1),
|
2023-02-28 07:29:32 -04:00
|
|
|
('STORE_NAME', 1, 1),
|
|
|
|
('LOAD_NAME', 2, 2),
|
2023-08-09 15:19:39 -03:00
|
|
|
('PUSH_NULL', None, 2),
|
2023-02-28 07:29:32 -04:00
|
|
|
('LOAD_NAME', 1, 2),
|
|
|
|
('CALL', 1, 2),
|
2022-11-14 09:56:40 -04:00
|
|
|
('POP_TOP', None),
|
|
|
|
('JUMP', loop_lbl),
|
|
|
|
exit_lbl,
|
|
|
|
('END_FOR', None),
|
2024-01-24 11:10:17 -04:00
|
|
|
('POP_TOP', None),
|
2023-05-07 14:47:28 -03:00
|
|
|
('LOAD_CONST', 0),
|
|
|
|
('RETURN_VALUE', None),
|
2022-11-14 09:56:40 -04:00
|
|
|
]
|
|
|
|
self.codegen_test(snippet, expected)
|
2024-02-15 10:32:21 -04:00
|
|
|
|
2024-04-24 06:46:17 -03:00
|
|
|
def test_function(self):
|
|
|
|
snippet = textwrap.dedent("""
|
|
|
|
def f(x):
|
|
|
|
return x + 42
|
|
|
|
""")
|
|
|
|
expected = [
|
|
|
|
# Function definition
|
|
|
|
('RESUME', 0),
|
|
|
|
('LOAD_CONST', 0),
|
|
|
|
('MAKE_FUNCTION', None),
|
|
|
|
('STORE_NAME', 0),
|
|
|
|
('LOAD_CONST', 1),
|
|
|
|
('RETURN_VALUE', None),
|
|
|
|
[
|
|
|
|
# Function body
|
|
|
|
('RESUME', 0),
|
|
|
|
('LOAD_FAST', 0),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 42),
|
2024-04-24 06:46:17 -03:00
|
|
|
('BINARY_OP', 0),
|
|
|
|
('RETURN_VALUE', None),
|
|
|
|
('LOAD_CONST', 0),
|
|
|
|
('RETURN_VALUE', None),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
self.codegen_test(snippet, expected)
|
|
|
|
|
|
|
|
def test_nested_functions(self):
|
|
|
|
snippet = textwrap.dedent("""
|
|
|
|
def f():
|
|
|
|
def h():
|
|
|
|
return 12
|
|
|
|
def g():
|
|
|
|
x = 1
|
|
|
|
y = 2
|
|
|
|
z = 3
|
|
|
|
u = 4
|
|
|
|
return 42
|
|
|
|
""")
|
|
|
|
expected = [
|
|
|
|
# Function definition
|
|
|
|
('RESUME', 0),
|
|
|
|
('LOAD_CONST', 0),
|
|
|
|
('MAKE_FUNCTION', None),
|
|
|
|
('STORE_NAME', 0),
|
|
|
|
('LOAD_CONST', 1),
|
|
|
|
('RETURN_VALUE', None),
|
|
|
|
[
|
|
|
|
# Function body
|
|
|
|
('RESUME', 0),
|
|
|
|
('LOAD_CONST', 1),
|
|
|
|
('MAKE_FUNCTION', None),
|
|
|
|
('STORE_FAST', 0),
|
|
|
|
('LOAD_CONST', 2),
|
|
|
|
('MAKE_FUNCTION', None),
|
|
|
|
('STORE_FAST', 1),
|
|
|
|
('LOAD_CONST', 0),
|
|
|
|
('RETURN_VALUE', None),
|
|
|
|
[
|
|
|
|
('RESUME', 0),
|
|
|
|
('NOP', None),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 12),
|
2024-04-24 06:46:17 -03:00
|
|
|
('RETURN_VALUE', None),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_CONST', 1),
|
2024-04-24 06:46:17 -03:00
|
|
|
('RETURN_VALUE', None),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
('RESUME', 0),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 1),
|
2024-04-24 06:46:17 -03:00
|
|
|
('STORE_FAST', 0),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 2),
|
2024-04-24 06:46:17 -03:00
|
|
|
('STORE_FAST', 1),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 3),
|
2024-04-24 06:46:17 -03:00
|
|
|
('STORE_FAST', 2),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 4),
|
2024-04-24 06:46:17 -03:00
|
|
|
('STORE_FAST', 3),
|
|
|
|
('NOP', None),
|
2024-10-29 08:15:42 -03:00
|
|
|
('LOAD_SMALL_INT', 42),
|
2024-04-24 06:46:17 -03:00
|
|
|
('RETURN_VALUE', None),
|
|
|
|
('LOAD_CONST', 0),
|
|
|
|
('RETURN_VALUE', None),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
self.codegen_test(snippet, expected)
|
|
|
|
|
2024-02-15 10:32:21 -04:00
|
|
|
def test_syntax_error__return_not_in_function(self):
|
|
|
|
snippet = "return 42"
|
2024-09-24 05:01:37 -03:00
|
|
|
with self.assertRaisesRegex(SyntaxError, "'return' outside function") as cm:
|
2024-02-15 10:32:21 -04:00
|
|
|
self.codegen_test(snippet, None)
|
2024-09-24 05:01:37 -03:00
|
|
|
self.assertIsNone(cm.exception.text)
|
|
|
|
self.assertEqual(cm.exception.offset, 1)
|
|
|
|
self.assertEqual(cm.exception.end_offset, 10)
|