Add #include_bin_optional

This commit is contained in:
Ry 2023-03-03 17:25:40 -08:00
parent 007caeb23f
commit a049f839f2
2 changed files with 13 additions and 6 deletions

View File

@ -2,7 +2,7 @@ WHITESPACE = _{ " " | "\t" | "\n" | "\r" | "\x0B" | "\x0C" | "\xA0" | SPACE_SEPA
COMMENT = _{ ";" ~ (!"\n" ~ ANY)* } COMMENT = _{ ";" ~ (!"\n" ~ ANY)* }
assembly = { assembly = {
SOI ~ (origin | include_bin | data | constant | label | instruction)* ~ EOI SOI ~ (origin | include_bin_optional | include_bin | data | constant | label | instruction)* ~ EOI
} }
origin = { origin = {
@ -13,6 +13,7 @@ origin_padding = { "org.pad" ~ operand_value }
origin_no_padding = { "org" ~ operand_value } origin_no_padding = { "org" ~ operand_value }
include_bin = { "#include_bin" ~ immediate_str } include_bin = { "#include_bin" ~ immediate_str }
include_bin_optional = { "#include_bin_optional" ~ immediate_str }
data = { data = {
data_byte | data_byte |

View File

@ -516,18 +516,23 @@ fn include_text_file(line_number: usize, text: &str, input_file: String) -> Stri
final_file final_file
} }
fn include_binary_file(pair: pest::iterators::Pair<Rule>) -> AstNode { fn include_binary_file(pair: pest::iterators::Pair<Rule>, optional: bool) -> AstNode {
let path_string = pair.into_inner().next().unwrap().as_str().trim(); let path_string = pair.into_inner().next().unwrap().as_str().trim();
//let path = canonicalize(path_string).expect(&format!("failed to include file \"{}\"", path_string));
let mut source_path = SOURCE_PATH.lock().unwrap().clone(); let mut source_path = SOURCE_PATH.lock().unwrap().clone();
source_path.push(path_string); source_path.push(path_string);
println!("Including file as binary data: {:#?}", source_path.file_name().expect("invalid filename")); println!("Including file as binary data: {:#?}", source_path.file_name().expect("invalid filename"));
let binary = read(source_path).expect("failed to include file"); let binary = read(&source_path);
if binary.is_err() && optional {
println!("Optional include was not found: {:#?}", source_path.file_name().expect("invalid filename"));
return AstNode::IncludedBinary(vec![]);
} else if binary.is_err() {
panic!("failed to include file");
}
AstNode::IncludedBinary(binary) AstNode::IncludedBinary(binary.unwrap())
} }
fn parse(source: &str) -> Result<Vec<AstNode>, Error<Rule>> { fn parse(source: &str) -> Result<Vec<AstNode>, Error<Rule>> {
@ -570,7 +575,8 @@ fn build_ast_from_expression(pair: pest::iterators::Pair<Rule>) -> AstNode {
Rule::label => parse_label(inner_pair.next().unwrap(), inner_pair.next()), Rule::label => parse_label(inner_pair.next().unwrap(), inner_pair.next()),
Rule::data => parse_data(inner_pair.next().unwrap()), Rule::data => parse_data(inner_pair.next().unwrap()),
Rule::origin => parse_origin(inner_pair.next().unwrap()), Rule::origin => parse_origin(inner_pair.next().unwrap()),
Rule::include_bin => include_binary_file(inner_pair.next().unwrap()), Rule::include_bin => include_binary_file(inner_pair.next().unwrap(), false),
Rule::include_bin_optional => include_binary_file(inner_pair.next().unwrap(), true),
_ => todo!("{:#?}", pair_rule), _ => todo!("{:#?}", pair_rule),
} }
} }