AstBuilder: use typedef in decls
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful

This commit is contained in:
Akemi Izuko 2023-11-17 15:46:13 -07:00
parent b6f4672412
commit e0535acc91
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC
3 changed files with 12 additions and 6 deletions

View file

@ -44,7 +44,7 @@ impl ToString for Quantifier {
} }
} }
#[derive(Copy, Clone, Eq)] #[derive(Copy, Clone, Hash, Eq)]
pub enum BaseType { pub enum BaseType {
Int, Int,
Real, Real,

View file

@ -84,6 +84,7 @@ impl ToString for Block {
#[derive(Default, Builder)] #[derive(Default, Builder)]
pub struct Declaration { pub struct Declaration {
type_ident: String,
variable: Variable, variable: Variable,
assn: Option<Expr>, assn: Option<Expr>,
} }
@ -94,7 +95,7 @@ impl ToString for Declaration {
s.push_str(&self.variable.get_quantifier().to_string()); s.push_str(&self.variable.get_quantifier().to_string());
s.push(' '); s.push(' ');
s.push_str(&self.variable.get_base().to_string()); s.push_str(&self.type_ident);
s.push(' '); s.push(' ');
s.push_str(&self.variable.to_string()); s.push_str(&self.variable.to_string());

View file

@ -10,7 +10,7 @@ pub struct AstBuilder {
params: Params, params: Params,
ast: GlobalBlock, ast: GlobalBlock,
name_counter: u64, name_counter: u64,
typedef_map: HashMap<String, BaseType>, typedef_map: HashMap<BaseType, Vec<String>>,
typedef_vec: Vec<(String, BaseType)>, typedef_vec: Vec<(String, BaseType)>,
// Random generation // Random generation
rng: rand::rngs::ThreadRng, rng: rand::rngs::ThreadRng,
@ -23,8 +23,8 @@ impl AstBuilder {
let mut typedef_map = HashMap::new(); let mut typedef_map = HashMap::new();
let mut typedef_vec = Vec::new(); let mut typedef_vec = Vec::new();
typedef_map.insert("integer".to_string(), BaseType::Int); typedef_map.insert(BaseType::Int, vec!["integer".to_string()]);
typedef_map.insert("real".to_string(), BaseType::Real); typedef_map.insert(BaseType::Real, vec!["real".to_string()]);
typedef_vec.push(("integer".to_string(), BaseType::Int)); typedef_vec.push(("integer".to_string(), BaseType::Int));
typedef_vec.push(("real".to_string(), BaseType::Real)); typedef_vec.push(("real".to_string(), BaseType::Real));
@ -80,6 +80,7 @@ impl AstBuilder {
Stat::new_decl( Stat::new_decl(
DeclarationBuilder::default() DeclarationBuilder::default()
.type_ident(self.get_ident_for_type(v.get_base()))
.variable(v) .variable(v)
.assn(Some(assn)) .assn(Some(assn))
.build() .build()
@ -95,7 +96,7 @@ impl AstBuilder {
.unwrap() .unwrap()
.clone(); .clone();
self.typedef_map.insert(new_name.clone(), basetype); self.typedef_map.get_mut(&basetype).unwrap().push(new_name.clone());
self.typedef_vec.push((new_name.clone(), basetype)); self.typedef_vec.push((new_name.clone(), basetype));
Stat::new_typedef( Stat::new_typedef(
@ -140,6 +141,10 @@ impl AstBuilder {
} }
} }
fn get_ident_for_type(&mut self, t: BaseType) -> String {
self.typedef_map[&t].choose(&mut self.rng).unwrap().clone()
}
fn gen_name(&mut self) -> String { fn gen_name(&mut self) -> String {
let s = format!("_{}", self.name_counter); let s = format!("_{}", self.name_counter);
self.name_counter += 1; self.name_counter += 1;