mers_lib to 0.5.0, readme updated

This commit is contained in:
Mark
2024-02-17 14:06:19 +01:00
parent 0c87c69743
commit cc4a4366c9
5 changed files with 100 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "mers_lib"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "library to use the mers language in other projects"

View File

@@ -3,5 +3,47 @@
The library behind [mers](https://github.com/Dummi26/mers).
With this, you can parse, compile, check and run mers code.
You can also add your own functions and types which can then be used from mers, if you really want to.
## Running mers
There are four steps to running mers code.
The examples show you how to actually implement them,
this readme only explains what they do any why.
### 1. Parsing
This first step converts the source code, a string, to a parsed mers statement.
In this step, syntax errors and unknown variables are caught.
### 2. Compiling
This converts a parsed mers statement to a compiled one. It almost never produces an error.
### 3. Checking
This step is optional. If you parse and compile your source code, you can (try to) run it.
However, mers assumes that all mers code you run is actually valid,
so if you don't check your codes validity, mers will probably panic while running your code.
This step performs all the type-checking and determines the output type of your code, if it is valid.
For example, the following code is valid and has the return type `Int/Float`:
```
my_condition := true
if my_condition {
5
} else {
1.4
}
```
### 4. Running
This step assumes that the code it is running is actually valid, so it never returns an error.
As long as `check` didn't return an error in Step 3, it is safe to assume that this will return the value produced by the code.
We can also assume that the return value has a type which is included in that determined by `check`.
If `check` returned an error, this will likely panic.

View File

@@ -35,23 +35,14 @@ fn main() -> Result<(), CheckError> {
Ok(())
}
/// example 00
fn parse_compile_check_run(src: String) -> Result<(Type, Data), CheckError> {
// prepare the string for parsing
let mut source = Source::new_from_string(src);
// this is used for error messages
let srca = Arc::new(source.clone());
// parse the code
let parsed = parse(&mut source, &srca)?;
// get infos
let (mut i1, mut i2, mut i3) = Config::new().bundle_std().infos();
// compile
let compiled = parsed.compile(&mut i1, CompInfo::default())?;
// check (this step is optional, but if it is skipped when it would have returned an error, `run` will likely panic)
let output_type = compiled.check(&mut i3, None)?;
// run
let output_value = compiled.run(&mut i2);
// check that the predicted output type was correct
assert!(output_value.get().as_type().is_included_in(&output_type));
// return the produced value
Ok((output_type, output_value))
}

View File

@@ -23,6 +23,18 @@ pub struct Function {
)>,
}
impl Function {
pub fn new(
out: impl Fn(&Type) -> Result<Type, CheckError> + Send + Sync + 'static,
run: impl Fn(Data) -> Data + Send + Sync + 'static,
) -> Self {
Self {
info: Arc::new(crate::info::Info::neverused()),
info_check: Arc::new(Mutex::new(crate::info::Info::neverused())),
out: Arc::new(move |a, _| out(a)),
run: Arc::new(move |a, _| run(a)),
inner_statements: None,
}
}
pub fn with_info_run(&self, info: Arc<Info>) -> Self {
Self {
info,