refactor for readablity, keep semantics identical

This commit is contained in:
Bahex 2024-12-12 05:04:47 +03:00
parent baf86dfb0e
commit 6eee83b38d

View File

@ -335,28 +335,27 @@ pub(crate) fn compile_load_env(
path: &[PathMember],
out_reg: RegId,
) -> Result<(), CompileError> {
if path.is_empty() {
builder.push(
match path {
[] => builder.push(
Instruction::LoadVariable {
dst: out_reg,
var_id: ENV_VARIABLE_ID,
}
.into_spanned(span),
)?;
} else {
let (key, optional) = match &path[0] {
PathMember::String { val, optional, .. } => (builder.data(val)?, *optional),
PathMember::Int { span, .. } => {
)?,
[PathMember::Int { span, .. }, ..] => {
return Err(CompileError::AccessEnvByInt { span: *span })
}
};
let tail = &path[1..];
[PathMember::String {
val: key, optional, ..
}, tail @ ..] => {
let key = builder.data(key)?;
if optional {
builder.push(Instruction::LoadEnvOpt { dst: out_reg, key }.into_spanned(span))?;
builder.push(if *optional {
Instruction::LoadEnvOpt { dst: out_reg, key }.into_spanned(span)
} else {
builder.push(Instruction::LoadEnv { dst: out_reg, key }.into_spanned(span))?;
}
Instruction::LoadEnv { dst: out_reg, key }.into_spanned(span)
})?;
if !tail.is_empty() {
let path = builder.literal(
@ -374,5 +373,6 @@ pub(crate) fn compile_load_env(
)?;
}
}
}
Ok(())
}