Skip to content

Commit

Permalink
Change type
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 14, 2025
1 parent f03c505 commit f66481f
Show file tree
Hide file tree
Showing 31 changed files with 233 additions and 193 deletions.
4 changes: 2 additions & 2 deletions src/core/builtins/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn getopts(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
let _ = core.db.set_param("OPTARG", "", None);

if let Err(e) = result {
let msg = format!("getopts: {}", &e);
let msg = format!("getopts: {:?}", &e);
error::print(&msg, core);
return 1;
}
Expand All @@ -104,7 +104,7 @@ pub fn getopts(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
let _ = core.db.set_param("OPTIND", &(index+2).to_string(), None);

if let Err(e) = result {
let msg = format!("getopts: {}", &e);
let msg = format!("getopts: {:?}", &e);
error::print(&msg, core);
return 1;
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/builtins/parameter.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::{error, ShellCore, utils, Feeder};
use crate::error::ExecError;
use crate::utils::exit;
use crate::elements::substitution::Substitution;
use crate::utils::arg;
Expand All @@ -27,7 +28,7 @@ pub fn print_all(core: &mut ShellCore) -> i32 {
0
}

fn set_local(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(), String> {
fn set_local(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(), ExecError> {
let mut feeder = Feeder::new(arg);
if feeder.scanner_name(core) == feeder.len() { // name only
let name = feeder.consume(feeder.len());
Expand All @@ -36,12 +37,12 @@ fn set_local(arg: &str, core: &mut ShellCore, layer: usize) -> Result<(), String

let mut sub = match Substitution::parse(&mut feeder, core) {
Some(s) => s,
_ => return Err(format!("local: `{}': not a valid identifier", arg)),
_ => return Err(ExecError::Other(format!("local: `{}': not a valid identifier", arg))),
};

match sub.eval(core, Some(layer), false) {
true => Ok(()),
false => Err(format!("local: `{}': evaluation error", arg)),
false => Err(ExecError::Other(format!("local: `{}': evaluation error", arg))),
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/core/builtins/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub fn read(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
return 1;
}else{
if let Err(e) = core.db.set_param(&a, "", None) {
//let msg = error::readonly(&a);
error::print(&e, core);
let msg = format!("{:?}", &e);
error::print(&msg, core);
return 1;
}
}
Expand All @@ -48,8 +48,8 @@ pub fn read(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
for w in line.trim_end().split(' ') {
if pos < args.len()-1 {
if let Err(e) = core.db.set_param(&args[pos], &w, None) {
//let msg = error::readonly(&args[pos]);
error::print(&e, core);
let msg = format!("{:?}", &e);
error::print(&msg, core);
return 1;
}
pos += 1;
Expand All @@ -59,8 +59,8 @@ pub fn read(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
}
overflow += &w;
if let Err(e) = core.db.set_param(&args[pos], &overflow, None) {
//let msg = error::readonly(&args[pos]);
error::print(&e, core);
let msg = format!("{:?}", &e);
error::print(&msg, core);
return 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl DataBase {
None
}

pub fn set_param(&mut self, name: &str, val: &str, layer: Option<usize>) -> Result<(), String> {
pub fn set_param(&mut self, name: &str, val: &str, layer: Option<usize>) -> Result<(), ExecError> {
Self::name_check(name)?;
self.write_check(name)?;
let layer = self.get_target_layer(name, layer);
Expand Down
2 changes: 1 addition & 1 deletion src/core/database/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait Data {
println!("{}={}", name, self.print_body());
}

fn set_as_single(&mut self, _: &str) -> Result<(), String> {Err("Undefined call".to_string())}
fn set_as_single(&mut self, _: &str) -> Result<(), ExecError> {Err(ExecError::Other("Undefined call".to_string()))}
fn set_as_array(&mut self, _: &str, _: &str) -> Result<(), String> {Err("not an array".to_string())}
fn set_as_assoc(&mut self, _: &str, _: &str) -> Result<(), String> {Err("not an associative table".to_string())}

Expand Down
7 changes: 4 additions & 3 deletions src/core/database/data/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//SPDXLicense-Identifier: BSD-3-Clause

use crate::core::HashMap;
use crate::error::ExecError;
use std::env;
use super::Data;

Expand All @@ -20,7 +21,7 @@ impl Data for SingleData {
fn boxed_clone(&self) -> Box<dyn Data> { Box::new(self.clone()) }
fn print_body(&self) -> String { self.body.clone() }

fn set_as_single(&mut self, value: &str) -> Result<(), String> {
fn set_as_single(&mut self, value: &str) -> Result<(), ExecError> {
self.body = value.to_string();
Ok(())
}
Expand All @@ -31,12 +32,12 @@ impl Data for SingleData {
}

impl SingleData {
pub fn set_new_entry(db_layer: &mut HashMap<String, Box<dyn Data>>, name: &str, value: &str)-> Result<(), String> {
pub fn set_new_entry(db_layer: &mut HashMap<String, Box<dyn Data>>, name: &str, value: &str)-> Result<(), ExecError> {
db_layer.insert( name.to_string(), Box::new(SingleData::from(value)) );
Ok(())
}

pub fn set_value(db_layer: &mut HashMap<String, Box<dyn Data>>, name: &str, val: &str) -> Result<(), String> {
pub fn set_value(db_layer: &mut HashMap<String, Box<dyn Data>>, name: &str, val: &str) -> Result<(), ExecError> {
if env::var(name).is_ok() {
env::set_var(name, val);
}
Expand Down
18 changes: 12 additions & 6 deletions src/elements/command/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{ShellCore, Feeder};
use super::{Command, Redirect};
use crate::elements::expr::arithmetic::ArithmeticExpr;
use crate::error::ExecError;

#[derive(Debug, Clone)]
pub struct ArithmeticCommand {
Expand All @@ -16,9 +17,12 @@ pub struct ArithmeticCommand {
impl Command for ArithmeticCommand {
fn run(&mut self, core: &mut ShellCore, _: bool) {
let exit_status = match self.eval(core).as_deref() {
Some("0") => 1,
Some(_) => 0,
None => 1,
Ok("0") => 1,
Ok(_) => 0,
Err(e) => {
eprintln!("{:?}", e);
1
},
};
core.db.exit_status = exit_status;
}
Expand All @@ -40,18 +44,20 @@ impl ArithmeticCommand {
}
}

pub fn eval(&mut self, core: &mut ShellCore) -> Option<String> {
pub fn eval(&mut self, core: &mut ShellCore) -> Result<String, ExecError> {
let mut ans = String::new();
for a in &mut self.expressions {
ans = a.eval(core)?;
/*
match a.eval(core) {
Ok(s) => ans = s,
Err(e) => {
eprintln!("{}", &e);
return None;
},
}
}*/
}
Some(ans)
Ok(ans)
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Self> {
Expand Down
3 changes: 2 additions & 1 deletion src/elements/command/for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl ForCommand {

if let Err(e) = core.db.set_param(&self.name, &p, None) {
core.db.exit_status = 1;
error::print(&e, core);
let msg = format!("{:?}", &e);
error::print(&msg, core);
}

if core.continue_counter > 0 {
Expand Down
45 changes: 24 additions & 21 deletions src/elements/expr/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub mod word;
mod int;
mod float;

use crate::{error, ShellCore};
use crate::ShellCore;
use crate::error::ExecError;
use crate::utils::exit;
use self::calculator::calculate;
use self::elem::ArithElem;
Expand All @@ -26,20 +27,15 @@ pub struct ArithmeticExpr {
}

impl ArithmeticExpr {
pub fn eval(&mut self, core: &mut ShellCore) -> Result<String, String> {
pub fn eval(&mut self, core: &mut ShellCore) -> Result<String, ExecError> {
match self.eval_elems(core, true)? {
ArithElem::Integer(n) => self.ans_to_string(n),
ArithElem::Float(f) => Ok(f.to_string()),
/*
Err(msg) => {
eprintln!("sush: {}: {}", &self.text, msg);
None
},*/
_ => exit::internal("invalid calculation result"),
}
}

pub fn eval_as_assoc_index(&mut self, core: &mut ShellCore) -> Result<String, String> {
pub fn eval_as_assoc_index(&mut self, core: &mut ShellCore) -> Result<String, ExecError> {
let mut ans = String::new();

for e in &self.elements {
Expand All @@ -54,7 +50,7 @@ impl ArithmeticExpr {
ans += "--";
}
},
None => return Err("not an assoc index".to_string()),
None => return Err(ExecError::ArrayIndexInvalid(w.text.clone())),
}
},
_ => ans += &e.to_string(),
Expand All @@ -72,16 +68,17 @@ impl ArithmeticExpr {
None
},
Err(msg) => {
eprintln!("sush: {}: {}", &self.text, msg);
eprintln!("sush: {}: {:?}", &self.text, msg);
None
},
_ => exit::internal("invalid calculation result"),
}
}

pub fn eval_elems(&mut self, core: &mut ShellCore, permit_empty: bool) -> Result<ArithElem, String> {
pub fn eval_elems(&mut self, core: &mut ShellCore, permit_empty: bool) -> Result<ArithElem, ExecError> {
if self.elements.is_empty() && ! permit_empty {
return Err("operand expexted (error token: \")\")".to_string());
return Err(ExecError::OperandExpected("\")\"".to_string()));
//return Err("operand expexted (error token: \")\")".to_string());
}
let es = match self.decompose_increments() {
Ok(data) => data,
Expand All @@ -91,7 +88,7 @@ impl ArithmeticExpr {
calculate(&es, core)
}

fn ans_to_string(&self, n: i64) -> Result<String, String> {
fn ans_to_string(&self, n: i64) -> Result<String, ExecError> {
let base_str = self.output_base.clone();

if base_str == "10" {
Expand All @@ -101,16 +98,22 @@ impl ArithmeticExpr {
let base = match base_str.parse::<i64>() {
Ok(b) => b,
_ => {
return Err(ExecError::InvalidBase(base_str));
/*
let msg = format!("sush: {0}: invalid arithmetic base (error token is \"{0}\")", base_str);
eprintln!("{}", &msg);
return Err(msg);
*/
},
};

if base <= 1 || base > 64 {
return Err(ExecError::InvalidBase(base_str));
/*
let msg = format!("sush: {0}: invalid arithmetic base (error token is \"{0}\")", base_str);
eprintln!("{}", &msg);
return Err(msg);
*/
}

let mut tmp = n.abs();
Expand Down Expand Up @@ -150,16 +153,16 @@ impl ArithmeticExpr {
std::str::from_utf8(&ascii).unwrap().to_string()
}

fn eval_in_cond(&mut self, core: &mut ShellCore) -> Result<ArithElem, String> {
let es = match self.decompose_increments() {
Ok(data) => data,
Err(err_msg) => return Err(err_msg),
};
fn eval_in_cond(&mut self, core: &mut ShellCore) -> Result<ArithElem, ExecError> {
let es = self.decompose_increments()?;

calculate(&es, core)
/*
match calculate(&es, core) {
Ok(ans) => Ok(ans),
Err(err_msg) => return Err(err_msg),
}
*/
}

fn preinc_to_unarys(&mut self, ans: &mut Vec<ArithElem>, pos: usize, inc: i64) -> i64 {
Expand All @@ -181,7 +184,7 @@ impl ArithmeticExpr {
0
}

fn decompose_increments(&mut self) -> Result<Vec<ArithElem>, String> {
fn decompose_increments(&mut self) -> Result<Vec<ArithElem>, ExecError> {
let mut ans = vec![];
let mut pre_increment = 0;

Expand All @@ -206,8 +209,8 @@ impl ArithmeticExpr {
}

match pre_increment {
1 => Err(error::syntax("++")),
-1 => Err(error::syntax("--")),
1 => Err(ExecError::OperandExpected("++".to_string())),
-1 => Err(ExecError::OperandExpected("--".to_string())),
_ => Ok(ans),
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/elements/expr/arithmetic/array_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::ShellCore;
use crate::error::ExecError;
use super::ArithElem;
use crate::elements::subscript::Subscript;

pub fn to_operand(name: &String, sub: &mut Subscript, pre_increment: i64, post_increment: i64,
core: &mut ShellCore) -> Result<ArithElem, String> {
core: &mut ShellCore) -> Result<ArithElem, ExecError> {
let key = sub.eval(core, name)?;
/*
let key = match sub.eval(core, name) {
Expand All @@ -21,14 +22,14 @@ pub fn to_operand(name: &String, sub: &mut Subscript, pre_increment: i64, post_i

let mut value_num = match value_str.parse::<i64>() {
Ok(n) => n,
Err(_) => return Err(format!("{}: not an interger", &name)),
Err(_) => return Err(ExecError::Other(format!("{}: not an interger", &name))),
};

if pre_increment != 0 {
value_num += pre_increment;
match set_value(name, &key, value_num, core) {
Ok(()) => {},
Err(e) => return Err(e),
Err(e) => return Err(ExecError::Other(e)),
}
}

Expand All @@ -38,7 +39,7 @@ pub fn to_operand(name: &String, sub: &mut Subscript, pre_increment: i64, post_i
value_num += post_increment;
match set_value(name, &key, value_num, core) {
Ok(()) => {},
Err(e) => return Err(e),
Err(e) => return Err(ExecError::Other(e)),
}
}
ans
Expand Down
Loading

0 comments on commit f66481f

Please sign in to comment.