2024-02-17 13:23:50 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use mers_lib::{
|
2024-02-22 13:34:02 +01:00
|
|
|
data::{self, Data, Type},
|
2024-02-17 13:23:50 +01:00
|
|
|
errors::CheckError,
|
2024-04-15 14:07:05 +02:00
|
|
|
prelude_compile::{parse, Config, Source},
|
|
|
|
program::parsed::CompInfo,
|
2024-02-17 13:23:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
fn main() -> Result<(), CheckError> {
|
|
|
|
let (_, func) = parse_compile_check_run(
|
|
|
|
// The `[(String -> String)]` type annotation ensures that decorate.mers returns a `String -> String` function.
|
|
|
|
"[(String -> String)] #include \"examples/decorate.mers\"".to_owned(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// We can unwrap the downcasts because mers has type-checked that `func` is a `(String -> String)`.
|
|
|
|
|
|
|
|
let func = func.get();
|
|
|
|
let func = func
|
|
|
|
.as_any()
|
|
|
|
.downcast_ref::<data::function::Function>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// use the function to decorate these 3 test strings
|
|
|
|
for input in ["my test string", "Main Menu", "O.o"] {
|
2024-06-19 12:35:23 +02:00
|
|
|
let result = func.run(Data::new(data::string::String(input.to_owned())))?;
|
2024-02-17 13:23:50 +01:00
|
|
|
let result = result.get();
|
|
|
|
let result = &result
|
|
|
|
.as_any()
|
|
|
|
.downcast_ref::<data::string::String>()
|
|
|
|
.unwrap()
|
|
|
|
.0;
|
|
|
|
eprintln!("{result}");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-17 14:06:19 +01:00
|
|
|
/// example 00
|
2024-02-17 13:23:50 +01:00
|
|
|
fn parse_compile_check_run(src: String) -> Result<(Type, Data), CheckError> {
|
|
|
|
let mut source = Source::new_from_string(src);
|
|
|
|
let srca = Arc::new(source.clone());
|
|
|
|
let parsed = parse(&mut source, &srca)?;
|
|
|
|
let (mut i1, mut i2, mut i3) = Config::new().bundle_std().infos();
|
|
|
|
let compiled = parsed.compile(&mut i1, CompInfo::default())?;
|
|
|
|
let output_type = compiled.check(&mut i3, None)?;
|
2024-06-19 12:35:23 +02:00
|
|
|
let output_value = compiled.run(&mut i2)?;
|
2024-02-17 13:23:50 +01:00
|
|
|
Ok((output_type, output_value))
|
|
|
|
}
|