dust/dust-lang/src/analyzer.rs

504 lines
17 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-08 17:01:25 +00:00
use crate::{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-05 04:40:51 +00:00
Statement::Add(left, right) => {
self.analyze_node(left)?;
self.analyze_node(right)?;
let left_type = left.inner.expected_type(self.variables);
let right_type = right.inner.expected_type(self.variables);
2024-08-07 15:57:15 +00:00
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), _) | (Some(Type::Float), _) | (Some(Type::String), _) => {
return Err(AnalyzerError::ExpectedIntegerFloatOrString {
actual: right.as_ref().clone(),
2024-08-09 01:47:49 +00:00
position: right.position,
});
}
_ => {
return Err(AnalyzerError::ExpectedIntegerFloatOrString {
actual: left.as_ref().clone(),
2024-08-09 01:47:49 +00:00
position: left.position,
});
}
2024-08-07 15:57:15 +00:00
}
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.inner {
2024-08-05 04:40:51 +00:00
// 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-09 01:47:49 +00:00
position: left.position,
2024-08-05 03:11:04 +00:00
});
}
self.analyze_node(right)?;
2024-08-05 03:11:04 +00:00
}
Statement::BuiltInFunctionCall { .. } => {}
Statement::Comparison(left, _, right) => {
self.analyze_node(left)?;
self.analyze_node(right)?;
if let Some(Type::Integer) | Some(Type::Float) =
left.inner.expected_type(self.variables)
{
} else {
return Err(AnalyzerError::ExpectedIntegerOrFloat {
actual: left.as_ref().clone(),
position: left.position,
});
}
if let Some(Type::Integer) | Some(Type::Float) =
right.inner.expected_type(self.variables)
{
} else {
return Err(AnalyzerError::ExpectedIntegerOrFloat {
actual: right.as_ref().clone(),
position: right.position,
});
}
}
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 04:40:51 +00:00
Statement::Multiply(left, right) => {
self.analyze_node(left)?;
self.analyze_node(right)?;
2024-08-07 15:57:15 +00:00
if let Some(Type::Integer) | Some(Type::Float) =
left.inner.expected_type(self.variables)
2024-08-07 15:57:15 +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-07 15:57:15 +00:00
});
}
if let Some(Type::Integer) | Some(Type::Float) =
right.inner.expected_type(self.variables)
2024-08-07 15:57:15 +00:00
{
} else {
return Err(AnalyzerError::ExpectedIntegerOrFloat {
actual: right.as_ref().clone(),
2024-08-09 01:47:49 +00:00
position: right.position,
2024-08-07 15:57:15 +00:00
});
}
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-08 17:01:25 +00:00
return Err(AnalyzerError::ExpectedIdentifierOrValue {
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
}
Statement::Subtract(left, right) => {
self.analyze_node(left)?;
self.analyze_node(right)?;
let left_type = left.inner.expected_type(self.variables);
let right_type = right.inner.expected_type(self.variables);
match (left_type, right_type) {
(Some(Type::Integer), Some(Type::Integer)) => {}
(Some(Type::Float), Some(Type::Float)) => {}
(Some(Type::Integer), _) | (Some(Type::Float), _) => {
return Err(AnalyzerError::ExpectedIntegerOrFloat {
actual: right.as_ref().clone(),
position: right.position,
});
}
_ => {
return Err(AnalyzerError::ExpectedIntegerOrFloat {
actual: left.as_ref().clone(),
position: left.position,
});
}
}
}
2024-08-05 03:11:04 +00:00
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnalyzerError {
ExpectedBoolean {
actual: Node<Statement>,
position: Span,
},
ExpectedFunction {
actual: Node<Statement>,
position: Span,
},
ExpectedIdentifier {
actual: Node<Statement>,
position: Span,
},
ExpectedIdentifierOrValue {
actual: Node<Statement>,
position: Span,
},
ExpectedIntegerOrFloat {
actual: Node<Statement>,
position: Span,
},
ExpectedIntegerFloatOrString {
actual: Node<Statement>,
position: Span,
},
UnexpectedIdentifier {
identifier: Node<Statement>,
position: Span,
},
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,
AnalyzerError::ExpectedFunction { position, .. } => *position,
AnalyzerError::ExpectedIdentifier { position, .. } => *position,
AnalyzerError::ExpectedIdentifierOrValue { position, .. } => *position,
AnalyzerError::ExpectedIntegerOrFloat { position, .. } => *position,
AnalyzerError::ExpectedIntegerFloatOrString { position, .. } => *position,
AnalyzerError::UnexpectedIdentifier { position, .. } => *position,
}
}
}
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 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 01:47:49 +00:00
AnalyzerError::ExpectedIdentifierOrValue { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected identifier or value, found {}", actual)
}
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 01:47:49 +00:00
AnalyzerError::UnexpectedIdentifier { identifier, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Unexpected identifier {}", identifier)
}
}
}
}
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]
fn add_expects_same_types() {
2024-08-08 17:01:25 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Add(
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(Statement::Constant(Value::float(1.0)), (1, 2))),
),
(0, 2),
)]
.into(),
};
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::ExpectedIntegerFloatOrString {
2024-08-09 01:47:49 +00:00
actual: Node::new(Statement::Constant(Value::float(1.0)), (1, 2)),
position: (1, 2)
})
)
}
#[test]
fn add_expects_integer_float_or_string() {
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Add(
2024-08-08 17:01:25 +00:00
Box::new(Node::new(Statement::Constant(Value::boolean(true)), (0, 1))),
Box::new(Node::new(Statement::Constant(Value::integer(1)), (1, 2))),
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(),
Err(AnalyzerError::ExpectedIntegerFloatOrString {
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-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
#[test]
fn multiply_expect_integer_or_float() {
2024-08-07 15:38:08 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Multiply(
2024-08-07 15:57:15 +00:00
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
Box::new(Node::new(
Statement::Constant(Value::boolean(false)),
(1, 2),
)),
),
(0, 2),
2024-08-07 15:38:08 +00:00
)]
.into(),
};
2024-08-07 15:57:15 +00:00
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
2024-08-05 04:40:51 +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(false)), (1, 2)),
position: (1, 2)
2024-08-05 04:40:51 +00:00
})
)
}
#[test]
2024-08-07 15:57:15 +00:00
fn assignment_expect_identifier() {
2024-08-07 15:38:08 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
2024-08-07 15:57:15 +00:00
Statement::Assign(
2024-08-07 15:38:08 +00:00
Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))),
2024-08-07 15:57:15 +00:00
Box::new(Node::new(Statement::Constant(Value::integer(2)), (1, 2))),
2024-08-07 15:38:08 +00:00
),
2024-08-07 15:57:15 +00:00
(0, 2),
2024-08-07 15:38:08 +00:00
)]
.into(),
};
2024-08-07 15:57:15 +00:00
let variables = HashMap::new();
let analyzer = Analyzer::new(&abstract_tree, &variables);
2024-08-07 15:38:08 +00:00
2024-08-07 15:57:15 +00:00
assert_eq!(
analyzer.analyze(),
Err(AnalyzerError::ExpectedIdentifier {
2024-08-09 01:47:49 +00:00
actual: Node::new(Statement::Constant(Value::integer(1)), (0, 1)),
position: (0, 1)
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
}