diff --git a/examples/00_Hello_World.mers b/examples/00_Hello_World.mers new file mode 100644 index 0000000..be43a45 --- /dev/null +++ b/examples/00_Hello_World.mers @@ -0,0 +1 @@ +"Hello, World!".println diff --git a/examples/01_Hello_Name.mers b/examples/01_Hello_Name.mers new file mode 100644 index 0000000..26c1420 --- /dev/null +++ b/examples/01_Hello_Name.mers @@ -0,0 +1,3 @@ +"What's you name?".println +name := ().read_line.trim +("Hello, ", name, "!").concat.println diff --git a/examples/02_Calc_Sum.mers b/examples/02_Calc_Sum.mers new file mode 100644 index 0000000..c9ead8f --- /dev/null +++ b/examples/02_Calc_Sum.mers @@ -0,0 +1,13 @@ +total := 0.0 +{() -> { + ("Total: ", total, ". Type a number to change.").concat.println + ( + ().read_line.trim.parse_float, + ( + n -> &total = (total, n).sum, + // not a number, so return a 1-tuple to break from the loop + () -> (()) + ) + ).try +}}.loop +"Goodbye.".println diff --git a/examples/03_Basic_Calculator.mers b/examples/03_Basic_Calculator.mers new file mode 100644 index 0000000..5926b54 --- /dev/null +++ b/examples/03_Basic_Calculator.mers @@ -0,0 +1,30 @@ +"- Calculator -".println +"Type = to set the value to that number.".println +"Type +, - or * to change the value.".println +"Type exit to exit.".println + +current := 0.0 + +{() -> { + ("[ ", current, " ]").concat.println + input := ().read_line.trim + num := ( + (input, 1).substring.trim.parse_float, + ( + () -> 0.0, + val -> val + ) + ).try + mode := (input, 0, 1).substring + if (mode, "+").eq { + ¤t = (current, num).sum + } else if (mode, "-").eq { + ¤t = (current, (num, -1).product).sum + } else if (mode, "*").eq { + ¤t = (current, num).product + } else if (mode, "=").eq { + ¤t = num + } else if (input, "exit").eq { + (()) + } +}}.loop diff --git a/mers_lib/src/program/configs/with_base.rs b/mers_lib/src/program/configs/with_base.rs index f1e0e0f..4f0deb3 100755 --- a/mers_lib/src/program/configs/with_base.rs +++ b/mers_lib/src/program/configs/with_base.rs @@ -62,6 +62,9 @@ impl Config { // found a function that won't fail for this arg_type! if !func_fallible { tuple_fallible = false; + if tuple_possible { + break; + } } } if tuple_fallible || !tuple_possible { diff --git a/mers_lib/src/program/configs/with_math.rs b/mers_lib/src/program/configs/with_math.rs index d86fb33..fac020a 100755 --- a/mers_lib/src/program/configs/with_math.rs +++ b/mers_lib/src/program/configs/with_math.rs @@ -247,7 +247,7 @@ impl Config { } } if usef { - Data::new(data::float::Float(prodi as f64 + prodf)) + Data::new(data::float::Float(prodi as f64 * prodf)) } else { Data::new(data::int::Int(prodi)) } diff --git a/mers_lib/src/program/configs/with_string.rs b/mers_lib/src/program/configs/with_string.rs index 0c4c18e..3c861ac 100644 --- a/mers_lib/src/program/configs/with_string.rs +++ b/mers_lib/src/program/configs/with_string.rs @@ -8,13 +8,25 @@ use crate::{ use super::Config; impl Config { + /// `trim: fn` removes leading and trailing whitespace from a string /// `substring: fn` extracts part of a string. usage: (str, start).substring or (str, start, end).substring. start and end may be negative, in which case they become str.len - n: (str, 0, -1) shortens the string by 1. /// `index_of: fn` finds the index of a pattern in a string /// `index_of_rev: fn` finds the last index of a pattern in a string /// `to_string: fn` turns any argument into a (more or less useful) string representation /// `concat: fn` concatenates all arguments given to it. arg must be an enumerable pub fn with_string(self) -> Self { - self.add_var("concat".to_string(), Data::new(data::function::Function { + self.add_var("trim".to_string(), Data::new(data::function::Function { + info: Arc::new(Info::neverused()), + info_check: Arc::new(Mutex::new(CheckInfo::neverused())), + out: Arc::new(|a, _i| if a.is_included_in(&data::string::StringT) { + Ok(Type::new(data::string::StringT)) + } else { + Err(CheckError(format!("cannot call trim on non-strings"))) + }), + run: Arc::new(|a, _i| { + Data::new(data::string::String(a.get().as_any().downcast_ref::().unwrap().0.trim().to_owned())) + }) + })).add_var("concat".to_string(), Data::new(data::function::Function { info: Arc::new(Info::neverused()), info_check: Arc::new(Mutex::new(CheckInfo::neverused())), out: Arc::new(|a, _i| if a.iterable().is_some() {