dust/dust-lang/src/analyzer.rs

596 lines
19 KiB
Rust
Raw Normal View History

//! Tools for analyzing an abstract syntax tree and catching errors before running the virtual
2024-08-09 02:44:34 +00:00
//! machine.
//!
2024-08-12 12:54:21 +00:00
//! This module provides two anlysis options:
//! - `analyze` convenience function, which takes a string input
//! - `Analyzer` struct, which borrows an abstract syntax tree and a context
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-17 02:43:29 +00:00
ast::{
2024-08-17 03:18:05 +00:00
AbstractSyntaxTree, BlockExpression, CallExpression, ElseExpression, FieldAccessExpression,
IfExpression, LetStatement, ListExpression, ListIndexExpression, LoopExpression, Node,
OperatorExpression, RangeExpression, Statement, StructExpression, TupleAccessExpression,
2024-08-17 02:43:29 +00:00
},
parse, Context, DustError, Expression, Identifier, Span, 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-12 12:54:21 +00:00
let context = Context::new();
let mut analyzer = Analyzer::new(&abstract_tree, &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,
2024-08-12 12:54:21 +00:00
context: &'a Context,
2024-08-05 03:11:04 +00:00
}
impl<'a> Analyzer<'a> {
2024-08-12 12:54:21 +00:00
pub fn new(abstract_tree: &'a AbstractSyntaxTree, context: &'a 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> {
for statement in &self.abstract_tree.statements {
self.analyze_statement(statement)?;
2024-08-05 03:11:04 +00:00
}
Ok(())
}
fn analyze_statement(&mut self, statement: &Statement) -> Result<(), AnalyzerError> {
match statement {
Statement::Expression(expression) => self.analyze_expression(expression)?,
Statement::ExpressionNullified(expression_node) => {
self.analyze_expression(&expression_node.inner)?;
}
Statement::Let(let_statement) => match &let_statement.inner {
LetStatement::Let { identifier, value } => {
let type_option = value.return_type(self.context);
if let Some(r#type) = type_option {
self.context.set_type(
identifier.inner.clone(),
r#type,
identifier.position,
);
} else {
2024-08-17 02:43:29 +00:00
return Err(AnalyzerError::ExpectedValue {
actual: statement.clone(),
});
}
self.analyze_expression(value)?;
}
LetStatement::LetMut { identifier, value } => {
let type_option = value.return_type(self.context);
if let Some(r#type) = type_option {
self.context.set_type(
identifier.inner.clone(),
r#type,
identifier.position,
);
} else {
2024-08-17 02:43:29 +00:00
return Err(AnalyzerError::ExpectedValue {
actual: statement.clone(),
});
}
self.analyze_expression(value)?;
}
LetStatement::LetType {
identifier,
r#type,
value,
} => todo!(),
LetStatement::LetMutType {
identifier,
r#type,
value,
} => todo!(),
},
2024-08-17 03:18:05 +00:00
Statement::StructDefinition(_) => {}
}
Ok(())
}
fn analyze_expression(&mut self, expression: &Expression) -> Result<(), AnalyzerError> {
match expression {
2024-08-17 03:18:05 +00:00
Expression::Block(block_expression) => self.analyze_block(&block_expression.inner)?,
Expression::Call(call_expression) => {
let CallExpression { invoker, arguments } = call_expression.inner.as_ref();
self.analyze_expression(invoker)?;
for argument in arguments {
self.analyze_expression(argument)?;
}
}
Expression::FieldAccess(field_access_expression) => {
let FieldAccessExpression { container, .. } =
field_access_expression.inner.as_ref();
self.analyze_expression(container)?;
}
Expression::Grouped(expression) => {
self.analyze_expression(expression.inner.as_ref())?;
}
Expression::Identifier(identifier) => {
let found = self
.context
.update_last_position(&identifier.inner, identifier.position);
if !found {
return Err(AnalyzerError::UndefinedVariable {
identifier: identifier.clone(),
});
}
}
2024-08-17 03:18:05 +00:00
Expression::If(if_expression) => self.analyze_if(&if_expression.inner)?,
Expression::List(list_expression) => match list_expression.inner.as_ref() {
ListExpression::AutoFill {
repeat_operand,
length_operand,
} => {
self.analyze_expression(repeat_operand)?;
self.analyze_expression(length_operand)?;
}
ListExpression::Ordered(expressions) => {
for expression in expressions {
self.analyze_expression(expression)?;
}
}
},
Expression::ListIndex(list_index_expression) => {
let ListIndexExpression { list, index } = list_index_expression.inner.as_ref();
self.analyze_expression(list)?;
self.analyze_expression(index)?;
}
Expression::Literal(_) => {
// Literals don't need to be analyzed
}
Expression::Loop(loop_expression) => match loop_expression.inner.as_ref() {
LoopExpression::Infinite { block } => self.analyze_block(&block.inner)?,
LoopExpression::While { condition, block } => {
self.analyze_expression(condition)?;
self.analyze_block(&block.inner)?;
}
LoopExpression::For {
iterator, block, ..
} => {
self.analyze_expression(iterator)?;
self.analyze_block(&block.inner)?;
}
},
Expression::Operator(operator_expression) => match operator_expression.inner.as_ref() {
OperatorExpression::Assignment { assignee, value } => {
self.analyze_expression(assignee)?;
self.analyze_expression(value)?;
}
OperatorExpression::Comparison { left, right, .. } => {
self.analyze_expression(left)?;
self.analyze_expression(right)?;
}
OperatorExpression::CompoundAssignment {
assignee, modifier, ..
} => {
self.analyze_expression(assignee)?;
self.analyze_expression(modifier)?;
}
OperatorExpression::ErrorPropagation(_) => todo!(),
2024-08-16 15:21:20 +00:00
OperatorExpression::Negation(expression) => {
self.analyze_expression(expression)?;
}
OperatorExpression::Not(expression) => {
self.analyze_expression(expression)?;
}
OperatorExpression::Math { left, right, .. } => {
self.analyze_expression(left)?;
self.analyze_expression(right)?;
}
OperatorExpression::Logic { left, right, .. } => {
self.analyze_expression(left)?;
self.analyze_expression(right)?;
}
},
2024-08-17 03:18:05 +00:00
Expression::Range(range_expression) => match range_expression.inner.as_ref() {
RangeExpression::Exclusive { start, end } => {
self.analyze_expression(start)?;
self.analyze_expression(end)?;
}
RangeExpression::Inclusive { start, end } => {
self.analyze_expression(start)?;
self.analyze_expression(end)?;
}
},
Expression::Struct(struct_expression) => match struct_expression.inner.as_ref() {
StructExpression::Unit { name } => {
let found = self
.context
.update_last_position(&name.inner, name.position);
if !found {
return Err(AnalyzerError::UndefinedType {
identifier: name.clone(),
});
}
}
StructExpression::Fields { name, fields } => {
let found = self
.context
.update_last_position(&name.inner, name.position);
if !found {
return Err(AnalyzerError::UndefinedType {
identifier: name.clone(),
});
}
for (_, expression) in fields {
self.analyze_expression(expression)?;
}
}
},
Expression::TupleAccess(tuple_access) => {
let TupleAccessExpression { tuple, .. } = tuple_access.inner.as_ref();
self.analyze_expression(tuple)?;
}
}
Ok(())
}
fn analyze_block(&mut self, block_expression: &BlockExpression) -> Result<(), AnalyzerError> {
match block_expression {
BlockExpression::Async(statements) => {
for statement in statements {
self.analyze_statement(statement)?;
}
}
BlockExpression::Sync(statements) => {
for statement in statements {
self.analyze_statement(statement)?;
}
}
}
Ok(())
}
fn analyze_if(&mut self, if_expression: &IfExpression) -> Result<(), AnalyzerError> {
match if_expression {
IfExpression::If {
condition,
if_block,
} => {
self.analyze_expression(condition)?;
self.analyze_block(&if_block.inner)?;
}
IfExpression::IfElse {
condition,
if_block,
r#else,
} => {
self.analyze_expression(condition)?;
self.analyze_block(&if_block.inner)?;
match r#else {
ElseExpression::Block(block_expression) => {
self.analyze_block(&block_expression.inner)?;
}
ElseExpression::If(if_expression) => {
self.analyze_if(&if_expression.inner)?;
}
}
}
}
2024-08-05 03:11:04 +00:00
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnalyzerError {
ExpectedBoolean {
2024-08-14 19:52:04 +00:00
actual: Statement,
},
ExpectedIdentifier {
2024-08-14 19:52:04 +00:00
actual: Statement,
},
ExpectedIdentifierOrString {
2024-08-14 19:52:04 +00:00
actual: Statement,
},
2024-08-12 14:43:18 +00:00
ExpectedIntegerOrRange {
2024-08-14 19:52:04 +00:00
actual: Statement,
},
2024-08-12 20:57:10 +00:00
ExpectedList {
2024-08-14 19:52:04 +00:00
actual: Statement,
2024-08-12 20:57:10 +00:00
},
ExpectedMap {
2024-08-14 19:52:04 +00:00
actual: Statement,
2024-08-12 20:57:10 +00:00
},
2024-08-09 08:23:02 +00:00
ExpectedValue {
2024-08-14 19:52:04 +00:00
actual: Statement,
2024-08-09 08:23:02 +00:00
},
2024-08-11 22:11:59 +00:00
ExpectedValueArgumentCount {
expected: usize,
actual: usize,
position: Span,
},
2024-08-12 23:39:26 +00:00
IndexOutOfBounds {
2024-08-14 19:52:04 +00:00
list: Statement,
index: Statement,
2024-08-12 23:39:26 +00:00
index_value: usize,
length: usize,
},
2024-08-11 23:00:37 +00:00
TypeConflict {
2024-08-14 19:52:04 +00:00
actual_statement: Statement,
2024-08-11 23:00:37 +00:00
actual_type: Type,
expected: Type,
},
UndefinedField {
2024-08-14 19:52:04 +00:00
identifier: Statement,
statement: Statement,
},
2024-08-13 20:21:44 +00:00
UndefinedType {
identifier: Node<Identifier>,
},
UnexpectedIdentifier {
identifier: Node<Identifier>,
},
2024-08-09 08:23:02 +00:00
UnexectedString {
2024-08-14 19:52:04 +00:00
actual: Statement,
2024-08-09 08:23:02 +00:00
},
2024-08-13 20:21:44 +00:00
UndefinedVariable {
identifier: Node<Identifier>,
2024-08-13 20:21:44 +00:00
},
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 {
2024-08-14 19:52:04 +00:00
AnalyzerError::ExpectedBoolean { actual } => actual.position(),
AnalyzerError::ExpectedIdentifier { actual } => actual.position(),
AnalyzerError::ExpectedIdentifierOrString { actual } => actual.position(),
AnalyzerError::ExpectedIntegerOrRange { actual } => actual.position(),
AnalyzerError::ExpectedList { actual } => actual.position(),
AnalyzerError::ExpectedMap { actual } => actual.position(),
AnalyzerError::ExpectedValue { actual } => actual.position(),
2024-08-11 22:11:59 +00:00
AnalyzerError::ExpectedValueArgumentCount { position, .. } => *position,
2024-08-14 19:52:04 +00:00
AnalyzerError::IndexOutOfBounds { index, .. } => index.position(),
2024-08-11 23:00:37 +00:00
AnalyzerError::TypeConflict {
actual_statement, ..
2024-08-14 19:52:04 +00:00
} => actual_statement.position(),
AnalyzerError::UndefinedField { identifier, .. } => identifier.position(),
2024-08-13 20:21:44 +00:00
AnalyzerError::UndefinedType { identifier } => identifier.position,
AnalyzerError::UndefinedVariable { identifier } => identifier.position,
AnalyzerError::UnexpectedIdentifier { identifier } => identifier.position,
2024-08-14 19:52:04 +00:00
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::ExpectedIdentifier { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected identifier, found {}", actual)
}
AnalyzerError::ExpectedIdentifierOrString { actual } => {
write!(f, "Expected identifier or string, found {}", actual)
}
2024-08-12 14:43:18 +00:00
AnalyzerError::ExpectedIntegerOrRange { actual, .. } => {
write!(f, "Expected integer or range, found {}", actual)
}
2024-08-12 20:57:10 +00:00
AnalyzerError::ExpectedList { actual } => write!(f, "Expected list, found {}", actual),
AnalyzerError::ExpectedMap { actual } => write!(f, "Expected map, found {}", actual),
2024-08-09 08:23:02 +00:00
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-12 23:39:26 +00:00
AnalyzerError::IndexOutOfBounds {
list,
index_value,
length,
..
2024-08-12 23:39:26 +00:00
} => write!(
f,
"Index {} out of bounds for list {} with length {}",
index_value, list, length
),
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
)
}
AnalyzerError::UndefinedField {
identifier,
statement: map,
} => {
write!(f, "Undefined field {} in map {}", identifier, map)
}
2024-08-13 20:21:44 +00:00
AnalyzerError::UndefinedType { identifier } => {
write!(f, "Undefined type {}", identifier)
}
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-14 19:52:04 +00:00
use crate::{Identifier, Value};
2024-08-05 03:11:04 +00:00
use super::*;
2024-08-14 18:28:39 +00:00
#[test]
fn add_assign_wrong_type() {
let source = "
a = 1
a += 1.0
";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-14 18:28:39 +00:00
}
#[test]
fn subtract_assign_wrong_type() {
let source = "
a = 1
a -= 1.0
";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-14 18:28:39 +00:00
}
#[test]
fn tuple_struct_with_wrong_field_types() {
let source = "
struct Foo(int, float)
Foo(1, 2)
";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
}
2024-08-12 23:39:26 +00:00
#[test]
fn constant_list_index_out_of_bounds() {
let source = "[1, 2, 3][3]";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-12 23:39:26 +00:00
}
#[test]
fn nonexistant_field_identifier() {
2024-08-12 23:39:26 +00:00
let source = "{ x = 1 }.y";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
}
#[test]
fn nonexistant_field_string() {
let source = "{ x = 1 }.'y'";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-12 23:39:26 +00:00
}
2024-08-12 02:02:17 +00:00
#[test]
fn malformed_list_index() {
2024-08-12 20:57:10 +00:00
let source = "[1, 2, 3]['foo']";
2024-08-12 02:02:17 +00:00
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-12 02:02:17 +00:00
}
#[test]
2024-08-12 23:39:26 +00:00
fn malformed_field_access() {
2024-08-12 02:02:17 +00:00
let source = "{ x = 1 }.0";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-12 02:02:17 +00:00
}
2024-08-11 23:00:37 +00:00
#[test]
fn length_no_arguments() {
let source = "length()";
2024-08-11 21:59:52 +00:00
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
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";
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
}
#[test]
2024-08-09 08:56:24 +00:00
fn integer_plus_boolean() {
2024-08-11 23:18:13 +00:00
let source = "42 + true";
2024-08-08 17:01:25 +00:00
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-08 17:01:25 +00:00
}
2024-08-08 17:01:25 +00:00
#[test]
fn is_even_expects_number() {
2024-08-11 23:18:13 +00:00
let source = "is_even('hello')";
2024-08-08 17:01:25 +00:00
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-08 17:01:25 +00:00
}
2024-08-11 23:18:13 +00:00
2024-08-05 03:11:04 +00:00
#[test]
fn is_odd_expects_number() {
2024-08-11 23:18:13 +00:00
let source = "is_odd('hello')";
2024-08-05 03:11:04 +00:00
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
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-11 23:18:13 +00:00
let source = "foo";
2024-08-05 04:40:51 +00:00
2024-08-14 19:52:04 +00:00
assert_eq!(analyze(source), todo!());
2024-08-05 04:40:51 +00:00
}
2024-08-05 03:11:04 +00:00
}