This commit is contained in:
parent
1176c5149d
commit
59be92491f
2 changed files with 100 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
|
95
src/ast/statement.rs
Normal file
95
src/ast/statement.rs
Normal file
|
@ -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<Block>),
|
||||
GlobalBlock(Box<GlobalBlock>),
|
||||
}
|
||||
|
||||
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<Stat>,
|
||||
}
|
||||
|
||||
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<Expr>,
|
||||
}
|
||||
|
||||
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<Stat>,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue