Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 16, 2025
1 parent 86d942b commit f210f7c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/core/builtins/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ fn set_local(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(), ExecEr
return core.db.set_param(&name, "", Some(layer));
}

Substitution::parse(&mut feeder, core)?.unwrap().eval(core, Some(layer), false)
match Substitution::parse(&mut feeder, core) {
Ok(ans) => ans.unwrap().eval(core, Some(layer), false),
Err(e) => Err(ExecError::ParseError(e)),
}
}

fn set_local_array(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(), ExecError> {
Expand Down
3 changes: 3 additions & 0 deletions src/error/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::ShellCore;
use crate::error::parse::ParseError;

#[derive(Debug)]
pub enum ExecError {
Expand All @@ -18,6 +19,7 @@ pub enum ExecError {
VariableReadOnly(String),
VariableInvalid(String),
OperandExpected(String),
ParseError(ParseError),
Recursion(String),
SubstringMinus(i64),
Other(String),
Expand All @@ -39,6 +41,7 @@ impl From<ExecError> for String {
ExecError::VariableReadOnly(name) => format!("{}: readonly variable", name),
ExecError::VariableInvalid(name) => format!("`{}': not a valid identifier", name),
ExecError::OperandExpected(token) => format!("{0}: syntax error: operand expected (error token is \"{0}\")", token),
ExecError::ParseError(p) => From::from(p),
ExecError::Recursion(token) => format!("{0}: expression recursion level exceeded (error token is \"{0}\")", token),
ExecError::SubstringMinus(n) => format!("{}: substring expression < 0", n),
ExecError::Other(name) => name,
Expand Down
12 changes: 5 additions & 7 deletions src/error/parse.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda [email protected]
//SPDX-License-Identifier: BSD-3-Clause

use super::exec::ExecError;

#[derive(Debug)]
pub enum ParseError {
UnexpectedSymbol(String),
UnexpectedEof,
Interrupted,
}

impl From<ParseError> for ExecError {
fn from(e: ParseError) -> ExecError {
impl From<ParseError> for String {
fn from(e: ParseError) -> String {
match e {
ParseError::UnexpectedSymbol(s) => ExecError::Other(s),
ParseError::UnexpectedEof => ExecError::Other("eof".to_string()),
ParseError::Interrupted => ExecError::Other("Interrupted".to_string()),
ParseError::UnexpectedSymbol(s) => format!("Unexpected token: {}", s),
ParseError::UnexpectedEof => "syntax error: unexpected end of file".to_string(),
ParseError::Interrupted => "interrupted".to_string(),
}
}
}

0 comments on commit f210f7c

Please sign in to comment.