1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use pyo3::{FromPyObject, PyAny, PyResult};
use proc_macro2::TokenStream;
use quote::quote;
use serde::{Serialize, Deserialize};

use crate::{
    CodeGen, PythonOptions, CodeGenContext,
    Arg, Keyword, ExprType,
    SymbolTableScopes,
};

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Call {
    pub func: Box<ExprType>,
    pub args: Vec<Arg>,
    pub keywords: Vec<Keyword>,
}


impl<'a> FromPyObject<'a> for Call {
    fn extract(ob: &'a PyAny) -> PyResult<Self> {
        let func = ob.getattr("func").expect("Call.func");
        let args = ob.getattr("args").expect("Call.args");
        let keywords = ob.getattr("keywords").expect("Call.keywords");
        Ok(Call {
            func: Box::new(func.extract().expect("Call.func")),
            args: args.extract().expect("Call.args"),
            keywords: keywords.extract().expect("Call.keywords"),
        })
    }
}


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 = self.func.to_rust(ctx.clone(), options.clone(), symbols.clone()).expect("Call.func");
        // XXX - How are we going to figure out the parameter list?
        //let symbol = symbols.get(&self.func.id).expect(format!("looking up function {}", self.func.id).as_str());
        //println!("symbol: {:?}", symbol);
        let mut args = TokenStream::new();
        for arg in self.args {
            let arg = arg.clone().to_rust(ctx.clone(), options.clone(), symbols.clone()).expect(format!("Call.args {:?}", arg).as_str());
            args.extend(arg);
            args.extend(quote!(,));
        }
        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("test".to_string()), options, SymbolTableScopes::new()).unwrap();
        println!("Rust code: {}", code);
    }
}