Skip to content

Commit

Permalink
Implement nested structures.
Browse files Browse the repository at this point in the history
Closes #341.
Closes #340.
Closes #339.
  • Loading branch information
flatheadmill committed Jun 17, 2020
1 parent e6e8621 commit 22745d5
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 140 deletions.
16 changes: 16 additions & 0 deletions test/cycle/structure.t.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('proof')(0, prove)

function prove (okay) {
require('./cycle')(okay, {
name: 'array/words',
define: {
object: {
value: {
first: 8,
second: 8
}
}
},
objects: [{ value: { first: 1, second: 2 } }]
})
}
19 changes: 6 additions & 13 deletions test/generated/array/words.parser.all.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
module.exports = function (parsers) {
parsers.all.object = function ($buffer, $start) {
let $i = [], $I = []

const object = {
array: []
value: {
first: 0,
second: 0
}
}

$i[0] = 0
$I[0] =
($buffer[$start++]) * 0x100 +
($buffer[$start++])

for (; $i[0] < $I[0]; $i[0]++) {
object.value.first = ($buffer[$start++])

object.array[$i[0]] =
($buffer[$start++]) * 0x100 +
($buffer[$start++])
}
object.value.second = ($buffer[$start++])

return object
}
Expand Down
25 changes: 7 additions & 18 deletions test/generated/array/words.parser.bff.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
module.exports = function (parsers) {
parsers.bff.object = function () {
return function parse ($buffer, $start, $end) {
let $i = [], $I = []

const object = {
array: []
value: {
first: 0,
second: 0
}
}

if ($end - $start < 2) {
return parsers.inc.object(object, 1, $i, $I)($buffer, $start, $end)
return parsers.inc.object(object, 1)($buffer, $start, $end)
}

$i[0] = 0
$I[0] =
($buffer[$start++]) * 0x100 +
($buffer[$start++])

for (; $i[0] < $I[0]; $i[0]++) {
object.value.first = ($buffer[$start++])

if ($end - $start < 2) {
return parsers.inc.object(object, 4, $i, $I)($buffer, $start, $end)
}

object.array[$i[0]] =
($buffer[$start++]) * 0x100 +
($buffer[$start++])
}
object.value.second = ($buffer[$start++])

return { start: $start, object: object, parse: null }
}
Expand Down
69 changes: 25 additions & 44 deletions test/generated/array/words.parser.inc.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,48 @@
module.exports = function (parsers) {
parsers.inc.object = function (object = {}, $step = 0, $i = [], $I = []) {
parsers.inc.object = function (object = {}, $step = 0) {
let $_, $bite
return function parse ($buffer, $start, $end) {
for (;;) {
switch ($step) {
case 0:
switch ($step) {
case 0:

object = {
array: []
object = {
value: {
first: 0,
second: 0
}
}

$step = 1

case 1:
$step = 1

$_ = 0
$step = 2
$bite = 1
case 1:

case 2:
$step = 2

while ($bite != -1) {
if ($start == $end) {
return { start: $start, object: null, parse }
}
$_ += ($buffer[$start++]) << $bite * 8 >>> 0
$bite--
}
case 2:

$I[0] = $_
if ($start == $end) {
return { start: $start, object: null, parse }
}

$i[0] = 0
case 3:
object.value.first = $buffer[$start++]


case 4:
case 3:

$_ = 0
$step = 5
$bite = 1
$step = 4

case 5:
case 4:

while ($bite != -1) {
if ($start == $end) {
return { start: $start, object: null, parse }
}
$_ += ($buffer[$start++]) << $bite * 8 >>> 0
$bite--
}
if ($start == $end) {
return { start: $start, object: null, parse }
}

object.array[$i[0]] = $_
object.value.second = $buffer[$start++]

if (++$i[0] != $I[0]) {
$step = 3
continue
}

case 6:
case 5:

return { start: $start, object: object, parse: null }
}
break
return { start: $start, object: object, parse: null }
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions test/generated/array/words.serializer.all.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
module.exports = function (serializers) {
serializers.all.object = function (object) {
return function ($buffer, $start, $end) {
let $i = []
$buffer[$start++] = (object.value.first & 0xff)

$buffer[$start++] = (object.array.length >>> 8 & 0xff)
$buffer[$start++] = (object.array.length & 0xff)

for ($i[0] = 0; $i[0] < object.array.length; $i[0]++) {
$buffer[$start++] = (object.array[$i[0]] >>> 8 & 0xff)
$buffer[$start++] = (object.array[$i[0]] & 0xff)
}
$buffer[$start++] = (object.value.second & 0xff)

return { start: $start, serialize: null }
}
Expand Down
19 changes: 3 additions & 16 deletions test/generated/array/words.serializer.bff.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
module.exports = function (serializers) {
serializers.bff.object = function (object) {
return function ($buffer, $start, $end) {
let $i = []

if ($end - $start < 2) {
if ($end - $start < 2 * object.value.length) {
return {
start: $start,
serialize: serializers.inc.object(object, 0)
}
}

$buffer[$start++] = (object.array.length >>> 8 & 0xff)
$buffer[$start++] = (object.array.length & 0xff)

for ($i[0] = 0; $i[0] < object.array.length; $i[0]++) {
if ($end - $start < 2) {
return {
start: $start,
serialize: serializers.inc.object(object, 2, $i)
}
}
$buffer[$start++] = (object.value.first & 0xff)

$buffer[$start++] = (object.array[$i[0]] >>> 8 & 0xff)
$buffer[$start++] = (object.array[$i[0]] & 0xff)
}
$buffer[$start++] = (object.value.second & 0xff)

return { start: $start, serialize: null }
}
Expand Down
68 changes: 29 additions & 39 deletions test/generated/array/words.serializer.inc.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,49 @@
module.exports = function (serializers) {
serializers.inc.object = function (object, $step = 0, $i = []) {
serializers.inc.object = function (object, $step = 0) {
let $bite, $stop, $_

return function serialize ($buffer, $start, $end) {
for (;;) {
switch ($step) {
case 0:

$step = 1
$bite = 1
$_ = object.array.length

case 1:

while ($bite != -1) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = ($_ >>> $bite * 8 & 0xff)
$bite--
}
switch ($step) {
case 0:

$i[0] = 0
$step = 1
$bite = 0
$_ = object.value.first

case 2:
case 1:

$step = 3
$bite = 1
$_ = object.array[$i[0]]
while ($bite != -1) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = ($_ >>> $bite * 8 & 0xff)
$bite--
}

case 3:

while ($bite != -1) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = ($_ >>> $bite * 8 & 0xff)
$bite--
}
case 2:

$step = 3
$bite = 0
$_ = object.value.second

if (++$i[0] != object.array.length) {
$step = 2
continue
}
case 3:

$step = 4
while ($bite != -1) {
if ($start == $end) {
return { start: $start, serialize }
}
$buffer[$start++] = ($_ >>> $bite * 8 & 0xff)
$bite--
}

case 4:

break
$step = 4

}
case 4:

break

}

return { start: $start, serialize: null }
Expand Down
2 changes: 0 additions & 2 deletions test/generated/array/words.sizeof.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module.exports = function (sizeOf) {

$_ += 2

$_ += 2 * object.array.length

return $_
}
}
45 changes: 45 additions & 0 deletions test/language/structure.t.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require('proof')(1, okay => {
const simplified = require('../../simplified')
okay(simplified({
object: {
value: {
first: 8,
second: 8
}
}
}), [{
type: 'structure',
vivify: 'object',
dotted: '',
fixed: true,
bits: 16,
fields: [{
type: 'structure',
vivify: 'object',
dotted: '.value',
fixed: true,
bits: 16,
fields: [{
type: 'integer',
vivify: 'number',
dotted: '.first',
fixed: true,
bits: 8,
endianness: 'big',
compliment: false,
name: 'first'
}, {
type: 'integer',
vivify: 'number',
dotted: '.second',
fixed: true,
bits: 8,
endianness: 'big',
compliment: false,
name: 'second'
}],
name: 'value'
}],
name: 'object'
}], 'structure')
})

0 comments on commit 22745d5

Please sign in to comment.