Skip to content

Commit

Permalink
feat: support varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Dec 9, 2023
1 parent f396fcb commit 84c72e6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 58 deletions.
88 changes: 50 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ edition = "2021"
repository = "https://github.com/mtshiba/pylyzer"

[workspace.dependencies]
erg_common = { version = "0.6.25-nightly.1", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.25-nightly.1", features = ["py_compat", "els"] }
els = { version = "0.1.37-nightly.1", features = ["py_compat"] }
erg_common = { version = "0.6.27-nightly.0", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.27-nightly.0", features = ["py_compat", "els"] }
els = { version = "0.1.39-nightly.0", features = ["py_compat"] }
rustpython-parser = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
rustpython-ast = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
# rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
Expand Down
34 changes: 20 additions & 14 deletions crates/py2erg/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,12 @@ impl ASTConverter {
}

fn convert_params(&mut self, params: Arguments) -> Params {
fn split_args(params: Arguments) -> (Vec<Arg>, Vec<(Arg, py_ast::Expr)>) {
#[allow(clippy::type_complexity)]
fn split_args(params: Arguments) -> (Vec<Arg>, Option<Arg>, Vec<(Arg, py_ast::Expr)>, Option<Arg>) {
let mut args = Vec::new();
let mut with_defaults = Vec::new();
let var_args = params.vararg.map(|x| *x);
let kw_args = params.kwarg.map(|x| *x);
for arg in params
.posonlyargs
.into_iter()
Expand All @@ -467,18 +470,20 @@ impl ASTConverter {
args.push(arg.def);
}
}
(args, with_defaults)
(args, var_args, with_defaults, kw_args)
}
let (non_defaults, defaults) = split_args(params);
let (non_defaults, var_args, defaults, kw_args) = split_args(params);
let non_defaults = non_defaults
.into_iter()
.map(|p| self.convert_nd_param(p))
.collect();
let var_params = var_args.map(|p| self.convert_nd_param(p));
let defaults = defaults
.into_iter()
.map(|(kw, default)| self.convert_default_param(kw, default))
.collect();
Params::new(non_defaults, None, defaults, None)
let kw_var_params = kw_args.map(|p| self.convert_nd_param(p));
Params::new(non_defaults, var_params, defaults, kw_var_params, None)
}

fn convert_for_param(&mut self, name: String, loc: PyLocation) -> NonDefaultParamSignature {
Expand Down Expand Up @@ -572,7 +577,7 @@ impl ASTConverter {

fn convert_for_body(&mut self, lhs: Option<py_ast::Expr>, body: Suite) -> Lambda {
let (param, block) = self.convert_opt_expr_to_param(lhs);
let params = Params::new(vec![param], None, vec![], None);
let params = Params::new(vec![param], None, vec![], None, None);
self.block_id_counter += 1;
self.block_ids.push(self.block_id_counter);
let body = body
Expand Down Expand Up @@ -652,7 +657,7 @@ impl ASTConverter {
}
}
}
let elems = ConstArgs::new(elems, None, vec![], None);
let elems = ConstArgs::new(elems, None, vec![], None, None);
TypeSpec::Enum(elems)
}
// TODO: distinguish from collections.abc.Callable
Expand Down Expand Up @@ -688,6 +693,7 @@ impl ASTConverter {
non_defaults,
None,
vec![],
None,
ARROW,
ret,
))
Expand Down Expand Up @@ -790,7 +796,7 @@ impl ASTConverter {
global,
Identifier::private("Array!".into()),
));
TypeSpec::poly(acc, ConstArgs::new(vec![elem_t, len], None, vec![], None))
TypeSpec::poly(acc, ConstArgs::new(vec![elem_t, len], None, vec![], None, None))
}
"dict" => {
let py_ast::Expr::Tuple(mut tuple) = args else {
Expand Down Expand Up @@ -834,7 +840,7 @@ impl ASTConverter {
global,
Identifier::private("Dict!".into()),
));
TypeSpec::poly(acc, ConstArgs::new(vec![dict], None, vec![], None))
TypeSpec::poly(acc, ConstArgs::new(vec![dict], None, vec![], None, None))
}
"tuple" => {
let py_ast::Expr::Tuple(tuple) = args else {
Expand Down Expand Up @@ -1022,7 +1028,7 @@ impl ASTConverter {
py_ast::Expr::IfExp(if_) => {
let loc = if_.location();
let block = self.convert_expr(*if_.body);
let params = Params::new(vec![], None, vec![], None);
let params = Params::new(vec![], None, vec![], None, None);
let sig = LambdaSignature::new(params.clone(), None, TypeBoundSpecs::empty());
let body = Lambda::new(sig, Token::DUMMY, Block::new(vec![block]), DefId(0));
let test = self.convert_expr(*if_.test);
Expand Down Expand Up @@ -1074,7 +1080,7 @@ impl ASTConverter {
let rp = Token::new(TokenKind::RParen, ")", loc.row.get(), last_col);
(lp, rp)
};
let args = Args::new(pos_args, None, kw_args, Some(paren));
let args = Args::new(pos_args, None, kw_args, None, Some(paren));
function.call_expr(args)
}
py_ast::Expr::BinOp(bin) => {
Expand Down Expand Up @@ -1374,7 +1380,7 @@ impl ASTConverter {
VisModifierSpec::Public(DOT),
VarName::from_static("__call__"),
);
let params = Params::new(params, None, vec![], None);
let params = Params::new(params, None, vec![], None, None);
let class_ident = Identifier::public_with_line(
DOT,
self.namespace.last().unwrap().into(),
Expand Down Expand Up @@ -1402,7 +1408,7 @@ impl ASTConverter {
VisModifierSpec::Public(DOT),
VarName::from_static("__call__"),
);
let params = Params::new(vec![], None, vec![], None);
let params = Params::empty();
let class_ident =
Identifier::public_with_line(DOT, self.namespace.last().unwrap().into(), line as u32);
let class_ident_expr = Expr::Accessor(Accessor::Ident(class_ident.clone()));
Expand Down Expand Up @@ -1896,7 +1902,7 @@ impl ASTConverter {
py_ast::Stmt::While(while_) => {
let loc = while_.location();
let test = self.convert_expr(*while_.test);
let params = Params::new(vec![], None, vec![], None);
let params = Params::empty();
let empty_sig = LambdaSignature::new(params, None, TypeBoundSpecs::empty());
let block = self.convert_block(while_.body, BlockKind::While);
let body = Lambda::new(empty_sig, Token::DUMMY, block, DefId(0));
Expand All @@ -1907,7 +1913,7 @@ impl ASTConverter {
py_ast::Stmt::If(if_) => {
let loc = if_.location();
let block = self.convert_block(if_.body, BlockKind::If);
let params = Params::new(vec![], None, vec![], None);
let params = Params::empty();
let sig = LambdaSignature::new(params.clone(), None, TypeBoundSpecs::empty());
let body = Lambda::new(sig, Token::DUMMY, block, DefId(0));
let test = self.convert_expr(*if_.test);
Expand Down
Loading

0 comments on commit 84c72e6

Please sign in to comment.