1
0

Compare commits

..

No commits in common. "a048577143379bce8f08f05ec65a98c62503a3b1" and "d0dba3528591765bbb50480cb652a28a3b21a6f4" have entirely different histories.

7 changed files with 123 additions and 239 deletions

View File

@ -1,6 +1,6 @@
//! In-memory representation of a Dust program.
use std::{
collections::{BTreeMap, HashMap, VecDeque},
collections::{HashMap, VecDeque},
fmt::{self, Display, Formatter},
};
@ -36,7 +36,7 @@ impl<T: Display> Display for Node<T> {
pub enum Statement {
// Top-level statements
Assignment {
identifier: Node<Identifier>,
identifier: Identifier,
value_node: Box<Node<Statement>>,
},
@ -67,7 +67,6 @@ pub enum Statement {
// Value collection expressions
List(Vec<Node<Statement>>),
Map(Vec<(Node<Identifier>, Node<Statement>)>),
// Hard-coded values
Constant(Value),
@ -84,26 +83,9 @@ impl Statement {
Statement::Identifier(identifier) => variables
.get(identifier)
.map(|value| value.r#type(variables)),
Statement::List(nodes) => {
let item_type = nodes.first().unwrap().inner.expected_type(variables)?;
Some(Type::List {
length: nodes.len(),
item_type: Box::new(item_type),
})
}
Statement::Map(nodes) => {
let mut types = BTreeMap::new();
for (identifier, item) in nodes {
types.insert(
identifier.inner.clone(),
item.inner.expected_type(variables)?,
);
}
Some(Type::Map(types))
}
Statement::List(nodes) => nodes
.first()
.and_then(|node| node.inner.expected_type(variables)),
Statement::PropertyAccess(_, _) => None,
}
}
@ -199,30 +181,14 @@ impl Display for Statement {
Statement::Identifier(identifier) => write!(f, "{identifier}"),
Statement::List(nodes) => {
write!(f, "[")?;
for (i, node) in nodes.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{node}")?;
}
write!(f, "]")
}
Statement::Map(nodes) => {
write!(f, "{{")?;
for (i, (identifier, node)) in nodes.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{identifier} = {node}")?;
}
write!(f, "}}")
}
Statement::PropertyAccess(left, right) => write!(f, "{left}.{right}"),
}
}

View File

