Skip to content

Commit

Permalink
Denest code in handle_compile and handle_docs functions (#397)
Browse files Browse the repository at this point in the history
* denested handle_compile and handle_docs

* applied clippy suggestion

* Apply suggestions from code review

Co-authored-by: Phoenix Himself <[email protected]>

* fix errors from changes introduced by code review

* remove redundant valid_ prefix from some variables

---------

Co-authored-by: Phoenix Himself <[email protected]>
  • Loading branch information
lucidashygirl and Ph0enixKM authored Aug 11, 2024
1 parent 3108f7d commit d339390
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 81 deletions.
150 changes: 80 additions & 70 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,59 +55,63 @@ fn main() -> Result<(), Box<dyn Error>> {
}

fn handle_compile(cli: Cli) -> Result<(), Box<dyn Error>> {
if let Some(input) = cli.input.clone() {
let input = String::from(input.to_string_lossy().trim());
let code = {
if input == "-" {
let mut buf = String::new();
match stdin().read_to_string(&mut buf) {
Ok(_) => buf,
Err(err) => handle_err(err),
}
} else {
match fs::read_to_string(&input) {
Ok(code) => code,
Err(err) => handle_err(err),
}
}
};
match AmberCompiler::new(code, Some(input), cli.clone()).compile() {
Ok((messages, code)) => {
messages.iter().for_each(|m| m.show());
// Save to the output file
if let Some(output) = cli.output {
let output = String::from(output.to_string_lossy());
if output == "--silent" {
return Ok(());
}
if output == "-" {
print!("{code}");
return Ok(());
}
match fs::File::create(&output) {
Ok(mut file) => {
write!(file, "{}", code).unwrap();
set_file_permission(&file, output);
}
Err(err) => {
Message::new_err_msg(err.to_string()).show();
std::process::exit(1);
}
}
}
// Execute the code
else {
(!messages.is_empty()).then(render_dash);
let exit_status = AmberCompiler::execute(code, &[])?;
std::process::exit(exit_status.code().unwrap_or(1));
}
}
Err(err) => {
err.show();
std::process::exit(1);
}
let input = if let Some(input) = cli.input.clone() {
String::from(input.to_string_lossy().trim())
} else {
return Ok(());
};

let code = if input == "-" {
let mut buf = String::new();
match stdin().read_to_string(&mut buf) {
Ok(_) => buf,
Err(err) => handle_err(err),
}
} else {
match fs::read_to_string(&input) {
Ok(code) => code,
Err(err) => handle_err(err),
}
};

let (messages, code) = match AmberCompiler::new(code, Some(input), cli.clone()).compile() {
Ok(result) => result,
Err(err) => {
err.show();
std::process::exit(1);
}
};
messages.iter().for_each(|m| m.show());
// Save to the output file
let output = if let Some(output) = cli.output {
String::from(output.to_string_lossy())
} else {
// Execute the code
(!messages.is_empty()).then(render_dash);
let exit_status = AmberCompiler::execute(code, &[])?;
std::process::exit(exit_status.code().unwrap_or(1));
};

if output == "--silent" {
return Ok(());
}

if output == "-" {
print!("{code}");
return Ok(());
}

match fs::File::create(&output) {
Ok(mut file) => {
write!(file, "{}", code).unwrap();
set_file_permission(&file, output);
}
Err(err) => {
Message::new_err_msg(err.to_string()).show();
std::process::exit(1);
}
}

Ok(())
}

Expand All @@ -127,38 +131,44 @@ fn handle_eval(code: String, cli: Cli) -> Result<(), Box<dyn Error>> {
}

fn handle_docs(cli: Cli) -> Result<(), Box<dyn Error>> {
if let Some(ref input) = cli.input {
let input = String::from(input.to_string_lossy());
let output = {
let out = cli.output.clone().unwrap_or_else(|| PathBuf::from("docs"));
String::from(out.to_string_lossy())
};
match fs::read_to_string(&input) {
Ok(code) => match AmberCompiler::new(code, Some(input), cli).generate_docs(output) {
Ok(_) => Ok(()),
Err(err) => {
err.show();
std::process::exit(1);
}
},
Err(err) => {
Message::new_err_msg(err.to_string()).show();
std::process::exit(1);
}
}
let input = if let Some(ref input) = cli.input {
String::from(input.to_string_lossy())
} else {
Message::new_err_msg(
"You need to provide a path to an entry file to generate the documentation",
)
.show();
std::process::exit(1);
};

let output = {
let out = cli.output.clone().unwrap_or_else(|| PathBuf::from("docs"));
String::from(out.to_string_lossy())
};

let code: String = match fs::read_to_string(&input) {
Ok(code) => code,
Err(err) => {
Message::new_err_msg(err.to_string()).show();
std::process::exit(1);
}
};

match AmberCompiler::new(code, Some(input), cli).generate_docs(output) {
Ok(_) => Ok(()),
Err(err) => {
err.show();
std::process::exit(1);
}
}
}

/*
#[cfg(target_os = "windows")]
fn set_file_permission(_file: &fs::File, _output: String) {
// We don't need to set permission on Windows
}
*/

#[cfg(not(target_os = "windows"))]
fn set_file_permission(file: &std::fs::File, path: String) {
Expand Down
22 changes: 11 additions & 11 deletions src/utils/context.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use heraclitus_compiler::prelude::*;
use std::collections::{HashMap, HashSet};
use crate::modules::expression::expr::Expr;
use crate::modules::types::Type;
use heraclitus_compiler::prelude::*;
use std::collections::{HashMap, HashSet};

use super::{function_interface::FunctionInterface, cc_flags::CCFlags};
use super::{cc_flags::CCFlags, function_interface::FunctionInterface};

#[derive(Clone, Debug)]
pub struct FunctionDecl {
pub name: String,
pub arg_names: Vec<String>,
pub arg_types: Vec<Type>,
pub arg_refs: Vec<bool>,
pub arg_optionals : Vec<Expr>,
pub arg_optionals: Vec<Expr>,
pub returns: Type,
pub is_args_typed: bool,
pub is_public: bool,
pub is_failable: bool,
pub id: usize
pub id: usize,
}

impl FunctionDecl {
Expand All @@ -30,7 +30,7 @@ impl FunctionDecl {
arg_optionals: self.arg_optionals,
returns: self.returns,
is_public: self.is_public,
is_failable: self.is_failable
is_failable: self.is_failable,
}
}
}
Expand All @@ -40,21 +40,21 @@ pub struct VariableDecl {
pub name: String,
pub kind: Type,
pub global_id: Option<usize>,
pub is_ref: bool
pub is_ref: bool,
}

#[derive(Clone, Debug)]
pub struct ScopeUnit {
pub vars: HashMap<String, VariableDecl>,
pub funs: HashMap<String, FunctionDecl>
pub funs: HashMap<String, FunctionDecl>,
}

/// Perform methods just on the scope
impl ScopeUnit {
pub fn new() -> ScopeUnit {
ScopeUnit {
vars: HashMap::new(),
funs: HashMap::new()
funs: HashMap::new(),
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ pub struct Context {
/// The return type of the currently parsed function
pub fun_ret_type: Option<Type>,
/// List of compiler flags
pub cc_flags: HashSet<CCFlags>
pub cc_flags: HashSet<CCFlags>,
}

// FIXME: Move the scope related structures to the separate file
Expand All @@ -137,7 +137,7 @@ impl Context {
is_unsafe_ctx: false,
pub_funs: vec![],
fun_ret_type: None,
cc_flags: HashSet::new()
cc_flags: HashSet::new(),
}
}

Expand Down

0 comments on commit d339390

Please sign in to comment.