Refactor: AttrDef -> ReDef

This commit is contained in:
Shunsuke Shibayama 2022-12-27 14:07:39 +09:00
parent 24627eb26c
commit a249de98b3
10 changed files with 83 additions and 68 deletions

View file

@ -3537,12 +3537,12 @@ impl Def {
/// This is not necessary for Erg syntax, but necessary for mapping ASTs in Python
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttrDef {
pub struct ReDef {
pub attr: Accessor,
pub expr: Box<Expr>,
}
impl NestedDisplay for AttrDef {
impl NestedDisplay for ReDef {
fn fmt_nest(&self, f: &mut fmt::Formatter<'_>, level: usize) -> fmt::Result {
self.attr.fmt_nest(f, level)?;
writeln!(f, " = ")?;
@ -3550,10 +3550,10 @@ impl NestedDisplay for AttrDef {
}
}
impl_display_from_nested!(AttrDef);
impl_locational!(AttrDef, attr, expr);
impl_display_from_nested!(ReDef);
impl_locational!(ReDef, attr, expr);
impl AttrDef {
impl ReDef {
pub fn new(attr: Accessor, expr: Expr) -> Self {
Self {
attr,
@ -3672,14 +3672,14 @@ pub enum Expr {
Methods(Methods),
ClassDef(ClassDef),
PatchDef(PatchDef),
AttrDef(AttrDef),
ReDef(ReDef),
/// for mapping to Python AST
Dummy(Dummy),
}
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Set, Record, BinOp, UnaryOp, Call, DataPack, Lambda, TypeAsc, Def, Methods, ClassDef, PatchDef, AttrDef, Dummy);
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Set, Record, BinOp, UnaryOp, Call, DataPack, Lambda, TypeAsc, Def, Methods, ClassDef, PatchDef, ReDef, Dummy);
impl_display_from_nested!(Expr);
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Set, Record, BinOp, UnaryOp, Call, DataPack, Lambda, TypeAsc, Def, Methods, ClassDef, PatchDef, AttrDef, Dummy);
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Set, Record, BinOp, UnaryOp, Call, DataPack, Lambda, TypeAsc, Def, Methods, ClassDef, PatchDef, ReDef, Dummy);
impl Expr {
pub fn is_match_call(&self) -> bool {

View file

@ -9,14 +9,14 @@ use erg_common::Str;
use erg_common::{enum_unwrap, get_hash, log, set};
use crate::ast::{
Accessor, Args, Array, ArrayComprehension, ArrayTypeSpec, ArrayWithLength, AttrDef, BinOp,
Block, Call, ClassAttr, ClassAttrs, ClassDef, ConstExpr, DataPack, Def, DefBody, DefId, Dict,
Dummy, Expr, Identifier, KeyValue, KwArg, Lambda, LambdaSignature, Literal, Methods,
MixedRecord, Module, NonDefaultParamSignature, NormalArray, NormalDict, NormalRecord,
NormalSet, NormalTuple, ParamPattern, ParamRecordAttr, Params, PatchDef, PosArg, Record,
RecordAttrOrIdent, RecordAttrs, Set as astSet, SetWithLength, Signature, SubrSignature, Tuple,
TupleTypeSpec, TypeAppArgs, TypeBoundSpecs, TypeSpec, TypeSpecWithOp, UnaryOp, VarName,
VarPattern, VarRecordAttr, VarSignature,
Accessor, Args, Array, ArrayComprehension, ArrayTypeSpec, ArrayWithLength, BinOp, Block, Call,
ClassAttr, ClassAttrs, ClassDef, ConstExpr, DataPack, Def, DefBody, DefId, Dict, Dummy, Expr,
Identifier, KeyValue, KwArg, Lambda, LambdaSignature, Literal, Methods, MixedRecord, Module,
NonDefaultParamSignature, NormalArray, NormalDict, NormalRecord, NormalSet, NormalTuple,
ParamPattern, ParamRecordAttr, Params, PatchDef, PosArg, ReDef, Record, RecordAttrOrIdent,
RecordAttrs, Set as astSet, SetWithLength, Signature, SubrSignature, Tuple, TupleTypeSpec,
TypeAppArgs, TypeBoundSpecs, TypeSpec, TypeSpecWithOp, UnaryOp, VarName, VarPattern,
VarRecordAttr, VarSignature,
};
use crate::token::{Token, TokenKind, COLON, DOT};
@ -254,10 +254,10 @@ impl Desugarer {
.collect();
Expr::PatchDef(PatchDef::new(def, methods))
}
Expr::AttrDef(adef) => {
let expr = desugar(*adef.expr);
let attr = Self::perform_desugar_acc(desugar, adef.attr);
Expr::AttrDef(AttrDef::new(attr, expr))
Expr::ReDef(redef) => {
let expr = desugar(*redef.expr);
let attr = Self::perform_desugar_acc(desugar, redef.attr);
Expr::ReDef(ReDef::new(attr, expr))
}
Expr::Lambda(lambda) => {
let mut chunks = vec![];