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,43 +335,43 @@ pub(crate) fn compile_load_env(
path: &[PathMember], path: &[PathMember],
out_reg: RegId, out_reg: RegId,
) -> Result<(), CompileError> { ) -> Result<(), CompileError> {
if path.is_empty() { match path {
builder.push( [] => builder.push(
Instruction::LoadVariable { Instruction::LoadVariable {
dst: out_reg, dst: out_reg,
var_id: ENV_VARIABLE_ID, var_id: ENV_VARIABLE_ID,
} }
.into_spanned(span), .into_spanned(span),
)?; )?,
} else { [PathMember::Int { span, .. }, ..] => {
let (key, optional) = match &path[0] { return Err(CompileError::AccessEnvByInt { span: *span })
PathMember::String { val, optional, .. } => (builder.data(val)?, *optional),
PathMember::Int { span, .. } => {
return Err(CompileError::AccessEnvByInt { span: *span })
}
};
let tail = &path[1..];
if optional {
builder.push(Instruction::LoadEnvOpt { dst: out_reg, key }.into_spanned(span))?;
} else {
builder.push(Instruction::LoadEnv { dst: out_reg, key }.into_spanned(span))?;
} }
[PathMember::String {
val: key, optional, ..
}, tail @ ..] => {
let key = builder.data(key)?;
if !tail.is_empty() { builder.push(if *optional {
let path = builder.literal( Instruction::LoadEnvOpt { dst: out_reg, key }.into_spanned(span)
Literal::CellPath(Box::new(CellPath { } else {
members: tail.to_vec(), Instruction::LoadEnv { dst: out_reg, key }.into_spanned(span)
})) })?;
.into_spanned(span),
)?; if !tail.is_empty() {
builder.push( let path = builder.literal(
Instruction::FollowCellPath { Literal::CellPath(Box::new(CellPath {
src_dst: out_reg, members: tail.to_vec(),
path, }))
} .into_spanned(span),
.into_spanned(span), )?;
)?; builder.push(
Instruction::FollowCellPath {
src_dst: out_reg,
path,
}
.into_spanned(span),
)?;
}
} }
} }
Ok(()) Ok(())