Skip to content

Commit 30a6a44

Browse files
committed
upgrade parser; add some tests; bump 0.1.2
1 parent bd4253d commit 30a6a44

File tree

4 files changed

+163
-46
lines changed

4 files changed

+163
-46
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "calx_vm"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
55
edition = "2018"
66
license = "MIT"
@@ -16,7 +16,7 @@ exclude = [
1616
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1717

1818
[dependencies]
19-
cirru_parser = "0.1.1"
19+
cirru_parser = "0.1.2"
2020
regex = "1"
2121
lazy_static = "1.4.0"
2222
clap = "3.0.0-beta.4"

src/bin/calx.rs

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,60 +41,56 @@ fn main() -> Result<(), String> {
4141
let disable_pre = matches.is_present("DISABLE_PRE");
4242

4343
let contents = fs::read_to_string(source).expect("Cirru file for instructions");
44-
let code = parse(&contents).expect("Some Cirru content");
44+
let xs = parse(&contents).expect("Some Cirru content");
4545

46-
if let Cirru::List(xs) = code {
47-
let mut fns: Vec<CalxFunc> = vec![];
48-
for x in xs {
49-
if let Cirru::List(ys) = x {
50-
let f = parse_function(&ys)?;
51-
fns.push(f);
52-
} else {
53-
panic!("expected top level expressions");
54-
}
46+
let mut fns: Vec<CalxFunc> = vec![];
47+
for x in xs {
48+
if let Cirru::List(ys) = x {
49+
let f = parse_function(&ys)?;
50+
fns.push(f);
51+
} else {
52+
panic!("expected top level expressions");
5553
}
54+
}
5655

57-
let mut imports: CalxImportsDict = HashMap::new();
58-
imports.insert(String::from("log"), (log_calx_value, 1));
59-
imports.insert(String::from("log2"), (log_calx_value, 2));
60-
imports.insert(String::from("log3"), (log_calx_value, 3));
56+
let mut imports: CalxImportsDict = HashMap::new();
57+
imports.insert(String::from("log"), (log_calx_value, 1));
58+
imports.insert(String::from("log2"), (log_calx_value, 2));
59+
imports.insert(String::from("log3"), (log_calx_value, 3));
6160

62-
let mut vm = CalxVM::new(fns, vec![], imports);
61+
let mut vm = CalxVM::new(fns, vec![], imports);
6362

64-
// if show_code {
65-
// for func in vm.funcs.to_owned() {
66-
// println!("loaded fn: {}", func);
67-
// }
68-
// }
63+
// if show_code {
64+
// for func in vm.funcs.to_owned() {
65+
// println!("loaded fn: {}", func);
66+
// }
67+
// }
6968

70-
let now = Instant::now();
71-
if !disable_pre {
72-
vm.preprocess()?;
73-
} else {
74-
println!("Preprocess disabled.")
75-
}
69+
let now = Instant::now();
70+
if !disable_pre {
71+
vm.preprocess()?;
72+
} else {
73+
println!("Preprocess disabled.")
74+
}
7675

77-
if show_code {
78-
for func in vm.funcs.to_owned() {
79-
println!("loaded fn: {}", func);
80-
}
76+
if show_code {
77+
for func in vm.funcs.to_owned() {
78+
println!("loaded fn: {}", func);
8179
}
80+
}
8281

83-
match vm.run() {
84-
Ok(()) => {
85-
let elapsed = now.elapsed();
82+
match vm.run() {
83+
Ok(()) => {
84+
let elapsed = now.elapsed();
8685

87-
println!("Took {:.3?}: {:?}", elapsed, vm.stack);
88-
Ok(())
89-
}
90-
Err(e) => {
91-
println!("VM state: {:?}", vm.stack);
92-
println!("{}", e);
93-
Err(String::from("Failed to run."))
94-
}
86+
println!("Took {:.3?}: {:?}", elapsed, vm.stack);
87+
Ok(())
88+
}
89+
Err(e) => {
90+
println!("VM state: {:?}", vm.stack);
91+
println!("{}", e);
92+
Err(String::from("Failed to run."))
9593
}
96-
} else {
97-
Err(String::from("TODO not cirru code"))
9894
}
9995
}
10096

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ mod parser;
22
mod primes;
33
mod vm;
44

5-
pub use parser::parse_function;
5+
pub use parser::{extract_nested, parse_function};
66
pub use primes::{Calx, CalxError, CalxFrame, CalxFunc};
77
pub use vm::{CalxImportsDict, CalxVM};

tests/parser_tests.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
use cirru_parser::Cirru;
2+
3+
use calx_vm::extract_nested;
4+
5+
/// extracting nested expression inside
6+
/// block and loop are special need to handle
7+
#[test]
8+
fn test_extracting() -> Result<(), String> {
9+
assert_eq!(
10+
Cirru::List(extract_nested(&Cirru::List(vec![
11+
Cirru::Leaf(String::from("a")),
12+
Cirru::Leaf(String::from("b"))
13+
]))?),
14+
Cirru::List(vec!(Cirru::List(vec![
15+
Cirru::Leaf(String::from("a")),
16+
Cirru::Leaf(String::from("b"))
17+
])))
18+
);
19+
20+
assert_eq!(
21+
Cirru::List(extract_nested(&Cirru::List(vec![
22+
Cirru::Leaf(String::from("a")),
23+
Cirru::Leaf(String::from("b")),
24+
Cirru::List(vec![
25+
Cirru::Leaf(String::from("c")),
26+
Cirru::Leaf(String::from("d")),
27+
])
28+
]))?),
29+
Cirru::List(vec!(
30+
Cirru::List(vec![
31+
Cirru::Leaf(String::from("c")),
32+
Cirru::Leaf(String::from("d"))
33+
]),
34+
Cirru::List(vec![
35+
Cirru::Leaf(String::from("a")),
36+
Cirru::Leaf(String::from("b"))
37+
])
38+
))
39+
);
40+
41+
assert_eq!(
42+
Cirru::List(extract_nested(&Cirru::List(vec![
43+
Cirru::Leaf(String::from("a")),
44+
Cirru::Leaf(String::from("b")),
45+
Cirru::List(vec![
46+
Cirru::Leaf(String::from("c")),
47+
Cirru::Leaf(String::from("d")),
48+
Cirru::List(vec![
49+
Cirru::Leaf(String::from("e")),
50+
Cirru::Leaf(String::from("f")),
51+
])
52+
])
53+
]))?),
54+
Cirru::List(vec!(
55+
Cirru::List(vec![
56+
Cirru::Leaf(String::from("e")),
57+
Cirru::Leaf(String::from("f"))
58+
]),
59+
Cirru::List(vec![
60+
Cirru::Leaf(String::from("c")),
61+
Cirru::Leaf(String::from("d"))
62+
]),
63+
Cirru::List(vec![
64+
Cirru::Leaf(String::from("a")),
65+
Cirru::Leaf(String::from("b"))
66+
])
67+
))
68+
);
69+
70+
assert_eq!(
71+
Cirru::List(extract_nested(&Cirru::List(vec![
72+
Cirru::Leaf(String::from("block")),
73+
Cirru::List(vec![
74+
Cirru::Leaf(String::from("c")),
75+
Cirru::Leaf(String::from("d")),
76+
Cirru::List(vec![
77+
Cirru::Leaf(String::from("e")),
78+
Cirru::Leaf(String::from("f")),
79+
])
80+
])
81+
]))?),
82+
Cirru::List(vec!(Cirru::List(vec![
83+
Cirru::Leaf(String::from("block")),
84+
Cirru::List(vec![
85+
Cirru::Leaf(String::from("e")),
86+
Cirru::Leaf(String::from("f"))
87+
]),
88+
Cirru::List(vec![
89+
Cirru::Leaf(String::from("c")),
90+
Cirru::Leaf(String::from("d"))
91+
])
92+
]),))
93+
);
94+
95+
assert_eq!(
96+
Cirru::List(extract_nested(&Cirru::List(vec![
97+
Cirru::Leaf(String::from("loop")),
98+
Cirru::List(vec![
99+
Cirru::Leaf(String::from("c")),
100+
Cirru::Leaf(String::from("d")),
101+
Cirru::List(vec![
102+
Cirru::Leaf(String::from("e")),
103+
Cirru::Leaf(String::from("f")),
104+
])
105+
])
106+
]))?),
107+
Cirru::List(vec!(Cirru::List(vec![
108+
Cirru::Leaf(String::from("loop")),
109+
Cirru::List(vec![
110+
Cirru::Leaf(String::from("e")),
111+
Cirru::Leaf(String::from("f"))
112+
]),
113+
Cirru::List(vec![
114+
Cirru::Leaf(String::from("c")),
115+
Cirru::Leaf(String::from("d"))
116+
])
117+
]),))
118+
);
119+
120+
Ok(())
121+
}

0 commit comments

Comments
 (0)