dust/dust-lang/src/analyzer.rs

924 lines
32 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-20 15:07:13 +00:00
AbstractSyntaxTree, AstError, BlockExpression, CallExpression, ElseExpression,
FieldAccessExpression, IfExpression, LetStatement, ListExpression, ListIndexExpression,
2024-08-20 22:48:25 +00:00
LiteralExpression, LoopExpression, MapExpression, Node, OperatorExpression,
PrimitiveValueExpression, RangeExpression, Span, Statement, StructDefinition,
StructExpression, TupleAccessExpression,
2024-08-17 02:43:29 +00:00
},
2024-08-20 23:44:22 +00:00
core_library, parse, Context, ContextError, DustError, Expression, Identifier, Rangeable,
RangeableType, StructType, 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-20 16:24:41 +00:00
let context = core_library().create_child();
2024-08-20 15:07:13 +00:00
let analyzer = Analyzer::new(&abstract_tree, context);
2024-08-11 21:59:52 +00:00
analyzer
.analyze()
2024-08-20 08:38:15 +00:00
.map_err(|analysis_error| DustError::Analysis {
analysis_error,
2024-08-11 21:59:52 +00:00
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-20 15:07:13 +00:00
context: Context,
2024-08-05 03:11:04 +00:00
}
2024-08-20 15:07:13 +00:00
impl<'recovered, 'a: 'recovered> Analyzer<'a> {
pub fn new(abstract_tree: &'a AbstractSyntaxTree, context: 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
}
2024-08-20 15:07:13 +00:00
pub fn analyze(&'recovered self) -> Result<(), AnalysisError> {
for statement in &self.abstract_tree.statements {
self.analyze_statement(statement)?;
2024-08-05 03:11:04 +00:00
}
Ok(())
}
2024-08-20 15:07:13 +00:00
fn analyze_statement(&'recovered self, statement: &Statement) -> Result<(), AnalysisError> {
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 {
2024-08-20 04:15:19 +00:00
LetStatement::Let { identifier, value }
| LetStatement::LetMut { identifier, value } => {
2024-08-20 15:07:13 +00:00
let r#type = value.return_type(&self.context)?;
2024-08-20 04:15:19 +00:00
if let Some(r#type) = r#type {
2024-08-20 21:01:30 +00:00
self.context
.set_variable_type(
identifier.inner.clone(),
r#type,
identifier.position,
)
.map_err(|error| AnalysisError::ContextError {
error,
position: identifier.position,
})?;
} else {
2024-08-20 04:15:19 +00:00
return Err(AnalysisError::ExpectedValueFromExpression {
2024-08-20 06:25:22 +00:00
expression: value.clone(),
found_type: r#type,
});
}
2024-08-20 06:25:22 +00:00
self.analyze_expression(value)?;
}
2024-08-20 04:15:19 +00:00
LetStatement::LetType { .. } => todo!(),
LetStatement::LetMutType { .. } => todo!(),
},
2024-08-20 06:25:22 +00:00
Statement::StructDefinition(struct_definition) => match &struct_definition.inner {
StructDefinition::Unit { name } => self.context.set_constructor_type(
name.inner.clone(),
StructType::Unit {
name: name.inner.clone(),
},
name.position,
2024-08-20 21:01:30 +00:00
),
2024-08-20 06:25:22 +00:00
StructDefinition::Tuple { name, items } => {
let fields = items.iter().map(|item| item.inner.clone()).collect();
self.context.set_constructor_type(
name.inner.clone(),
StructType::Tuple {
2024-08-20 04:15:19 +00:00
name: name.inner.clone(),
2024-08-20 06:25:22 +00:00
fields,
},
name.position,
2024-08-20 21:01:30 +00:00
)
2024-08-20 06:25:22 +00:00
}
StructDefinition::Fields { name, fields } => {
let fields = fields
.iter()
.map(|(identifier, r#type)| {
(identifier.inner.clone(), r#type.inner.clone())
})
.collect();
self.context.set_constructor_type(
name.inner.clone(),
StructType::Fields {
name: name.inner.clone(),
fields,
},
name.position,
2024-08-20 21:01:30 +00:00
)
2024-08-20 06:25:22 +00:00
}
2024-08-20 21:01:30 +00:00
}
.map_err(|error| AnalysisError::ContextError {
error,
position: struct_definition.position,
})?,
}
Ok(())
}
2024-08-20 16:24:41 +00:00
fn analyze_expression(&self, expression: &Expression) -> Result<(), AnalysisError> {
match expression {
2024-08-17 03:18:05 +00:00
Expression::Block(block_expression) => self.analyze_block(&block_expression.inner)?,
2024-08-20 18:45:43 +00:00
Expression::Break(break_node) => {
if let Some(expression) = &break_node.inner {
self.analyze_expression(expression)?;
}
}
2024-08-17 03:18:05 +00:00
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) => {
2024-08-20 04:15:19 +00:00
let FieldAccessExpression { container, field } =
2024-08-17 03:18:05 +00:00
field_access_expression.inner.as_ref();
2024-08-20 04:15:19 +00:00
self.context
2024-08-20 21:01:30 +00:00
.update_last_position(&field.inner, field.position)
.map_err(|error| AnalysisError::ContextError {
error,
position: field.position,
})?;
2024-08-17 03:18:05 +00:00
self.analyze_expression(container)?;
}
Expression::Grouped(expression) => {
self.analyze_expression(expression.inner.as_ref())?;
}
Expression::Identifier(identifier) => {
2024-08-20 16:24:41 +00:00
let found = self
.context
2024-08-20 21:01:30 +00:00
.update_last_position(&identifier.inner, identifier.position)
.map_err(|error| AnalysisError::ContextError {
error,
position: identifier.position,
})?;
2024-08-20 16:24:41 +00:00
if !found {
return Err(AnalysisError::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();
2024-08-20 23:20:38 +00:00
self.analyze_expression(list)?;
self.analyze_expression(index)?;
2024-08-20 22:48:25 +00:00
let list_type = list.return_type(&self.context)?;
2024-08-20 23:20:38 +00:00
let index_type = if let Some(r#type) = index.return_type(&self.context)? {
r#type
} else {
return Err(AnalysisError::ExpectedValueFromExpression {
expression: index.clone(),
found_type: None,
});
};
2024-08-20 22:48:25 +00:00
let literal_type = if let Expression::Literal(Node { inner, .. }) = index {
Some(inner.as_ref().clone())
} else {
None
};
2024-08-20 23:20:38 +00:00
if let Some(LiteralExpression::Primitive(PrimitiveValueExpression::Integer(
integer,
))) = literal_type
{
if integer < 0 {
return Err(AnalysisError::NegativeIndex {
index: index.clone(),
index_value: integer,
list: list.clone(),
});
}
2024-08-20 23:44:22 +00:00
} else if let Type::Range { r#type } = &index_type {
if let RangeableType::Integer = r#type {
// Ok
} else {
return Err(AnalysisError::ExpectedType {
expected: Type::Range {
r#type: RangeableType::Integer,
},
actual: index_type,
actual_expression: index.clone(),
});
}
2024-08-20 23:20:38 +00:00
} else {
return Err(AnalysisError::ExpectedType {
expected: Type::Integer,
actual: index_type,
actual_expression: index.clone(),
});
}
2024-08-20 22:48:25 +00:00
if let Some(Type::List { length, .. }) = list_type {
if let Some(LiteralExpression::Primitive(PrimitiveValueExpression::Integer(
integer,
))) = literal_type
{
2024-08-20 23:20:38 +00:00
if integer >= length as i64 {
2024-08-20 22:48:25 +00:00
return Err(AnalysisError::IndexOutOfBounds {
index: index.clone(),
length,
list: list.clone(),
index_value: integer,
});
}
}
}
if let Some(Type::String {
length: Some(length),
}) = list_type
{
if let Some(LiteralExpression::Primitive(PrimitiveValueExpression::Integer(
integer,
))) = literal_type
{
2024-08-20 23:20:38 +00:00
if integer >= length as i64 {
2024-08-20 22:48:25 +00:00
return Err(AnalysisError::IndexOutOfBounds {
index: index.clone(),
length,
list: list.clone(),
index_value: integer,
});
}
}
}
if list_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
expression: list.clone(),
found_type: list_type,
});
}
2024-08-17 03:18:05 +00:00
}
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)?;
}
},
2024-08-17 14:07:38 +00:00
Expression::Map(map_expression) => {
let MapExpression { pairs } = map_expression.inner.as_ref();
for (_, expression) in pairs {
self.analyze_expression(expression)?;
}
}
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)?;
2024-08-20 04:15:19 +00:00
2024-08-20 15:07:13 +00:00
let expected_type = assignee.return_type(&self.context)?;
let actual_type = modifier.return_type(&self.context)?;
2024-08-20 04:15:19 +00:00
if expected_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
2024-08-20 06:25:22 +00:00
expression: assignee.clone(),
found_type: expected_type,
2024-08-20 04:15:19 +00:00
});
}
if actual_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
2024-08-20 06:25:22 +00:00
expression: modifier.clone(),
found_type: actual_type,
2024-08-20 04:15:19 +00:00
});
}
if let (Some(expected_type), Some(actual_type)) = (expected_type, actual_type) {
2024-08-20 06:25:22 +00:00
expected_type.check(&actual_type).map_err(|_| {
2024-08-20 04:15:19 +00:00
AnalysisError::TypeConflict {
actual_expression: modifier.clone(),
actual_type,
expected: expected_type,
}
})?;
}
}
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)?;
2024-08-20 22:48:25 +00:00
let left_type = left.return_type(&self.context)?;
let right_type = right.return_type(&self.context)?;
if left_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
expression: left.clone(),
found_type: left_type,
});
}
if right_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
expression: right.clone(),
found_type: right_type,
});
}
if left_type != right_type {
return Err(AnalysisError::ExpectedType {
expected: left_type.unwrap(),
actual: right_type.unwrap(),
actual_expression: right.clone(),
});
}
}
OperatorExpression::Logic { left, right, .. } => {
self.analyze_expression(left)?;
self.analyze_expression(right)?;
2024-08-20 22:48:25 +00:00
let left_type = left.return_type(&self.context)?;
let right_type = right.return_type(&self.context)?;
if left_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
expression: left.clone(),
found_type: left_type,
});
}
if right_type.is_none() {
return Err(AnalysisError::ExpectedValueFromExpression {
expression: right.clone(),
found_type: right_type,
});
}
if left_type != right_type {
return Err(AnalysisError::ExpectedType {
expected: left_type.unwrap(),
actual: right_type.unwrap(),
actual_expression: right.clone(),
});
}
}
},
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::Fields { name, fields } => {
2024-08-20 06:25:22 +00:00
self.context
2024-08-20 21:01:30 +00:00
.update_last_position(&name.inner, name.position)
.map_err(|error| AnalysisError::ContextError {
error,
position: name.position,
2024-08-20 22:48:25 +00:00
})?;
2024-08-17 03:18:05 +00:00
2024-08-20 06:25:22 +00:00
for (_, expression) in fields {
self.analyze_expression(expression)?;
}
2024-08-17 03:18:05 +00:00
}
},
Expression::TupleAccess(tuple_access) => {
let TupleAccessExpression { tuple, .. } = tuple_access.inner.as_ref();
self.analyze_expression(tuple)?;
}
}
Ok(())
}
2024-08-20 15:07:13 +00:00
fn analyze_block(
&'recovered self,
block_expression: &BlockExpression,
) -> Result<(), AnalysisError> {
2024-08-17 03:18:05 +00:00
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(())
}
2024-08-20 15:07:13 +00:00
fn analyze_if(&'recovered self, if_expression: &IfExpression) -> Result<(), AnalysisError> {
2024-08-17 03:18:05 +00:00
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 AnalysisError {
2024-08-20 15:07:13 +00:00
AstError(AstError),
2024-08-20 21:01:30 +00:00
ContextError {
error: ContextError,
position: Span,
},
2024-08-20 22:48:25 +00:00
ExpectedType {
expected: Type,
actual: Type,
actual_expression: Expression,
},
ExpectedTypeMultiple {
expected: Vec<Type>,
actual: Type,
actual_expression: Expression,
},
ExpectedIdentifier {
2024-08-20 22:48:25 +00:00
actual: Expression,
},
ExpectedIdentifierOrString {
2024-08-20 22:48:25 +00:00
actual: Expression,
2024-08-12 20:57:10 +00:00
},
2024-08-20 04:15:19 +00:00
ExpectedValueFromStatement {
2024-08-14 19:52:04 +00:00
actual: Statement,
2024-08-09 08:23:02 +00:00
},
2024-08-20 04:15:19 +00:00
ExpectedValueFromExpression {
2024-08-20 06:25:22 +00:00
expression: Expression,
found_type: Option<Type>,
2024-08-20 04:15:19 +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-20 22:48:25 +00:00
list: Expression,
index: Expression,
index_value: i64,
2024-08-12 23:39:26 +00:00
length: usize,
},
2024-08-20 23:20:38 +00:00
NegativeIndex {
list: Expression,
index: Expression,
index_value: i64,
},
2024-08-11 23:00:37 +00:00
TypeConflict {
2024-08-20 04:15:19 +00:00
actual_expression: Expression,
2024-08-11 23:00:37 +00:00
actual_type: Type,
expected: Type,
},
UndefinedField {
2024-08-20 22:48:25 +00:00
identifier: Expression,
statement: Expression,
},
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-20 22:48:25 +00:00
actual: Expression,
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-20 15:07:13 +00:00
impl From<AstError> for AnalysisError {
fn from(v: AstError) -> Self {
Self::AstError(v)
}
}
impl AnalysisError {
2024-08-20 21:01:30 +00:00
pub fn position(&self) -> Span {
match self {
AnalysisError::AstError(ast_error) => ast_error.position(),
AnalysisError::ContextError { position, .. } => *position,
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedType {
actual_expression, ..
} => actual_expression.position(),
AnalysisError::ExpectedTypeMultiple {
actual_expression, ..
} => actual_expression.position(),
AnalysisError::ExpectedIdentifier { actual } => actual.position(),
AnalysisError::ExpectedIdentifierOrString { actual } => actual.position(),
2024-08-20 06:25:22 +00:00
AnalysisError::ExpectedValueFromExpression { expression, .. } => expression.position(),
2024-08-20 04:15:19 +00:00
AnalysisError::ExpectedValueFromStatement { actual } => actual.position(),
AnalysisError::ExpectedValueArgumentCount { position, .. } => *position,
AnalysisError::IndexOutOfBounds { index, .. } => index.position(),
2024-08-20 23:20:38 +00:00
AnalysisError::NegativeIndex { index, .. } => index.position(),
AnalysisError::TypeConflict {
2024-08-20 04:15:19 +00:00
actual_expression, ..
} => actual_expression.position(),
AnalysisError::UndefinedField { identifier, .. } => identifier.position(),
AnalysisError::UndefinedType { identifier } => identifier.position,
AnalysisError::UndefinedVariable { identifier } => identifier.position,
AnalysisError::UnexpectedIdentifier { identifier } => identifier.position,
AnalysisError::UnexectedString { actual } => actual.position(),
2024-08-20 21:01:30 +00:00
}
2024-08-09 05:43:58 +00:00
}
}
impl Error for AnalysisError {}
2024-08-09 00:58:56 +00:00
impl Display for AnalysisError {
2024-08-09 00:58:56 +00:00
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
2024-08-20 15:07:13 +00:00
AnalysisError::AstError(ast_error) => write!(f, "{}", ast_error),
2024-08-20 22:48:25 +00:00
AnalysisError::ContextError { error, .. } => write!(f, "{}", error),
AnalysisError::ExpectedType {
expected,
actual,
actual_expression,
} => {
write!(
f,
"Expected type {:?}, found {:?} in {}",
expected, actual, actual_expression
)
2024-08-20 21:01:30 +00:00
}
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedTypeMultiple {
expected,
actual,
actual_expression,
} => {
write!(
f,
"Expected one of {:?}, found {:?} in {}",
expected, actual, actual_expression
)
2024-08-09 00:58:56 +00:00
}
2024-08-20 22:48:25 +00:00
AnalysisError::ExpectedIdentifier { actual, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Expected identifier, found {}", actual)
}
AnalysisError::ExpectedIdentifierOrString { actual } => {
write!(f, "Expected identifier or string, found {}", actual)
}
2024-08-20 06:25:22 +00:00
AnalysisError::ExpectedValueFromExpression {
expression,
found_type,
} => {
2024-08-20 04:15:19 +00:00
write!(
f,
2024-08-20 06:25:22 +00:00
"Expected {} to produce a value, found {:?}",
expression, found_type
2024-08-20 04:15:19 +00:00
)
}
AnalysisError::ExpectedValueFromStatement { actual, .. } => {
write!(f, "Expected statement to produce a value, found {}", actual)
2024-08-09 08:23:02 +00:00
}
AnalysisError::ExpectedValueArgumentCount {
2024-08-11 22:11:59 +00:00
expected, actual, ..
} => write!(f, "Expected {} value arguments, found {}", expected, actual),
AnalysisError::IndexOutOfBounds {
2024-08-12 23:39:26 +00:00
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-20 23:20:38 +00:00
AnalysisError::NegativeIndex {
list, index_value, ..
} => write!(f, "Negative index {} for list {}", index_value, list),
AnalysisError::TypeConflict {
2024-08-20 04:15:19 +00:00
actual_expression: actual_statement,
2024-08-11 23:00:37 +00:00
actual_type,
expected,
} => {
write!(
f,
"Expected type {}, found {}, which has type {}",
expected, actual_statement, actual_type
)
}
AnalysisError::UndefinedField {
identifier,
statement: map,
} => {
write!(f, "Undefined field {} in map {}", identifier, map)
}
AnalysisError::UndefinedType { identifier } => {
2024-08-13 20:21:44 +00:00
write!(f, "Undefined type {}", identifier)
}
AnalysisError::UndefinedVariable { identifier } => {
2024-08-09 22:14:46 +00:00
write!(f, "Undefined variable {}", identifier)
}
AnalysisError::UnexpectedIdentifier { identifier, .. } => {
2024-08-09 00:58:56 +00:00
write!(f, "Unexpected identifier {}", identifier)
}
AnalysisError::UnexectedString { actual, .. } => {
2024-08-09 08:23:02 +00:00
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 {
use super::*;
2024-08-14 18:28:39 +00:00
#[test]
fn add_assign_wrong_type() {
let source = "
2024-08-20 04:15:19 +00:00
let mut a = 1;
2024-08-14 18:28:39 +00:00
a += 1.0
";
2024-08-20 04:15:19 +00:00
assert_eq!(
analyze(source),
2024-08-20 08:38:15 +00:00
Err(DustError::Analysis {
2024-08-20 04:15:19 +00:00
analysis_error: AnalysisError::TypeConflict {
actual_expression: Expression::literal(1.0, (45, 48)),
actual_type: Type::Float,
expected: Type::Integer,
},
source,
})
);
2024-08-14 18:28:39 +00:00
}
#[test]
fn subtract_assign_wrong_type() {
let source = "
2024-08-20 16:24:41 +00:00
let mut a = 1;
2024-08-14 18:28:39 +00:00
a -= 1.0
";
2024-08-20 04:15:19 +00:00
assert_eq!(
analyze(source),
2024-08-20 08:38:15 +00:00
Err(DustError::Analysis {
2024-08-20 04:15:19 +00:00
analysis_error: AnalysisError::TypeConflict {
actual_expression: Expression::literal(1.0, (45, 48)),
actual_type: Type::Float,
expected: Type::Integer,
},
source,
})
);
2024-08-14 18:28:39 +00:00
}
#[test]
fn tuple_struct_with_wrong_field_types() {
let source = "
2024-08-20 22:48:25 +00:00
struct Foo(int, float);
Foo(1, 2)
";
2024-08-20 22:48:25 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_error: AnalysisError::TypeConflict {
actual_expression: Expression::literal(2, (52, 53)),
actual_type: Type::Integer,
expected: Type::Float,
},
source,
})
);
}
2024-08-12 23:39:26 +00:00
#[test]
fn constant_list_index_out_of_bounds() {
let source = "[1, 2, 3][3]";
2024-08-20 22:48:25 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_error: AnalysisError::IndexOutOfBounds {
list: Expression::list(
vec![
Expression::literal(1, (1, 2)),
Expression::literal(2, (4, 5)),
Expression::literal(3, (7, 8)),
],
(0, 9)
),
index: Expression::literal(3, (10, 11)),
index_value: 3,
length: 3,
},
source,
})
);
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-20 23:20:38 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_error: AnalysisError::ExpectedType {
expected: Type::Integer,
actual: Type::String { length: Some(3) },
actual_expression: Expression::literal("foo", (10, 15)),
},
source,
})
);
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-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-20 22:48:25 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_error: AnalysisError::ExpectedType {
expected: Type::Float,
actual: Type::Integer,
actual_expression: Expression::literal(2, (7, 8)),
},
source,
})
);
}
#[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-20 23:20:38 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_error: AnalysisError::ExpectedType {
expected: Type::Integer,
actual: Type::Boolean,
actual_expression: Expression::literal(true, (5, 9)),
},
source,
})
);
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]
2024-08-20 23:20:38 +00:00
fn nonexistant_field() {
let source = "'hello'.foo";
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-20 16:24:41 +00:00
assert_eq!(
analyze(source),
Err(DustError::Analysis {
analysis_error: AnalysisError::UndefinedVariable {
identifier: Node::new(Identifier::new("foo"), (0, 3))
},
source,
})
);
2024-08-05 04:40:51 +00:00
}
2024-08-05 03:11:04 +00:00
}