use std::fmt::Debug; use crate::token::Span; /// The [Phase] trait is used for type state. The AST can be in one of multiple /// type states: /// 1. [Parsed] - AST that was produced through parsing. pub trait Phase { type ExtraData: PartialEq + Debug; } /// See [Phase] for more information. #[derive(Debug)] pub struct Parsed; impl Phase for Parsed { type ExtraData = (); } pub type ParsedExpression = Expression; /// This represents an expression in the source code. It holds the /// [ExpressionKind], the [Span] and extra information according to the [Phase]. #[derive(Debug, PartialEq)] pub struct Expression { pub kind: ExpressionKind

, pub span: Span, pub extra: P::ExtraData, } /// Represents the different kinds of [Expression]s, e.g. literals, unary or /// binary expressions. #[derive(Debug, PartialEq)] pub enum ExpressionKind { Identifier(String), LitString(String), LitInteger(u64), LitBool(bool), Unary { op: UnaryOp, op_span: Span, operand: Box>, }, Binary { op: BinaryOp, op_span: Span, left: Box>, right: Box>, }, Call { func: Box>, args: Vec>, }, Index { expr: Box>, index: Box>, }, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum UnaryOp { /// Bitwise Not BitNot, /// Logical Not Not, /// Negate Neg, /// Address Of AddrOf, /// Deref Deref, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum BinaryOp { /// Addition Add, /// Subtraction Sub, /// Multiplication Mul, /// Division Div, /// Remainder Rem, /// Bitwise And BitAnd, /// Bitwise Or BitOr, /// Bitwise Xor BitXor, /// Bitwise Shift Left BitShl, /// Bitwise Shift Right BitShr, /// Logical And And, /// Logical Or Or, /// Equal Eq, /// Not Equal Ne, /// Less than Lt, /// Less than or Equal Le, /// Greater than Gt, /// Greater than or Equal Ge, /// Assign Assign, /// Member Access Dot, }