Add math-parser library (#2033)

* start of parser

* ops forgot

* reorder files and work on executer

* start of parser

* ops forgot

* reorder files and work on executer

* Cleanup and fix tests

* Integrate into the editor

* added unit checking at parse time

* fix tests

* fix issues

* fix editor intergration

* update pest grammer to support units

* units should be working, need to set up tests to know

* make unit type store exponants as i32

* remove scale, insted just multiply the literal by the scale

* unit now contains empty unit,remove options

* add more tests and implement almost all unary operators

* add evaluation context and variables

* function calling, api might be refined later

* add constants, change function call to not be as built into the parser
and add tests

* add function definitions

* remove meval

* remove raw-rs from workspace

* add support for numberless units

* fix unit handleing logic, add some "unit" tests(haha)

* make it so units cant do implcit mul with idents

* add bench and better tests

* fix editor api

* remove old test

* change hashmap context to use deref

* change constants to use hashmap instad of function

---------

Co-authored-by: hypercube <0hypercube@gmail.com>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
urisinger
2024-11-21 20:24:01 +02:00
committed by GitHub
parent 51ce51ea8c
commit 9fb494764c
14 changed files with 1260 additions and 94 deletions

View File

@@ -0,0 +1,83 @@
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use crate::value::Value;
//TODO: editor integration, implement these traits for whatever is needed, maybe merge them if needed
pub trait ValueProvider {
fn get_value(&self, name: &str) -> Option<Value>;
}
pub trait FunctionProvider {
fn run_function(&self, name: &str, args: &[Value]) -> Option<Value>;
}
pub struct ValueMap(HashMap<String, Value>);
pub struct NothingMap;
impl ValueProvider for &ValueMap {
fn get_value(&self, name: &str) -> Option<Value> {
self.0.get(name).cloned()
}
}
impl ValueProvider for NothingMap {
fn get_value(&self, _: &str) -> Option<Value> {
None
}
}
impl ValueProvider for ValueMap {
fn get_value(&self, name: &str) -> Option<Value> {
self.0.get(name).cloned()
}
}
impl Deref for ValueMap {
type Target = HashMap<String, Value>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ValueMap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl FunctionProvider for NothingMap {
fn run_function(&self, _: &str, _: &[Value]) -> Option<Value> {
None
}
}
pub struct EvalContext<V: ValueProvider, F: FunctionProvider> {
values: V,
functions: F,
}
impl Default for EvalContext<NothingMap, NothingMap> {
fn default() -> Self {
Self {
values: NothingMap,
functions: NothingMap,
}
}
}
impl<V: ValueProvider, F: FunctionProvider> EvalContext<V, F> {
pub fn new(values: V, functions: F) -> Self {
Self { values, functions }
}
pub fn get_value(&self, name: &str) -> Option<Value> {
self.values.get_value(name)
}
pub fn run_function(&self, name: &str, args: &[Value]) -> Option<Value> {
self.functions.run_function(name, args)
}
}