mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-01 05:11:09 +00:00
Implement Record/TupleTypeSpec
This commit is contained in:
parent
9a88266bde
commit
b28c6bd118
5 changed files with 83 additions and 4 deletions
|
@ -1768,7 +1768,7 @@ pub enum TypeSpec {
|
|||
Set(SetTypeSpec),
|
||||
Tuple(Vec<TypeSpec>),
|
||||
Dict(Vec<(TypeSpec, TypeSpec)>),
|
||||
// Dict(),
|
||||
Record(Vec<(Identifier, TypeSpec)>),
|
||||
// Option(),
|
||||
And(Box<TypeSpec>, Box<TypeSpec>),
|
||||
Not(Box<TypeSpec>, Box<TypeSpec>),
|
||||
|
@ -1799,11 +1799,18 @@ impl fmt::Display for TypeSpec {
|
|||
Self::Tuple(tys) => write!(f, "({})", fmt_vec(tys)),
|
||||
Self::Dict(dict) => {
|
||||
write!(f, "{{")?;
|
||||
for (k, v) in dict {
|
||||
for (k, v) in dict.iter() {
|
||||
write!(f, "{k}: {v}, ")?;
|
||||
}
|
||||
write!(f, "}}")
|
||||
}
|
||||
Self::Record(rec) => {
|
||||
write!(f, "{{")?;
|
||||
for (k, v) in rec.iter() {
|
||||
write!(f, "{k} = {v}; ")?;
|
||||
}
|
||||
write!(f, "}}")
|
||||
}
|
||||
Self::Enum(elems) => write!(f, "{{{elems}}}"),
|
||||
Self::Interval { op, lhs, rhs } => write!(f, "{lhs}{}{rhs}", op.inspect()),
|
||||
Self::Subr(s) => write!(f, "{s}"),
|
||||
|
@ -1824,6 +1831,7 @@ impl Locational for TypeSpec {
|
|||
// TODO: ユニット
|
||||
Self::Tuple(tys) => Location::concat(tys.first().unwrap(), tys.last().unwrap()),
|
||||
Self::Dict(dict) => Location::concat(&dict.first().unwrap().0, &dict.last().unwrap().1),
|
||||
Self::Record(rec) => Location::concat(&rec.first().unwrap().0, &rec.last().unwrap().1),
|
||||
Self::Enum(set) => set.loc(),
|
||||
Self::Interval { lhs, rhs, .. } => Location::concat(lhs, rhs),
|
||||
Self::Subr(s) => s.loc(),
|
||||
|
|
|
@ -3046,6 +3046,38 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
fn record_to_record_type_spec(
|
||||
record: Record,
|
||||
) -> Result<Vec<(Identifier, TypeSpec)>, ParseError> {
|
||||
match record {
|
||||
Record::Normal(rec) => {
|
||||
let mut rec_spec = vec![];
|
||||
for mut def in rec.attrs.into_iter() {
|
||||
let ident = def.sig.ident().unwrap().clone();
|
||||
// TODO: check block.len() == 1
|
||||
let value = Self::expr_to_type_spec(def.body.block.pop().unwrap())?;
|
||||
rec_spec.push((ident, value));
|
||||
}
|
||||
Ok(rec_spec)
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn tuple_to_tuple_type_spec(tuple: Tuple) -> Result<Vec<TypeSpec>, ParseError> {
|
||||
match tuple {
|
||||
Tuple::Normal(tup) => {
|
||||
let mut tup_spec = vec![];
|
||||
let (elems, ..) = tup.elems.deconstruct();
|
||||
for elem in elems.into_iter() {
|
||||
let value = Self::expr_to_type_spec(elem.expr)?;
|
||||
tup_spec.push(value);
|
||||
}
|
||||
Ok(tup_spec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expr_to_type_spec(rhs: Expr) -> Result<TypeSpec, ParseError> {
|
||||
match rhs {
|
||||
Expr::Accessor(acc) => Self::accessor_to_type_spec(acc),
|
||||
|
@ -3069,6 +3101,14 @@ impl Parser {
|
|||
let dict = Self::dict_to_dict_type_spec(dict)?;
|
||||
Ok(TypeSpec::Dict(dict))
|
||||
}
|
||||
Expr::Record(rec) => {
|
||||
let rec = Self::record_to_record_type_spec(rec)?;
|
||||
Ok(TypeSpec::Record(rec))
|
||||
}
|
||||
Expr::Tuple(tup) => {
|
||||
let tup = Self::tuple_to_tuple_type_spec(tup)?;
|
||||
Ok(TypeSpec::Tuple(tup))
|
||||
}
|
||||
Expr::BinOp(bin) => {
|
||||
if bin.op.kind.is_range_op() {
|
||||
let op = bin.op;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue