Params: add verification pass to params
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful

This commit is contained in:
Akemi Izuko 2023-11-18 13:09:30 -07:00
parent 1652ca66ff
commit 4f6f81714d
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC
3 changed files with 194 additions and 12 deletions

View file

@ -1,3 +1,5 @@
verify_config = true
[global_flow] [global_flow]
gen_decl = 0.6 gen_decl = 0.6
gen_subroutine = 0.0 gen_subroutine = 0.0
@ -18,7 +20,7 @@
gen_integer = 0.5 gen_integer = 0.5
gen_real = 0.5 gen_real = 0.5
gen_tuple = 0.0 gen_tuple = 0.0
qual_const = 0.0 qual_const = 1.0
qual_var = 0.0 qual_var = 0.0
[statements.gen_global_decl] [statements.gen_global_decl]

View file

@ -17,12 +17,17 @@ struct Args {
config: PathBuf, config: PathBuf,
} }
fn main() { fn main() -> Result<(), String> {
let args = Args::parse(); let args = Args::parse();
let params = Params::parse(&args.config); let params = Params::parse(&args.config);
let mut builder = AstBuilder::from(params);
if params.verify_config {
params.verify()?;
}
let mut builder = AstBuilder::from(params);
builder.generate(); builder.generate();
println!("{}", builder.to_string()); println!("{}", builder.to_string());
Ok(())
} }

View file

