2024-08-07 14:03:33 +00:00
|
|
|
use std::{
|
2024-08-07 15:38:08 +00:00
|
|
|
collections::{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 19:47:37 +00:00
|
|
|
use crate::{Identifier, ReservedIdentifier, Type, Value};
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-07 15:38:08 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-08-07 19:47:37 +00:00
|
|
|
pub struct AbstractSyntaxTree<P> {
|
|
|
|
pub nodes: VecDeque<Node<P>>,
|
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-07 19:47:37 +00:00
|
|
|
pub struct Node<P> {
|
|
|
|
pub statement: Statement<P>,
|
|
|
|
pub position: P,
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 19:47:37 +00:00
|
|
|
impl<P> Node<P> {
|
|
|
|
pub fn new(operation: Statement<P>, position: P) -> Self {
|
2024-08-05 04:40:51 +00:00
|
|
|
Self {
|
|
|
|
statement: operation,
|
2024-08-07 19:47:37 +00:00
|
|
|
position,
|
2024-08-05 04:40:51 +00:00
|
|
|
}
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:47:37 +00:00
|
|
|
impl<P> Display for Node<P> {
|
2024-08-07 14:03:33 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.statement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-08-07 19:47:37 +00:00
|
|
|
pub enum Statement<P> {
|
2024-08-05 03:11:04 +00:00
|
|
|
// Top-level statements
|
2024-08-07 19:47:37 +00:00
|
|
|
Assign(Box<Node<P>>, Box<Node<P>>),
|
2024-08-05 03:11:04 +00:00
|
|
|
|
|
|
|
// Expressions
|
2024-08-07 19:47:37 +00:00
|
|
|
Add(Box<Node<P>>, Box<Node<P>>),
|
|
|
|
PropertyAccess(Box<Node<P>>, Box<Node<P>>),
|
|
|
|
List(Vec<Node<P>>),
|
|
|
|
Multiply(Box<Node<P>>, Box<Node<P>>),
|
2024-08-05 03:11:04 +00:00
|
|
|
|
|
|
|
// Hard-coded values
|
|
|
|
Constant(Value),
|
|
|
|
Identifier(Identifier),
|
2024-08-05 19:54:48 +00:00
|
|
|
ReservedIdentifier(ReservedIdentifier),
|
2024-08-05 03:11:04 +00:00
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-07 19:47:37 +00:00
|
|
|
impl<P> Statement<P> {
|
2024-08-07 14:03:33 +00:00
|
|
|
pub fn expected_type(&self, variables: &HashMap<Identifier, Value>) -> Option<Type> {
|
|
|
|
match self {
|
|
|
|
Statement::Add(left, _) => left.statement.expected_type(variables),
|
|
|
|
Statement::Assign(_, _) => None,
|
|
|
|
Statement::Constant(value) => Some(value.r#type(variables)),
|
|
|
|
Statement::Identifier(identifier) => variables
|
|
|
|
.get(identifier)
|
|
|
|
.map(|value| value.r#type(variables)),
|
|
|
|
Statement::List(_) => None,
|
|
|
|
Statement::Multiply(left, _) => left.statement.expected_type(variables),
|
|
|
|
Statement::PropertyAccess(_, _) => None,
|
|
|
|
Statement::ReservedIdentifier(reserved) => match reserved {
|
2024-08-07 14:41:27 +00:00
|
|
|
ReservedIdentifier::IsEven | ReservedIdentifier::IsOdd => Some(Type::Boolean),
|
2024-08-07 14:03:33 +00:00
|
|
|
ReservedIdentifier::Length => Some(Type::Integer),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:47:37 +00:00
|
|
|
impl<P> Display for Statement<P> {
|
2024-08-07 14:03:33 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-08-07 14:41:27 +00:00
|
|
|
Statement::Assign(left, right) => write!(f, "{left} = {right}"),
|
|
|
|
Statement::Add(left, right) => write!(f, "{left} + {right}"),
|
|
|
|
Statement::PropertyAccess(left, right) => write!(f, "{left}.{right}"),
|
2024-08-07 14:03:33 +00:00
|
|
|
Statement::List(nodes) => {
|
|
|
|
write!(f, "[")?;
|
|
|
|
for (i, node) in nodes.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2024-08-07 14:41:27 +00:00
|
|
|
write!(f, "{node}")?;
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
2024-08-07 14:41:27 +00:00
|
|
|
Statement::Multiply(left, right) => write!(f, "{left} * {right}"),
|
|
|
|
Statement::Constant(value) => write!(f, "{value}"),
|
|
|
|
Statement::Identifier(identifier) => write!(f, "{identifier}"),
|
|
|
|
Statement::ReservedIdentifier(identifier) => write!(f, "{identifier}"),
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|