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 std::path::Path;
use std::rc::Rc;
pub mod compiler;
pub mod tokenizer;
pub mod vm;
pub mod typer;
mod error;
use error::Error;
use tokenizer::TokenStream;
pub fn run_file(path: &Path, print: bool) -> Result<(), Vec<Error>> {
run(tokenizer::file_to_tokens(path), path, print)
}
pub fn run_string(s: &str, print: bool) -> Result<(), Vec<Error>> {
run(tokenizer::string_to_tokens(s), Path::new("builtin"), print)
}
pub fn run(tokens: TokenStream, path: &Path, print: bool) -> Result<(), Vec<Error>> {
match compiler::compile("main", path, tokens) {
Ok(block) =>
vm::VM::new().print_blocks(print)
.print_ops(print)
.run(Rc::new(block)).or_else(|e| Err(vec![e])),
Err(errors) => Err(errors),
}
}
#[cfg(test)]
mod tests {
use super::{run_file, run_string};
use std::path::Path;
#[macro_export]
macro_rules! assert_errs {
($result:expr, [ $( $kind:pat ),* ]) => {
println!("{} => {:?}", stringify!($result), $result);
assert!(matches!(
$result.unwrap_err().as_slice(),
&[$($crate::error::Error {
kind: $kind,
file: _,
line: _,
message: _,
},
)*]
))
};
}
#[macro_export]
macro_rules! test_string {
($fn:ident, $prog:literal) => {
#[test]
fn $fn() {
$crate::run_string($prog, true).unwrap();
}
};
($fn:ident, $prog:literal, $errs:tt) => {
#[test]
fn $fn() {
$crate::assert_errs!($crate::run_string($prog, true), $errs);
}
}
}
#[macro_export]
macro_rules! test_file {
($fn:ident, $path:literal) => {
#[test]
fn $fn() {
let file = Path::new($path);
run_file(&file, true).unwrap();
}
};
}
use crate::error::ErrorKind;
#[test]
fn unreachable_token() {
assert_errs!(run_string("<!>\n", true), [ErrorKind::Unreachable]);
}
test_file!(order_of_operations, "tests/order-of-operations.tdy");
test_file!(variables, "tests/variables.tdy");
test_file!(scoping, "tests/scoping.tdy");
test_file!(if_, "tests/if.tdy");
test_file!(for_, "tests/for.tdy");
test_file!(fun, "tests/fun.tdy");
}
|