use super::*;
impl Compiler<'_> {
pub(super) fn plain(
&mut self,
parts: &[StrPart],
raw: &str,
id_site: (Option<&StableId>, Pos),
pos: Pos,
out: &mut Vec<StmtId>,
) {
let (id, id_at) = id_site;
let Some(binding) = self.registry.plain_binding() else {
self.error(pos, DiagCode::UnboundSugar, vec![("kind", s("plain"))]);
return;
};
self.note_id_site(StopKind::Plain, id, id_at);
self.require_id(id, "plain", pos);
let text = self.text_parts(parts, pos.line, true);
let Binding::Command(cid) = binding else {
return;
};
let name = self.registry.command(cid).name.clone();
let body = plain_body(parts, raw);
let hash = fnv1a(&[name.as_bytes(), body.as_bytes()]);
let args = vec![StopArg {
expr: text,
text: true,
}];
let stop = self.make_stop(&name, args, id, hash, pos, false);
self.mark_text_stop(stop, StopKind::Plain, parts);
self.push_stmt(out, StmtKind::Eval(stop), pos.line);
}
pub(super) fn usable_identities(&self) -> Vec<String> {
let mut names: Vec<String> = self.md().identities.keys().cloned().collect();
for &i in self.md().imports.values() {
names.extend(self.modules[i].identities.keys().cloned());
}
names.sort();
names.dedup();
names
}
pub(super) fn warn_identity_typo(&mut self, name: &str, pos: Pos) {
let names = self.usable_identities();
let found = suggest(name, names.iter().map(String::as_str));
if !found.is_empty() {
self.warn(pos, DiagCode::IdentityTypo, name_args(name, found));
}
}
pub(super) fn resolve_identity(&mut self, r: &IdentRef) -> Option<Identity> {
self.resolve_identity_with(r, false)
}
pub(super) fn resolve_identity_const(&mut self, r: &IdentRef) -> Option<Identity> {
self.resolve_identity_with(r, true)
}
pub(super) fn resolve_identity_with(&mut self, r: &IdentRef, early: bool) -> Option<Identity> {
let make = |display: &String| Identity {
id: r.name.clone(),
name: display.clone(),
};
let lookup = |this: &Self, midx: usize, name: &str| -> Option<String> {
let m = &this.modules[midx];
if early {
m.early_identities.get(name).cloned()
} else {
m.identities.get(name).cloned()
}
};
if let Some((m, mpos)) = &r.module {
let midx = self.resolve_module(m, *mpos)?;
return lookup(self, midx, &r.name).as_ref().map(make);
}
if let Some(display) = lookup(self, self.cur_mod, &r.name) {
return Some(make(&display));
}
let mut found: Vec<(String, String)> = self
.md()
.imports
.iter()
.filter_map(|(m, &i)| lookup(self, i, &r.name).map(|d| (m.clone(), d)))
.collect();
found.sort();
for (m, _) in &found {
self.mark_import(m);
}
match found.len() {
0 => None,
1 => Some(make(&found[0].1)),
_ => {
let candidates = found
.iter()
.map(|(m, _)| format!("{m}.{}", r.name))
.collect();
self.error(
r.pos,
DiagCode::AmbiguousIdentity,
vec![
("name", s(&r.name)),
("candidates", DiagArg::List(candidates)),
],
);
None
}
}
}
pub(super) fn quote(
&mut self,
identity: Option<&IdentRef>,
parts: &[StrPart],
id_site: (Option<&StableId>, Pos),
pos: Pos,
out: &mut Vec<StmtId>,
) {
let (id, id_at) = id_site;
let resolved = identity.and_then(|r| self.resolve_identity(r));
let Some(binding) = self.registry.quote_binding() else {
self.error(pos, DiagCode::UnboundSugar, vec![("kind", s("quote"))]);
return;
};
self.note_id_site(StopKind::Quote, id, id_at);
self.require_id(id, "quote", pos);
let ident_value = Value::Option(
resolved
.clone()
.map(|i| Box::new(Value::Identity(i.into()))),
);
let who_arg = self.node(ExprKind::Const(ident_value), pos.line);
let text = self.text_parts(parts, pos.line, true);
let Binding::Command(cid) = binding else {
return;
};
let name = self.registry.command(cid).name.clone();
let who = resolved.as_ref().map_or("", |i| i.id.as_str());
let body = quoted_body(parts);
let hash = fnv1a(&[name.as_bytes(), who.as_bytes(), body.as_bytes()]);
let args = vec![
StopArg {
expr: who_arg,
text: false,
},
StopArg {
expr: text,
text: true,
},
];
let stop = self.make_stop(&name, args, id, hash, pos, false);
self.mark_text_stop(stop, StopKind::Quote, parts);
self.push_stmt(out, StmtKind::Eval(stop), pos.line);
}
pub(super) fn choice(&mut self, options: &[ChoiceOpt], pos: Pos, out: &mut Vec<StmtId>) {
let Some(mut st) = self.choice_prepare(options, pos) else {
return;
};
for (o, ir) in options.iter().zip(&mut st.options) {
ir.body = self.block(&o.body);
}
self.choice_finish(st, pos, out);
}
#[inline(never)]
pub(super) fn choice_prepare(
&mut self,
options: &[ChoiceOpt],
pos: Pos,
) -> Option<ChoiceState> {
let Some(binding) = self.registry.choice_binding() else {
self.error(pos, DiagCode::UnboundSugar, vec![("kind", s("choice"))]);
return None;
};
let refs_before = {
let r = &self.ctx().refs;
(r.stop_ids.len(), r.targets.len())
};
let mut infos = Vec::new();
let mut opts = Vec::new();
for o in options {
let line = o.pos.line;
if let Some(op) = o.once
&& o.id.is_none()
{
self.error(op, DiagCode::OnceWithoutId, vec![]);
}
if !matches!(binding, Binding::Ignored)
&& let Some(sid) = &o.id
&& !self.ctx().ids.insert(sid.id.clone())
{
self.error(
sid.pos,
DiagCode::DuplicateStableId,
vec![("id", s(&sid.id))],
);
}
self.require_id(o.id.as_ref(), "choice", o.pos);
self.note_id_site(StopKind::Choice, o.id.as_ref(), o.id_at);
let once = o.once.and(o.id.as_ref()).map(|i| i.id.clone());
let cond = o.cond.as_ref().map(|c| self.condition(c));
let text = self.text_parts(&o.parts, line, true);
let id = Value::Option(
o.id.as_ref()
.map(|i| Box::new(Value::Str(i.id.clone().into()))),
);
opts.push(IrChoiceOpt {
once,
cond,
text,
id,
body: Vec::new(),
line,
});
infos.push(ChoiceOptInfo {
id: o.id.as_ref().map(|i| i.id.clone()),
text: text_parts_of(&o.parts),
line,
});
}
let name = match binding {
Binding::Command(cid) => self.registry.command(cid).name.clone(),
Binding::Ignored => String::new(),
};
let cmd = if name.is_empty() {
0
} else {
self.command_index(&name)
};
let raws: Vec<String> = options.iter().map(|o| quoted_body(&o.parts)).collect();
let mut parts: Vec<&[u8]> = vec![name.as_bytes()];
parts.extend(raws.iter().map(|r| r.as_bytes()));
let hash = fnv1a(&parts);
let ids = infos.iter().filter_map(|o: &ChoiceOptInfo| o.id.clone());
self.ctx().refs.stop_ids.extend(ids);
let info = StopInfo {
kind: StopKind::Choice,
text: Vec::new(),
text_arg: None,
args_pc: 0,
pc: 0,
id: None,
hash,
line: pos.line,
choice: Some(ChoiceInfo { options: infos }),
binds: false,
};
self.fn_stop(pos, "stop");
Some(ChoiceState {
ignored: matches!(binding, Binding::Ignored),
cmd,
options: opts,
info,
refs_before,
})
}
#[inline(never)]
pub(super) fn choice_finish(&mut self, st: ChoiceState, pos: Pos, out: &mut Vec<StmtId>) {
if st.ignored {
let (ids, targets) = st.refs_before;
let r = &mut self.ctx().refs;
r.stop_ids.truncate(ids);
r.targets.truncate(targets);
return;
}
let choice = Choice {
cmd: st.cmd,
options: st.options,
info: st.info,
};
self.push_stmt(out, StmtKind::Choice(Box::new(choice)), pos.line);
}
pub(super) fn stop_arg(&mut self, e: &Expr, hint: Option<&Ty>) -> (Option<Ty>, StopArg) {
if let Expr::Str(parts, p) = e {
let expr = self.text_parts(parts, p.line, true);
return (Some(Ty::Str), StopArg { expr, text: true });
}
let (t, expr) = self.expr_ir(e, hint);
(t, StopArg { expr, text: false })
}
pub(super) fn text_parts(&mut self, parts: &[StrPart], line: u32, record: bool) -> ExprId {
let mut pieces = Vec::new();
for p in parts {
match p {
StrPart::Text(t, _) => pieces.push(TextPiece::Lit(t.clone())),
StrPart::Interp(e) => {
let (t, expr) = self.expr_ir(e, None);
pieces.push(TextPiece::Interp {
expr,
to_str: t != Some(Ty::Str),
});
}
}
}
self.node(
ExprKind::Text {
parts: pieces,
record,
},
line,
)
}
pub(super) fn note_id_site(&mut self, kind: StopKind, id: Option<&StableId>, id_at: Pos) {
let module = self.cur_mod as u32;
let label = self.ctx().name.clone();
self.id_sites.push(IdSite {
module,
label,
kind,
id: id.map(|i| i.id.clone()),
line: id_at.line,
col: id_at.col,
});
}
pub(super) fn require_id(&mut self, id: Option<&StableId>, kind: &str, pos: Pos) {
if self.require_text_ids && id.is_none() {
self.error(pos, DiagCode::MissingTextId, vec![("kind", s(kind))]);
}
}
pub(super) fn mark_text_stop(&mut self, stop: ExprId, kind: StopKind, parts: &[StrPart]) {
let text = text_parts_of(parts);
let info = self.stop_info_mut(stop);
info.kind = kind;
info.text = text;
}
pub(super) fn make_stop(
&mut self,
name: &str,
args: Vec<StopArg>,
id: Option<&StableId>,
hash: u64,
pos: Pos,
bind: bool,
) -> ExprId {
self.fn_stop(pos, "stop");
let cmd = self.command_index(name);
if let Some(sid) = id {
self.ctx().refs.stop_ids.push(sid.id.clone());
}
if let Some(sid) = id
&& !self.ctx().ids.insert(sid.id.clone())
{
self.error(
sid.pos,
DiagCode::DuplicateStableId,
vec![("id", s(&sid.id))],
);
}
let info = StopInfo {
kind: StopKind::Command,
text: Vec::new(),
text_arg: None,
args_pc: 0,
pc: 0,
id: id.map(|i| i.id.clone()),
hash,
line: pos.line,
choice: None,
binds: bind,
};
let stop = Stop {
cmd,
mark_seen: id.map(|i| i.id.clone()),
args,
info,
};
self.node(ExprKind::Stop(Box::new(stop)), pos.line)
}
pub(super) fn is_identity_name(&self, name: &str) -> bool {
!self.is_import(name)
&& !self.is_own_name(name)
&& !self.registry.has_namespace(name)
&& (self.md().early_identities.contains_key(name)
|| self
.md()
.imports
.values()
.any(|&i| self.modules[i].early_identities.contains_key(name)))
}
}
fn plain_body(parts: &[StrPart], raw: &str) -> String {
let body = crate::syntax::canon::body(parts);
if raw.starts_with('\\') && !body.starts_with('\\') {
format!("\\{body}")
} else {
body
}
}
fn quoted_body(parts: &[StrPart]) -> String {
let body = crate::syntax::canon::body(parts);
body.strip_prefix('「')
.and_then(|b| b.strip_suffix('」'))
.map_or(body.clone(), str::to_string)
}