mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-03 10:23:20 +00:00
chore: impl TryFrom<Expr> for $Variant
This commit is contained in:
parent
f039643a30
commit
0c89ca99cd
12 changed files with 59 additions and 35 deletions
|
@ -2967,7 +2967,7 @@ impl PyCodeGenerator {
|
|||
log!(info "entered {} ({chunk})", fn_name!());
|
||||
self.push_lnotab(&chunk);
|
||||
match chunk {
|
||||
Expr::Lit(lit) => self.emit_load_const(lit.value),
|
||||
Expr::Literal(lit) => self.emit_load_const(lit.value),
|
||||
Expr::Accessor(acc) => self.emit_acc(acc),
|
||||
Expr::Def(def) => self.emit_def(def),
|
||||
Expr::ClassDef(class) => self.emit_class_def(class),
|
||||
|
@ -3043,7 +3043,7 @@ impl PyCodeGenerator {
|
|||
}
|
||||
}
|
||||
match expr {
|
||||
Expr::Lit(lit) => self.emit_load_const(lit.value),
|
||||
Expr::Literal(lit) => self.emit_load_const(lit.value),
|
||||
Expr::Accessor(acc) => self.emit_acc(acc),
|
||||
Expr::Def(def) => self.emit_def(def),
|
||||
Expr::ClassDef(class) => self.emit_class_def(class),
|
||||
|
@ -3248,7 +3248,7 @@ impl PyCodeGenerator {
|
|||
None => {}
|
||||
}
|
||||
let none = Token::new_fake(TokenKind::NoneLit, "None", line, 0, 0);
|
||||
attrs.push(Expr::Lit(Literal::new(ValueObj::None, none)));
|
||||
attrs.push(Expr::Literal(Literal::new(ValueObj::None, none)));
|
||||
let block = Block::new(attrs);
|
||||
let body = DefBody::new(EQUAL, block, DefId(0));
|
||||
self.emit_subr_def(Some(class_name), subr_sig, body);
|
||||
|
|
|
@ -1094,7 +1094,7 @@ impl Context {
|
|||
|
||||
fn resolve_expr_t(&self, expr: &mut hir::Expr, qnames: &Set<Str>) -> TyCheckResult<()> {
|
||||
match expr {
|
||||
hir::Expr::Lit(_) => Ok(()),
|
||||
hir::Expr::Literal(_) => Ok(()),
|
||||
hir::Expr::Accessor(acc) => {
|
||||
if acc
|
||||
.ref_t()
|
||||
|
|
|
@ -501,7 +501,7 @@ impl ASTLowerer {
|
|||
|
||||
pub(crate) fn fake_lower_expr(&self, expr: ast::Expr) -> LowerResult<hir::Expr> {
|
||||
match expr {
|
||||
ast::Expr::Literal(lit) => Ok(hir::Expr::Lit(self.lower_literal(lit)?)),
|
||||
ast::Expr::Literal(lit) => Ok(hir::Expr::Literal(self.lower_literal(lit)?)),
|
||||
ast::Expr::BinOp(binop) => Ok(hir::Expr::BinOp(self.fake_lower_binop(binop)?)),
|
||||
ast::Expr::UnaryOp(unop) => Ok(hir::Expr::UnaryOp(self.fake_lower_unaryop(unop)?)),
|
||||
ast::Expr::Array(arr) => Ok(hir::Expr::Array(self.fake_lower_array(arr)?)),
|
||||
|
@ -855,7 +855,7 @@ impl ASTLowerer {
|
|||
log!(info "entered {}", fn_name!());
|
||||
match expr {
|
||||
ast::Expr::Literal(lit) if lit.is_doc_comment() => {
|
||||
Ok(hir::Expr::Lit(self.lower_literal(lit)?))
|
||||
Ok(hir::Expr::Literal(self.lower_literal(lit)?))
|
||||
}
|
||||
ast::Expr::Accessor(acc) if allow_acc => Ok(hir::Expr::Accessor(self.lower_acc(acc)?)),
|
||||
ast::Expr::Def(def) => Ok(hir::Expr::Def(self.declare_def(def)?)),
|
||||
|
|
|
@ -121,7 +121,7 @@ impl SideEffectChecker {
|
|||
Expr::UnaryOp(unary) => {
|
||||
self.check_expr(&unary.expr);
|
||||
}
|
||||
Expr::Accessor(_) | Expr::Lit(_) => {}
|
||||
Expr::Accessor(_) | Expr::Literal(_) => {}
|
||||
Expr::Array(array) => match array {
|
||||
Array::Normal(arr) => {
|
||||
for elem in arr.elems.pos_args.iter() {
|
||||
|
@ -314,7 +314,7 @@ impl SideEffectChecker {
|
|||
/// ```
|
||||
fn check_expr(&mut self, expr: &Expr) {
|
||||
match expr {
|
||||
Expr::Lit(_) => {}
|
||||
Expr::Literal(_) => {}
|
||||
Expr::Def(def) => {
|
||||
self.check_def(def);
|
||||
}
|
||||
|
|
|
@ -7,12 +7,12 @@ use erg_common::error::Location;
|
|||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::traits::{Locational, NestedDisplay, NoTypeDisplay, Stream};
|
||||
use erg_common::Str;
|
||||
use erg_common::{
|
||||
enum_unwrap, fmt_option, fmt_vec, impl_display_for_enum, impl_display_from_nested,
|
||||
impl_locational, impl_locational_for_enum, impl_nested_display_for_chunk_enum,
|
||||
impl_nested_display_for_enum, impl_no_type_display_for_enum, impl_stream,
|
||||
};
|
||||
use erg_common::{impl_from_trait_for_enum, impl_try_from_trait_for_enum, Str};
|
||||
|
||||
use erg_parser::ast::{self, AscriptionKind};
|
||||
use erg_parser::ast::{
|
||||
|
@ -2491,7 +2491,7 @@ impl TypeAscription {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Expr {
|
||||
Lit(Literal),
|
||||
Literal(Literal),
|
||||
Accessor(Accessor),
|
||||
Array(Array),
|
||||
Tuple(Tuple),
|
||||
|
@ -2513,11 +2513,13 @@ pub enum Expr {
|
|||
Dummy(Dummy), // for mapping to Python AST
|
||||
}
|
||||
|
||||
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_no_type_display_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_nested_display_for_chunk_enum!(Expr; Literal, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_no_type_display_for_enum!(Expr; Literal, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_display_from_nested!(Expr);
|
||||
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_t_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_locational_for_enum!(Expr; Literal, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_t_for_enum!(Expr; Literal, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Code, Compound, TypeAsc, Set, Import, Dummy);
|
||||
impl_from_trait_for_enum!(Expr; Literal, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Set, Dummy);
|
||||
impl_try_from_trait_for_enum!(Expr; Literal, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, PatchDef, ReDef, Set, Dummy);
|
||||
|
||||
impl Default for Expr {
|
||||
fn default() -> Self {
|
||||
|
@ -2577,14 +2579,14 @@ impl Expr {
|
|||
|
||||
pub fn is_doc_comment(&self) -> bool {
|
||||
match self {
|
||||
Expr::Lit(lit) => lit.is_doc_comment(),
|
||||
Expr::Literal(lit) => lit.is_doc_comment(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn name(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Lit(_) => "literal",
|
||||
Self::Literal(_) => "literal",
|
||||
Self::Accessor(_) => "accessor",
|
||||
Self::Array(_) => "array",
|
||||
Self::Tuple(_) => "tuple",
|
||||
|
|
|
@ -112,7 +112,7 @@ impl<'a> HIRLinker<'a> {
|
|||
/// ```
|
||||
fn resolve_pymod_path(expr: &mut Expr) {
|
||||
match expr {
|
||||
Expr::Lit(_) => {}
|
||||
Expr::Literal(_) => {}
|
||||
Expr::Accessor(acc) => {
|
||||
if let Accessor::Attr(attr) = acc {
|
||||
Self::resolve_pymod_path(&mut attr.obj);
|
||||
|
@ -223,7 +223,7 @@ impl<'a> HIRLinker<'a> {
|
|||
|
||||
fn replace_import(&self, expr: &mut Expr) {
|
||||
match expr {
|
||||
Expr::Lit(_) => {}
|
||||
Expr::Literal(_) => {}
|
||||
Expr::Accessor(acc) => {
|
||||
/*if acc.ref_t().is_py_module() {
|
||||
let import = Expr::Import(acc.clone());
|
||||
|
@ -450,7 +450,7 @@ impl<'a> HIRLinker<'a> {
|
|||
/// ```
|
||||
fn replace_py_import(&self, expr: &mut Expr) {
|
||||
let args = &mut enum_unwrap!(expr, Expr::Call).args;
|
||||
let mod_name_lit = enum_unwrap!(args.remove_left_or_key("Path").unwrap(), Expr::Lit);
|
||||
let mod_name_lit = enum_unwrap!(args.remove_left_or_key("Path").unwrap(), Expr::Literal);
|
||||
let mod_name_str = enum_unwrap!(mod_name_lit.value.clone(), ValueObj::Str);
|
||||
let mut dir = self.cfg.input.dir();
|
||||
let mod_path = self
|
||||
|
@ -482,7 +482,7 @@ impl<'a> HIRLinker<'a> {
|
|||
mod_name_lit.col_begin().unwrap(),
|
||||
mod_name_lit.col_end().unwrap(),
|
||||
);
|
||||
let mod_name = Expr::Lit(Literal::try_from(token).unwrap());
|
||||
let mod_name = Expr::Literal(Literal::try_from(token).unwrap());
|
||||
args.insert_pos(0, PosArg::new(mod_name));
|
||||
let line = expr.ln_begin().unwrap_or(0);
|
||||
for attr in comps {
|
||||
|
|
|
@ -203,7 +203,7 @@ impl ASTLowerer {
|
|||
|
||||
fn check_doc_comment(&mut self, chunk: &Expr) {
|
||||
match chunk {
|
||||
Expr::Lit(lit) if lit.is_doc_comment() => {
|
||||
Expr::Literal(lit) if lit.is_doc_comment() => {
|
||||
let first_line = lit.ln_begin().unwrap_or(1);
|
||||
let ValueObj::Str(content) = &lit.value else {
|
||||
return;
|
||||
|
|
|
@ -1737,7 +1737,7 @@ impl ASTLowerer {
|
|||
},
|
||||
ast::ClassAttr::Doc(doc) => match self.lower_literal(doc) {
|
||||
Ok(doc) => {
|
||||
hir_methods.push(hir::Expr::Lit(doc));
|
||||
hir_methods.push(hir::Expr::Literal(doc));
|
||||
}
|
||||
Err(errs) => {
|
||||
self.errs.extend(errs);
|
||||
|
@ -1944,7 +1944,7 @@ impl ASTLowerer {
|
|||
},
|
||||
ast::ClassAttr::Doc(doc) => match self.lower_literal(doc) {
|
||||
Ok(doc) => {
|
||||
hir_methods.push(hir::Expr::Lit(doc));
|
||||
hir_methods.push(hir::Expr::Literal(doc));
|
||||
}
|
||||
Err(errs) => {
|
||||
self.errs.extend(errs);
|
||||
|
@ -2440,7 +2440,7 @@ impl ASTLowerer {
|
|||
log!(info "entered {}", fn_name!());
|
||||
let casted = self.module.context.get_casted_type(&expr);
|
||||
let mut expr = match expr {
|
||||
ast::Expr::Literal(lit) => hir::Expr::Lit(self.lower_literal(lit)?),
|
||||
ast::Expr::Literal(lit) => hir::Expr::Literal(self.lower_literal(lit)?),
|
||||
ast::Expr::Array(arr) => hir::Expr::Array(self.lower_array(arr)?),
|
||||
ast::Expr::Tuple(tup) => hir::Expr::Tuple(self.lower_tuple(tup)?),
|
||||
ast::Expr::Record(rec) => hir::Expr::Record(self.lower_record(rec)?),
|
||||
|
|
|
@ -452,7 +452,7 @@ impl PyScriptGenerator {
|
|||
|
||||
fn transpile_expr(&mut self, expr: Expr) -> String {
|
||||
match expr {
|
||||
Expr::Lit(lit) => self.transpile_lit(lit),
|
||||
Expr::Literal(lit) => self.transpile_lit(lit),
|
||||
Expr::Call(call) => self.transpile_call(call),
|
||||
Expr::BinOp(bin) => self.transpile_binop(bin),
|
||||
Expr::UnaryOp(unary) => self.transpile_unaryop(unary),
|
||||
|
@ -1152,7 +1152,7 @@ impl JsonGenerator {
|
|||
}
|
||||
Some(ValueObj::Record(attrs))
|
||||
}
|
||||
Expr::Lit(lit) => Some(lit.value),
|
||||
Expr::Literal(lit) => Some(lit.value),
|
||||
Expr::Accessor(acc) => self.binds.get(&acc.var_info().def_loc).cloned(),
|
||||
Expr::BinOp(bin) => {
|
||||
let lhs = self.expr_into_value(*bin.lhs)?;
|
||||
|
@ -1187,7 +1187,7 @@ impl JsonGenerator {
|
|||
|
||||
fn transpile_expr(&mut self, expr: Expr) -> String {
|
||||
match expr {
|
||||
Expr::Lit(lit) => lit.token.content.to_string(),
|
||||
Expr::Literal(lit) => lit.token.content.to_string(),
|
||||
Expr::Accessor(acc) => {
|
||||
if let Some(val) = self.binds.get(&acc.var_info().def_loc) {
|
||||
val.to_string()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue