diff --git a/Cargo.toml b/Cargo.toml index 2cdc178..2c1f4e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] clap = { version = "4.4.8", features = ["derive"]} derive_builder = "0.12.0" +serde = { version = "1.0.192", features = ["derive"]} toml = "0.8.8" diff --git a/src/main.rs b/src/main.rs index 5fd8f69..506e701 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,13 @@ use std::fs; use std::path::PathBuf; use clap::Parser; +#[macro_use] +extern crate derive_builder; + mod ast; +mod params; + +use params::Params; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -12,10 +18,7 @@ struct Args { fn main() { let args = Args::parse(); - let params_toml = fs::read_to_string(&args.config) - .expect("Failed to read parameters file into string") - .parse::() - .expect("Failed to parse TOML from parameters file"); + let params_toml = Params::parse(&args.config); - println!("{}", params_toml["global_flow"]["gen_decl"]); + println!("{}", params_toml.global_flow.gen_decl); } diff --git a/src/params.rs b/src/params.rs new file mode 100644 index 0000000..c4c62e8 --- /dev/null +++ b/src/params.rs @@ -0,0 +1,162 @@ +use std::fs; +use std::path::PathBuf; + +use serde::Deserialize; + +// Makes all these structs public and deserializable +macro_rules! toml_struct { + (struct $name:ident {$($field:ident: $t:ty,)*}) => { + #[derive(Deserialize, Clone)] + pub struct $name { + $(pub $field: $t),* + } + } +} + +toml_struct!{ + struct Params { + global_flow: GlobalFlow, + statements: Statments, + types: Types, + } +} + +toml_struct!{ + struct GlobalFlow { + gen_decl: f64, + gen_subroutine: f64, + gen_typedef: f64, + end_generation: f64, + } +} + +toml_struct!{ + struct Statments { + gen_assign: GenAssign, + gen_decl: GenDecl, + gen_global_decl: GenGlobalDecl, + gen_if_statement: GenIfStatement, + gen_loop: GenLoop, + } +} + +toml_struct!{ + struct GenAssign { + gen_boolean: f64, + gen_character: f64, + gen_integer: f64, + gen_real: f64, + gen_tuple: f64, + } +} + +toml_struct!{ + struct GenDecl { + gen_boolean: f64, + gen_character: f64, + gen_integer: f64, + gen_real: f64, + gen_tuple: f64, + qual_const: f64, + qual_var: f64, + } +} + +toml_struct!{ + struct GenGlobalDecl { + gen_boolean: f64, + gen_character: f64, + gen_integer: f64, + gen_real: f64, + gen_tuple: f64, + } +} + +toml_struct!{ + struct GenIfStatement { + gen_statement: f64, + end_gen_statement: f64, + fork_elif: f64, + fork_else: f64, + fork_end: f64, + } +} + +toml_struct!{ + struct GenLoop { + gen_inf_loop: f64, + gen_cond_loop: f64, + gen_statement: f64, + end_gen_statement: f64, + } +} + +toml_struct!{ + struct Types { + gen_boolean: GenBoolean, + gen_integer: GenInteger, + gen_real: GenReal, + } +} + +toml_struct!{ + struct GenBoolean { + expr_and: f64, + expr_or: f64, + expr_not: f64, + expr_gt: f64, + expr_ge: f64, + expr_lt: f64, + expr_le: f64, + expr_eq: f64, + expr_eq_real: f64, + expr_eq_char: f64, + expr_eq_tuple: f64, + expr_ne: f64, + expr_ne_real: f64, + expr_ne_char: f64, + expr_ne_tuple: f64, + get_instant: f64, + } +} + +toml_struct!{ + struct GenInteger { + expr_add: f64, + expr_sub: f64, + expr_mul: f64, + expr_div: f64, + expr_rem: f64, + expr_exp: f64, + expr_pos: f64, + expr_neg: f64, + get_instant: f64, + } +} + +toml_struct!{ + struct GenReal { + expr_add: f64, + expr_sub: f64, + expr_mul: f64, + expr_div: f64, + expr_rem: f64, + expr_exp: f64, + expr_pos: f64, + expr_neg: f64, + gen_integer: f64, + get_instant: f64, + } +} + +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 + } +}