Pass all tests

This commit is contained in:
Jeff 2024-03-17 13:36:31 -04:00
parent 199e1c9184
commit fed119f38b
14 changed files with 235 additions and 238 deletions

View File

@ -80,7 +80,7 @@ impl AbstractTree for Assignment {
context.set_value(self.identifier, value)?;
}
AssignmentOperator::AddAssign => {
if let Some(previous_value) = context.use_value(&self.identifier)? {
if let Some(previous_value) = context.get_value(&self.identifier)? {
let new_value = previous_value.add(&value)?;
context.set_value(self.identifier, new_value)?;
@ -91,7 +91,7 @@ impl AbstractTree for Assignment {
}
}
AssignmentOperator::SubAssign => {
if let Some(previous_value) = context.use_value(&self.identifier)? {
if let Some(previous_value) = context.get_value(&self.identifier)? {
let new_value = previous_value.subtract(&value)?;
context.set_value(self.identifier, new_value)?;
@ -131,7 +131,7 @@ mod tests {
.unwrap();
assert_eq!(
context.use_value(&Identifier::new("foobar")),
context.get_value(&Identifier::new("foobar")),
Ok(Some(Value::integer(42)))
)
}
@ -154,7 +154,7 @@ mod tests {
.unwrap();
assert_eq!(
context.use_value(&Identifier::new("foobar")),
context.get_value(&Identifier::new("foobar")),
Ok(Some(Value::integer(42)))
)
}
@ -177,7 +177,7 @@ mod tests {
.unwrap();
assert_eq!(
context.use_value(&Identifier::new("foobar")),
context.get_value(&Identifier::new("foobar")),
Ok(Some(Value::integer(42)))
)
}

View File

@ -41,12 +41,7 @@ impl AbstractTree for Block {
let mut previous = Action::None;
for statement in self.statements {
let action = statement.node.run(_context)?;
previous = match action {
Action::Return(value) => Action::Return(value),
Action::None => Action::None,
Action::Break => return Ok(action),
};
previous = statement.node.run(_context)?;
}
Ok(previous)

View File

@ -51,8 +51,9 @@ impl AbstractTree for FunctionCall {
arguments.push(value);
}
let function_context = Context::inherit_data_from(context)?;
let function_context = Context::new();
function_context.inherit_data_from(&context)?;
function.clone().call(arguments, function_context)
}
}

View File

@ -25,7 +25,7 @@ impl Identifier {
impl AbstractTree for Identifier {
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
if let Some(r#type) = context.use_type(self)? {
if let Some(r#type) = context.get_type(self)? {
Ok(r#type)
} else {
Err(ValidationError::VariableNotFound(self.clone()))
@ -33,7 +33,7 @@ impl AbstractTree for Identifier {
}
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
if context.add_allowance(self)? {
if context.contains(self)? {
Ok(())
} else {
Err(ValidationError::VariableNotFound(self.clone()))
@ -41,7 +41,7 @@ impl AbstractTree for Identifier {
}
fn run(self, context: &Context) -> Result<Action, RuntimeError> {
let return_action = context.use_value(&self)?.map(|value| Action::Return(value));
let return_action = context.get_value(&self)?.map(|value| Action::Return(value));
if let Some(action) = return_action {
Ok(action)

View File

@ -32,8 +32,13 @@ impl AbstractTree for IfElse {
}
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
self.if_expression.node.validate(context)?;
self.if_block.validate(context)?;
if let Type::Boolean = self.if_expression.node.expected_type(context)? {
if let Some(else_block) = &self.else_block {
else_block.validate(context)?;
let expected = self.if_block.expected_type(context)?;
let actual = else_block.expected_type(context)?;

View File

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
@ -5,7 +7,7 @@ use crate::{
use super::{AbstractTree, Action, Statement, Type, WithPosition};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug)]
pub struct Loop {
statements: Vec<WithPosition<Statement>>,
}
@ -30,38 +32,92 @@ impl AbstractTree for Loop {
}
fn run(self, _context: &Context) -> Result<Action, RuntimeError> {
let mut index = 0;
loop {
if index == self.statements.len() - 1 {
index = 0;
} else {
index += 1;
}
let statement = self.statements[index].clone();
let action = statement.node.run(_context)?;
for statement in &self.statements {
let action = statement.node.clone().run(_context)?;
match action {
Action::Return(_) => {}
Action::None => {}
r#break => return Ok(r#break),
Action::Break => return Ok(Action::Break),
}
}
}
}
}
impl Eq for Loop {}
impl PartialEq for Loop {
fn eq(&self, other: &Self) -> bool {
self.statements.eq(&other.statements)
}
}
impl PartialOrd for Loop {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Loop {
fn cmp(&self, other: &Self) -> Ordering {
self.statements.cmp(&other.statements)
}
}
#[cfg(test)]
mod tests {
use crate::{
abstract_tree::{
Assignment, AssignmentOperator, Block, Expression, Identifier, IfElse, Logic, ValueNode,
},
Value,
};
use super::*;
#[test]
fn basic_loop() {
let result = Loop {
statements: vec![Statement::Break.with_position((0, 0))],
}
.run(&Context::new());
let result = Loop::new(vec![Statement::Break.with_position((0, 0))]).run(&Context::new());
assert_eq!(result, Ok(Action::Break))
}
#[test]
fn complex_loop() {
let result = Block::new(vec![
Statement::Assignment(Assignment::new(
Identifier::new("i"),
None,
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::Integer(1)))
.with_position((0, 0)),
))
.with_position((0, 0)),
Statement::Loop(Loop::new(vec![Statement::IfElse(IfElse::new(
Expression::Logic(Box::new(Logic::Greater(
Expression::Identifier(Identifier::new("i")).with_position((10, 11)),
Expression::Value(ValueNode::Integer(2)).with_position((14, 15)),
)))
.with_position((10, 15)),
Block::new(vec![Statement::Break.with_position((18, 24))]),
Some(Block::new(vec![Statement::Assignment(Assignment::new(
Identifier::new("i"),
None,
AssignmentOperator::AddAssign,
Statement::Expression(Expression::Value(ValueNode::Integer(1)))
.with_position((38, 39)),
))
.with_position((33, 39))])),
))
.with_position((0, 0))]))
.with_position((0, 0)),
Statement::Expression(Expression::Identifier(Identifier::new("i")))
.with_position((0, 0)),
])
.run(&Context::new());
assert_eq!(result, Ok(Action::Return(Value::integer(3))))
}
}

View File

@ -87,7 +87,9 @@ impl AbstractTree for ValueNode {
body,
} = self
{
let function_context = Context::inherit_types_from(context)?;
let function_context = Context::new();
function_context.inherit_types_from(context)?;
for (identifier, r#type) in parameters {
function_context.set_type(identifier.clone(), r#type.node.clone())?;

View File

@ -6,36 +6,13 @@ use std::{
use crate::{
abstract_tree::{Identifier, Type},
error::RwLockPoisonError,
value::{BuiltInFunction, ValueInner},
value::BuiltInFunction,
Value,
};
#[derive(Clone, Debug)]
pub struct Context {
inner: Arc<RwLock<BTreeMap<Identifier, (ValueData, UsageData)>>>,
}
#[derive(Clone, Debug)]
pub struct UsageData(Arc<RwLock<UsageDataInner>>);
#[derive(Clone, Debug)]
pub struct UsageDataInner {
pub allowances: usize,
pub uses: usize,
}
impl Default for UsageData {
fn default() -> Self {
UsageData(Arc::new(RwLock::new(UsageDataInner {
allowances: 0,
uses: 0,
})))
}
}
#[derive(Clone, Debug)]
pub enum ValueData {
Type(Type),
Value(Value),
inner: Arc<RwLock<BTreeMap<Identifier, ValueData>>>,
}
impl Context {
@ -45,96 +22,47 @@ impl Context {
}
}
pub fn with_data(data: BTreeMap<Identifier, (ValueData, UsageData)>) -> Self {
pub fn with_data(data: BTreeMap<Identifier, ValueData>) -> Self {
Self {
inner: Arc::new(RwLock::new(data)),
}
}
pub fn inherit_types_from(other: &Context) -> Result<Self, RwLockPoisonError> {
let mut new_data = BTreeMap::new();
pub fn inherit_types_from(&self, other: &Context) -> Result<(), RwLockPoisonError> {
let mut self_data = self.inner.write()?;
for (identifier, (value_data, usage_data)) in other.inner.read()?.iter() {
for (identifier, value_data) in other.inner.read()?.iter() {
if let ValueData::Type(r#type) = value_data {
if let Type::Function { .. } = r#type {
new_data.insert(identifier.clone(), (value_data.clone(), usage_data.clone()));
self_data.insert(identifier.clone(), value_data.clone());
}
}
}
Ok(Self::with_data(new_data))
Ok(())
}
pub fn inherit_data_from(other: &Context) -> Result<Self, RwLockPoisonError> {
let mut new_data = BTreeMap::new();
pub fn inherit_data_from(&self, other: &Context) -> Result<(), RwLockPoisonError> {
let mut self_data = self.inner.write()?;
for (identifier, (value_data, usage_data)) in other.inner.read()?.iter() {
if let ValueData::Type(r#type) = value_data {
if let Type::Function { .. } = r#type {
new_data.insert(identifier.clone(), (value_data.clone(), usage_data.clone()));
}
}
if let ValueData::Value(value) = value_data {
if let ValueInner::Function { .. } = value.inner().as_ref() {
new_data.insert(identifier.clone(), (value_data.clone(), usage_data.clone()));
}
}
for (identifier, value_data) in other.inner.read()?.iter() {
self_data.insert(identifier.clone(), value_data.clone());
}
Ok(Self::with_data(new_data))
Ok(())
}
pub fn add_allowance(&self, identifier: &Identifier) -> Result<bool, RwLockPoisonError> {
if let Some((_, usage_data)) = self.inner.read()?.get(identifier) {
usage_data.0.write()?.allowances += 1;
Ok(true)
} else {
Ok(false)
}
pub fn contains(&self, identifier: &Identifier) -> Result<bool, RwLockPoisonError> {
Ok(self.inner.read()?.contains_key(identifier))
}
pub fn use_data(
&self,
identifier: &Identifier,
) -> Result<Option<ValueData>, RwLockPoisonError> {
let should_remove =
if let Some((value_data, usage_data)) = self.inner.read()?.get(identifier) {
let mut usage_data = usage_data.0.write()?;
log::trace!("Adding use for variable: {identifier}");
usage_data.uses += 1;
if usage_data.uses == usage_data.allowances {
true
} else {
return Ok(Some(value_data.clone()));
}
} else {
false
pub fn get_type(&self, identifier: &Identifier) -> Result<Option<Type>, RwLockPoisonError> {
if let Some(value_data) = self.inner.read()?.get(identifier) {
let r#type = match value_data {
ValueData::Type(r#type) => r#type.clone(),
ValueData::Value(value) => value.r#type(),
};
if should_remove {
log::trace!("Removing varialble: {identifier}");
self.inner.write()?.remove(identifier);
}
let value_data = match identifier.as_str() {
"output" => ValueData::Value(BuiltInFunction::output()),
_ => return Ok(None),
};
Ok(Some(value_data))
}
pub fn use_type(&self, identifier: &Identifier) -> Result<Option<Type>, RwLockPoisonError> {
if let Some((ValueData::Type(r#type), usage_data)) = self.inner.read()?.get(identifier) {
log::trace!("Adding use for variable: {identifier}");
usage_data.0.write()?.uses += 1;
return Ok(Some(r#type.clone()));
}
@ -146,43 +74,23 @@ impl Context {
Ok(Some(r#type))
}
pub fn use_value(&self, identifier: &Identifier) -> Result<Option<Value>, RwLockPoisonError> {
let should_remove = if let Some((ValueData::Value(value), usage_data)) =
self.inner.read()?.get(identifier)
{
let mut usage_data = usage_data.0.write()?;
log::trace!("Adding use for variable: {identifier}");
usage_data.uses += 1;
if usage_data.uses == usage_data.allowances {
true
pub fn get_value(&self, identifier: &Identifier) -> Result<Option<Value>, RwLockPoisonError> {
if let Some(ValueData::Value(value)) = self.inner.read()?.get(identifier) {
Ok(Some(value.clone()))
} else {
return Ok(Some(value.clone()));
}
} else {
false
};
if should_remove {
log::trace!("Removing varialble: {identifier}");
self.inner.write()?.remove(identifier);
}
let value = match identifier.as_str() {
"output" => BuiltInFunction::output(),
"output" => Value::built_in_function(BuiltInFunction::Output),
_ => return Ok(None),
};
Ok(Some(value))
}
}
pub fn set_type(&self, identifier: Identifier, r#type: Type) -> Result<(), RwLockPoisonError> {
self.inner
.write()?
.insert(identifier, (ValueData::Type(r#type), UsageData::default()));
.insert(identifier, ValueData::Type(r#type));
Ok(())
}
@ -190,12 +98,14 @@ impl Context {
pub fn set_value(&self, identifier: Identifier, value: Value) -> Result<(), RwLockPoisonError> {
let mut inner = self.inner.write()?;
if let Some((_value_data, usage_data)) = inner.remove(&identifier) {
inner.insert(identifier, (ValueData::Value(value), usage_data));
} else {
inner.insert(identifier, (ValueData::Value(value), UsageData::default()));
}
inner.insert(identifier, ValueData::Value(value));
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ValueData {
Type(Type),
Value(Value),
}

View File

@ -1,6 +1,6 @@
use std::sync::PoisonError;
use std::{ops::Range, sync::PoisonError};
use ariadne::{Color, Label, Report, ReportKind};
use ariadne::{Label, ReportBuilder};
use chumsky::{prelude::Rich, span::Span};
use crate::{
@ -18,7 +18,10 @@ pub enum Error {
expected: String,
span: (usize, usize),
},
Runtime(RuntimeError),
Runtime {
error: RuntimeError,
position: SourcePosition,
},
Validation {
error: ValidationError,
position: SourcePosition,
@ -26,7 +29,10 @@ pub enum Error {
}
impl Error {
pub fn report(&self) -> Report {
pub fn build_report(
self,
mut builder: ReportBuilder<'_, Range<usize>>,
) -> ReportBuilder<'_, Range<usize>> {
match self {
Error::Parse { expected, span } => {
let message = match expected.as_str() {
@ -34,9 +40,7 @@ impl Error {
expected => format!("Expected {expected}."),
};
Report::build(ReportKind::Custom("Lexing Error", Color::White), (), span.0)
.with_label(Label::new(span.0..span.1).with_message(message))
.finish()
builder.add_label(Label::new(span.0..span.1).with_message(message));
}
Error::Lex { expected, span } => {
let message = match expected.as_str() {
@ -44,30 +48,27 @@ impl Error {
expected => format!("Expected {expected}."),
};
Report::build(ReportKind::Custom("Lexing Error", Color::White), (), span.0)
.with_label(Label::new(span.0..span.1).with_message(message))
.finish()
builder.add_label(Label::new(span.0..span.1).with_message(message));
}
Error::Runtime(_) => todo!(),
Error::Validation { error, position } => {
let mut report = Report::build(
ReportKind::Custom("Validation Error: The code was not run.", Color::White),
(),
0,
)
.with_label(
Label::new(position.0..position.1).with_message("Error found in this item."),
);
match error {
Error::Runtime { error, position } => match error {
RuntimeError::RwLockPoison(_) => todo!(),
RuntimeError::ValidationFailure(validation_error) => {
builder =
Error::Validation {
error: validation_error,
position,
}
.build_report(builder.with_note(
"The interpreter failed to catch this error during validation.",
));
}
},
Error::Validation { error, position } => match error {
ValidationError::ExpectedBoolean => {
report =
report.with_label(Label::new(0..0).with_message("Expected boolean."));
builder.add_label(Label::new(0..0).with_message("Expected boolean."));
}
ValidationError::ExpectedIntegerOrFloat => {
report = report.with_label(
Label::new(0..0).with_message("Expected integer or float."),
);
builder.add_label(Label::new(0..0).with_message("Expected integer or float."));
}
ValidationError::RwLockPoison(_) => todo!(),
ValidationError::TypeCheck {
@ -77,7 +78,7 @@ impl Error {
} => {
let TypeConflict { actual, expected } = conflict;
report = report.with_labels([
builder.add_labels([
Label::new(expected_postion.0..expected_postion.1)
.with_message(format!("Type {expected} established here.")),
Label::new(actual_position.0..actual_position.1)
@ -85,21 +86,21 @@ impl Error {
]);
}
ValidationError::VariableNotFound(identifier) => {
report = report
.with_label(Label::new(0..0).with_message(format!(
"The variable {identifier} does not exist."
)));
builder.add_label(
Label::new(position.0..position.1)
.with_message(format!("The variable {identifier} does not exist."))
.with_priority(1),
);
}
ValidationError::CannotIndex(_) => todo!(),
ValidationError::CannotIndexWith(_, _) => todo!(),
ValidationError::InterpreterExpectedReturn => todo!(),
ValidationError::ExpectedFunction => todo!(),
ValidationError::ExpectedValue => todo!(),
},
}
report.finish()
}
}
builder
}
}
@ -121,12 +122,6 @@ impl<'src> From<Rich<'_, Token<'src>>> for Error {
}
}
impl From<RuntimeError> for Error {
fn from(error: RuntimeError) -> Self {
Error::Runtime(error)
}
}
#[derive(Debug, PartialEq)]
pub enum RuntimeError {
RwLockPoison(RwLockPoisonError),

View File

@ -56,7 +56,12 @@ impl Interpreter {
Action::Return(value) => Some(value),
Action::None => continue,
},
Err(runtime_error) => return Err(vec![Error::Runtime(runtime_error)]),
Err(runtime_error) => {
return Err(vec![Error::Runtime {
error: runtime_error,
position: statement.position,
}])
}
}
}

View File

@ -1,6 +1,6 @@
//! Command line interface for the dust programming language.
use ariadne::Source;
use ariadne::{Color, Report, ReportKind, Source};
use clap::Parser;
use colored::Colorize;
@ -53,9 +53,17 @@ fn main() {
}
}
Err(errors) => {
let mut report_builder =
Report::build(ReportKind::Custom("Dust Error", Color::White), (), 5);
for error in errors {
error.report().eprint(Source::from(&source)).unwrap();
report_builder = error.build_report(report_builder);
}
report_builder
.finish()
.eprint(Source::from(source))
.unwrap()
}
}
}

View File

@ -656,6 +656,26 @@ mod tests {
),)
.with_position((7, 9))]),)
);
assert_eq!(
parse(&lex("loop { if i > 2 { break } else { i += 1 } }").unwrap()).unwrap()[0].node,
Statement::Loop(Loop::new(vec![Statement::IfElse(IfElse::new(
Expression::Logic(Box::new(Logic::Greater(
Expression::Identifier(Identifier::new("i")).with_position((10, 11)),
Expression::Value(ValueNode::Integer(2)).with_position((14, 15))
)))
.with_position((10, 15)),
Block::new(vec![Statement::Break.with_position((18, 24))]),
Some(Block::new(vec![Statement::Assignment(Assignment::new(
Identifier::new("i"),
None,
AssignmentOperator::AddAssign,
Statement::Expression(Expression::Value(ValueNode::Integer(1)))
.with_position((38, 39))
))
.with_position((33, 39))]))
),)
.with_position((7, 42))]))
);
}
#[test]

View File

@ -141,8 +141,8 @@ fn map_type_errors() {
actual: Type::String,
expected: Type::Boolean
},
actual_position: (0, 0).into(),
expected_position: (0, 0).into(),
actual_position: (15, 20).into(),
expected_position: (8, 13).into(),
},
position: (0, 22).into()
}])

View File

@ -30,8 +30,8 @@ fn set_variable_with_type_error() {
actual: Type::Boolean,
expected: Type::String
},
actual_position: (0, 0).into(),
expected_position: (0, 0).into()
actual_position: (14, 18).into(),
expected_position: (8, 12).into()
},
position: (0, 18).into()
}])
@ -43,13 +43,13 @@ fn function_variable() {
assert_eq!(
interpret("foobar = (x: int): int { x }; foobar"),
Ok(Some(Value::function(
vec![(Identifier::new("x"), Type::Integer.with_position((0, 0)))],
Type::Integer.with_position((0, 0)),
vec![(Identifier::new("x"), Type::Integer.with_position((13, 16)))],
Type::Integer.with_position((19, 23)),
Block::new(vec![Statement::Expression(Expression::Identifier(
Identifier::new("x")
))
.with_position((0, 0))])
.with_position((0, 0))
.with_position((25, 26))])
.with_position((9, 28))
)))
);
}