@ -22,6 +22,7 @@ macro_rules! toml_struct {
toml_struct!{ toml_struct!{
struct Params { struct Params {
verify_config: bool,
global_flow: GlobalFlow, global_flow: GlobalFlow,
statements: Statments, statements: Statments,
types: Types, types: Types,
@ -29,6 +30,37 @@ toml_struct!{
} }
} }
impl Params {
pub fn parse(p: &PathBuf) -> Self {
let file_string = fs::read_to_string(p)
.expect("Failed to read parameters file into string");
let params: Params = toml::from_str(&file_string)
.expect("Failed to parse TOML from parameters file");
params
}
pub fn verify(&self) -> Result<(), String> {
let global_flow = self.global_flow.verify();
let statements = self.statements.verify();
let types = self.types.verify();
let dev = self.dev.verify();
if global_flow.is_err() {
global_flow
} else if statements.is_err() {
statements
} else if types.is_err() {
types
} else if dev.is_err() {
dev
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct GlobalFlow { struct GlobalFlow {
gen_decl: f64, gen_decl: f64,
@ -38,6 +70,18 @@ toml_struct!{
} }
} }
impl GlobalFlow {
fn verify(&self) -> Result<(), String> {
let sum = self.gen_decl + self.gen_subroutine + self.gen_typedef + self.end_generation;
if (sum - 1.0).abs() > f64::EPSILON {
Err(format!("global_flow probability sum is = {}", sum))
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct Statments { struct Statments {
gen_assign: GenAssign, gen_assign: GenAssign,
@ -48,6 +92,30 @@ toml_struct!{
} }
} }
impl Statments {
fn verify(&self) -> Result<(), String> {
let gen_assign = self.gen_assign.verify();
let gen_decl = self.gen_decl.verify();
let gen_global_decl = self.gen_global_decl.verify();
let gen_if_statement = self.gen_if_statement.verify();
let gen_loop = self.gen_loop.verify();
if gen_assign.is_err() {
gen_assign
} else if gen_decl.is_err() {
gen_decl
} else if gen_global_decl.is_err() {
gen_global_decl
} else if gen_if_statement.is_err() {
gen_if_statement
} else if gen_loop.is_err() {
gen_loop
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct GenAssign { struct GenAssign {
gen_boolean: f64, gen_boolean: f64,
@ -58,6 +126,12 @@ toml_struct!{
} }
} }
impl GenAssign {
fn verify(&self) -> Result<(), String> {
Ok(())
}
}
toml_struct!{ toml_struct!{
struct GenDecl { struct GenDecl {
gen_boolean: f64, gen_boolean: f64,
@ -70,6 +144,26 @@ toml_struct!{
} }
} }
impl GenDecl {
fn verify(&self) -> Result<(), String> {
let sum_type = self.gen_boolean
+ self.gen_character
+ self.gen_integer
+ self.gen_real
+ self.gen_tuple;
let sum_qual = self.qual_const + self.qual_var;
if (sum_type - 1.0).abs() > f64::EPSILON {
Err(format!("statements.gen_decl probability sum for types is = {}", sum_type))
} else if (sum_qual - 1.0).abs() > f64::EPSILON {
Err(format!("statements.gen_decl probability sum for qualifiers is = {}", sum_qual))
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct GenGlobalDecl { struct GenGlobalDecl {
gen_boolean: f64, gen_boolean: f64,
@ -80,6 +174,12 @@ toml_struct!{
} }
} }
impl GenGlobalDecl {
fn verify(&self) -> Result<(), String> {
Ok(())
}
}
toml_struct!{ toml_struct!{
struct GenIfStatement { struct GenIfStatement {
gen_statement: f64, gen_statement: f64,
@ -90,6 +190,12 @@ toml_struct!{
} }
} }
impl GenIfStatement {
fn verify(&self) -> Result<(), String> {
Ok(())
}
}
toml_struct!{ toml_struct!{
struct GenLoop { struct GenLoop {
gen_inf_loop: f64, gen_inf_loop: f64,
@ -99,6 +205,12 @@ toml_struct!{
} }
} }
impl GenLoop {
fn verify(&self) -> Result<(), String> {
Ok(())
}
}
toml_struct!{ toml_struct!{
struct Types { struct Types {
gen_boolean: GenBoolean, gen_boolean: GenBoolean,
@ -107,6 +219,24 @@ toml_struct!{
} }
} }
impl Types {
fn verify(&self) -> Result<(), String> {
let gen_boolean = self.gen_boolean.verify();
let gen_integer = self.gen_integer.verify();
let gen_real = self.gen_real.verify();
if gen_boolean.is_err() {
gen_boolean
} else if gen_integer.is_err() {
gen_integer
} else if gen_real.is_err() {
gen_real
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct GenBoolean { struct GenBoolean {
expr_and: f64, expr_and: f64,
@ -128,6 +258,12 @@ toml_struct!{
} }
} }
impl GenBoolean {
fn verify(&self) -> Result<(), String> {
Ok(())
}
}
toml_struct!{ toml_struct!{
struct GenInteger { struct GenInteger {
expr_add: f64, expr_add: f64,
@ -143,6 +279,28 @@ toml_struct!{
} }
} }
impl GenInteger {
fn verify(&self) -> Result<(), String> {
let sum = self.expr_add
+ self.expr_sub
+ self.expr_mul
+ self.expr_div
+ self.expr_rem
+ self.expr_exp
+ self.expr_pos
+ self.expr_neg
+ self.get_instant;
if self.max_depth <= 0 {
Err("types.gen_integer.max_depth must be at least 1".to_string())
} else if (sum - 1.0).abs() > f64::EPSILON {
Err(format!("types.gen_integer probability sum is = {}", sum))
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct GenReal { struct GenReal {
expr_add: f64, expr_add: f64,
@ -159,6 +317,29 @@ toml_struct!{
} }
} }
impl GenReal {
fn verify(&self) -> Result<(), String> {
let sum = self.expr_add
+ self.expr_sub
+ self.expr_mul
+ self.expr_div
+ self.expr_rem
+ self.expr_exp
+ self.expr_pos
+ self.expr_neg
+ self.gen_integer
+ self.get_instant;
if self.max_depth <= 0 {
Err("types.gen_real.max_depth must be at least 1".to_string())
} else if (sum - 1.0).abs() > f64::EPSILON {
Err(format!("types.gen_real probability sum is = {}", sum))
} else {
Ok(())
}
}
}
toml_struct!{ toml_struct!{
struct Dev { struct Dev {
float_gen_distro_pos_stddiv: f32, float_gen_distro_pos_stddiv: f32,
@ -168,14 +349,8 @@ toml_struct!{
} }
} }
impl Params { impl Dev {
pub fn parse(p: &PathBuf) -> Self { fn verify(&self) -> Result<(), String> {
let file_string = fs::read_to_string(p) Ok(())
.expect("Failed to read parameters file into string");
let params: Params = toml::from_str(&file_string)
.expect("Failed to parse TOML from parameters file");
params
} }
} }