From 1a4cb8db601e11cc4313772b9aa12fa32a09eef9 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Mon, 11 Sep 2023 22:19:41 +0900 Subject: [PATCH] Update convert.rs --- Cargo.lock | 32 +++++++------- crates/py2erg/convert.rs | 91 ++++++++++++++++++++++++---------------- 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c12606d..355ff92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,9 +155,9 @@ checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "els" -version = "0.1.33-nightly.1" +version = "0.1.33-nightly.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d90ce06b81836342feef59d9f1730340b39e32a9f488827c12919e3a9784cb2" +checksum = "4ccf778a0d7538e66cca834f20f5d8ed6ccf1220ff16ed0f222f6b66564d5996" dependencies = [ "erg_common", "erg_compiler", @@ -181,9 +181,9 @@ dependencies = [ [[package]] name = "erg_common" -version = "0.6.21-nightly.1" +version = "0.6.21-nightly.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9e3dc0e21777787ea3fdc174f29fd6cdc8d7a6d671fcf7ed583c5f2c614956" +checksum = "6b9c92d6525572e26c3f4d3ae527745615aa04dcbec0f7ed86b625262c9512f6" dependencies = [ "backtrace-on-stack-overflow", "parking_lot", @@ -192,9 +192,9 @@ dependencies = [ [[package]] name = "erg_compiler" -version = "0.6.21-nightly.1" +version = "0.6.21-nightly.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfff095a4ce56412940dcf8d43e6897f85242365a9529c0d7e0cb10029668090" +checksum = "df901b20dbfe677b04bfe1a57d50cd12217267ff39d97e222db768f42db6c23b" dependencies = [ "erg_common", "erg_parser", @@ -202,9 +202,9 @@ dependencies = [ [[package]] name = "erg_parser" -version = "0.6.21-nightly.1" +version = "0.6.21-nightly.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a76f132bafed4fd84fb90a1c8f3ecde6a238f462ab2e8ca18e9d19ac27fd07c" +checksum = "bb1fc83c4eaf03438a7c3be8a12de5c567509ec1ce02c9d202ea9ac81b671383" dependencies = [ "erg_common", "unicode-xid", @@ -428,9 +428,9 @@ dependencies = [ [[package]] name = "molc" -version = "0.1.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19b669aab31ca7552fc43cb9ab08e325113aa090f7bf97a2112b3d6241ba898" +checksum = "9485212d67a88d2169ee683420e1a2fe763afb96a3901ebfabb5ba095d0d2eaa" dependencies = [ "lsp-types", "serde", @@ -798,14 +798,14 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.31", + "syn 2.0.32", ] [[package]] name = "serde_json" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" +checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2" dependencies = [ "itoa", "ryu", @@ -820,7 +820,7 @@ checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.31", + "syn 2.0.32", ] [[package]] @@ -866,9 +866,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.31" +version = "2.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398" +checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" dependencies = [ "proc-macro2", "quote", diff --git a/crates/py2erg/convert.rs b/crates/py2erg/convert.rs index 2319e41..af87eb0 100644 --- a/crates/py2erg/convert.rs +++ b/crates/py2erg/convert.rs @@ -16,7 +16,7 @@ use erg_compiler::erg_parser::ast::{ PreDeclTypeSpec, ReDef, Record, RecordAttrs, Set, Signature, SubrSignature, SubrTypeSpec, Tuple, TupleTypeSpec, TypeAscription, TypeBoundSpecs, TypeSpec, TypeSpecWithOp, UnaryOp, VarName, VarPattern, VarRecordAttr, VarRecordAttrs, VarRecordPattern, VarSignature, - VisModifierSpec, + VisModifierSpec, ArrayComprehension, SetComprehension, }; use erg_compiler::erg_parser::desugar::Desugarer; use erg_compiler::erg_parser::token::{Token, TokenKind, COLON, DOT, EQUAL}; @@ -796,11 +796,7 @@ impl ASTConverter { let py_ast::Expr::Tuple(mut tuple) = args else { return Self::gen_dummy_type_spec(args.location()); }; - let (l_brace, r_brace) = Self::gen_enclosure_tokens( - TokenKind::LBrace, - tuple.elts.iter(), - tuple.location(), - ); + let (l_brace, r_brace) = Self::gen_enclosure_tokens(TokenKind::LBrace, tuple.range); let key_t = self.convert_expr(tuple.elts.remove(0)); let key_t = match Parser::validate_const_expr(key_t) { Ok(key_t) => key_t, @@ -844,11 +840,7 @@ impl ASTConverter { let py_ast::Expr::Tuple(tuple) = args else { return Self::gen_dummy_type_spec(args.location()); }; - let parens = Self::gen_enclosure_tokens( - TokenKind::LParen, - tuple.elts.iter(), - tuple.location(), - ); + let parens = Self::gen_enclosure_tokens(TokenKind::LParen, tuple.range); let tys = tuple .elts .into_iter() @@ -917,33 +909,24 @@ impl ASTConverter { } } - fn gen_enclosure_tokens<'i, Elems>( - l_kind: TokenKind, - elems: Elems, - expr_loc: PyLocation, - ) -> (Token, Token) - where - Elems: Iterator + ExactSizeIterator, - { + fn gen_enclosure_tokens(l_kind: TokenKind, expr_range: PySourceRange) -> (Token, Token) { let (l_cont, r_cont, r_kind) = match l_kind { TokenKind::LBrace => ("{", "}", TokenKind::RBrace), TokenKind::LParen => ("(", ")", TokenKind::RParen), TokenKind::LSqBr => ("[", "]", TokenKind::RSqBr), _ => unreachable!(), }; - let (l_end, c_end) = if elems.len() == 0 { - (expr_loc.row.get(), expr_loc.column.get() - 1) - } else { - let last = elems.last().unwrap(); - (last.location().row.get(), last.location().column.get()) - }; + let (line_end, c_end) = ( + expr_range.end.unwrap_or(expr_range.start).row.get(), + expr_range.end.unwrap_or(expr_range.start).column.to_zero_indexed(), + ); let l_brace = Token::new( l_kind, l_cont, - expr_loc.row.get(), - expr_loc.column.get() - 1, + expr_range.start.row.get(), + expr_range.start.column.to_zero_indexed(), ); - let r_brace = Token::new(r_kind, r_cont, l_end, c_end); + let r_brace = Token::new(r_kind, r_cont, line_end, c_end); (l_brace, r_brace) } @@ -1152,8 +1135,7 @@ impl ASTConverter { Expr::Lambda(Lambda::new(sig, op, Block::new(body), DefId(0))) } py_ast::Expr::List(list) => { - let (l_sqbr, r_sqbr) = - Self::gen_enclosure_tokens(TokenKind::LSqBr, list.elts.iter(), list.location()); + let (l_sqbr, r_sqbr) = Self::gen_enclosure_tokens(TokenKind::LSqBr, list.range); let elements = list .elts .into_iter() @@ -1163,9 +1145,28 @@ impl ASTConverter { let arr = Expr::Array(Array::Normal(NormalArray::new(l_sqbr, r_sqbr, elems))); Self::mutate_expr(arr) } + py_ast::Expr::ListComp(comp) => { + let (l_sqbr, r_sqbr) = Self::gen_enclosure_tokens(TokenKind::LSqBr, comp.range); + let layout = self.convert_expr(*comp.elt); + let generator = comp.generators.into_iter().next().unwrap(); + let target = self.convert_expr(generator.target); + let Expr::Accessor(Accessor::Ident(ident)) = target else { + log!(err "unimplemented: {target}"); + let loc = pyloc_to_ergloc(comp.range); + return Expr::Dummy(Dummy::new(Some(loc), vec![])); + }; + let iter = self.convert_expr(generator.iter); + let guard = generator + .ifs + .into_iter() + .next() + .map(|ex| self.convert_expr(ex)); + let generators = vec![(ident, iter)]; + let arr = Expr::Array(Array::Comprehension(ArrayComprehension::new(l_sqbr, r_sqbr, Some(layout), generators, guard))); + Self::mutate_expr(arr) + } py_ast::Expr::Set(set) => { - let (l_brace, r_brace) = - Self::gen_enclosure_tokens(TokenKind::LBrace, set.elts.iter(), set.location()); + let (l_brace, r_brace) = Self::gen_enclosure_tokens(TokenKind::LBrace, set.range); let elements = set .elts .into_iter() @@ -1175,12 +1176,28 @@ impl ASTConverter { Expr::Set(Set::Normal(NormalSet::new(l_brace, r_brace, elems))) // Self::mutate_expr(set) } + py_ast::Expr::SetComp(comp) => { + let (l_brace, r_brace) = Self::gen_enclosure_tokens(TokenKind::LBrace, comp.range); + let layout = self.convert_expr(*comp.elt); + let generator = comp.generators.into_iter().next().unwrap(); + let target = self.convert_expr(generator.target); + let Expr::Accessor(Accessor::Ident(ident)) = target else { + log!(err "unimplemented: {target}"); + let loc = pyloc_to_ergloc(comp.range); + return Expr::Dummy(Dummy::new(Some(loc), vec![])); + }; + let iter = self.convert_expr(generator.iter); + let guard = generator + .ifs + .into_iter() + .next() + .map(|ex| self.convert_expr(ex)); + let generators = vec![(ident, iter)]; + Expr::Set(Set::Comprehension(SetComprehension::new(l_brace, r_brace, Some(layout), generators, guard))) + // Self::mutate_expr(set) + } py_ast::Expr::Dict(dict) => { - let (l_brace, r_brace) = Self::gen_enclosure_tokens( - TokenKind::LBrace, - dict.values.iter(), - dict.location(), - ); + let (l_brace, r_brace) = Self::gen_enclosure_tokens(TokenKind::LBrace, dict.range); let kvs = dict .keys .into_iter()