From 8a6eb35a900081967db16d313ab7ed470de6570f Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 23 Oct 2021 22:01:52 +0200 Subject: Loop expressions and concrete walker errors. --- src/parse/ast/expression.rs | 15 ++++++++++++++- src/parse/ast/mod.rs | 2 +- src/parse/ast/nodes.rs | 6 ++++++ src/parse/ast/statement.rs | 13 +++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) (limited to 'src/parse/ast') diff --git a/src/parse/ast/expression.rs b/src/parse/ast/expression.rs index 1749c40..c7f6d9b 100644 --- a/src/parse/ast/expression.rs +++ b/src/parse/ast/expression.rs @@ -1,6 +1,8 @@ use std::fmt::{self, Display, Formatter}; -use super::nodes::{BinaryOperator, BlockNode, FnNode, Identifier, IfNode, Literal, UnaryOperator}; +use super::nodes::{ + BinaryOperator, BlockNode, FnNode, Identifier, IfNode, Literal, LoopNode, UnaryOperator, +}; #[derive(Debug, Clone)] pub enum Expression { @@ -17,6 +19,7 @@ pub enum Expression { Block(Box), Fn(Box), If(Box), + Loop(Box), Literal(Literal), Identifier(Identifier), } @@ -97,6 +100,16 @@ impl Expression { Self::block_fmt(f, e, depth + 1)?; } } + Expression::Loop(node) => { + writeln!(f, "{}Loop:", pad)?; + if let Some(loop_condition) = &node.condition { + writeln!(f, "{}- Condition:", pad)?; + loop_condition.nested_fmt(f, depth + 1)?; + } + + writeln!(f, "{}- Body:", pad)?; + Self::block_fmt(f, &node.body, depth + 1)?; + } } Ok(()) diff --git a/src/parse/ast/mod.rs b/src/parse/ast/mod.rs index 93944b2..7d0b58b 100644 --- a/src/parse/ast/mod.rs +++ b/src/parse/ast/mod.rs @@ -3,8 +3,8 @@ use std::fmt::Display; use self::statement::Statement; pub mod expression; -pub mod statement; pub mod nodes; +pub mod statement; #[derive(Debug)] pub struct Program { diff --git a/src/parse/ast/nodes.rs b/src/parse/ast/nodes.rs index 822ffb6..2a4f7c0 100644 --- a/src/parse/ast/nodes.rs +++ b/src/parse/ast/nodes.rs @@ -106,6 +106,12 @@ pub struct IfNode { pub else_block: Option, } +#[derive(Debug, Clone)] +pub struct LoopNode { + pub condition: Option, + pub body: BlockNode, +} + #[derive(Debug, Clone)] pub struct BlockNode { pub statements: Vec, diff --git a/src/parse/ast/statement.rs b/src/parse/ast/statement.rs index b0e626d..99ee0e0 100644 --- a/src/parse/ast/statement.rs +++ b/src/parse/ast/statement.rs @@ -6,6 +6,8 @@ use super::expression::Expression; pub enum Statement { Expression(Expression), Print(Expression), + Break(Option), + Continue, Return(Expression), } @@ -32,6 +34,17 @@ impl Statement { writeln!(f, "{}Return:", pad)?; expression.nested_fmt(f, depth + 1)?; } + Statement::Break(expression) => { + if let Some(returned_on_break) = expression { + writeln!(f, "{}Break:", pad)?; + returned_on_break.nested_fmt(f, depth + 1)?; + } else { + writeln!(f, "{}Break", pad)?; + } + } + Statement::Continue => { + writeln!(f, "{}Continue", pad)?; + } } Ok(()) -- cgit 1.4.1