Improve AST evaluation efficiency by sharing functions.

This commit is contained in:
Stephen Chung
2020-04-04 22:00:44 +08:00
parent d1cffac420
commit 29150faef2
10 changed files with 374 additions and 212 deletions

View File

@@ -22,8 +22,7 @@ fn test_fn() -> Result<(), EvalAltResult> {
fn test_call_fn() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
engine.consume(
true,
let ast = engine.compile(
r"
fn hello(x, y) {
x + y
@@ -31,14 +30,20 @@ fn test_call_fn() -> Result<(), EvalAltResult> {
fn hello(x) {
x * 2
}
",
fn hello() {
42
}
",
)?;
let r: i64 = engine.call_fn("hello", (42 as INT, 123 as INT))?;
let r: i64 = engine.call_fn(&ast, "hello", (42 as INT, 123 as INT))?;
assert_eq!(r, 165);
let r: i64 = engine.call_fn("hello", 123 as INT)?;
let r: i64 = engine.call_fn1(&ast, "hello", 123 as INT)?;
assert_eq!(r, 246);
let r: i64 = engine.call_fn0(&ast, "hello")?;
assert_eq!(r, 42);
Ok(())
}