@ -80,9 +80,7 @@ impl<'a> Analyzer<'a> {
Statement::Assignment {
value_node: value, ..
} => {
self.analyze_node(value)?;
if value.inner.expected_type(self.variables).is_none() {
if let None = value.inner.expected_type(self.variables) {
return Err(AnalyzerError::ExpectedValue {
actual: value.as_ref().clone(),
position: value.position,
@ -163,11 +161,6 @@ impl<'a> Analyzer<'a> {
self.analyze_node(statement)?;
}
}
Statement::Map(properties) => {
for (_key, value_node) in properties {
self.analyze_node(value_node)?;
}
}
Statement::PropertyAccess(left, right) => {
if let Statement::Identifier(_) | Statement::Constant(_) | Statement::List(_) =
&left.inner

View File

@ -189,16 +189,6 @@ impl Lexer {
(Token::Less, (self.position - 1, self.position))
}
}
'{' => {
self.position += 1;
(Token::LeftCurlyBrace, (self.position - 1, self.position))
}
'}' => {
self.position += 1;
(Token::RightCurlyBrace, (self.position - 1, self.position))
}
_ => {
self.position += 1;
@ -425,27 +415,6 @@ impl From<ParseIntError> for LexError {
mod tests {
use super::*;
#[test]
fn map() {
let input = "{ x = 42, y = 'foobar' }";
assert_eq!(
lex(input),
Ok(vec![
(Token::LeftCurlyBrace, (0, 1)),
(Token::Identifier("x"), (2, 3)),
(Token::Equal, (4, 5)),
(Token::Integer(42), (6, 8)),
(Token::Comma, (8, 9)),
(Token::Identifier("y"), (10, 11)),
(Token::Equal, (12, 13)),
(Token::String("foobar"), (14, 22)),
(Token::RightCurlyBrace, (23, 24)),
(Token::Eof, (24, 24)),
])
)
}
#[test]
fn greater_than() {
let input = ">";

View File

@ -27,16 +27,16 @@ use crate::{
/// Ok(AbstractSyntaxTree {
/// nodes: [
/// Node {
/// inner: Statement::Assignment {
/// identifier: Node {
/// inner: Identifier::new("x"),
/// inner: Statement::Assign(
/// Box::new(Node {
/// inner: Statement::Identifier("x".into()),
/// position: (0, 1),
/// },
/// value_node: Box::new(Node {
/// }),
/// Box::new(Node {
/// inner: Statement::Constant(Value::integer(42)),
/// position: (4, 6),
/// })
/// },
/// ),
/// position: (0, 6),
/// }
/// ].into(),
@ -86,16 +86,16 @@ pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
/// nodes,
/// Into::<VecDeque<Node<Statement>>>::into([
/// Node {
/// inner: Statement::Assignment {
/// identifier: Node {
/// inner: Identifier::new("x"),
/// inner: Statement::Assign(
/// Box::new(Node {
/// inner: Statement::Identifier("x".into()),
/// position: (0, 1),
/// },
/// value_node: Box::new(Node {
/// }),
/// Box::new(Node {
/// inner: Statement::Constant(Value::integer(42)),
/// position: (4, 6),
/// }),
/// },
/// })
/// ),
/// position: (0, 6),
/// }
/// ]),
@ -166,6 +166,28 @@ impl<'src> Parser<'src> {
(left_start, right_end),
));
}
(Token::Equal, _) => {
self.next_token()?;
let identifier = if let Statement::Identifier(identifier) = left_node.inner {
identifier
} else {
return Err(ParseError::ExpectedIdentifier {
actual: left_node.inner,
position: left_node.position,
});
};
let right_node = self.parse_node(self.current_precedence())?;
let right_end = right_node.position.1;
return Ok(Node::new(
Statement::Assignment {
identifier,
value_node: Box::new(right_node),
},
(left_start, right_end),
));
}
(Token::Greater, _) => {
let operator = Node::new(BinaryOperator::Greater, self.current.1);
@ -315,76 +337,16 @@ impl<'src> Parser<'src> {
(Token::Identifier(text), span) => {
self.next_token()?;
if let (Token::Equal, _) = self.current {
self.next_token()?;
let value = self.parse_node(0)?;
let right_end = value.position.1;
Ok(Node::new(
Statement::Assignment {
identifier: Node::new(Identifier::new(text), span),
value_node: Box::new(value),
},
(span.0, right_end),
))
} else {
Ok(Node::new(
Statement::Identifier(Identifier::new(text)),
span,
))
}
}
(Token::String(string), span) => {
self.next_token()?;
Ok(Node::new(Statement::Constant(Value::string(string)), span))
}
(Token::LeftCurlyBrace, left_span) => {
self.next_token()?;
let mut nodes = Vec::new();
loop {
if let (Token::RightCurlyBrace, right_span) = self.current {
self.next_token()?;
return Ok(Node::new(
Statement::Map(nodes),
(left_span.0, right_span.1),
));
}
let identifier = if let (Token::Identifier(text), right_span) = self.current {
self.next_token()?;
Node::new(Identifier::new(text), right_span)
} else {
return Err(ParseError::ExpectedIdentifier {
actual: self.current.0.to_owned(),
position: self.current.1,
});
};
if let Token::Equal = self.current.0 {
self.next_token()?;
} else {
return Err(ParseError::ExpectedToken {
expected: TokenOwned::Equal,
actual: self.current.0.to_owned(),
position: self.current.1,
});
}
let current_value_node = self.parse_node(0)?;
nodes.push((identifier, current_value_node));
if let Token::Comma = self.current.0 {
self.next_token()?;
}
}
}
(Token::LeftParenthesis, left_span) => {
self.next_token()?;
@ -395,8 +357,7 @@ impl<'src> Parser<'src> {
Ok(Node::new(node.inner, (left_span.0, right_span.1)))
} else {
Err(ParseError::ExpectedToken {
expected: TokenOwned::RightParenthesis,
Err(ParseError::ExpectedClosingParenthesis {
actual: self.current.0.to_owned(),
position: self.current.1,
})
@ -426,8 +387,7 @@ impl<'src> Parser<'src> {
if let Ok(instruction) = self.parse_node(0) {
nodes.push(instruction);
} else {
return Err(ParseError::ExpectedToken {
expected: TokenOwned::RightSquareBrace,
return Err(ParseError::ExpectedClosingSquareBrace {
actual: self.current.0.to_owned(),
position: self.current.1,
});
@ -452,8 +412,7 @@ impl<'src> Parser<'src> {
if let (Token::LeftParenthesis, _) = self.current {
self.next_token()?;
} else {
return Err(ParseError::ExpectedToken {
expected: TokenOwned::LeftParenthesis,
return Err(ParseError::ExpectedOpeningParenthesis {
actual: self.current.0.to_owned(),
position: self.current.1,
});
@ -479,8 +438,7 @@ impl<'src> Parser<'src> {
value_arguments = Some(vec![node]);
}
} else {
return Err(ParseError::ExpectedToken {
expected: TokenOwned::RightParenthesis,
return Err(ParseError::ExpectedClosingParenthesis {
actual: self.current.0.to_owned(),
position: self.current.1,
});
@ -507,6 +465,7 @@ impl<'src> Parser<'src> {
match self.current.0 {
Token::Greater | Token::GreaterEqual | Token::Less | Token::LessEqual => 5,
Token::Dot => 4,
Token::Equal => 3,
Token::Star => 2,
Token::Plus => 1,
Token::Minus => 1,
@ -521,12 +480,20 @@ pub enum ParseError {
error: LexError,
position: Span,
},
ExpectedIdentifier {
ExpectedClosingParenthesis {
actual: TokenOwned,
position: Span,
},
ExpectedClosingSquareBrace {
actual: TokenOwned,
position: Span,
},
ExpectedIdentifier {
actual: Statement,
position: (usize, usize),
},
ExpectedToken {
expected: TokenOwned,
ExpectedOpeningParenthesis {
actual: TokenOwned,
position: Span,
},
@ -540,8 +507,10 @@ impl ParseError {
pub fn position(&self) -> Span {
match self {
Self::LexError { position, .. } => *position,
Self::ExpectedClosingParenthesis { position, .. } => *position,
Self::ExpectedClosingSquareBrace { position, .. } => *position,
Self::ExpectedIdentifier { position, .. } => *position,
Self::ExpectedToken { position, .. } => *position,
Self::ExpectedOpeningParenthesis { position, .. } => *position,
Self::UnexpectedToken { position, .. } => *position,
}
}
@ -560,12 +529,18 @@ impl Display for ParseError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::LexError { error, .. } => write!(f, "{}", error),
Self::ExpectedClosingParenthesis { actual, .. } => {
write!(f, "Expected closing parenthesis, found {actual}",)
}
Self::ExpectedClosingSquareBrace { actual, .. } => {
write!(f, "Expected closing square brace, found {actual}",)
}
Self::ExpectedIdentifier { actual, .. } => {
write!(f, "Expected identifier, found {actual}")
}
Self::ExpectedToken {
expected, actual, ..
} => write!(f, "Expected token {expected}, found {actual}"),
Self::ExpectedOpeningParenthesis { actual, .. } => {
write!(f, "Expected opening parenthesis, found {actual}",)
}
Self::UnexpectedToken { actual, .. } => write!(f, "Unexpected token {actual}"),
}
}
@ -577,44 +552,6 @@ mod tests {
use super::*;
#[test]
fn malformed_assignment() {
let input = "false = 1";
assert_eq!(
parse(input),
Err(ParseError::UnexpectedToken {
actual: TokenOwned::Equal,
position: (6, 7)
})
);
}
#[test]
fn map() {
let input = "{ x = 42, y = 'foobar' }";
assert_eq!(
parse(input),
Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::Map(vec![
(
Node::new(Identifier::new("x"), (2, 3)),
Node::new(Statement::Constant(Value::integer(42)), (6, 8))
),
(
Node::new(Identifier::new("y"), (10, 11)),
Node::new(Statement::Constant(Value::string("foobar")), (14, 22))
)
]),
(0, 24)
)]
.into()
})
);
}
#[test]
fn less_than() {
let input = "1 < 2";
@ -1027,7 +964,7 @@ mod tests {
Ok(AbstractSyntaxTree {
nodes: [Node::new(
Statement::Assignment {
identifier: Node::new(Identifier::new("a"), (0, 1)),
identifier: Identifier::new("a"),
value_node: Box::new(Node::new(
Statement::BinaryOperation {
left: Box::new(Node::new(

View File

@ -31,14 +31,12 @@ pub enum Token<'src> {
Equal,
Greater,
GreaterEqual,
LeftCurlyBrace,
LeftParenthesis,
LeftSquareBrace,
Less,
LessEqual,
Minus,
Plus,
RightCurlyBrace,
RightParenthesis,
RightSquareBrace,
Star,
@ -61,7 +59,6 @@ impl<'src> Token<'src> {
Token::Integer(integer) => TokenOwned::Integer(*integer),
Token::IsEven => TokenOwned::IsEven,
Token::IsOdd => TokenOwned::IsOdd,
Token::LeftCurlyBrace => TokenOwned::LeftCurlyBrace,
Token::LeftParenthesis => TokenOwned::LeftParenthesis,
Token::LeftSquareBrace => TokenOwned::LeftSquareBrace,
Token::Length => TokenOwned::Length,
@ -70,7 +67,6 @@ impl<'src> Token<'src> {
Token::Minus => TokenOwned::Minus,
Token::Plus => TokenOwned::Plus,
Token::ReadLine => TokenOwned::ReadLine,
Token::RightCurlyBrace => TokenOwned::RightCurlyBrace,
Token::RightParenthesis => TokenOwned::RightParenthesis,
Token::RightSquareBrace => TokenOwned::RightSquareBrace,
Token::Star => TokenOwned::Star,
@ -95,7 +91,6 @@ impl<'src> Token<'src> {
Token::Integer(_) => "integer",
Token::IsEven => "is_even",
Token::IsOdd => "is_odd",
Token::LeftCurlyBrace => "{",
Token::LeftParenthesis => "(",
Token::LeftSquareBrace => "[",
Token::Length => "length",
@ -104,7 +99,6 @@ impl<'src> Token<'src> {
Token::Minus => "-",
Token::Plus => "+",
Token::ReadLine => "read_line",
Token::RightCurlyBrace => "}",
Token::RightParenthesis => ")",
Token::RightSquareBrace => "]",
Token::Star => "*",
@ -138,7 +132,6 @@ impl<'src> PartialEq for Token<'src> {
(Token::Integer(left), Token::Integer(right)) => left == right,
(Token::IsEven, Token::IsEven) => true,
(Token::IsOdd, Token::IsOdd) => true,
(Token::LeftCurlyBrace, Token::LeftCurlyBrace) => true,
(Token::LeftParenthesis, Token::LeftParenthesis) => true,
(Token::LeftSquareBrace, Token::LeftSquareBrace) => true,
(Token::Length, Token::Length) => true,
@ -147,7 +140,6 @@ impl<'src> PartialEq for Token<'src> {
(Token::Minus, Token::Minus) => true,
(Token::Plus, Token::Plus) => true,
(Token::ReadLine, Token::ReadLine) => true,
(Token::RightCurlyBrace, Token::RightCurlyBrace) => true,
(Token::RightParenthesis, Token::RightParenthesis) => true,
(Token::RightSquareBrace, Token::RightSquareBrace) => true,
(Token::Star, Token::Star) => true,
@ -188,14 +180,12 @@ pub enum TokenOwned {
Equal,
Greater,
GreaterOrEqual,
LeftCurlyBrace,
LeftParenthesis,
LeftSquareBrace,
Less,
LessOrEqual,
Minus,
Plus,
RightCurlyBrace,
RightParenthesis,
RightSquareBrace,
Star,
@ -218,7 +208,6 @@ impl Display for TokenOwned {
TokenOwned::Integer(integer) => write!(f, "{integer}"),
TokenOwned::IsEven => Token::IsEven.fmt(f),
TokenOwned::IsOdd => Token::IsOdd.fmt(f),
TokenOwned::LeftCurlyBrace => Token::LeftCurlyBrace.fmt(f),
TokenOwned::LeftParenthesis => Token::LeftParenthesis.fmt(f),
TokenOwned::LeftSquareBrace => Token::LeftSquareBrace.fmt(f),
TokenOwned::Length => Token::Length.fmt(f),
@ -227,7 +216,6 @@ impl Display for TokenOwned {
TokenOwned::Minus => Token::Minus.fmt(f),
TokenOwned::Plus => Token::Plus.fmt(f),
TokenOwned::ReadLine => Token::ReadLine.fmt(f),
TokenOwned::RightCurlyBrace => Token::RightCurlyBrace.fmt(f),
TokenOwned::RightParenthesis => Token::RightParenthesis.fmt(f),
TokenOwned::RightSquareBrace => Token::RightSquareBrace.fmt(f),
TokenOwned::Star => Token::Star.fmt(f),

View File

@ -51,6 +51,7 @@ pub enum Type {
length: usize,
item_type: Box<Type>,
},
ListOf(Box<Type>),
Map(BTreeMap<Identifier, Type>),
Range,
String,
@ -110,6 +111,11 @@ impl Type {
}
}
}
(Type::ListOf(left), Type::ListOf(right)) => {
if left.check(right).is_ok() {
return Ok(());
}
}
(
Type::Structure {
name: left_name,
@ -154,6 +160,29 @@ impl Type {
return Ok(());
}
(
Type::ListOf(left_type),
Type::List {
item_type: right_type,
..
},
)
| (
Type::List {
item_type: right_type,
..
},
Type::ListOf(left_type),
) => {
if right_type.check(left_type).is_err() {
return Err(TypeConflict {
actual: other.clone(),
expected: self.clone(),
});
} else {
return Ok(());
}
}
(
Type::Function {
type_parameters: left_type_parameters,
@ -245,6 +274,7 @@ impl Display for Type {
}
Type::Integer => write!(f, "int"),
Type::List { length, item_type } => write!(f, "[{length}; {}]", item_type),
Type::ListOf(item_type) => write!(f, "[{}]", item_type),
Type::Map(map) => {
write!(f, "{{ ")?;
@ -319,6 +349,10 @@ mod tests {
}),
Ok(())
);
assert_eq!(
Type::ListOf(Box::new(Type::Integer)).check(&Type::ListOf(Box::new(Type::Integer))),
Ok(())
);
let mut map = BTreeMap::new();
@ -359,6 +393,7 @@ mod tests {
length: 10,
item_type: Box::new(Type::Integer),
},
Type::ListOf(Box::new(Type::Boolean)),
Type::Map(BTreeMap::new()),
Type::Range,
Type::String,
@ -380,4 +415,16 @@ mod tests {
}
}
}
#[test]
fn check_list_types() {
let list = Type::List {
length: 42,
item_type: Box::new(Type::Integer),
};
let list_of = Type::ListOf(Box::new(Type::Integer));
assert_eq!(list.check(&list_of), Ok(()));
assert_eq!(list_of.check(&list), Ok(()));
}
}

View File

@ -1,6 +1,6 @@
//! Virtual machine for running the abstract syntax tree.
use std::{
collections::{BTreeMap, HashMap},
collections::HashMap,
error::Error,
fmt::{self, Display, Formatter},
};
@ -65,7 +65,7 @@ impl Vm {
});
};
variables.insert(identifier.inner, value);
variables.insert(identifier, value);
Ok(None)
}
@ -216,22 +216,6 @@ impl Vm {
Ok(Some(Value::list(values)))
}
Statement::Map(nodes) => {
let mut values = BTreeMap::new();
for (identifier, value_node) in nodes {
let position = value_node.position;
let value = if let Some(value) = self.run_node(value_node, variables)? {
value
} else {
return Err(VmError::ExpectedValue { position });
};
values.insert(identifier.inner, value);
}
Ok(Some(Value::map(values)))
}
Statement::PropertyAccess(left, right) => {
let left_span = left.position;
let left_value = if let Some(value) = self.run_node(*left, variables)? {