1
0
dust/dust-lang/src/abstract_tree.rs

94 lines
3.1 KiB
Rust
Raw Normal View History

use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
};
2024-08-05 03:11:04 +00:00
use serde::{Deserialize, Serialize};
use crate::{Identifier, ReservedIdentifier, Span, Type, Value};
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
2024-08-05 03:11:04 +00:00
pub struct Node {
2024-08-05 04:40:51 +00:00
pub statement: Statement,
2024-08-05 03:11:04 +00:00
pub span: Span,
}
impl Node {
pub fn new(operation: Statement, span: Span) -> Self {
2024-08-05 04:40:51 +00:00
Self {
statement: operation,
span,
}
2024-08-05 03:11:04 +00:00
}
}
impl Display for Node {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.statement)
}
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
2024-08-05 03:11:04 +00:00
pub enum Statement {
// Top-level statements
2024-08-05 04:40:51 +00:00
Assign(Box<Node>, Box<Node>),
2024-08-05 03:11:04 +00:00
// Expressions
2024-08-05 04:40:51 +00:00
Add(Box<Node>, Box<Node>),
2024-08-07 14:41:27 +00:00
BuiltInValue(Box<Node>),
2024-08-05 18:31:08 +00:00
PropertyAccess(Box<Node>, Box<Node>),
2024-08-05 03:11:04 +00:00
List(Vec<Node>),
2024-08-05 04:40:51 +00:00
Multiply(Box<Node>, Box<Node>),
2024-08-05 03:11:04 +00:00
// Hard-coded values
Constant(Value),
Identifier(Identifier),
ReservedIdentifier(ReservedIdentifier),
2024-08-05 03:11:04 +00:00
}
impl Statement {
pub fn expected_type(&self, variables: &HashMap<Identifier, Value>) -> Option<Type> {
match self {
Statement::Add(left, _) => left.statement.expected_type(variables),
Statement::Assign(_, _) => None,
2024-08-07 14:41:27 +00:00
Statement::BuiltInValue(reserved) => reserved.statement.expected_type(variables),
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),
ReservedIdentifier::Length => Some(Type::Integer),
},
}
}
}
impl Display for Statement {
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::BuiltInValue(reserved) => write!(f, "{reserved}"),
Statement::PropertyAccess(left, right) => write!(f, "{left}.{right}"),
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}")?;
}
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}"),
}
}
}