This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e7f43a
commit c03b3b9
Showing
8 changed files
with
317 additions
and
235 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,77 @@ | ||
use basm; | ||
use beast; | ||
use clap::ArgMatches; | ||
// use clap::ArgMatches; | ||
use core::error::*; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use std::str::FromStr; | ||
|
||
pub fn compile(matches: &ArgMatches) -> Result<()> { | ||
let input_file_name = if let Some(file_name) = matches.value_of("input") { | ||
file_name | ||
} else { | ||
bail!("no file name given"); | ||
}; | ||
const BAKERVM_IMAGE_EXTENSION: &str = "img"; | ||
pub const DEFAULT_LANG: Lang = Lang::Beast; | ||
|
||
let start_dir = env::current_dir().chain_err(|| "unable to get current directory")?; | ||
#[derive(Debug)] | ||
pub enum Lang { | ||
Beast, | ||
Basm, | ||
} | ||
|
||
if matches.value_of("lang") == Some("basm") { | ||
let program = basm::compile(input_file_name.to_owned()) | ||
.chain_err(|| "unable to compile file")?; | ||
impl FromStr for Lang { | ||
type Err = &'static str; | ||
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> { | ||
match s { | ||
"basm" => Ok(Lang::Basm), | ||
"beast" | "bst" => Ok(Lang::Beast), | ||
_ => bail!("unknown language. Language must be one of [beast, bst, basm]"), | ||
} | ||
} | ||
} | ||
|
||
pub fn compile(lang: Option<Lang>, input: PathBuf, output: Option<PathBuf>) -> Result<()> { | ||
let input = input | ||
.canonicalize() | ||
.chain_err(|| "unable to canonicalize input path")?; | ||
|
||
let output_file_name = if let Some(file_name) = matches.value_of("output") { | ||
file_name.to_owned() | ||
// We don't want to fail at recognizing the language, so we just default to | ||
// Beast | ||
let lang = lang.unwrap_or_else(|| { | ||
if let Some(extension) = input.extension() { | ||
if let Some(extension) = extension.to_str() { | ||
extension.parse().unwrap_or(DEFAULT_LANG) | ||
} else { | ||
DEFAULT_LANG | ||
} | ||
} else { | ||
format!("{}.img", input_file_name) | ||
}; | ||
DEFAULT_LANG | ||
} | ||
}); | ||
|
||
let mut fallback_output = input.clone(); | ||
|
||
env::set_current_dir(start_dir).chain_err(|| "unable to switch directories")?; | ||
ensure!( | ||
fallback_output.set_extension(BAKERVM_IMAGE_EXTENSION), | ||
"unable to set file extension" | ||
); | ||
|
||
let mut file = File::create(output_file_name).chain_err(|| "unable to create file")?; | ||
let output = output.unwrap_or(fallback_output); | ||
|
||
file.write_all(&program[..]).chain_err(|| "unable to write program data")?; | ||
let start_dir = env::current_dir().chain_err(|| "unable to get current directory")?; | ||
|
||
file.sync_all().chain_err(|| "unable to sync output file to file system")?; | ||
} else { | ||
let program = beast::compile(input_file_name.to_owned()) | ||
.chain_err(|| "unable to compile file")?; | ||
let program = match lang { | ||
Lang::Basm => basm::compile(input).chain_err(|| "unable to compile basm file")?, | ||
Lang::Beast => beast::compile(input).chain_err(|| "unable to compile Beast file")?, | ||
}; | ||
|
||
let output_file_name = if let Some(file_name) = matches.value_of("output") { | ||
file_name.to_owned() | ||
} else { | ||
format!("{}.img", input_file_name) | ||
}; | ||
} | ||
env::set_current_dir(start_dir).chain_err(|| "unable to switch directories")?; | ||
|
||
let mut file = File::create(output).chain_err(|| "unable to create file")?; | ||
|
||
file.write_all(&program[..]) | ||
.chain_err(|| "unable to write program data")?; | ||
|
||
file.sync_all() | ||
.chain_err(|| "unable to sync output file to file system")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
mod pack; | ||
mod compile; | ||
|
||
pub use self::compile::compile; | ||
pub use self::pack::pack; | ||
pub use self::compile::*; | ||
// pub use self::pack::pack; |
Oops, something went wrong.