Compare commits
2 commits
e0535acc91
...
a40d4c3410
Author | SHA1 | Date | |
---|---|---|---|
Akemi Izuko | a40d4c3410 | ||
Akemi Izuko | 393349cad5 |
11
params.toml
11
params.toml
|
@ -64,16 +64,17 @@
|
||||||
get_instant = 0.0
|
get_instant = 0.0
|
||||||
|
|
||||||
[types.gen_integer]
|
[types.gen_integer]
|
||||||
expr_add = 0.0
|
expr_add = 0.2
|
||||||
expr_sub = 0.0
|
expr_sub = 0.2
|
||||||
expr_mul = 0.0
|
expr_mul = 0.1
|
||||||
expr_div = 0.0
|
expr_div = 0.1
|
||||||
expr_rem = 0.0
|
expr_rem = 0.0
|
||||||
expr_exp = 0.0
|
expr_exp = 0.0
|
||||||
expr_pos = 0.0
|
expr_pos = 0.0
|
||||||
expr_neg = 0.0
|
expr_neg = 0.0
|
||||||
|
|
||||||
get_instant = 0.0
|
get_instant = 0.4
|
||||||
|
max_depth = 20
|
||||||
|
|
||||||
[types.gen_real]
|
[types.gen_real]
|
||||||
expr_add = 0.0
|
expr_add = 0.0
|
||||||
|
|
|
@ -135,7 +135,7 @@ impl BinaryOperator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub(left: Expr, right: Expr) -> Self {
|
pub fn subtract(left: Expr, right: Expr) -> Self {
|
||||||
let is_both_same = left.get_base() == right.get_base();
|
let is_both_same = left.get_base() == right.get_base();
|
||||||
let is_one_real = left.get_base() == BaseType::Real || right.get_base() == BaseType::Real;
|
let is_one_real = left.get_base() == BaseType::Real || right.get_base() == BaseType::Real;
|
||||||
let is_one_int = left.get_base() == BaseType::Int || right.get_base() == BaseType::Int;
|
let is_one_int = left.get_base() == BaseType::Int || right.get_base() == BaseType::Int;
|
||||||
|
|
|
@ -6,12 +6,18 @@ use std::collections::HashMap;
|
||||||
use crate::params::Params;
|
use crate::params::Params;
|
||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct StateTracking {
|
||||||
|
int_recursion_depth: u32,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AstBuilder {
|
pub struct AstBuilder {
|
||||||
params: Params,
|
params: Params,
|
||||||
ast: GlobalBlock,
|
ast: GlobalBlock,
|
||||||
name_counter: u64,
|
name_counter: u64,
|
||||||
typedef_map: HashMap<BaseType, Vec<String>>,
|
typedef_map: HashMap<BaseType, Vec<String>>,
|
||||||
typedef_vec: Vec<(String, BaseType)>,
|
typedef_vec: Vec<(String, BaseType)>,
|
||||||
|
state: StateTracking,
|
||||||
// Random generation
|
// Random generation
|
||||||
rng: rand::rngs::ThreadRng,
|
rng: rand::rngs::ThreadRng,
|
||||||
rng_float: FloatGenerator,
|
rng_float: FloatGenerator,
|
||||||
|
@ -33,6 +39,7 @@ impl AstBuilder {
|
||||||
name_counter: 0,
|
name_counter: 0,
|
||||||
typedef_map,
|
typedef_map,
|
||||||
typedef_vec,
|
typedef_vec,
|
||||||
|
state: StateTracking::default(),
|
||||||
rng: rand::thread_rng(),
|
rng: rand::thread_rng(),
|
||||||
rng_float: FloatGenerator::new(¶ms),
|
rng_float: FloatGenerator::new(¶ms),
|
||||||
rng_int: IntGenerator::new(¶ms),
|
rng_int: IntGenerator::new(¶ms),
|
||||||
|
@ -119,7 +126,34 @@ impl AstBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_integer(&mut self) -> Expr {
|
fn gen_integer(&mut self) -> Expr {
|
||||||
Expr::new_literal(self.gen_literal(BaseType::Int))
|
let p: f64 = self.rng.gen();
|
||||||
|
let mut ps = &self.params.types.gen_integer;
|
||||||
|
|
||||||
|
self.state.int_recursion_depth += 1;
|
||||||
|
|
||||||
|
if p < ps.get_instant || self.state.int_recursion_depth >= ps.max_depth {
|
||||||
|
self.state.int_recursion_depth -= 1;
|
||||||
|
return Expr::new_literal(self.gen_literal(BaseType::Int));
|
||||||
|
}
|
||||||
|
|
||||||
|
let lhs = self.gen_integer();
|
||||||
|
let rhs = self.gen_integer();
|
||||||
|
self.state.int_recursion_depth -= 1;
|
||||||
|
|
||||||
|
ps = &self.params.types.gen_integer;
|
||||||
|
|
||||||
|
if p < ps.get_instant + ps.expr_add {
|
||||||
|
Expr::new_binary_op(BinaryOperator::add(lhs, rhs))
|
||||||
|
} else if p < ps.get_instant + ps.expr_add + ps.expr_sub {
|
||||||
|
Expr::new_binary_op(BinaryOperator::subtract(lhs, rhs))
|
||||||
|
} else if p < ps.get_instant + ps.expr_add + ps.expr_sub + ps.expr_mul {
|
||||||
|
Expr::new_binary_op(BinaryOperator::multiply(lhs, rhs))
|
||||||
|
} else if p < ps.get_instant + ps.expr_add + ps.expr_sub + ps.expr_mul + ps.expr_div {
|
||||||
|
Expr::new_binary_op(BinaryOperator::divide(lhs, rhs))
|
||||||
|
} else {
|
||||||
|
Expr::new_literal(self.gen_literal(BaseType::Int)) // TODO: this shouldn't be here
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_real(&mut self) -> Expr {
|
fn gen_real(&mut self) -> Expr {
|
||||||
|
|
|
@ -132,6 +132,7 @@ toml_struct!{
|
||||||
expr_pos: f64,
|
expr_pos: f64,
|
||||||
expr_neg: f64,
|
expr_neg: f64,
|
||||||
get_instant: f64,
|
get_instant: f64,
|
||||||
|
max_depth: u32,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue