|
5 | 5 | //! https://github.com/python/cpython/blob/master/Python/compile.c |
6 | 6 | //! https://github.com/micropython/micropython/blob/master/py/compile.c |
7 | 7 |
|
8 | | -use crate::bytecode::{self, CallType, CodeObject, Instruction, Varargs}; |
9 | 8 | use crate::error::{CompileError, CompileErrorType}; |
10 | 9 | use crate::symboltable::{make_symbol_table, statements_to_symbol_table, SymbolRole, SymbolScope}; |
11 | 10 | use num_complex::Complex64; |
| 11 | +use rustpython_bytecode::bytecode::{self, CallType, CodeObject, Instruction, Varargs}; |
12 | 12 | use rustpython_parser::{ast, parser}; |
13 | 13 |
|
14 | 14 | struct Compiler { |
@@ -599,9 +599,9 @@ impl Compiler { |
599 | 599 | let line_number = self.get_source_line_number(); |
600 | 600 | self.code_object_stack.push(CodeObject::new( |
601 | 601 | args.args.iter().map(|a| a.arg.clone()).collect(), |
602 | | - Varargs::from(&args.vararg), |
| 602 | + compile_varargs(&args.vararg), |
603 | 603 | args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(), |
604 | | - Varargs::from(&args.kwarg), |
| 604 | + compile_varargs(&args.kwarg), |
605 | 605 | self.source_path.clone().unwrap(), |
606 | 606 | line_number, |
607 | 607 | name.to_string(), |
@@ -1743,7 +1743,7 @@ impl Compiler { |
1743 | 1743 | } => { |
1744 | 1744 | self.compile_expression(value)?; |
1745 | 1745 | self.emit(Instruction::FormatValue { |
1746 | | - conversion: *conversion, |
| 1746 | + conversion: conversion.map(compile_conversion_flag), |
1747 | 1747 | spec: spec.clone(), |
1748 | 1748 | }); |
1749 | 1749 | } |
@@ -1773,7 +1773,7 @@ impl Compiler { |
1773 | 1773 |
|
1774 | 1774 | // Low level helper functions: |
1775 | 1775 | fn emit(&mut self, instruction: Instruction) { |
1776 | | - let location = self.current_source_location.clone(); |
| 1776 | + let location = compile_location(&self.current_source_location); |
1777 | 1777 | let cur_code_obj = self.current_code_object(); |
1778 | 1778 | cur_code_obj.instructions.push(instruction); |
1779 | 1779 | cur_code_obj.locations.push(location); |
@@ -1834,13 +1834,33 @@ fn get_doc(body: &[ast::LocatedStatement]) -> (&[ast::LocatedStatement], Option< |
1834 | 1834 | (body, None) |
1835 | 1835 | } |
1836 | 1836 |
|
| 1837 | +fn compile_location(location: &ast::Location) -> bytecode::Location { |
| 1838 | + bytecode::Location::new(location.get_row(), location.get_column()) |
| 1839 | +} |
| 1840 | + |
| 1841 | +fn compile_varargs(varargs: &ast::Varargs) -> bytecode::Varargs { |
| 1842 | + match varargs { |
| 1843 | + ast::Varargs::None => bytecode::Varargs::None, |
| 1844 | + ast::Varargs::Unnamed => bytecode::Varargs::Unnamed, |
| 1845 | + ast::Varargs::Named(param) => bytecode::Varargs::Named(param.arg.clone()), |
| 1846 | + } |
| 1847 | +} |
| 1848 | + |
| 1849 | +fn compile_conversion_flag(conversion_flag: ast::ConversionFlag) -> bytecode::ConversionFlag { |
| 1850 | + match conversion_flag { |
| 1851 | + ast::ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii, |
| 1852 | + ast::ConversionFlag::Repr => bytecode::ConversionFlag::Repr, |
| 1853 | + ast::ConversionFlag::Str => bytecode::ConversionFlag::Str, |
| 1854 | + } |
| 1855 | +} |
| 1856 | + |
1837 | 1857 | #[cfg(test)] |
1838 | 1858 | mod tests { |
1839 | 1859 | use super::Compiler; |
1840 | | - use crate::bytecode::CodeObject; |
1841 | | - use crate::bytecode::Constant::*; |
1842 | | - use crate::bytecode::Instruction::*; |
1843 | 1860 | use crate::symboltable::make_symbol_table; |
| 1861 | + use rustpython_bytecode::bytecode::CodeObject; |
| 1862 | + use rustpython_bytecode::bytecode::Constant::*; |
| 1863 | + use rustpython_bytecode::bytecode::Instruction::*; |
1844 | 1864 | use rustpython_parser::parser; |
1845 | 1865 |
|
1846 | 1866 | fn compile_exec(source: &str) -> CodeObject { |
|
0 commit comments