Compare commits

..

2 commits

Author SHA1 Message Date
Akemi Izuko f9e4e554b6
AstBuilder: update to new ast format
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful
2023-11-17 14:50:06 -07:00
Akemi Izuko 83562d4654
Ast: export objects as public 2023-11-17 14:49:43 -07:00
3 changed files with 45 additions and 44 deletions

View file

@ -1,18 +1,19 @@
use std::fmt;
mod expr;
mod statement;
use std::fmt;
pub use expr::Expr;
pub use expr::Literal;
pub use expr::Variable;
pub use expr::VariableBuilder;
pub use expr::BinaryOperator;
use expr::{
Expr,
Literal,
Variable,
BinaryOperator,
};
use statement::Stat;
use statement::Block;
use statement::GlobalBlock;
pub use statement::Stat;
pub use statement::Block;
pub use statement::GlobalBlock;
pub use statement::Declaration;
pub use statement::DeclarationBuilder;
pub trait GazType {
fn get_base(&self) -> BaseType;

View file

@ -4,7 +4,7 @@ use rand_distr::Distribution;
use crate::params::Params;
use crate::ast::{
Expr,
Statement,
Stat,
Quantifier,
BaseType,
GlobalBlock,
@ -39,7 +39,6 @@ impl AstBuilder {
}
pub fn generate(&mut self) {
let mut p: f64;
loop {
@ -54,54 +53,48 @@ impl AstBuilder {
}
}
fn gen_decl(&mut self) -> Box<dyn Statement> {
fn gen_decl(&mut self) -> Stat {
let p: f64 = self.rng.gen();
let ps = &self.params.statements.gen_decl;
let t = if p < ps.gen_integer {
BaseType::Int
let t: BaseType;
let assn: Expr;
if p < ps.gen_integer {
t = BaseType::Int;
assn = self.gen_integer();
} else {
BaseType::Real
};
t = BaseType::Real;
assn = self.gen_real();
}
let v = self.gen_variable_quantified(t, Quantifier::Const);
Box::new(
Stat::new_decl(
DeclarationBuilder::default()
.variable(v)
.assn(self.gen_literal(t))
.assn(Some(assn))
.build()
.unwrap()
)
}
fn gen_variable(&mut self, t: BaseType) -> Variable {
VariableBuilder::default()
.type_(t)
.name(self.gen_name())
.quantifer(Quantifier::Var)
.build()
.unwrap()
}
fn gen_variable_quantified(&mut self, t: BaseType, q: Quantifier) -> Variable {
VariableBuilder::default()
.type_(t)
.name(self.gen_name())
.quantifer(q)
.quantifier(q)
.build()
.unwrap()
}
fn gen_name(&mut self) -> String {
let s = format!("_{}", self.name_counter);
self.name_counter += 1;
s
fn gen_integer(&mut self) -> Expr {
Expr::new_literal(self.gen_literal(BaseType::Int))
}
//fn gen_expr(&mut self, type_: BaseType) -> Box<dyn Expr> {
//}
fn gen_real(&mut self) -> Expr {
Expr::new_literal(self.gen_literal(BaseType::Real))
}
fn gen_literal(&mut self, t: BaseType) -> Literal {
match t {
@ -117,6 +110,12 @@ impl AstBuilder {
BaseType::Unset => panic!("Attempted to generate literal of type Unset"),
}
}
fn gen_name(&mut self) -> String {
let s = format!("_{}", self.name_counter);
self.name_counter += 1;
s
}
}
impl ToString for AstBuilder {
@ -154,15 +153,16 @@ impl Distribution<i32> for IntGenerator {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i32 {
let p: f64 = rng.gen();
if p < 0.33 {
if p < 0.3 {
i32::MAX - (self.distro_pos.sample(rng).abs() as i32)
} else if p < 0.66 {
} else if p < 0.7 {
self.distro_zero.sample(rng) as i32
} else {
-1 * (i32::MAX - (self.distro_pos.sample(rng).abs() as i32))
}
}
}
/// Generates values from regions of interest for a float
///
/// These are concentrated around f32::MIN, 0, and f32::MAX, though any f32 value is possible

View file

@ -5,11 +5,11 @@ use clap::Parser;
extern crate derive_builder;
mod ast;
//mod ast_builder;
mod ast_builder;
mod params;
use params::Params;
//use ast_builder::AstBuilder;
use ast_builder::AstBuilder;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@ -20,9 +20,9 @@ struct Args {
fn main() {
let args = Args::parse();
let params = Params::parse(&args.config);
//let mut builder = AstBuilder::from(params);
let mut builder = AstBuilder::from(params);
//builder.generate();
builder.generate();
//println!("{}", builder.to_string());
println!("{}", builder.to_string());
}