mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-02 21:44:34 +00:00
Implement helper methods for AST/HIR construction
This commit is contained in:
parent
ae15f95191
commit
a0714b218c
8 changed files with 114 additions and 70 deletions
|
@ -3222,6 +3222,49 @@ impl Expr {
|
|||
pub fn static_local(name: &'static str) -> Self {
|
||||
Self::Accessor(Accessor::local(Token::static_symbol(name)))
|
||||
}
|
||||
|
||||
pub fn attr(self, ident: Identifier) -> Accessor {
|
||||
Accessor::attr(self, ident)
|
||||
}
|
||||
|
||||
pub fn attr_expr(self, ident: Identifier) -> Self {
|
||||
Self::Accessor(self.attr(ident))
|
||||
}
|
||||
|
||||
pub fn subscr(self, index: Expr, r_sqbr: Token) -> Accessor {
|
||||
Accessor::subscr(self, index, r_sqbr)
|
||||
}
|
||||
|
||||
pub fn subscr_expr(self, index: Expr, r_sqbr: Token) -> Self {
|
||||
Self::Accessor(self.subscr(index, r_sqbr))
|
||||
}
|
||||
|
||||
pub fn tuple_attr(self, index: Literal) -> Accessor {
|
||||
Accessor::tuple_attr(self, index)
|
||||
}
|
||||
|
||||
pub fn tuple_attr_expr(self, index: Literal) -> Self {
|
||||
Self::Accessor(self.tuple_attr(index))
|
||||
}
|
||||
|
||||
pub fn call(self, args: Args) -> Call {
|
||||
match self {
|
||||
Self::Accessor(Accessor::Attr(attr)) => Call::new(*attr.obj, Some(attr.ident), args),
|
||||
other => Call::new(other, None, args),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call_expr(self, args: Args) -> Self {
|
||||
Self::Call(self.call(args))
|
||||
}
|
||||
|
||||
pub fn type_asc(self, op: Token, t_spec: TypeSpec) -> TypeAscription {
|
||||
TypeAscription::new(self, op, t_spec)
|
||||
}
|
||||
|
||||
pub fn type_asc_expr(self, op: Token, t_spec: TypeSpec) -> Self {
|
||||
Self::TypeAsc(self.type_asc(op, t_spec))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
|
|
|
@ -15,8 +15,8 @@ use crate::ast::{
|
|||
DefBody, DefId, Dict, Expr, Identifier, KeyValue, KwArg, Lambda, LambdaSignature, Literal,
|
||||
Methods, Module, NonDefaultParamSignature, NormalArray, NormalDict, NormalRecord, NormalSet,
|
||||
NormalTuple, ParamPattern, Params, PosArg, Record, RecordAttrs, Set as astSet, SetWithLength,
|
||||
ShortenedRecord, Signature, SubrSignature, Tuple, TypeAscription, TypeBoundSpecs, TypeSpec,
|
||||
UnaryOp, VarName, VarPattern, VarRecordAttr, VarSignature,
|
||||
ShortenedRecord, Signature, SubrSignature, Tuple, TypeBoundSpecs, TypeSpec, UnaryOp, VarName,
|
||||
VarPattern, VarRecordAttr, VarSignature,
|
||||
};
|
||||
use crate::token::{Token, TokenKind};
|
||||
|
||||
|
@ -201,7 +201,7 @@ impl Desugarer {
|
|||
}
|
||||
Expr::TypeAsc(tasc) => {
|
||||
let expr = desugar(*tasc.expr);
|
||||
Expr::TypeAsc(TypeAscription::new(expr, tasc.op, tasc.t_spec))
|
||||
expr.type_asc_expr(tasc.op, tasc.t_spec)
|
||||
}
|
||||
Expr::Methods(method_defs) => {
|
||||
let mut new_defs = vec![];
|
||||
|
@ -323,7 +323,7 @@ impl Desugarer {
|
|||
vec![],
|
||||
None,
|
||||
);
|
||||
let call = Call::new(match_symbol, None, args);
|
||||
let call = match_symbol.call(args);
|
||||
(call, return_t_spec)
|
||||
}
|
||||
|
||||
|
@ -439,9 +439,7 @@ impl Desugarer {
|
|||
) {
|
||||
let obj = Expr::local(buf_name, sig.ln_begin().unwrap(), sig.col_begin().unwrap());
|
||||
let acc = match buf_index {
|
||||
BufIndex::Tuple(n) => {
|
||||
Accessor::tuple_attr(obj, Literal::nat(n, sig.ln_begin().unwrap()))
|
||||
}
|
||||
BufIndex::Tuple(n) => obj.tuple_attr(Literal::nat(n, sig.ln_begin().unwrap())),
|
||||
BufIndex::Array(n) => {
|
||||
let r_brace = Token::new(
|
||||
TokenKind::RBrace,
|
||||
|
@ -449,13 +447,9 @@ impl Desugarer {
|
|||
sig.ln_begin().unwrap(),
|
||||
sig.col_begin().unwrap(),
|
||||
);
|
||||
Accessor::subscr(
|
||||
obj,
|
||||
Expr::Lit(Literal::nat(n, sig.ln_begin().unwrap())),
|
||||
r_brace,
|
||||
)
|
||||
obj.subscr(Expr::Lit(Literal::nat(n, sig.ln_begin().unwrap())), r_brace)
|
||||
}
|
||||
BufIndex::Record(attr) => Accessor::attr(obj, attr.clone()),
|
||||
BufIndex::Record(attr) => obj.attr(attr.clone()),
|
||||
};
|
||||
let id = DefId(get_hash(&(&acc, buf_name)));
|
||||
let block = Block::new(vec![Expr::Accessor(acc)]);
|
||||
|
|
|
@ -885,7 +885,7 @@ impl Parser {
|
|||
Some(arg) if arg.is(Symbol) || arg.category_is(TC::Literal) => {
|
||||
let args = self.try_reduce_args(false).map_err(|_| self.stack_dec())?;
|
||||
let obj = enum_unwrap!(stack.pop(), Some:(ExprOrOp::Expr:(_)));
|
||||
stack.push(ExprOrOp::Expr(Expr::Call(Call::new(obj, None, args))));
|
||||
stack.push(ExprOrOp::Expr(obj.call_expr(args)));
|
||||
}
|
||||
Some(op) if op.category_is(TC::DefOp) => {
|
||||
let op = self.lpop();
|
||||
|
@ -934,7 +934,7 @@ impl Parser {
|
|||
.try_reduce_expr(false, false, false, false)
|
||||
.map_err(|_| self.stack_dec())?;
|
||||
let t_spec = Self::expr_to_type_spec(t_spec).map_err(|e| self.errs.push(e))?;
|
||||
let expr = Expr::TypeAsc(TypeAscription::new(lhs, op, t_spec));
|
||||
let expr = lhs.type_asc_expr(op, t_spec);
|
||||
stack.push(ExprOrOp::Expr(expr));
|
||||
}
|
||||
Some(op) if op.category_is(TC::BinOp) => {
|
||||
|
@ -985,8 +985,7 @@ impl Parser {
|
|||
stack.push(ExprOrOp::Expr(Expr::Call(call)));
|
||||
} else {
|
||||
let ident = Identifier::new(None, VarName::new(symbol));
|
||||
let acc = Accessor::attr(obj, ident);
|
||||
stack.push(ExprOrOp::Expr(Expr::Accessor(acc)));
|
||||
stack.push(ExprOrOp::Expr(obj.attr_expr(ident)));
|
||||
}
|
||||
}
|
||||
line_break if line_break.is(Newline) => {
|
||||
|
@ -1049,13 +1048,12 @@ impl Parser {
|
|||
let mut call = Expr::Call(Call::new(obj, Some(ident), args));
|
||||
while let Some(res) = self.opt_reduce_args(false) {
|
||||
let args = res.map_err(|_| self.stack_dec())?;
|
||||
call = Expr::Call(Call::new(call, None, args));
|
||||
call = call.call_expr(args);
|
||||
}
|
||||
stack.push(ExprOrOp::Expr(call));
|
||||
} else {
|
||||
let ident = Identifier::new(Some(vis), VarName::new(symbol));
|
||||
let acc = Accessor::attr(obj, ident);
|
||||
stack.push(ExprOrOp::Expr(Expr::Accessor(acc)));
|
||||
stack.push(ExprOrOp::Expr(obj.attr_expr(ident)));
|
||||
}
|
||||
}
|
||||
line_break if line_break.is(Newline) => {
|
||||
|
@ -1201,7 +1199,7 @@ impl Parser {
|
|||
.try_reduce_expr(false, in_type_args, in_brace, false)
|
||||
.map_err(|_| self.stack_dec())?;
|
||||
let t_spec = Self::expr_to_type_spec(t_spec).map_err(|e| self.errs.push(e))?;
|
||||
let expr = Expr::TypeAsc(TypeAscription::new(lhs, op, t_spec));
|
||||
let expr = lhs.type_asc_expr(op, t_spec);
|
||||
stack.push(ExprOrOp::Expr(expr));
|
||||
}
|
||||
Some(op) if op.category_is(TC::BinOp) => {
|
||||
|
@ -1252,8 +1250,7 @@ impl Parser {
|
|||
stack.push(ExprOrOp::Expr(Expr::Call(call)));
|
||||
} else {
|
||||
let ident = Identifier::new(Some(vis), VarName::new(symbol));
|
||||
let acc = Accessor::attr(obj, ident);
|
||||
stack.push(ExprOrOp::Expr(Expr::Accessor(acc)));
|
||||
stack.push(ExprOrOp::Expr(obj.attr_expr(ident)));
|
||||
}
|
||||
}
|
||||
other => {
|
||||
|
@ -1353,10 +1350,10 @@ impl Parser {
|
|||
Signature::Var(var) => {
|
||||
let mut last = def.body.block.pop().unwrap();
|
||||
for deco in decos.into_iter() {
|
||||
last = Expr::Call(Call::new(
|
||||
deco.into_expr(),
|
||||
last = deco.into_expr().call_expr(Args::new(
|
||||
vec![PosArg::new(last)],
|
||||
vec![],
|
||||
None,
|
||||
Args::new(vec![PosArg::new(last)], vec![], None),
|
||||
));
|
||||
}
|
||||
def.body.block.push(last);
|
||||
|
@ -1501,11 +1498,11 @@ impl Parser {
|
|||
match token.kind {
|
||||
Symbol => {
|
||||
let ident = Identifier::new(Some(vis), VarName::new(token));
|
||||
obj = Expr::Accessor(Accessor::attr(obj, ident));
|
||||
obj = obj.attr_expr(ident);
|
||||
}
|
||||
NatLit => {
|
||||
let index = Literal::from(token);
|
||||
obj = Expr::Accessor(Accessor::tuple_attr(obj, index));
|
||||
obj = obj.tuple_attr_expr(index);
|
||||
}
|
||||
Newline => {
|
||||
self.restore(token);
|
||||
|
@ -1527,8 +1524,7 @@ impl Parser {
|
|||
token.content = Str::rc(&token.content[1..]);
|
||||
token.kind = NatLit;
|
||||
token.col_begin += 1;
|
||||
let index = Literal::from(token);
|
||||
obj = Expr::Accessor(Accessor::tuple_attr(obj, index));
|
||||
obj = obj.tuple_attr_expr(Literal::from(token));
|
||||
}
|
||||
Some(t) if t.is(DblColon) && obj.col_end() == t.col_begin() => {
|
||||
let vis = self.lpop();
|
||||
|
@ -1536,7 +1532,7 @@ impl Parser {
|
|||
match token.kind {
|
||||
Symbol => {
|
||||
let ident = Identifier::new(None, VarName::new(token));
|
||||
obj = Expr::Accessor(Accessor::attr(obj, ident));
|
||||
obj = obj.attr_expr(ident);
|
||||
}
|
||||
LBrace => {
|
||||
self.restore(token);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue