AstBuilder: operators for reals
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful
This commit is contained in:
parent
a40d4c3410
commit
a1ae14934b
3 changed files with 41 additions and 9 deletions
15
params.toml
15
params.toml
|
@ -74,20 +74,21 @@
|
|||
expr_neg = 0.0
|
||||
|
||||
get_instant = 0.4
|
||||
max_depth = 20
|
||||
max_depth = 10
|
||||
|
||||
[types.gen_real]
|
||||
expr_add = 0.0
|
||||
expr_sub = 0.0
|
||||
expr_mul = 0.0
|
||||
expr_div = 0.0
|
||||
expr_add = 0.1
|
||||
expr_sub = 0.1
|
||||
expr_mul = 0.1
|
||||
expr_div = 0.3
|
||||
expr_rem = 0.0
|
||||
expr_exp = 0.0
|
||||
expr_pos = 0.0
|
||||
expr_neg = 0.0
|
||||
|
||||
gen_integer = 0.0
|
||||
get_instant = 0.0
|
||||
gen_integer = 0.1
|
||||
get_instant = 0.3
|
||||
max_depth = 10
|
||||
|
||||
[types.gen_tuple]
|
||||
gen_each_field = 0.5
|
||||
|
|
|
@ -9,6 +9,7 @@ use crate::ast::*;
|
|||
#[derive(Default)]
|
||||
struct StateTracking {
|
||||
int_recursion_depth: u32,
|
||||
real_recursion_depth: u32,
|
||||
}
|
||||
|
||||
pub struct AstBuilder {
|
||||
|
@ -153,11 +154,40 @@ impl AstBuilder {
|
|||
} else {
|
||||
Expr::new_literal(self.gen_literal(BaseType::Int)) // TODO: this shouldn't be here
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn gen_real(&mut self) -> Expr {
|
||||
Expr::new_literal(self.gen_literal(BaseType::Real))
|
||||
let p: f64 = self.rng.gen();
|
||||
let mut ps = &self.params.types.gen_real;
|
||||
|
||||
self.state.real_recursion_depth += 1;
|
||||
|
||||
if p < ps.get_instant || self.state.real_recursion_depth >= ps.max_depth {
|
||||
self.state.real_recursion_depth -= 1;
|
||||
return Expr::new_literal(self.gen_literal(BaseType::Real));
|
||||
} else if p < ps.get_instant + ps.gen_integer {
|
||||
self.state.real_recursion_depth -= 1;
|
||||
return self.gen_integer();
|
||||
}
|
||||
|
||||
let lhs = self.gen_real();
|
||||
let rhs = self.gen_real();
|
||||
self.state.real_recursion_depth -= 1;
|
||||
|
||||
ps = &self.params.types.gen_real;
|
||||
let prevp = ps.get_instant + ps.gen_integer;
|
||||
|
||||
if p < prevp + ps.expr_add {
|
||||
Expr::new_binary_op(BinaryOperator::add(lhs, rhs))
|
||||
} else if p < prevp + ps.expr_add + ps.expr_sub {
|
||||
Expr::new_binary_op(BinaryOperator::subtract(lhs, rhs))
|
||||
} else if p < prevp + ps.expr_add + ps.expr_sub + ps.expr_mul {
|
||||
Expr::new_binary_op(BinaryOperator::multiply(lhs, rhs))
|
||||
} else if p < prevp + 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::Real)) // TODO: this shouldn't be here
|
||||
}
|
||||
}
|
||||
|
||||
fn gen_literal(&mut self, t: BaseType) -> Literal {
|
||||
|
|
|
@ -148,6 +148,7 @@ toml_struct!{
|
|||
expr_neg: f64,
|
||||
gen_integer: f64,
|
||||
get_instant: f64,
|
||||
max_depth: u32,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue