Math & Phyics Constants (#6)

Reviewed-on: #6
This commit is contained in:
Joe Bellus 2022-10-20 14:28:26 +00:00
commit 88b847852a
2 changed files with 34 additions and 2 deletions

View File

@ -10,6 +10,8 @@ impl<'a> Default for Engine<'a> {
fn default() -> Self { fn default() -> Self {
let mut engine = rhai::Engine::new(); let mut engine = rhai::Engine::new();
engine.set_fast_operators(false); engine.set_fast_operators(false);
engine.register_static_module("physics", rhai::exported_module!(physics).into());
engine.register_static_module("math", rhai::exported_module!(math).into());
dataframe::setup_engine(&mut engine); dataframe::setup_engine(&mut engine);
Self { Self {
@ -58,7 +60,7 @@ impl Default for Output {
impl PartialEq for Output { impl PartialEq for Output {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { match (self, other) {
(Self::Scalar(l0), Self::Scalar(r0)) => l0.to_string() == r0.to_string(), (Self::Scalar(l0), Self::Scalar(r0)) => *l0.to_string() == *r0.to_string(),
(Self::DataFrame(l0), Self::DataFrame(r0)) => l0 == r0, (Self::DataFrame(l0), Self::DataFrame(r0)) => l0 == r0,
(Self::Series(l0), Self::Series(r0)) => l0 == r0, (Self::Series(l0), Self::Series(r0)) => l0 == r0,
(Self::Error(l0), Self::Error(r0)) => l0 == r0, (Self::Error(l0), Self::Error(r0)) => l0 == r0,
@ -88,12 +90,39 @@ impl Output {
} }
} }
use rhai::plugin::*;
#[rhai::export_module]
mod physics {
pub const PLANK: f64 = 6.626e-34;
pub const G: f64 = 6.67430e-11;
pub const COULUMN: f64 = 8.987;
pub const STEFAN_BOLTZMANN: f64 = 5.670e-8;
pub const BOLTZMANN: f64 = 1.380650e23;
pub const C: f64 = 299792458.0;
pub const AVOGADRO: f64 = 6.02214076e23;
pub const ATOMIC_MASS: f64 = 1.66053906660e-27;
pub const ELECTRON_MASS: f64 = 9.1093837015e-31;
pub const PROTON_MASS: f64 = 1.67262192369e-27;
pub const NEUTRON_MASS: f64 = 1.67492749804e-27;
pub const HUBBLE: f64 = 69.3;
}
#[rhai::export_module]
mod math {
pub const PI: f64 = std::f64::consts::PI;
pub const E: f64 = std::f64::consts::E;
pub const SQRT2: f64 = std::f64::consts::SQRT_2;
pub const TAU: f64 = std::f64::consts::TAU;
}
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use super::*; use super::*;
pub fn process(script: &str) -> Output { pub fn process(script: &str) -> Output {
let mut engine = Engine::default(); let mut engine = super::Engine::default();
engine.process_script(script) engine.process_script(script)
} }
} }

View File

@ -330,6 +330,9 @@ impl EditorData {
} }
pub fn current_line_index(&self) -> usize { pub fn current_line_index(&self) -> usize {
if self.cursor_pos == 0 {
return 0;
}
self.content.char_to_line(self.cursor_pos) self.content.char_to_line(self.cursor_pos)
} }