use pyo3::{FromPyObject};
use crate::codegen::{CodeGen, PythonOptions, CodeGenContext};
use proc_macro2::TokenStream;
use quote::{quote, format_ident};
use crate::{tree::Arg, Name, Keyword};
use crate::symbols::SymbolTableScopes;
use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, Default, FromPyObject, Serialize, Deserialize, PartialEq)]
pub struct Call {
pub func: Name,
pub args: Vec<Arg>,
pub keywords: Vec<Keyword>,
}
impl<'a> CodeGen for Call {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(self, ctx: Self::Context, options: Self::Options, symbols: Self::SymbolTable) -> Result<TokenStream, Box<dyn std::error::Error>> {
let name = format_ident!("{}", self.func.id);
let symbol = symbols.get(&self.func.id).expect(format!("looking up function {}", self.func.id).as_str());
println!("symbol: {:?}", symbol);
let args = self.args[0].clone().to_rust(ctx, options, symbols).expect(format!("parsing arguments {:?}", self.args[0]).as_str());
Ok(quote!(#name(#args)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lookup_of_function() {
let options = PythonOptions::default();
let result = crate::parse("def foo(a = 7):
pass
foo(b=9)", "test").unwrap();
println!("Python tree: {:#?}", result);
let code = result.to_rust(CodeGenContext::Module, options, SymbolTableScopes::new()).unwrap();
println!("Rust code: {}", code);
}
}