forked from boa-dev/boa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
122 lines (110 loc) · 3.17 KB
/
lib.rs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#![forbid(
//missing_docs,
//warnings,
anonymous_parameters,
unused_extern_crates,
unused_import_braces,
missing_copy_implementations,
//trivial_casts,
variant_size_differences,
missing_debug_implementations,
trivial_numeric_casts
)]
// Debug trait derivation will show an error if forbidden.
#![deny(unused_qualifications)]
#![deny(clippy::all)]
#![warn(
// clippy::pedantic,
clippy::restriction,
clippy::cognitive_complexity,
//missing_docs
)]
#![allow(
clippy::missing_docs_in_private_items,
clippy::missing_inline_in_public_items,
clippy::implicit_return,
clippy::wildcard_enum_match_arm,
clippy::cognitive_complexity,
clippy::module_name_repetitions,
clippy::print_stdout,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::non_ascii_literal,
clippy::float_arithmetic
)]
pub mod environment;
pub mod exec;
pub mod js;
pub mod syntax;
use crate::{
exec::{Executor, Interpreter},
js::value::ResultValue,
syntax::{ast::expr::Expr, lexer::Lexer, parser::Parser},
};
use wasm_bindgen::prelude::*;
fn parser_expr(src: &str) -> Expr {
let mut lexer = Lexer::new(src);
lexer.lex().expect("lexing failed");
let tokens = lexer.tokens;
Parser::new(tokens).parse_all().expect("parsing failed")
}
/// Execute the code using an existing Interpreter
/// The str is consumed and the state of the Interpreter is changed
pub fn forward(engine: &mut Interpreter, src: &str) -> String {
// Setup executor
let expr = parser_expr(src);
let result = engine.run(&expr);
match result {
Ok(v) => v.to_string(),
Err(v) => format!("{}: {}", "Error", v.to_string()),
}
}
/// Execute the code using an existing Interpreter
/// The str is consumed and the state of the Interpreter is changed
/// Similar to `forward`, except the current value is returned instad of the string
/// If the interpreter fails parsing an error value is returned instead (error object)
pub fn forward_val(engine: &mut Interpreter, src: &str) -> ResultValue {
// Setup executor
let expr = parser_expr(src);
engine.run(&expr)
}
/// Create a clean Interpreter and execute the code
pub fn exec(src: &str) -> String {
let mut engine: Interpreter = Executor::new();
forward(&mut engine, src)
}
// WASM
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn evaluate(src: &str) -> String {
let mut lexer = Lexer::new(&src);
match lexer.lex() {
Ok(_v) => (),
Err(v) => log(&v.to_string()),
}
let tokens = lexer.tokens;
// Setup executor
let expr: Expr;
match Parser::new(tokens).parse_all() {
Ok(v) => {
expr = v;
}
Err(_v) => {
log("parsing fail");
return String::from("parsing failed");
}
}
let mut engine: Interpreter = Executor::new();
let result = engine.run(&expr);
log("test2");
match result {
Ok(v) => v.to_string(),
Err(v) => format!("{}: {}", "error", v.to_string()),
}
}