1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use wasm_bindgen::prelude::*;
use crate::{parse, Expression};
use crate::normal::Strategy;
#[wasm_bindgen]
pub struct Exp(Expression);
#[wasm_bindgen]
impl Exp {
#[wasm_bindgen(constructor)]
pub fn new(v: JsValue) -> Result<Exp, JsValue> {
if let Some(s) = v.as_string() {
let parser = parse::ExpressionParser::new();
match parser.parse(&s) {
Ok(e) => Ok(Exp(e)),
Err(e) => Err(JsValue::from_str(&format!("{}", e))),
}
} else if let Some(n) = v.as_f64() {
Ok(Exp(Expression::from(n as u64)))
} else if let Some(b) = v.as_bool() {
Ok(Exp(Expression::from(b)))
} else {
Err(JsValue::from_str("invalid constructor type"))
}
}
pub fn applicative(&self, η: bool) -> Self {
Exp(self.0.normalize(&Strategy::Applicative(η)))
}
pub fn call_by_value(&self) -> Self {
Exp(self.0.normalize(&Strategy::CallByValue))
}
pub fn normal(&self, η: bool) -> Self {
Exp(self.0.normalize(&Strategy::Normal(η)))
}
pub fn call_by_name(&self) -> Self {
Exp(self.0.normalize(&Strategy::CallByName))
}
pub fn head_spine(&self, η: bool) -> Self {
Exp(self.0.normalize(&Strategy::HeadSpine(η)))
}
#[wasm_bindgen(method, js_name = toString)]
pub fn to_string(&self) -> String {
format!("{}", self.0)
}
#[wasm_bindgen(method, js_name = toNumber)]
pub fn to_number(&self) -> usize {
u64::from(self.0.clone()) as usize
}
#[wasm_bindgen(method, js_name = toBool)]
pub fn to_bool(&self) -> bool {
self.0.clone().into()
}
}