pub(super) const ARITY_HEADLINE: &str = "very args. much wrong.";
pub(super) const RUNTIME_ERROR_HEADLINE: &str = "very error. much broken.";
pub(super) const NAME_PREFIX: &str = "v_";
pub(super) const FUNC_PREFIX: &str = "f_";
pub(super) const FUNC_BODY_PREFIX: &str = "b_";
pub(super) const CLOSURE_PREFIX: &str = "c_";
pub(super) const CLOSURE_BODY_PREFIX: &str = "cb_";
pub(super) const CTOR_PREFIX: &str = "n_";
pub(super) const METHOD_PREFIX: &str = "mf_";
pub(super) const METHOD_BODY_PREFIX: &str = "mb_";
pub(super) fn func_wrapper(file_id: u32, name: &str) -> String {
if file_id == 0 {
format!("{FUNC_PREFIX}{name}")
} else {
format!("{FUNC_PREFIX}{file_id}_{name}")
}
}
pub(super) fn func_body(file_id: u32, name: &str) -> String {
if file_id == 0 {
format!("{FUNC_BODY_PREFIX}{name}")
} else {
format!("{FUNC_BODY_PREFIX}{file_id}_{name}")
}
}
pub(super) fn field_name(file_id: u32, name: &str) -> String {
if file_id == 0 {
format!("{NAME_PREFIX}{name}")
} else {
format!("g{file_id}_{name}")
}
}
pub(super) fn signature(captures: &[String], params: &[String], owned: bool) -> String {
let mut parts: Vec<String> = captures
.iter()
.map(|c| format!("{NAME_PREFIX}{c}: Cell"))
.collect();
parts.extend(params.iter().map(|p| {
if owned {
format!("mut {NAME_PREFIX}{p}: Value")
} else {
format!("{NAME_PREFIX}{p}: Value")
}
}));
parts.push("env: &mut Env".to_string());
parts.join(", ")
}
pub(super) fn escape_str(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\n' => out.push_str("\\n"),
'\t' => out.push_str("\\t"),
'\r' => out.push_str("\\r"),
other if other.is_control() => out.extend(other.escape_default()),
other => out.push(other),
}
}
out
}