diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 1df0230..80f595d 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,4 +1,5 @@ mod expr; +mod statement; use expr::{ Expr, @@ -7,6 +8,10 @@ use expr::{ BinaryOperator, }; +use statement::Stat; +use statement::Block; +use statement::GlobalBlock; + pub trait GazType { fn get_base(&self) -> BaseType; } diff --git a/src/ast/statement.rs b/src/ast/statement.rs new file mode 100644 index 0000000..aadc6d5 --- /dev/null +++ b/src/ast/statement.rs @@ -0,0 +1,95 @@ +use std::string::ToString; + +use super::BaseType; +use super::Quantifier; +use super::Expr; +use super::Variable; +use super::GazType; + +pub enum Stat { + Block(Box), + GlobalBlock(Box), +} + +impl ToString for Stat { + fn to_string(&self) -> String { + match self { + Stat::Block(x) => x.to_string(), + Stat::GlobalBlock(x) => x.to_string(), + } + } +} + +pub struct Block { + statements: Vec, +} + +impl Block { + pub fn push(&mut self, node: Stat) { + self.statements.push(node); + } +} + +impl ToString for Block { + fn to_string(&self) -> String { + let mut s = String::from("{ "); + + for stat in &self.statements { + s.push_str(&stat.to_string()); + s.push(' '); + } + + s.push('}'); + s + } +} + +#[derive(Default, Builder)] +pub struct Declaration { + variable: Variable, + assn: Option, +} + +impl ToString for Declaration { + fn to_string(&self) -> String { + let mut s = String::new(); + + s.push_str(&self.variable.get_quantifier().to_string()); + s.push(' '); + s.push_str(&self.variable.get_base().to_string()); + s.push(' '); + s.push_str(&self.variable.to_string()); + + if self.assn.is_some() { + s.push_str(" = "); + s.push_str(&self.assn.as_ref().unwrap().to_string()); + } + + s.push(';'); + s + } +} + +#[derive(Default)] +pub struct GlobalBlock { + statements: Vec, +} + +impl GlobalBlock { + pub fn push(&mut self, node: Stat) { + self.statements.push(node); + } +} + +impl ToString for GlobalBlock { + fn to_string(&self) -> String { + let mut s = String::new(); + + for stat in &self.statements { + s.push_str(&stat.to_string()); + } + + s.push('\n'); + s + } +}