Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data.strz directive for zero-terminated strings #6

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/fox32.pest
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ data = {
data_half |
data_word |
data_str |
data_strz |
data_fill
}
data_byte = { "data.8" ~ operand_value }
data_half = { "data.16" ~ operand_value }
data_word = { "data.32" ~ operand_value }
data_str = { "data.str" ~ immediate_str }
data_strz = { "data.strz" ~ immediate_str }
data_fill = { "data.fill" ~ operand_value ~ "," ~ operand_value }

constant = { "const" ~ constant_name ~ operand_value }
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ enum AstNode {
DataHalf(u16),
DataWord(u32),
DataStr(String),
DataStrZero(String),
DataFill {
value: u8,
size: u32,
Expand Down Expand Up @@ -630,6 +631,10 @@ fn parse_data(pair: pest::iterators::Pair<Rule>) -> AstNode {
let string = pair.into_inner().next().unwrap().into_inner().next().unwrap().as_str();
AstNode::DataStr(string.to_string())
},
Rule::data_strz => {
let string = pair.into_inner().next().unwrap().into_inner().next().unwrap().as_str();
AstNode::DataStrZero(string.to_string())
},
Rule::data_fill => {
let value = {
let ast = parse_operand(pair.clone().into_inner().next().unwrap(), false);
Expand Down Expand Up @@ -982,6 +987,11 @@ fn assemble_node(node: AstNode) -> AssembledInstruction {
AstNode::DataStr(string) => {
return string.as_bytes().into();
},
AstNode::DataStrZero(string) => {
let mut bytes: Vec<u8> = string.as_bytes().into();
bytes.push(0);
return bytes.into();
},
AstNode::LabelOperand {name, size, is_relative} => {
// label is used on its own, not as an operand:
// LabelOperand was previously only checked as part of operands
Expand Down