-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.rs
61 lines (52 loc) · 1.4 KB
/
stats.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
use std::fs;
use nolana::{
allocator::Allocator,
ast::{CallExpression, CallKind, Program},
parser::{Parser, ParserReturn},
visit::{walk::walk_call_expression, Visit},
};
#[derive(Debug)]
struct MolangStats {
pub math_functions: u32,
pub queries: u32,
}
impl MolangStats {
pub fn new(program: &Program) -> Self {
let mut stats = Self {
math_functions: 0,
queries: 0,
};
stats.visit_program(program);
stats
}
}
impl<'a> Visit<'a> for MolangStats {
fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
match it.kind {
CallKind::Math => self.math_functions += 1,
CallKind::Query => self.queries += 1,
}
walk_call_expression(self, it);
}
}
fn main() {
let source_text = fs::read_to_string("examples/sample.molang").unwrap();
let allocator = Allocator::default();
let ParserReturn {
program,
errors,
panicked,
} = Parser::new(&allocator, &source_text).parse();
if !errors.is_empty() {
for error in errors {
let error = error.with_source_code(source_text.clone());
print!("{error:?}");
}
if panicked {
println!("The encountered errors were unrecoverable");
}
return;
}
let molang_stats = MolangStats::new(&program);
println!("{molang_stats:?}");
}