diff options
Diffstat (limited to 'src/parse/ast/expression.rs')
| -rw-r--r-- | src/parse/ast/expression.rs | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/src/parse/ast/expression.rs b/src/parse/ast/expression.rs index 8563492..1749c40 100644 --- a/src/parse/ast/expression.rs +++ b/src/parse/ast/expression.rs @@ -1,10 +1,8 @@ use std::fmt::{self, Display, Formatter}; -use super::value::{ - BinaryOperator, Block, ConditionalBlock, FnHeader, Identifier, Literal, UnaryOperator, -}; +use super::nodes::{BinaryOperator, BlockNode, FnNode, Identifier, IfNode, Literal, UnaryOperator}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Expression { Binary { left: Box<Expression>, @@ -16,15 +14,9 @@ pub enum Expression { right: Box<Expression>, }, Group(Box<Expression>), - Block(Box<Block>), - Fn { - header: FnHeader, - body: Box<Block>, - }, - If { - conditionals: Vec<ConditionalBlock>, - else_block: Option<Box<Block>>, - }, + Block(Box<BlockNode>), + Fn(Box<FnNode>), + If(Box<IfNode>), Literal(Literal), Identifier(Identifier), } @@ -60,16 +52,16 @@ impl Expression { Expression::Block(block) => { Self::block_fmt(f, block, depth + 1)?; } - Expression::Fn { header, body } => { + Expression::Fn(node) => { write!(f, "{}Fn (", pad)?; // Write self receiver - if header.has_self_receiver { + if node.header.has_self_receiver { write!(f, "self, ")?; } // Write parameters - for p in header.parameters.iter() { + for p in node.header.parameters.iter() { write!( f, "{}: {}, ", @@ -82,9 +74,9 @@ impl Expression { writeln!( f, ") -> {}:", - header.return_type.as_ref().unwrap_or(&"_".into()) + node.header.return_type.as_ref().unwrap_or(&"_".into()) )?; - Self::block_fmt(f, body, depth + 1)?; + Self::block_fmt(f, &node.body, depth + 1)?; } Expression::Literal(literal) => { writeln!(f, "{}Literal: {:?}", pad, literal)?; @@ -92,25 +84,25 @@ impl Expression { Expression::Identifier(identifier) => { writeln!(f, "{}Identifier: {:?}", pad, identifier)?; } - Expression::If { conditionals, else_block } => { + Expression::If(node) => { writeln!(f, "{}If:", pad)?; - for (i, c) in conditionals.iter().enumerate() { + for (i, c) in node.conditionals.iter().enumerate() { writeln!(f, "{}- Condition {}:", pad, i)?; c.condition.nested_fmt(f, depth + 1)?; writeln!(f, "{}- Body {}:", pad, i)?; Self::block_fmt(f, &c.block, depth + 1)?; } - if let Some(e) = else_block { + if let Some(e) = &node.else_block { writeln!(f, "{}- Else:", pad)?; Self::block_fmt(f, e, depth + 1)?; } - }, + } } Ok(()) } - fn block_fmt(f: &mut Formatter<'_>, block: &Block, depth: usize) -> fmt::Result { + fn block_fmt(f: &mut Formatter<'_>, block: &BlockNode, depth: usize) -> fmt::Result { let pad = " ".repeat(depth); writeln!(f, "{}Block:", pad)?; for (i, statement) in block.statements.iter().enumerate() { |
