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

451 lines
15 KiB
Rust
Raw Normal View History

2024-08-09 02:44:34 +00:00
//! Tools for analyzing an abstract syntax tree and catch errors before running the virtual
//! machine.
//!
//! This module provides to anlysis options, both of which borrow an abstract syntax tree and a
//! hash map of variables:
//! - `analyze` convenience function
//! - `Analyzer` struct
2024-08-09 00:58:56 +00:00
use std::{
collections::HashMap,
error::Error,
fmt::{self, Display, Formatter},
};
2024-08-07 15:57:15 +00:00
2024-08-09 08:23:02 +00:00
use crate::{
abstract_tree::BinaryOperator, AbstractSyntaxTree, BuiltInFunction, Identifier, Node, Span,
Statement, Type, Value,
};
2024-08-07 15:57:15 +00:00
/// Analyzes the abstract syntax tree for errors.
///
/// # Examples
/// ```
/// # use std::collections::HashMap;
/// # use dust_lang::*;
/// let input = "x = 1 + false";
/// let abstract_tree = parse(input).unwrap();
/// let variables = HashMap::new();
/// let result = analyze(&abstract_tree, &variables);
///
/// assert!(result.is_err());
/// ```
pub fn analyze(
abstract_tree: &AbstractSyntaxTree,
2024-08-07 15:57:15 +00:00
variables: &HashMap<Identifier, Value>,
) -> Result<(), AnalyzerError> {
2024-08-07 15:57:15 +00:00
let analyzer = Analyzer::new(abstract_tree, variables);
2024-08-05 04:40:51 +00:00
analyzer.analyze()
}
2024-08-07 16:13:49 +00:00
/// Static analyzer that checks for potential runtime errors.
///
/// # Examples
/// ```
/// # use std::collections::HashMap;
/// # use dust_lang::*;
/// let input = "x = 1 + false";
/// let abstract_tree = parse(input).unwrap();
/// let variables = HashMap::new();
/// let analyzer = Analyzer::new(&abstract_tree, &variables);
/// let result = analyzer.analyze();
///
/// assert!(result.is_err());
pub struct Analyzer<'a> {
abstract_tree: &'a AbstractSyntaxTree,
2024-08-07 15:57:15 +00:00
variables: &'a HashMap<Identifier, Value>,
2024-08-05 03:11:04 +00:00
}
impl<'a> Analyzer<'a> {
2024-08-07 15:57:15 +00:00
pub fn new(
abstract_tree: &'a AbstractSyntaxTree,
2024-08-07 15:57:15 +00:00
variables: &'a HashMap<Identifier, Value>,
) -> Self {
Self {
abstract_tree,
variables,
}
2024-08-05 03:11:04 +00:00
}
pub fn analyze(&self) -> Result<(), AnalyzerError> {
2024-08-07 15:38:08 +00:00
for node in &self.abstract_tree.nodes {
2024-08-05 03:11:04 +00:00
self.analyze_node(node)?;
}
Ok(())
}
fn analyze_node(&self, node: &Node<Statement>) -> Result<(), AnalyzerError> {
match &node.inner {
2024-08-09 08:23:02 +00:00
Statement::Assignment {
2024-08-09 08:56:24 +00:00
value_node: value, ..
2024-08-09 08:23:02 +00:00
} => {
if let None = value.inner.expected_type(self.variables) {
return Err(AnalyzerError::ExpectedValue {
actual: value.as_ref().clone(),
position: value.position,
2024-08-05 03:11:04 +00:00
});
}
}
2024-08-09 08:23:02 +00:00
Statement::BinaryOperation {
left,
operator,
right,
} => {
self.analyze_node(left)?;
self.analyze_node(right)?;
2024-08-09 08:56:24 +00:00
let left_type = left.inner.expected_type(self.variables);
let right_type = right.inner.expected_type(self.variables);
if let BinaryOperator::Add
| BinaryOperator::Subtract
| BinaryOperator::Multiply
| BinaryOperator::Divide
| BinaryOperator::Greater
| BinaryOperator::GreaterOrEqual
| BinaryOperator::Less
| BinaryOperator::LessOrEqual = operator.inner
{
match (left_type, right_type) {
(Some(Type::Integer), Some(Type::Integer)) => {}
(Some(Type::Float), Some(Type::Float)) => {}
(Some(Type::String), Some(Type::String)) => {}
(Some(Type::Integer), _) => {
return Err(AnalyzerError::ExpectedInteger {
actual: right.as_ref().clone(),
position: right.position,
});
}
(Some(Type::Float), _) => {
return Err(AnalyzerError::ExpectedFloat {
actual: right.as_ref().clone(),
position: right.position,
});
}
(Some(Type::String), _) => {
return Err(AnalyzerError::ExpectedString {
actual: right.as_ref().clone(),
position: right.position,
});
}
(_, _) => {
return Err(AnalyzerError::ExpectedIntegerFloatOrString {
actual: right.as_ref().clone(),
position: right.position,
})
}
}
}
}
2024-08-09 08:23:02 +00:00
Statement::BuiltInFunctionCall { .. } => {}
2024-08-05 03:11:04 +00:00
Statement::Constant(_) => {}
Statement::FunctionCall { function, .. } => {
if let Statement::Identifier(_) = &function.inner {
// Function is in the correct position
} else {
return Err(AnalyzerError::ExpectedIdentifier {
actual: function.as_ref().clone(),
2024-08-09 01:47:49 +00:00
position: function.position,
});
}
}
2024-08-05 04:40:51 +00:00
Statement::Identifier(_) => {
return Err(AnalyzerError::UnexpectedIdentifier {
identifier: node.clone(),
2024-08-09 01:47:49 +00:00
position: node.position,
2024-08-05 04:40:51 +00:00
});
}
Statement::List(statements) => {
for statement in statements {
self.analyze_node(statement)?;
2024-08-05 03:11:04 +00:00
}
}
2024-08-05 18:31:08 +00:00
Statement::PropertyAccess(left, right) => {
2024-08-07 15:57:15 +00:00
if let Statement::Identifier(_) | Statement::Constant(_) | Statement::List(_) =
&left.inner
2024-08-07 15:57:15 +00:00
{
// Left side is valid
2024-08-05 18:31:08 +00:00
} else {
2024-08-09 08:23:02 +00:00
return Err(AnalyzerError::ExpectedValue {
2024-08-05 18:31:08 +00:00
actual: left.as_ref().clone(),
2024-08-09 01:47:49 +00:00
position: left.position,
2024-08-05 18:31:08 +00:00
});
}
if let Statement::BuiltInFunctionCall { function, .. } = &right.inner {
2024-08-08 17:01:25 +00:00
if function == &BuiltInFunction::IsEven || function == &BuiltInFunction::IsOdd {
if let Some(Type::Integer) = left.inner.expected_type(self.variables) {
2024-08-08 17:01:25 +00:00
} else {
return Err(AnalyzerError::ExpectedIntegerOrFloat {
actual: left.as_ref().clone(),
2024-08-09 01:47:49 +00:00
position: left.position,
2024-08-08 17:01:25 +00:00
});
}
}
}
self.analyze_node(right)?;
2024-08-05 18:31:08 +00:00
}
2024-08-05 03:11:04 +00:00
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnalyzerError {
ExpectedBoolean {
actual: Node<Statement>,
position: Span,
},
2024-08-09 08:23:02 +00:00
ExpectedFloat {
actual: Node<Statement>,
position: (usize, usize),
},
ExpectedFunction {
actual: Node<Statement>,
position: Span,
},
ExpectedIdentifier {
actual: Node<Statement>,
position: Span,
},
2024-08-09 08:23:02 +00:00
ExpectedInteger {
actual: Node<Statement>,
position: Span,
},
ExpectedIntegerOrFloat {
actual: Node<Statement>,
position: Span,
},
ExpectedIntegerFloatOrString {
actual: Node<Statement>,
position: Span,
},
2024-08-09 08:56:24 +00:00
ExpectedSameType {
left: Node<Statement>,
right: Node<Statement>,
position: Span,
},
2024-08-09 08:23:02 +00:00
ExpectedString {
actual: Node<Statement>,
position: (usize, usize),
},
ExpectedValue {
actual: Node<Statement>,
position: Span,
},
UnexpectedIdentifier {
identifier: Node<Statement>,
position: Span,
},
2024-08-09 08:23:02 +00:00
UnexectedString {
actual: Node<Statement>,
position: (usize, usize),
},
2024-08-05 03:11:04 +00:00
}
2024-08-09 05:43:58 +00:00
impl AnalyzerError {
pub fn position(&self) -> Span {
match self {
AnalyzerError::ExpectedBoolean { position, .. } => *position,
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedFloat { position, .. } => *position,
2024-08-09 05:43:58 +00:00
AnalyzerError::ExpectedFunction { position, .. } => *position,
AnalyzerError::ExpectedIdentifier { position, .. } => *position,
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedValue { position, .. } => *position,
AnalyzerError::ExpectedInteger { position, .. } => *position,
2024-08-09 05:43:58 +00:00
AnalyzerError::ExpectedIntegerOrFloat { position, .. } => *position,
AnalyzerError::ExpectedIntegerFloatOrString { position, .. } => *position,
2024-08-09 08:56:24 +00:00
AnalyzerError::ExpectedSameType { position, .. } => *position,
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedString { position, .. } => *position,
2024-08-09 05:43:58 +00:00
AnalyzerError::UnexpectedIdentifier { position, .. } => *position,
2024-08-09 08:23:02 +00:00
AnalyzerError::UnexectedString { position, .. } => *position,
2024-08-09 05:43:58 +00:00
}
}
}
2024-08-09 00:58:56 +00:00
impl Error for AnalyzerError {}
impl Display for AnalyzerError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
2024-08-09 01:47:49 +00:00
AnalyzerError::ExpectedBoolean { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected boolean, found {}", actual)
}
2024-08-09 01:47:49 +00:00
AnalyzerError::ExpectedFunction { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected function, found {}", actual)
}
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedFloat { actual, .. } => {
write!(f, "Expected float, found {}", actual)
}
2024-08-09 01:47:49 +00:00
AnalyzerError::ExpectedIdentifier { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected identifier, found {}", actual)
}
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedInteger { actual, .. } => {
write!(f, "Expected integer, found {}", actual)
2024-08-09 00:58:56 +00:00
}
2024-08-09 01:47:49 +00:00
AnalyzerError::ExpectedIntegerOrFloat { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected integer or float, found {}", actual)
}
2024-08-09 01:47:49 +00:00
AnalyzerError::ExpectedIntegerFloatOrString { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected integer, float, or string, found {}", actual)
}
2024-08-09 08:56:24 +00:00
AnalyzerError::ExpectedSameType { left, right, .. } => {
write!(f, "Expected same type, found {} and {}", left, right)
}
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedString { actual, .. } => {
write!(f, "Expected string, found {}", actual)
}
AnalyzerError::ExpectedValue { actual, .. } => {
write!(f, "Expected value, found {}", actual)
}
2024-08-09 01:47:49 +00:00
AnalyzerError::UnexpectedIdentifier { identifier, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Unexpected identifier {}", identifier)
}
2024-08-09 08:23:02 +00:00
AnalyzerError::UnexectedString { actual, .. } => {
write!(f, "Unexpected string {}", actual)
}
2024-08-09 00:58:56 +00:00
}
}
}
2024-08-05 03:11:04 +00:00
#[cfg(test)]
mod tests {
2024-08-08 17:01:25 +00:00
use crate::{BuiltInFunction, Identifier, Value};
2024-08-05 03:11:04 +00:00
use super::*;
2024-08-08 17:01:25 +00:00
#[test]
2024-08-09 08:56:24 +00:00
fn float_plus_integer() {
2024-08-08 17:01:25 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
2024-08-09 08:23:02 +00:00
Statement::BinaryOperation {
2024-08-09 08:56:24 +00:00
left: Box::new(Node::new(Statement::Constant(Value::float(1.0)), (0, 1))),
2024-08-09 08:23:02 +00:00
operator: Node::new(BinaryOperator::Add, (1, 2)),
2024-08-09 08:56:24 +00:00
right: Box::new(Node::new(Statement::Constant(Value::integer(1)), (3, 4))),
2024-08-09 08:23:02 +00:00
},
2024-08-09 08:56:24 +00:00
(0, 2),
)]
.into(),
};
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
assert_eq!(
analyzer.analyze(),
2024-08-09 08:56:24 +00:00
Err(AnalyzerError::ExpectedFloat {
actual: Node::new(Statement::Constant(Value::integer(1)), (3, 4)),
position: (3, 4)
})
)
}
#[test]
2024-08-09 08:56:24 +00:00
fn integer_plus_boolean() {
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
2024-08-09 08:23:02 +00:00
Statement::BinaryOperation {
left: Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
operator: Node::new(BinaryOperator::Add, (1, 2)),
right: Box::new(Node::new(Statement::Constant(Value::boolean(true)), (3, 4))),
},
2024-08-08 17:01:25 +00:00
(0, 2),
)]
.into(),
};
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
assert_eq!(
analyzer.analyze(),
2024-08-09 08:56:24 +00:00
Err(AnalyzerError::ExpectedInteger {
actual: Node::new(Statement::Constant(Value::boolean(true)), (3, 4)),
position: (3, 4)
2024-08-08 17:01:25 +00:00
})
)
}
2024-08-08 17:01:25 +00:00
#[test]
fn is_even_expects_number() {
2024-08-08 17:01:25 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::PropertyAccess(
Box::new(Node::new(Statement::Constant(Value::boolean(true)), (0, 1))),
Box::new(Node::new(
Statement::BuiltInFunctionCall {
function: BuiltInFunction::IsEven,
2024-08-08 17:01:25 +00:00
type_arguments: None,
value_arguments: None,
},
(1, 2),
)),
),
(0, 2),
)]
.into(),
};
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::ExpectedIntegerOrFloat {
2024-08-09 01:47:49 +00:00
actual: Node::new(Statement::Constant(Value::boolean(true)), (0, 1)),
position: (0, 1)
2024-08-08 17:01:25 +00:00
})
)
}
2024-08-05 03:11:04 +00:00
#[test]
fn is_odd_expects_number() {
2024-08-07 15:38:08 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::PropertyAccess(
Box::new(Node::new(Statement::Constant(Value::boolean(true)), (0, 1))),
2024-08-07 15:57:15 +00:00
Box::new(Node::new(
Statement::BuiltInFunctionCall {
function: BuiltInFunction::IsOdd,
type_arguments: None,
value_arguments: None,
},
2024-08-07 15:57:15 +00:00
(1, 2),
)),
2024-08-07 15:38:08 +00:00
),
(0, 2),
)]
.into(),
};
2024-08-07 15:57:15 +00:00
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
2024-08-05 03:11:04 +00:00
assert_eq!(
analyzer.analyze(),
2024-08-07 15:57:15 +00:00
Err(AnalyzerError::ExpectedIntegerOrFloat {
2024-08-09 01:47:49 +00:00
actual: Node::new(Statement::Constant(Value::boolean(true)), (0, 1)),
position: (0, 1)
2024-08-05 03:11:04 +00:00
})
)
}
2024-08-05 04:40:51 +00:00
2024-08-07 15:57:15 +00:00
#[test]
fn unexpected_identifier() {
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Identifier(Identifier::new("x")),
(0, 1),
)]
.into(),
};
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
2024-08-05 04:40:51 +00:00
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::UnexpectedIdentifier {
2024-08-09 01:47:49 +00:00
identifier: Node::new(Statement::Identifier(Identifier::new("x")), (0, 1)),
position: (0, 1)
2024-08-05 04:40:51 +00:00
})
)
}
2024-08-05 03:11:04 +00:00
}