implement dynamic key; implement __curPos; other small changes

This commit is contained in:
2026-05-02 21:23:39 +08:00
parent 035ebd3808
commit 1550868e90
13 changed files with 169 additions and 141 deletions
+70 -6
View File
@@ -98,7 +98,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
AttrSet(attrs) => attrs.downgrade(ctx),
UnaryOp(op) => op.downgrade(ctx),
Ident(ident) => ident.downgrade(ctx),
CurPos(curpos) => Ok(ctx.new_expr(Ir::CurPos(curpos.syntax().text_range()))),
CurPos(curpos) => curpos.downgrade(ctx),
With(with) => with.downgrade(ctx),
HasAttr(has) => has.downgrade(ctx),
Paren(paren) => paren
@@ -234,6 +234,62 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
}
}
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::CurPos {
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
fn byte_offset_to_line_col(content: &str, offset: usize) -> (u32, u32) {
let mut line = 1u32;
let mut col = 1u32;
for (idx, ch) in content.char_indices() {
if idx >= offset {
break;
}
if ch == '\n' {
line += 1;
col = 1;
} else {
col += 1;
}
}
(line, col)
}
let span = self.syntax().text_range();
let source = ctx.get_current_source();
let (line, column) = byte_offset_to_line_col(&source.src, span.start().into());
let file_sym = ctx.intern_string("file");
let line_sym = ctx.intern_string("line");
let column_sym = ctx.intern_string("column");
let file: GhostRoMaybeThunkRef = ctx
.bump()
.alloc(GhostCell::new(MaybeThunk::Str(ctx.intern_string(source.get_name()))).into());
let line = ctx
.bump()
.alloc(GhostCell::new(MaybeThunk::Int(i64::from(line))).into());
let column = ctx
.bump()
.alloc(GhostCell::new(MaybeThunk::Int(i64::from(column))).into());
let map = {
let mut map = HashMap::new_in(ctx.bump());
map.insert(file_sym, (file, TextRange::default()));
map.insert(line_sym, (line, TextRange::default()));
map.insert(column_sym, (column, TextRange::default()));
map
};
Ok(ctx.new_expr(Ir::AttrSet {
stcs: map,
dyns: Vec::new_in(ctx.bump()),
}))
}
}
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::AttrSet {
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
let rec = self.rec_token().is_some();
@@ -266,8 +322,8 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::BinOp {
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
use ast::BinOpKind as Kind;
use BinOpKind::*;
use ast::BinOpKind as Kind;
let span = self.syntax().text_range();
let lhs = self.lhs().require(ctx, span)?.downgrade(ctx)?;
@@ -290,12 +346,20 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
Kind::Or => Or,
Kind::PipeLeft => {
let arg = ctx.maybe_thunk(rhs);
return Ok(ctx.new_expr(Ir::Call { func: lhs, arg, span }))
},
return Ok(ctx.new_expr(Ir::Call {
func: lhs,
arg,
span,
}));
}
Kind::PipeRight => {
let arg = ctx.maybe_thunk(lhs);
return Ok(ctx.new_expr(Ir::Call { func: rhs, arg, span }))
},
return Ok(ctx.new_expr(Ir::Call {
func: rhs,
arg,
span,
}));
}
};
Ok(ctx.new_expr(Ir::BinOp { lhs, rhs, kind }))
}
-1
View File
@@ -215,7 +215,6 @@ pub enum Ir<'ir, R: RefExt<'ir> + ?Sized + 'ir> {
thunks: Vec<'ir, (ThunkId, R::IrRef)>,
},
MaybeThunk(R::MaybeThunkRef),
CurPos(TextRange),
ReplBinding(StringId),
ScopedImportBinding(StringId),
}