fix product function and add examples

This commit is contained in:
Mark 2023-10-19 19:38:13 +02:00
parent 1cb84fea43
commit 42288494f0
7 changed files with 64 additions and 2 deletions

View File

@ -0,0 +1 @@
"Hello, World!".println

View File

@ -0,0 +1,3 @@
"What's you name?".println
name := ().read_line.trim
("Hello, ", name, "!").concat.println

13
examples/02_Calc_Sum.mers Normal file
View File

@ -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

View File

@ -0,0 +1,30 @@
"- Calculator -".println
"Type =<num> to set the value to that number.".println
"Type +<num>, -<num> or *<num> 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 {
&current = (current, num).sum
} else if (mode, "-").eq {
&current = (current, (num, -1).product).sum
} else if (mode, "*").eq {
&current = (current, num).product
} else if (mode, "=").eq {
&current = num
} else if (input, "exit").eq {
(())
}
}}.loop

View File

@ -62,6 +62,9 @@ impl Config {
// found a function that won't fail for this arg_type! // found a function that won't fail for this arg_type!
if !func_fallible { if !func_fallible {
tuple_fallible = false; tuple_fallible = false;
if tuple_possible {
break;
}
} }
} }
if tuple_fallible || !tuple_possible { if tuple_fallible || !tuple_possible {

View File

@ -247,7 +247,7 @@ impl Config {
} }
} }
if usef { if usef {
Data::new(data::float::Float(prodi as f64 + prodf)) Data::new(data::float::Float(prodi as f64 * prodf))
} else { } else {
Data::new(data::int::Int(prodi)) Data::new(data::int::Int(prodi))
} }

View File

@ -8,13 +8,25 @@ use crate::{
use super::Config; use super::Config;
impl 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. /// `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: fn` finds the index of a pattern in a string
/// `index_of_rev: fn` finds the last 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 /// `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 /// `concat: fn` concatenates all arguments given to it. arg must be an enumerable
pub fn with_string(self) -> Self { 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::<data::string::String>().unwrap().0.trim().to_owned()))
})
})).add_var("concat".to_string(), Data::new(data::function::Function {
info: Arc::new(Info::neverused()), info: Arc::new(Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, _i| if a.iterable().is_some() { out: Arc::new(|a, _i| if a.iterable().is_some() {