Ast: trait objects on blocks

This commit is contained in:
Akemi Izuko 2023-11-16 15:59:01 -07:00
parent 560423b742
commit ba0aafd146
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC

View file

@ -41,12 +41,30 @@ impl ToString for BaseType {
}
}
#[derive(Builder)]
pub struct Block<T: Statement> {
statements: Vec<T>,
#[derive(Default)]
pub struct GlobalBlock {
statements: Vec<Box<dyn Statement>>,
}
impl<T: Statement> ToString for Block<T> {
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(' ');
}
s.push('\n');
s
}
}
pub struct Block {
statements: Vec<Box<dyn Statement>>,
}
impl ToString for Block {
fn to_string(&self) -> String {
let mut s = String::from("{ ");
@ -60,9 +78,9 @@ impl<T: Statement> ToString for Block<T> {
}
}
impl<T: Statement> AstNode for Block<T> {}
impl AstNode for Block {}
impl<T: Statement> Statement for Block<T> {}
impl Statement for Block {}
pub enum Literal {
Int(i32),