2024-08-09 01:59:09 +00:00
|
|
|
//! In-memory representation of a Dust program.
|
2024-08-07 14:03:33 +00:00
|
|
|
use std::{
|
2024-08-09 10:09:59 +00:00
|
|
|
collections::{BTreeMap, HashMap, VecDeque},
|
2024-08-07 14:03:33 +00:00
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
};
|
2024-08-05 03:11:04 +00:00
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-08-07 23:44:01 +00:00
|
|
|
use crate::{BuiltInFunction, Identifier, Span, Type, Value};
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-09 01:59:09 +00:00
|
|
|
/// In-memory representation of a Dust program.
|
2024-08-07 15:38:08 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-08-07 23:03:50 +00:00
|
|
|
pub struct AbstractSyntaxTree {
|
2024-08-09 07:00:48 +00:00
|
|
|
pub nodes: VecDeque<Node<Statement>>,
|
2024-08-07 15:38:08 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-08-09 07:00:48 +00:00
|
|
|
pub struct Node<T> {
|
|
|
|
pub inner: T,
|
2024-08-07 23:03:50 +00:00
|
|
|
pub position: Span,
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 07:00:48 +00:00
|
|
|
impl<T> Node<T> {
|
|
|
|
pub fn new(inner: T, position: Span) -> Self {
|
|
|
|
Self { inner, position }
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 07:00:48 +00:00
|
|
|
impl<T: Display> Display for Node<T> {
|
2024-08-07 14:03:33 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-08-09 07:00:48 +00:00
|
|
|
write!(f, "{}", self.inner)
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-08-07 23:03:50 +00:00
|
|
|
pub enum Statement {
|
2024-08-09 15:41:23 +00:00
|
|
|
// Variable assignment
|
2024-08-09 08:23:02 +00:00
|
|
|
Assignment {
|
2024-08-09 09:18:39 +00:00
|
|
|
identifier: Node<Identifier>,
|
2024-08-09 08:23:02 +00:00
|
|
|
value_node: Box<Node<Statement>>,
|
|
|
|
},
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-09 15:41:23 +00:00
|
|
|
// A sequence of statements
|
|
|
|
Block(Vec<Node<Statement>>),
|
|
|
|
|
2024-08-09 08:23:02 +00:00
|
|
|
// Logic, math and comparison expressions
|
|
|
|
BinaryOperation {
|
|
|
|
left: Box<Node<Statement>>,
|
|
|
|
operator: Node<BinaryOperator>,
|
|
|
|
right: Box<Node<Statement>>,
|
|
|
|
},
|
2024-08-05 03:11:04 +00:00
|
|
|
|
2024-08-09 07:00:48 +00:00
|
|
|
// Function calls
|
2024-08-07 22:24:25 +00:00
|
|
|
BuiltInFunctionCall {
|
|
|
|
function: BuiltInFunction,
|
2024-08-09 07:00:48 +00:00
|
|
|
type_arguments: Option<Vec<Node<Statement>>>,
|
|
|
|
value_arguments: Option<Vec<Node<Statement>>>,
|
2024-08-07 22:24:25 +00:00
|
|
|
},
|
|
|
|
FunctionCall {
|
2024-08-09 07:00:48 +00:00
|
|
|
function: Box<Node<Statement>>,
|
|
|
|
type_arguments: Option<Vec<Node<Statement>>>,
|
|
|
|
value_arguments: Option<Vec<Node<Statement>>>,
|
2024-08-07 22:24:25 +00:00
|
|
|
},
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-09 08:23:02 +00:00
|
|
|
// Property access expression
|
2024-08-09 07:00:48 +00:00
|
|
|
PropertyAccess(Box<Node<Statement>>, Box<Node<Statement>>),
|
|
|
|
|
2024-08-09 08:23:02 +00:00
|
|
|
// Identifier expression
|
|
|
|
Identifier(Identifier),
|
|
|
|
|
|
|
|
// Value collection expressions
|
2024-08-09 07:00:48 +00:00
|
|
|
List(Vec<Node<Statement>>),
|
2024-08-09 10:09:59 +00:00
|
|
|
Map(Vec<(Node<Identifier>, Node<Statement>)>),
|
2024-08-05 03:11:04 +00:00
|
|
|
|
2024-08-09 15:41:23 +00:00
|
|
|
// Hard-coded value
|
2024-08-05 03:11:04 +00:00
|
|
|
Constant(Value),
|
2024-08-09 15:41:23 +00:00
|
|
|
|
|
|
|
// A statement that always returns None. Created with a semicolon, it causes the preceding
|
|
|
|
// statement to return None. This is analagous to the semicolon or unit type in Rust.
|
|
|
|
Nil(Box<Node<Statement>>),
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-07 23:03:50 +00:00
|
|
|
impl Statement {
|
2024-08-07 14:03:33 +00:00
|
|
|
pub fn expected_type(&self, variables: &HashMap<Identifier, Value>) -> Option<Type> {
|
|
|
|
match self {
|
2024-08-09 08:23:02 +00:00
|
|
|
Statement::Assignment { .. } => None,
|
2024-08-09 15:41:23 +00:00
|
|
|
Statement::Block(nodes) => nodes.last().unwrap().inner.expected_type(variables),
|
2024-08-09 08:23:02 +00:00
|
|
|
Statement::BinaryOperation { left, .. } => left.inner.expected_type(variables),
|
|
|
|
Statement::BuiltInFunctionCall { function, .. } => function.expected_return_type(),
|
2024-08-07 14:03:33 +00:00
|
|
|
Statement::Constant(value) => Some(value.r#type(variables)),
|
2024-08-09 07:00:48 +00:00
|
|
|
Statement::FunctionCall { function, .. } => function.inner.expected_type(variables),
|
2024-08-07 14:03:33 +00:00
|
|
|
Statement::Identifier(identifier) => variables
|
|
|
|
.get(identifier)
|
|
|
|
.map(|value| value.r#type(variables)),
|
2024-08-09 10:09:59 +00:00
|
|
|
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))
|
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
Statement::PropertyAccess(_, _) => None,
|
2024-08-09 15:41:23 +00:00
|
|
|
Statement::Nil(_) => None,
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 23:03:50 +00:00
|
|
|
impl Display for Statement {
|
2024-08-07 14:03:33 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-08-09 08:23:02 +00:00
|
|
|
Statement::Assignment {
|
|
|
|
identifier,
|
|
|
|
value_node: value,
|
|
|
|
} => {
|
|
|
|
write!(f, "{identifier} = {value}")
|
|
|
|
}
|
2024-08-09 15:41:23 +00:00
|
|
|
Statement::Block(statements) => {
|
|
|
|
write!(f, "{{ ")?;
|
|
|
|
|
|
|
|
for (i, statement) in statements.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, " ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{statement}")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, " }}")
|
|
|
|
}
|
2024-08-09 08:23:02 +00:00
|
|
|
Statement::BinaryOperation {
|
|
|
|
left,
|
|
|
|
operator,
|
|
|
|
right,
|
|
|
|
} => {
|
|
|
|
write!(f, "{left} {operator} {right}")
|
|
|
|
}
|
2024-08-07 22:24:25 +00:00
|
|
|
Statement::BuiltInFunctionCall {
|
|
|
|
function,
|
|
|
|
type_arguments: type_parameters,
|
|
|
|
value_arguments: value_parameters,
|
|
|
|
} => {
|
|
|
|
write!(f, "{function}")?;
|
|
|
|
|
|
|
|
if let Some(type_parameters) = type_parameters {
|
|
|
|
write!(f, "<")?;
|
|
|
|
|
|
|
|
for (i, type_parameter) in type_parameters.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{type_parameter}")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
|
|
|
if let Some(value_parameters) = value_parameters {
|
|
|
|
for (i, value_parameter) in value_parameters.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{value_parameter}")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
2024-08-09 07:00:48 +00:00
|
|
|
Statement::Constant(value) => write!(f, "{value}"),
|
2024-08-07 22:24:25 +00:00
|
|
|
Statement::FunctionCall {
|
|
|
|
function,
|
|
|
|
type_arguments: type_parameters,
|
|
|
|
value_arguments: value_parameters,
|
|
|
|
} => {
|
|
|
|
write!(f, "{function}")?;
|
|
|
|
|
|
|
|
if let Some(type_parameters) = type_parameters {
|
|
|
|
write!(f, "<")?;
|
|
|
|
|
|
|
|
for (i, type_parameter) in type_parameters.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{type_parameter}")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
|
|
|
if let Some(value_parameters) = value_parameters {
|
|
|
|
for (i, value_parameter) in value_parameters.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{value_parameter}")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
2024-08-09 07:00:48 +00:00
|
|
|
Statement::Identifier(identifier) => write!(f, "{identifier}"),
|
2024-08-07 14:03:33 +00:00
|
|
|
Statement::List(nodes) => {
|
|
|
|
write!(f, "[")?;
|
2024-08-09 10:09:59 +00:00
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
for (i, node) in nodes.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2024-08-09 10:09:59 +00:00
|
|
|
|
2024-08-07 14:41:27 +00:00
|
|
|
write!(f, "{node}")?;
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
2024-08-09 10:09:59 +00:00
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
write!(f, "]")
|
|
|
|
}
|
2024-08-09 10:09:59 +00:00
|
|
|
Statement::Map(nodes) => {
|
|
|
|
write!(f, "{{")?;
|
|
|
|
|
|
|
|
for (i, (identifier, node)) in nodes.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{identifier} = {node}")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
2024-08-09 15:41:23 +00:00
|
|
|
Statement::Nil(node) => write!(f, "{node};"),
|
2024-08-09 07:00:48 +00:00
|
|
|
Statement::PropertyAccess(left, right) => write!(f, "{left}.{right}"),
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-09 07:00:48 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-08-09 08:23:02 +00:00
|
|
|
pub enum BinaryOperator {
|
2024-08-09 08:56:24 +00:00
|
|
|
// Math
|
2024-08-09 08:23:02 +00:00
|
|
|
Add,
|
|
|
|
Divide,
|
2024-08-09 11:02:55 +00:00
|
|
|
Modulo,
|
2024-08-09 08:56:24 +00:00
|
|
|
Multiply,
|
|
|
|
Subtract,
|
|
|
|
|
|
|
|
// Comparison
|
2024-08-09 11:15:09 +00:00
|
|
|
Equal,
|
2024-08-09 08:23:02 +00:00
|
|
|
Greater,
|
|
|
|
GreaterOrEqual,
|
|
|
|
Less,
|
|
|
|
LessOrEqual,
|
2024-08-09 08:56:24 +00:00
|
|
|
|
|
|
|
// Logic
|
|
|
|
And,
|
|
|
|
Or,
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 08:23:02 +00:00
|
|
|
impl Display for BinaryOperator {
|
2024-08-09 07:00:48 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2024-08-09 08:23:02 +00:00
|
|
|
BinaryOperator::Add => write!(f, "+"),
|
2024-08-09 08:56:24 +00:00
|
|
|
BinaryOperator::And => write!(f, "&&"),
|
2024-08-09 08:23:02 +00:00
|
|
|
BinaryOperator::Divide => write!(f, "/"),
|
2024-08-09 11:15:09 +00:00
|
|
|
BinaryOperator::Equal => write!(f, "=="),
|
2024-08-09 08:23:02 +00:00
|
|
|
BinaryOperator::Greater => write!(f, ">"),
|
|
|
|
BinaryOperator::GreaterOrEqual => write!(f, ">="),
|
|
|
|
BinaryOperator::Less => write!(f, "<"),
|
|
|
|
BinaryOperator::LessOrEqual => write!(f, "<="),
|
2024-08-09 11:02:55 +00:00
|
|
|
BinaryOperator::Modulo => write!(f, "%"),
|
2024-08-09 08:23:02 +00:00
|
|
|
BinaryOperator::Multiply => write!(f, "*"),
|
2024-08-09 08:56:24 +00:00
|
|
|
BinaryOperator::Or => write!(f, "||"),
|
2024-08-09 08:23:02 +00:00
|
|
|
BinaryOperator::Subtract => write!(f, "-"),
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|