Add sized int value defs
Integer value definitions now respect their sizes (.u8, .u16, etc) Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -196,7 +196,7 @@ impl Assemble for ValueDef {
|
||||
|
||||
fn assemble(&self, _: &mut Asm) -> Result<Self::Out> {
|
||||
match self {
|
||||
ValueDef::Int(x) => Ok(x.to_le_bytes().to_vec()),
|
||||
ValueDef::Int(x, s) => Ok(x.to_le_bytes().iter().copied().take(s.len()).collect()),
|
||||
ValueDef::String(s) => {
|
||||
let bytes = s.bytes();
|
||||
let mut out = s.len().to_le_bytes().to_vec();
|
||||
|
||||
@@ -78,7 +78,7 @@ impl DataLine {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ValueDef {
|
||||
Int(u64),
|
||||
Int(u64, IntSize),
|
||||
String(String),
|
||||
ZString(String),
|
||||
}
|
||||
@@ -86,7 +86,7 @@ pub enum ValueDef {
|
||||
impl ValueDef {
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
ValueDef::Int(_) => 8,
|
||||
ValueDef::Int(_, s) => s.len(),
|
||||
ValueDef::String(s) => 8 + s.as_bytes().len(),
|
||||
ValueDef::ZString(s) => s.as_bytes().len() + 1,
|
||||
}
|
||||
@@ -176,6 +176,17 @@ pub enum IntSize {
|
||||
U64,
|
||||
}
|
||||
|
||||
impl IntSize {
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
IntSize::U8 => 1,
|
||||
IntSize::U16 => 2,
|
||||
IntSize::U32 => 4,
|
||||
IntSize::U64 => 8,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Inst {
|
||||
Add(Value, Value),
|
||||
|
||||
@@ -17,6 +17,10 @@ u8 "U8"
|
||||
u16 "U16"
|
||||
u32 "U32"
|
||||
u64 "U64"
|
||||
\.u8 "U8_DEF"
|
||||
\.u16 "U16_DEF"
|
||||
\.u32 "U32_DEF"
|
||||
\.u64 "U64_DEF"
|
||||
\.[iu](8|16|32|64) "INT_DEF"
|
||||
\.string "STR_DEF"
|
||||
\.zstring "ZSTR_DEF"
|
||||
|
||||
@@ -47,7 +47,10 @@ DataLine -> DataLine:
|
||||
;
|
||||
|
||||
ValueDef -> ValueDef:
|
||||
'INT_DEF' Int { ValueDef::Int($2) }
|
||||
'U8_DEF' Int { ValueDef::Int($2, IntSize::U8) }
|
||||
| 'U16_DEF' Int { ValueDef::Int($2, IntSize::U16) }
|
||||
| 'U32_DEF' Int { ValueDef::Int($2, IntSize::U32) }
|
||||
| 'U64_DEF' Int { ValueDef::Int($2, IntSize::U64) }
|
||||
| 'STR_DEF' String { ValueDef::String($2) }
|
||||
| 'ZSTR_DEF' String { ValueDef::ZString($2) }
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user