Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 20, 2025
1 parent 58fe307 commit 7a73031
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod r#while;
pub mod r#if;

use crate::{ShellCore, Feeder, Script};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use crate::utils::exit;
Expand Down Expand Up @@ -45,7 +44,7 @@ pub trait Command {
core.initialize_as_subshell(Pid::from_raw(0), pipe.pgid);
io::connect(pipe, self.get_redirects(), core);
if let Err(e) = self.run(core, true) {
exec::print_error(e, core);
e.print(core);
}
exit::normal(core)
},
Expand Down
3 changes: 1 addition & 2 deletions src/elements/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use super::pipeline::Pipeline;
use crate::{Feeder, ShellCore};
use crate::core::jobtable::JobEntry;
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use crate::utils::exit;
Expand Down Expand Up @@ -73,7 +72,7 @@ impl Job {
Ok(ForkResult::Child) => {
core.initialize_as_subshell(Pid::from_raw(0), pgid);
if let Err(e) = self.exec(core, false) {
exec::print_error(e, core);
e.print(core);
}
exit::normal(core)
},
Expand Down
3 changes: 1 addition & 2 deletions src/elements/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use super::job::Job;
use crate::{Feeder, ShellCore};
use crate::error::parse;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;

Expand Down Expand Up @@ -83,7 +82,7 @@ impl Script {
Status::UnexpectedSymbol(s) => {
eprintln!("Unexpected token: {}", s);
let e = ParseError::UnexpectedSymbol(s.clone());
parse::print_error(e.clone(), core);
e.print(core);
core.db.set_param("?", "2").unwrap();
feeder.consume(feeder.len());
return Err(e);
Expand Down
4 changes: 0 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ pub mod input;
pub mod parse;

/*
use crate::ShellCore;
use nix::sys::signal::Signal;
use nix::unistd::Pid;
pub fn print(s: &str, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
if core.flags.contains('i') {
Expand Down
32 changes: 20 additions & 12 deletions src/error/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::ShellCore;
use crate::error::parse::ParseError;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ExecError {
Internal,
ArrayIndexInvalid(String),
Expand All @@ -28,15 +28,21 @@ pub enum ExecError {

impl From<ExecError> for String {
fn from(e: ExecError) -> String {
Self::from(&e)
}
}

impl From<&ExecError> for String {
fn from(e: &ExecError) -> String {
match e {
ExecError::Internal => "INTERNAL ERROR".to_string(),
ExecError::ArrayIndexInvalid(name) => format!("`{}': not a valid index", name),
ExecError::BadSubstitution(s) => format!("`{}': bad substitution", s),
ExecError::DivZero => "divided by 0".to_string(),
ExecError::Exponent(s) => format!("exponent less than 0 (error token is \"{}\")", s),
ExecError::InvalidName(name) => format!("`{}': invalid name", name),
ExecError::InvalidBase(b) => format!("sush: {0}: invalid arithmetic base (error token is \"{0}\")", b),
ExecError::InvalidOption(opt) => format!("sush: {}: invalid option", opt),
ExecError::InvalidBase(b) => format!("{0}: invalid arithmetic base (error token is \"{0}\")", b),
ExecError::InvalidOption(opt) => format!("{}: invalid option", opt),
ExecError::Interrupted => "interrupted".to_string(),
ExecError::AssignmentToNonVariable(right) => format!("attempted assignment to non-variable (error token is \"{}\")", right),
ExecError::ValidOnlyInFunction(com) => format!("{}: can only be used in a function", &com),
Expand All @@ -46,18 +52,20 @@ impl From<ExecError> for String {
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,
ExecError::Other(name) => name.to_string(),
}
}
}

pub fn print_error(e: ExecError, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
let s: String = From::<ExecError>::from(e);
if core.flags.contains('i') {
eprintln!("{}: {}", &name, &s);
}else{
let lineno = core.db.get_param("LINENO").unwrap_or("".to_string());
eprintln!("{}: line {}: {}", &name, &lineno, s);
impl ExecError {
pub fn print(&self, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
let s: String = From::<&ExecError>::from(self);
if core.flags.contains('i') {
eprintln!("{}: {}", &name, &s);
}else{
let lineno = core.db.get_param("LINENO").unwrap_or("".to_string());
eprintln!("{}: line {}: {}", &name, &lineno, s);
}
}
}
4 changes: 2 additions & 2 deletions src/error/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub enum InputError {
History,
}

impl From<InputError> for String {
fn from(e: InputError) -> String {
impl From<&InputError> for String {
fn from(e: &InputError) -> String {
match e {
InputError::Eof => "syntax error: unexpected end of file".to_string(),
InputError::Interrupt => "interrupted".to_string(),
Expand Down
30 changes: 12 additions & 18 deletions src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,26 @@ use super::input::InputError;
pub enum ParseError {
UnexpectedSymbol(String),
Input(InputError),
/*
UnexpectedEof,
Interrupted,
*/
}

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

pub fn print_error(e: ParseError, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
let s: String = From::<ParseError>::from(e);
if core.flags.contains('i') {
eprintln!("{}: {}", &name, &s);
}else{
let lineno = core.db.get_param("LINENO").unwrap_or("".to_string());
eprintln!("{}: line {}: {}", &name, &lineno, s);
impl ParseError {
pub fn print(&self, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
let s: String = From::<&ParseError>::from(self);
if core.flags.contains('i') {
eprintln!("{}: {}", &name, &s);
}else{
let lineno = core.db.get_param("LINENO").unwrap_or("".to_string());
eprintln!("{}: line {}: {}", &name, &lineno, s);
}
}
}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::{env, process};
use crate::core::ShellCore;
use crate::utils::exit;
use crate::elements::script::Script;
use crate::error::exec;
use crate::error::input::InputError;
use crate::feeder::Feeder;
use utils::file_check;
Expand Down Expand Up @@ -56,7 +55,7 @@ fn main_loop(core: &mut ShellCore) {

if let Ok(Some(mut s)) = Script::parse(&mut feeder, core){
if let Err(e) = s.exec(core) {
exec::print_error(e, core);
e.print(core);
}
}
core.sigint.store(false, Relaxed);
Expand Down

0 comments on commit 7a73031

Please sign in to comment.