Compare commits

..

2 commits

Author SHA1 Message Date
Akemi Izuko a40d4c3410
AstBuilder: binary op for integers
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful
2023-11-18 00:15:19 -07:00
Akemi Izuko 393349cad5
Params: add recursion depth param 2023-11-18 00:14:44 -07:00
4 changed files with 43 additions and 7 deletions

View file

@ -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

View file

@ -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;

View file

@ -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(&params), rng_float: FloatGenerator::new(&params),
rng_int: IntGenerator::new(&params), rng_int: IntGenerator::new(&params),
@ -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 {

View file

@ -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,
} }
} }