mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-23 19:21:26 +02:00
Moved js_eval into Context
This commit is contained in:
parent
3fc5a71b34
commit
f81e1731b3
@ -18,7 +18,7 @@ pub struct Context {
|
|||||||
pos: usize,
|
pos: usize,
|
||||||
symbol_table: SymbolTable,
|
symbol_table: SymbolTable,
|
||||||
angle_unit: String,
|
angle_unit: String,
|
||||||
timeout: Option<u128>,
|
timeout: Option<u32>,
|
||||||
/// This is true whenever the parser is currently parsing a unit declaration.
|
/// This is true whenever the parser is currently parsing a unit declaration.
|
||||||
/// It is necessary to keep track of this in order to know when to find (figure out) units that haven't been defined yet.
|
/// It is necessary to keep track of this in order to know when to find (figure out) units that haven't been defined yet.
|
||||||
/// Unit names are instead treated as variables.
|
/// Unit names are instead treated as variables.
|
||||||
@ -31,7 +31,9 @@ pub struct Context {
|
|||||||
contains_equal_sign: bool,
|
contains_equal_sign: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
impl Context {
|
impl Context {
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut context = Self {
|
let mut context = Self {
|
||||||
tokens: Vec::new(),
|
tokens: Vec::new(),
|
||||||
@ -60,11 +62,23 @@ impl Context {
|
|||||||
/// Set the timeout in milliseconds.
|
/// Set the timeout in milliseconds.
|
||||||
/// The calculation will stop after this amount of time has passed.
|
/// The calculation will stop after this amount of time has passed.
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub fn set_timeout(mut self, timeout: Option<u128>) -> Self {
|
pub fn set_timeout(mut self, timeout: Option<u32>) -> Self {
|
||||||
self.timeout = timeout;
|
self.timeout = timeout;
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen(js_name = evaluate)]
|
||||||
|
#[cfg(not(feature = "rug"))]
|
||||||
|
pub fn js_eval(&mut self, input: &str) -> Result<Option<KalkNum>, JsValue> {
|
||||||
|
let result = eval(self, input);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(Some(value)) => Ok(Some(value)),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(err) => Err(err.to_string().into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Context {
|
impl Default for Context {
|
||||||
@ -132,24 +146,15 @@ pub fn eval(
|
|||||||
&context.angle_unit,
|
&context.angle_unit,
|
||||||
#[cfg(feature = "rug")]
|
#[cfg(feature = "rug")]
|
||||||
precision,
|
precision,
|
||||||
context.timeout,
|
if let Some(timeout) = context.timeout {
|
||||||
|
Some(timeout as u128)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
);
|
);
|
||||||
interpreter.interpret(statements)
|
interpreter.interpret(statements)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen(js_name = evaluate)]
|
|
||||||
#[cfg(not(feature = "rug"))]
|
|
||||||
pub fn js_eval(input: &str) -> Result<Option<KalkNum>, JsValue> {
|
|
||||||
let mut context = Context::new();
|
|
||||||
let result = eval(&mut context, input);
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(Some(value)) => Ok(Some(value)),
|
|
||||||
Ok(None) => Ok(None),
|
|
||||||
Err(err) => Err(err.to_string().into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse expressions/declarations and return a syntax tree.
|
/// Parse expressions/declarations and return a syntax tree.
|
||||||
///
|
///
|
||||||
/// `None` will be returned if the last statement is a declaration.
|
/// `None` will be returned if the last statement is a declaration.
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
main();
|
main();
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const kalk = await import("kalk-rs");
|
const kalk = await import("../kalk/pkg");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(kalk.evaluate("5^3").toScientificNotation().toString());
|
const context = new kalk.Context();
|
||||||
|
console.log(context.evaluate("sum(1, 5, n)").toScientificNotation().toString());
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user