aboutsummaryrefslogtreecommitdiff
path: root/src/parse/ast
diff options
context:
space:
mode:
Diffstat (limited to 'src/parse/ast')
-rw-r--r--src/parse/ast/expression.rs15
-rw-r--r--src/parse/ast/mod.rs2
-rw-r--r--src/parse/ast/nodes.rs6
-rw-r--r--src/parse/ast/statement.rs13
4 files changed, 34 insertions, 2 deletions
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<BlockNode>),
Fn(Box<FnNode>),
If(Box<IfNode>),
+ Loop(Box<LoopNode>),
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
@@ -107,6 +107,12 @@ pub struct IfNode {
}
#[derive(Debug, Clone)]
+pub struct LoopNode {
+ pub condition: Option<Expression>,
+ pub body: BlockNode,
+}
+
+#[derive(Debug, Clone)]
pub struct BlockNode {
pub statements: Vec<Statement>,
pub tail_expression: Option<Expression>,
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<Expression>),
+ 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(())