AstBuilder: typedef statements
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful

This commit is contained in:
Akemi Izuko 2023-11-17 15:32:12 -07:00
parent 4006e0778c
commit fb2866223a
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC
2 changed files with 46 additions and 17 deletions

View file

@ -1,7 +1,7 @@
[global_flow] [global_flow]
gen_decl = 0.7 gen_decl = 0.5
gen_subroutine = 0.0 gen_subroutine = 0.0
gen_typedef = 0.0 gen_typedef = 0.4
end_generation = 0.1 end_generation = 0.1
[statements] [statements]

View file

@ -1,26 +1,18 @@
use rand::Rng; use rand::Rng;
use rand::seq::SliceRandom;
use rand_distr::Distribution; use rand_distr::Distribution;
use std::collections::HashMap;
use crate::params::Params; use crate::params::Params;
use crate::ast::{ use crate::ast::*;
Expr,
Stat,
Quantifier,
BaseType,
GlobalBlock,
Block,
Literal,
Declaration,
DeclarationBuilder,
Variable,
VariableBuilder,
BinaryOperator,
};
pub struct AstBuilder { pub struct AstBuilder {
params: Params, params: Params,
ast: GlobalBlock, ast: GlobalBlock,
name_counter: u64, name_counter: u64,
typedef_map: HashMap<String, BaseType>,
typedef_vec: Vec<(String, BaseType)>,
// Random generation
rng: rand::rngs::ThreadRng, rng: rand::rngs::ThreadRng,
rng_float: FloatGenerator, rng_float: FloatGenerator,
rng_int: IntGenerator, rng_int: IntGenerator,
@ -28,9 +20,19 @@ pub struct AstBuilder {
impl AstBuilder { impl AstBuilder {
pub fn from(params: Params) -> Self { pub fn from(params: Params) -> Self {
let mut typedef_map = HashMap::new();
let mut typedef_vec = Vec::new();
typedef_map.insert("integer".to_string(), BaseType::Int);
typedef_map.insert("real".to_string(), BaseType::Real);
typedef_vec.push(("integer".to_string(), BaseType::Int));
typedef_vec.push(("real".to_string(), BaseType::Real));
Self { Self {
ast: GlobalBlock::default(), ast: GlobalBlock::default(),
name_counter: 0, name_counter: 0,
typedef_map,
typedef_vec,
rng: rand::thread_rng(), rng: rand::thread_rng(),
rng_float: FloatGenerator::new(&params), rng_float: FloatGenerator::new(&params),
rng_int: IntGenerator::new(&params), rng_int: IntGenerator::new(&params),
@ -41,11 +43,17 @@ impl AstBuilder {
pub fn generate(&mut self) { pub fn generate(&mut self) {
let mut p: f64; let mut p: f64;
let p1 = self.params.global_flow.end_generation;
let p2 = p1 + self.params.global_flow.gen_typedef;
loop { loop {
p = self.rng.gen(); p = self.rng.gen();
if p < self.params.global_flow.end_generation { if p < p1 {
break; break;
} else if p < p2 {
let typedef = self.gen_typedef();
self.ast.push(typedef);
} else { } else {
let decl = self.gen_decl(); let decl = self.gen_decl();
self.ast.push(decl); self.ast.push(decl);
@ -79,6 +87,27 @@ impl AstBuilder {
) )
} }
fn gen_typedef(&mut self) -> Stat {
let new_name = self.gen_name();
let (old_name, basetype) = self.typedef_vec
.as_slice()
.choose(&mut self.rng)
.unwrap()
.clone();
self.typedef_map.insert(new_name.clone(), basetype);
self.typedef_vec.push((new_name.clone(), basetype));
Stat::new_typedef(
TypeDefBuilder::default()
.old_name(old_name)
.new_name(new_name)
.basetype(basetype)
.build()
.unwrap()
)
}
fn gen_variable_quantified(&mut self, t: BaseType, q: Quantifier) -> Variable { fn gen_variable_quantified(&mut self, t: BaseType, q: Quantifier) -> Variable {
VariableBuilder::default() VariableBuilder::default()
.type_(t) .type_(t)