use crate::declaration::Parameter;
use crate::operator::{ArithmeticOperator, BinaryOperator, UnaryOperator};
use crate::types::Type;
#[derive(Copy, Clone, Debug, Default)]
pub struct Span {
pub line: u32,
pub column: u32,
}
impl std::fmt::Display for Span {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}:{}", self.line, self.column)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Binding {
Value,
Reference,
Variable,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Literal {
Bool(bool),
Integer(u128),
Float(f64),
String(Vec<u8>),
Null,
}
#[derive(Clone, Debug)]
pub struct Expression {
pub kind: ExpressionKind,
pub resolved_type: Option<Type>,
pub span: Span,
}
impl Expression {
pub fn new(kind: ExpressionKind, resolved_type: Option<Type>) -> Self {
Self {
kind,
resolved_type,
span: Span::default(),
}
}
pub fn with_span(kind: ExpressionKind, resolved_type: Option<Type>, span: Span) -> Self {
Self {
kind,
resolved_type,
span,
}
}
}
#[derive(Clone, Debug)]
pub struct Block {
pub statements: Vec<Statement>,
pub result: Option<Box<Expression>>,
}
impl Block {
pub fn empty() -> Self {
Self {
statements: Vec::new(),
result: None,
}
}
pub fn expression(expression: Expression) -> Self {
Self {
statements: Vec::new(),
result: Some(Box::new(expression)),
}
}
}
#[derive(Clone, Debug)]
pub struct MatchArm {
pub pattern: MatchPattern,
pub body: Block,
}
#[derive(Clone, Debug)]
pub enum MatchPattern {
Integer(u128),
Variable(String),
Wildcard,
Variant(String, String),
}
#[derive(Clone, Debug)]
pub enum Statement {
Expression(Expression),
Let {
name: String,
binding: Binding,
declared_type: Option<Type>,
value: Expression,
},
Assign(Expression, Expression),
Return(Option<Expression>),
Label {
name: String,
parameters: Vec<Parameter>,
initial_arguments: Vec<Expression>,
},
Jump {
label: String,
arguments: Vec<Expression>,
},
MultiReplace {
bindings: Vec<Option<(String, Binding)>>,
targets: Vec<Expression>,
values: Vec<Expression>,
},
Defer(Box<Self>),
}
#[derive(Clone, Debug)]
pub enum ExpressionKind {
Literal(Literal),
Variable(String),
BinaryOperation(BinaryOperator, Box<Expression>, Box<Expression>),
UnaryOperation(UnaryOperator, Box<Expression>),
Call(Box<Expression>, Vec<Expression>),
Field(Box<Expression>, String),
Index(Box<Expression>, Box<Expression>),
Dereference(Box<Expression>),
Convert(Box<Expression>, Type),
Transmute(Box<Expression>, Type),
SizeOf(Type),
TypeConstruction(String, Vec<(String, Expression)>),
ArrayLiteral(Vec<Expression>),
TupleLiteral(Vec<Expression>),
Block(Block),
If {
condition: Box<Expression>,
then_branch: Block,
else_branch: Option<Block>,
},
Match {
value: Box<Expression>,
arms: Vec<MatchArm>,
},
Replace(Box<Expression>, Box<Expression>),
OpAssign(ArithmeticOperator, Box<Expression>, Box<Expression>),
Slice(
Box<Expression>,
Option<Box<Expression>>,
Option<Box<Expression>>,
),
Print(Vec<Expression>),
}