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

661 lines
22 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::{
error::Error,
fmt::{self, Display, Formatter},
};
2024-08-07 15:57:15 +00:00
2024-08-09 08:23:02 +00:00
use crate::{
2024-08-11 23:00:37 +00:00
abstract_tree::BinaryOperator, parse, AbstractSyntaxTree, Context, DustError, Node, Span,
Statement, Type,
2024-08-09 08:23:02 +00:00
};
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";
2024-08-11 23:00:37 +00:00
/// let result = analyze(input);
2024-08-07 15:57:15 +00:00
///
/// assert!(result.is_err());
/// ```
2024-08-11 23:00:37 +00:00
pub fn analyze(source: &str) -> Result<(), DustError> {
2024-08-11 21:59:52 +00:00
let abstract_tree = parse(source)?;
2024-08-11 23:00:37 +00:00
let mut context = Context::new();
let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
2024-08-11 21:59:52 +00:00
analyzer
.analyze()
.map_err(|analyzer_error| DustError::AnalyzerError {
analyzer_error,
source,
})
2024-08-05 04:40:51 +00:00
}
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();
2024-08-10 08:32:27 +00:00
/// let mut context = Context::new();
/// let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
2024-08-07 16:13:49 +00:00
/// let result = analyzer.analyze();
///
/// assert!(result.is_err());
pub struct Analyzer<'a> {
abstract_tree: &'a AbstractSyntaxTree,
context: &'a mut Context,
2024-08-05 03:11:04 +00:00
}
impl<'a> Analyzer<'a> {
pub fn new(abstract_tree: &'a AbstractSyntaxTree, context: &'a mut Context) -> Self {
2024-08-07 15:57:15 +00:00
Self {
abstract_tree,
context,
2024-08-07 15:57:15 +00:00
}
2024-08-05 03:11:04 +00:00
}
pub fn analyze(&mut 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(&mut self, node: &Node<Statement>) -> Result<(), AnalyzerError> {
match &node.inner {
2024-08-09 08:23:02 +00:00
Statement::BinaryOperation {
left,
operator,
right,
} => {
if let BinaryOperator::Assign = operator.inner {
if let Statement::Identifier(identifier) = &left.inner {
2024-08-10 00:24:18 +00:00
self.analyze_node(right)?;
let right_type = right.inner.expected_type(self.context);
self.context.set_type(
identifier.clone(),
right_type.ok_or(AnalyzerError::ExpectedValue {
actual: right.as_ref().clone(),
})?,
);
2024-08-09 22:14:46 +00:00
return Ok(());
}
}
2024-08-10 00:24:18 +00:00
self.analyze_node(left)?;
self.analyze_node(right)?;
let left_type = left.inner.expected_type(self.context);
let right_type = right.inner.expected_type(self.context);
2024-08-09 08:56:24 +00:00
if let BinaryOperator::Add
| BinaryOperator::Subtract
| BinaryOperator::Multiply
| BinaryOperator::Divide
| BinaryOperator::Greater
| BinaryOperator::GreaterOrEqual
| BinaryOperator::Less
| BinaryOperator::LessOrEqual = operator.inner
{
2024-08-11 23:00:37 +00:00
if let Some(expected_type) = left_type {
if let Some(actual_type) = right_type {
expected_type.check(&actual_type).map_err(|conflict| {
AnalyzerError::TypeConflict {
actual_statement: right.as_ref().clone(),
actual_type: conflict.actual,
expected: conflict.expected,
}
})?;
} else {
return Err(AnalyzerError::ExpectedValue {
2024-08-09 08:56:24 +00:00
actual: right.as_ref().clone(),
});
}
}
}
}
2024-08-09 15:41:23 +00:00
Statement::Block(statements) => {
for statement in statements {
self.analyze_node(statement)?;
}
}
2024-08-11 22:11:59 +00:00
Statement::BuiltInFunctionCall {
function,
value_arguments,
..
} => {
let value_parameters = function.value_parameters();
if let Some(arguments) = value_arguments {
for argument in arguments {
self.analyze_node(argument)?;
}
if arguments.len() != value_parameters.len() {
return Err(AnalyzerError::ExpectedValueArgumentCount {
expected: value_parameters.len(),
actual: arguments.len(),
position: node.position,
});
}
2024-08-11 23:00:37 +00:00
for ((_identifier, parameter_type), argument) in
value_parameters.iter().zip(arguments)
{
let argument_type_option = argument.inner.expected_type(self.context);
if let Some(argument_type) = argument_type_option {
parameter_type.check(&argument_type).map_err(|conflict| {
AnalyzerError::TypeConflict {
actual_statement: argument.clone(),
actual_type: conflict.actual,
expected: parameter_type.clone(),
}
})?;
} else {
return Err(AnalyzerError::ExpectedValue {
actual: argument.clone(),
});
}
}
if arguments.is_empty() && !value_parameters.is_empty() {
return Err(AnalyzerError::ExpectedValueArgumentCount {
expected: value_parameters.len(),
actual: 0,
position: node.position,
});
}
} else if !value_parameters.is_empty() {
return Err(AnalyzerError::ExpectedValueArgumentCount {
expected: value_parameters.len(),
actual: 0,
position: node.position,
});
2024-08-11 22:11:59 +00:00
}
}
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-09 22:14:46 +00:00
Statement::Identifier(identifier) => {
2024-08-10 08:45:30 +00:00
let exists = self.context.add_allowed_use(identifier);
if !exists {
2024-08-09 22:14:46 +00:00
return Err(AnalyzerError::UndefinedVariable {
identifier: node.clone(),
});
}
2024-08-05 04:40:51 +00:00
}
Statement::If { condition, body } => {
self.analyze_node(condition)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
// Condition is valid
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.as_ref().clone(),
position: condition.position,
});
}
self.analyze_node(body)?;
}
Statement::IfElse {
condition,
if_body,
else_body,
} => {
self.analyze_node(condition)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
// Condition is valid
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.as_ref().clone(),
position: condition.position,
});
}
self.analyze_node(if_body)?;
self.analyze_node(else_body)?;
}
Statement::IfElseIf {
condition,
if_body,
else_ifs,
} => {
self.analyze_node(condition)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
// Condition is valid
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.as_ref().clone(),
position: condition.position,
});
}
self.analyze_node(if_body)?;
for (condition, body) in else_ifs {
self.analyze_node(condition)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
// Condition is valid
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.clone(),
position: condition.position,
});
}
self.analyze_node(body)?;
}
}
Statement::IfElseIfElse {
condition,
if_body,
else_ifs,
else_body,
} => {
self.analyze_node(condition)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
// Condition is valid
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.as_ref().clone(),
position: condition.position,
});
}
self.analyze_node(if_body)?;
for (condition, body) in else_ifs {
self.analyze_node(condition)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
// Condition is valid
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.clone(),
position: condition.position,
});
}
self.analyze_node(body)?;
}
self.analyze_node(else_body)?;
}
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-09 10:09:59 +00:00
Statement::Map(properties) => {
for (_key, value_node) in properties {
self.analyze_node(value_node)?;
}
}
2024-08-10 09:23:43 +00:00
Statement::Nil(node) => {
self.analyze_node(node)?;
}
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-11 23:00:37 +00:00
self.analyze_node(left)?;
self.analyze_node(right)?;
2024-08-05 18:31:08 +00:00
}
2024-08-10 09:23:43 +00:00
Statement::While { condition, body } => {
self.analyze_node(condition)?;
self.analyze_node(body)?;
if let Some(Type::Boolean) = condition.inner.expected_type(self.context) {
} else {
return Err(AnalyzerError::ExpectedBoolean {
actual: condition.as_ref().clone(),
position: condition.position,
});
}
2024-08-09 15:41:23 +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,
},
2024-08-11 23:00:37 +00:00
ExpectedNumber {
actual: Node<Statement>,
position: Span,
},
2024-08-11 23:00:37 +00:00
ExpectedNumberOrString {
actual: Node<Statement>,
position: Span,
},
2024-08-09 08:23:02 +00:00
ExpectedString {
actual: Node<Statement>,
position: (usize, usize),
},
ExpectedValue {
actual: Node<Statement>,
},
2024-08-11 22:11:59 +00:00
ExpectedValueArgumentCount {
expected: usize,
actual: usize,
position: Span,
},
2024-08-11 23:00:37 +00:00
TypeConflict {
actual_statement: Node<Statement>,
actual_type: Type,
expected: Type,
},
2024-08-09 22:14:46 +00:00
UndefinedVariable {
identifier: Node<Statement>,
},
UnexpectedIdentifier {
identifier: Node<Statement>,
},
2024-08-09 08:23:02 +00:00
UnexectedString {
actual: Node<Statement>,
},
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-11 23:00:37 +00:00
AnalyzerError::ExpectedValue { actual } => actual.position,
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedInteger { position, .. } => *position,
2024-08-11 23:00:37 +00:00
AnalyzerError::ExpectedNumber { position, .. } => *position,
AnalyzerError::ExpectedNumberOrString { position, .. } => *position,
2024-08-09 08:23:02 +00:00
AnalyzerError::ExpectedString { position, .. } => *position,
2024-08-11 22:11:59 +00:00
AnalyzerError::ExpectedValueArgumentCount { position, .. } => *position,
2024-08-11 23:00:37 +00:00
AnalyzerError::TypeConflict {
actual_statement, ..
} => actual_statement.position,
2024-08-09 22:14:46 +00:00
AnalyzerError::UndefinedVariable { identifier } => identifier.position,
2024-08-11 23:00:37 +00:00
AnalyzerError::UnexpectedIdentifier { identifier } => identifier.position,
AnalyzerError::UnexectedString { actual } => actual.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-11 23:00:37 +00:00
AnalyzerError::ExpectedNumber { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected integer or float, found {}", actual)
}
2024-08-11 23:00:37 +00:00
AnalyzerError::ExpectedNumberOrString { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected integer, float, or string, found {}", actual)
}
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-11 22:11:59 +00:00
AnalyzerError::ExpectedValueArgumentCount {
expected, actual, ..
} => write!(f, "Expected {} value arguments, found {}", expected, actual),
2024-08-11 23:00:37 +00:00
AnalyzerError::TypeConflict {
actual_statement,
actual_type,
expected,
} => {
write!(
f,
"Expected type {}, found {}, which has type {}",
expected, actual_statement, actual_type
)
}
2024-08-09 22:14:46 +00:00
AnalyzerError::UndefinedVariable { identifier } => {
write!(f, "Undefined variable {}", identifier)
}
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-11 21:59:52 +00:00
#[test]
2024-08-11 23:00:37 +00:00
fn is_even_wrong_type() {
let source = "is_even('hello')";
assert_eq!(
analyze(source),
Err(DustError::AnalyzerError {
analyzer_error: AnalyzerError::ExpectedInteger {
actual: Node::new(Statement::Constant(Value::string("hello")), (1, 1)),
position: (1, 1)
2024-08-11 21:59:52 +00:00
},
2024-08-11 23:00:37 +00:00
source
})
);
}
#[test]
fn length_no_arguments() {
let source = "length()";
2024-08-11 21:59:52 +00:00
assert_eq!(
2024-08-11 23:00:37 +00:00
analyze(source),
Err(DustError::AnalyzerError {
analyzer_error: AnalyzerError::ExpectedValueArgumentCount {
expected: 1,
actual: 0,
position: (0, 6)
},
source
2024-08-11 21:59:52 +00:00
})
2024-08-11 23:00:37 +00:00
);
2024-08-11 21:59:52 +00:00
}
2024-08-08 17:01:25 +00:00
#[test]
2024-08-09 08:56:24 +00:00
fn float_plus_integer() {
2024-08-11 23:00:37 +00:00
let source = "42.0 + 2";
assert_eq!(
2024-08-11 23:00:37 +00:00
analyze(source),
Err(DustError::AnalyzerError {
analyzer_error: AnalyzerError::TypeConflict {
actual_statement: Node::new(Statement::Constant(Value::integer(2)), (7, 8)),
actual_type: Type::Integer,
expected: Type::Float,
},
source
})
)
}
#[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 mut context = Context::new();
let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
2024-08-08 17:01:25 +00:00
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 mut context = Context::new();
let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
2024-08-08 17:01:25 +00:00
assert_eq!(
analyzer.analyze(),
2024-08-11 23:00:37 +00:00
Err(AnalyzerError::ExpectedNumber {
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(),
};
let mut context = Context::new();
let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
2024-08-05 03:11:04 +00:00
assert_eq!(
analyzer.analyze(),
2024-08-11 23:00:37 +00:00
Err(AnalyzerError::ExpectedNumber {
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]
2024-08-09 22:14:46 +00:00
fn undefined_variable() {
2024-08-07 15:57:15 +00:00
let abstract_tree = AbstractSyntaxTree {
nodes: [Node::new(
Statement::Identifier(Identifier::new("x")),
(0, 1),
)]
.into(),
};
let mut context = Context::new();
let mut analyzer = Analyzer::new(&abstract_tree, &mut context);
2024-08-05 04:40:51 +00:00
assert_eq!(
analyzer.analyze(),
2024-08-09 22:14:46 +00:00
Err(AnalyzerError::UndefinedVariable {
identifier: Node::new(Statement::Identifier(Identifier::new("x")), (0, 1))
2024-08-05 04:40:51 +00:00
})
)
}
2024-08-05 03:11:04 +00:00
}