1
0
dust/dust-lang/src/analyzer.rs

148 lines
4.2 KiB
Rust
Raw Normal View History

2024-08-05 18:31:08 +00:00
use crate::{Node, Statement};
2024-08-05 03:11:04 +00:00
2024-08-05 04:40:51 +00:00
pub fn analyze(abstract_tree: Vec<Node>) -> Result<(), AnalyzerError> {
let analyzer = Analyzer::new(abstract_tree);
analyzer.analyze()
}
2024-08-05 03:11:04 +00:00
pub struct Analyzer {
abstract_tree: Vec<Node>,
}
impl Analyzer {
pub fn new(abstract_tree: Vec<Node>) -> Self {
Analyzer { abstract_tree }
}
pub fn analyze(&self) -> Result<(), AnalyzerError> {
for node in &self.abstract_tree {
self.analyze_node(node)?;
}
Ok(())
}
fn analyze_node(&self, node: &Node) -> Result<(), AnalyzerError> {
2024-08-05 04:40:51 +00:00
match &node.statement {
Statement::Add(left, right) => {
self.analyze_node(&left)?;
self.analyze_node(&right)?;
2024-08-05 03:11:04 +00:00
}
2024-08-05 04:40:51 +00:00
Statement::Assign(left, right) => {
if let Statement::Identifier(_) = &left.statement {
// Identifier is in the correct position
2024-08-05 03:11:04 +00:00
} else {
return Err(AnalyzerError::ExpectedIdentifier {
2024-08-05 04:40:51 +00:00
actual: left.as_ref().clone(),
2024-08-05 03:11:04 +00:00
});
}
2024-08-05 04:40:51 +00:00
self.analyze_node(&right)?;
2024-08-05 03:11:04 +00:00
}
Statement::Constant(_) => {}
2024-08-05 04:40:51 +00:00
Statement::Identifier(_) => {
return Err(AnalyzerError::UnexpectedIdentifier {
identifier: node.clone(),
});
}
Statement::List(statements) => {
for statement in statements {
self.analyze_node(statement)?;
2024-08-05 03:11:04 +00:00
}
}
2024-08-05 04:40:51 +00:00
Statement::Multiply(left, right) => {
self.analyze_node(&left)?;
self.analyze_node(&right)?;
2024-08-05 03:11:04 +00:00
}
2024-08-05 18:31:08 +00:00
Statement::PropertyAccess(left, right) => {
if let Statement::Identifier(_) = &left.statement {
// Identifier is in the correct position
} else {
return Err(AnalyzerError::ExpectedIdentifier {
actual: left.as_ref().clone(),
});
}
self.analyze_node(&right)?;
}
Statement::ReservedIdentifier(_) => {}
2024-08-05 03:11:04 +00:00
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnalyzerError {
ExpectedIdentifier { actual: Node },
2024-08-05 04:40:51 +00:00
UnexpectedIdentifier { identifier: Node },
2024-08-05 03:11:04 +00:00
}
#[cfg(test)]
mod tests {
2024-08-05 04:40:51 +00:00
use crate::{Identifier, Value};
2024-08-05 03:11:04 +00:00
use super::*;
#[test]
2024-08-05 04:40:51 +00:00
fn assignment_expect_identifier() {
2024-08-05 03:11:04 +00:00
let abstract_tree = vec![Node::new(
2024-08-05 04:40:51 +00:00
Statement::Assign(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(Statement::Constant(Value::integer(2)), (1, 2))),
),
(0, 2),
2024-08-05 03:11:04 +00:00
)];
let analyzer = Analyzer::new(abstract_tree);
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::ExpectedIdentifier {
actual: Node::new(Statement::Constant(Value::integer(1)), (0, 1))
})
)
}
2024-08-05 04:40:51 +00:00
#[test]
fn unexpected_identifier_simple() {
let abstract_tree = vec![Node::new(
Statement::Identifier(Identifier::new("x")),
(0, 1),
)];
let analyzer = Analyzer::new(abstract_tree);
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::UnexpectedIdentifier {
identifier: Node::new(Statement::Identifier(Identifier::new("x")), (0, 1))
})
)
}
#[test]
fn unexpected_identifier_nested() {
let abstract_tree = vec![Node::new(
Statement::Add(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(
Statement::Identifier(Identifier::new("x")),
(1, 2),
)),
),
(0, 1),
)];
let analyzer = Analyzer::new(abstract_tree);
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::UnexpectedIdentifier {
identifier: Node::new(Statement::Identifier(Identifier::new("x")), (1, 2))
})
)
}
2024-08-05 03:11:04 +00:00
}