Skip to content

Commit

Permalink
Two's compliment packed integers.
Browse files Browse the repository at this point in the history
Closes #425.
  • Loading branch information
flatheadmill committed Apr 26, 2020
1 parent 9bffb01 commit 388e50c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions test/cycle/packed.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ function prove (okay) {
header: {
literal: 'deaf',
one: 1,
two: 3,
two: -3,
three: 12
}
}
},
objects: [{ header: { one: 1, two: 5, three: 1 } }]
objects: [{ header: { one: 1, two: -4, three: 1 } }]
})
}
2 changes: 2 additions & 0 deletions test/generated/packed.parser.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = function (parsers) {

object.header.one = $_ >>> 15 & 0x1
object.header.two = $_ >>> 12 & 0x7
object.header.two =
object.header.two & 0x4 ? (0x7 - object.header.two + 1) * -1 : object.header.two
object.header.three = $_ & 0xfff

return object
Expand Down
2 changes: 2 additions & 0 deletions test/generated/packed.parser.bff.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = function (parsers) {

object.header.one = $_ >>> 15 & 0x1
object.header.two = $_ >>> 12 & 0x7
object.header.two =
object.header.two & 0x4 ? (0x7 - object.header.two + 1) * -1 : object.header.two
object.header.three = $_ & 0xfff

return { start: $start, object: object, parse: null }
Expand Down
2 changes: 2 additions & 0 deletions test/generated/packed.parser.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module.exports = function (parsers) {

object.header.one = $_ >>> 15 & 0x1
object.header.two = $_ >>> 12 & 0x7
object.header.two =
object.header.two & 0x4 ? (0x7 - object.header.two + 1) * -1 : object.header.two
object.header.three = $_ & 0xfff


Expand Down
4 changes: 2 additions & 2 deletions test/language/packed.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require('proof')(1, okay => {
header: {
literal: 'deaf',
one: 1,
two: 3,
two: -3,
three: 12
}
}
Expand Down Expand Up @@ -40,7 +40,7 @@ require('proof')(1, okay => {
bits: 3,
fixed: true,
endianness: 'big',
compliment: false
compliment: true
}, {
name: 'three',
dotted: '.three',
Expand Down
14 changes: 13 additions & 1 deletion unpack.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fiddle = require('./fiddle/unpack')
const unsign = require('./fiddle/unsign')
const $ = require('programmatic')

function unpack (path, field, assignee) {
let bits = field.bits, offset = 0, bit = 0
Expand All @@ -7,8 +9,18 @@ function unpack (path, field, assignee) {
bit += field.bits
return field
})
// TODO Faster with an if statement rather than a reassignment (see
// generated code) or with a temporary variable.
return packing.filter(field => field.type == 'integer').map(function (field) {
return `${path}${field.dotted} = ${fiddle(bits, field.offset, field.bits, assignee)}`
const assign = `${path}${field.dotted} = ${fiddle(bits, field.offset, field.bits, assignee)}`
if (field.compliment) {
return $(`
`, assign, `
${path}${field.dotted} =
`, unsign(path + field.dotted, field.bits), `
`)
}
return assign
}).join('\n')
}

Expand Down

0 comments on commit 388e50c

Please sign in to comment.