-
-
Notifications
You must be signed in to change notification settings - Fork 31k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
06d4e02
commit a3ac923
Showing
11 changed files
with
321 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
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): | ||
|
||
def codegen_test(self, snippet, expected_insts): | ||
import ast | ||
a = ast.parse(snippet, "my_file.py", "exec"); | ||
insts = self.generate_code(a) | ||
self.assertInstructionsMatch(insts, expected_insts) | ||
|
||
def test_if_expression(self): | ||
snippet = "42 if True else 24" | ||
false_lbl = self.Label() | ||
expected = [ | ||
('RESUME', 0, 0), | ||
('LOAD_CONST', 0, 1), | ||
('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1), | ||
('LOAD_CONST', 1, 1), | ||
('JUMP', exit_lbl := self.Label()), | ||
false_lbl, | ||
('LOAD_CONST', 2, 1), | ||
exit_lbl, | ||
('POP_TOP', None), | ||
] | ||
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), | ||
('STORE_NAME', None, 1), | ||
('PUSH_NULL', None, 2), | ||
('LOAD_NAME', None, 2), | ||
('LOAD_NAME', None, 2), | ||
('CALL', None, 2), | ||
('POP_TOP', None), | ||
('JUMP', loop_lbl), | ||
exit_lbl, | ||
('END_FOR', None), | ||
] | ||
self.codegen_test(snippet, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.