Skip to content

Commit e8cf4b9

Browse files
committed
Got for working with bitstrings
1 parent cfbc4e9 commit e8cf4b9

File tree

4 files changed

+94
-32
lines changed

4 files changed

+94
-32
lines changed

src/javascript/lib/core/bit_string.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
class BitString {
22
constructor(...args){
3-
this.raw_value = function(){
4-
return Object.freeze(args);
5-
};
6-
73
this.value = Object.freeze(this.process(args));
84
this.length = this.value.length;
9-
this.bit_size = this.raw_value().reduce((prev, current) => prev + (current.unit * current.size), 0);
10-
this.byte_size = (this.bit_size / 8) + (this.bit_size % 8 > 0 ? 1 : 0);
5+
this.bit_size = this.length * 8;
6+
this.byte_size = this.length;
117
}
128

139
get(index){
@@ -18,6 +14,12 @@ class BitString {
1814
return this.value.length;
1915
}
2016

17+
slice(start, end = null){
18+
let s = this.value.slice(start, end);
19+
let ms = s.map((elem) => BitString.integer(elem));
20+
return new BitString(...ms);
21+
}
22+
2123
[Symbol.iterator]() {
2224
return this.value[Symbol.iterator]();
2325
}
@@ -34,14 +36,14 @@ class BitString {
3436
return "<<" + s + ">>";
3537
}
3638

37-
process(){
39+
process(bitStringParts){
3840
let processed_values = [];
3941

4042
var i;
41-
for (i = 0; i < this.raw_value().length; i++) {
42-
let processed_value = this['process_' + this.raw_value()[i].type](this.raw_value()[i]);
43+
for (i = 0; i < bitStringParts.length; i++) {
44+
let processed_value = this['process_' + bitStringParts[i].type](bitStringParts[i]);
4345

44-
for(let attr of this.raw_value()[i].attributes){
46+
for(let attr of bitStringParts[i].attributes){
4547
processed_value = this['process_' + attr](processed_value);
4648
}
4749

src/javascript/lib/core/patterns/types.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ export class Bound {
5959
export class BitStringMatch {
6060
values: Array<Object>
6161

62-
//{'value': value, 'attributes': [type, unit, size]};
63-
64-
//length = unit * size
65-
66-
//use that info to map value to subset of bitstring
67-
//use type to turn subset of bitstring into desired type
68-
//if value is a variable, add variable to args list
69-
//if value is just a value, match up substring with generated bitstring from value
70-
7162
constructor(...values: Array<Object>){
7263
this.values = values;
7364
}
@@ -76,11 +67,15 @@ export class BitStringMatch {
7667
return values.length;
7768
}
7869

79-
size(){
70+
bit_size() {
71+
return this.byte_size() * 8;
72+
}
73+
74+
byte_size(){
8075
let s = 0;
8176

8277
for(let val of this.values){
83-
s = s + val.unit * val.size;
78+
s = s + ((val.unit * val.size)/8);
8479
}
8580

8681
return s;

src/javascript/lib/core/special_forms.js

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,58 @@ function _for(collections, fun, filter = () => true, into = [], previousValues =
2929
let collection = collections[0][1];
3030

3131
if(collections.length === 1){
32+
if(collection instanceof BitString){
33+
let bsSlice = collection.slice(0, pattern.byte_size());
34+
let i = 1;
3235

33-
for(let elem of collection){
34-
let r = Patterns.match_no_throw(pattern, elem);
35-
let args = previousValues.concat(r);
36+
while(bsSlice.byte_size == pattern.byte_size()){
37+
let r = Patterns.match_no_throw(pattern, bsSlice);
38+
let args = previousValues.concat(r);
3639

37-
if(r && filter.apply(this, args)){
38-
into = into.concat([fun.apply(this, args)]);
40+
if(r && filter.apply(this, args)){
41+
into = into.concat([fun.apply(this, args)]);
42+
}
43+
44+
bsSlice = collection.slice(pattern.byte_size() * i, pattern.byte_size() * (i + 1));
45+
i++;
46+
}
47+
48+
return into;
49+
}else{
50+
for(let elem of collection){
51+
let r = Patterns.match_no_throw(pattern, elem);
52+
let args = previousValues.concat(r);
53+
54+
if(r && filter.apply(this, args)){
55+
into = into.concat([fun.apply(this, args)]);
56+
}
3957
}
40-
}
4158

42-
return into;
59+
return into;
60+
}
4361
}else{
4462
let _into = [];
4563

46-
for(let elem of collection){
47-
let r = Patterns.match_no_throw(pattern, elem);
48-
if(r){
49-
_into = into.concat(this._for(collections.slice(1), fun, filter, _into, previousValues.concat(r)));
64+
if(collection instanceof BitString){
65+
let bsSlice = collection.slice(0, pattern.byte_size());
66+
let i = 1;
67+
68+
while(bsSlice.byte_size == pattern.byte_size()){
69+
let r = Patterns.match_no_throw(pattern, bsSlice);
70+
if(r){
71+
_into = into.concat(this._for(collections.slice(1), fun, filter, _into, previousValues.concat(r)));
72+
}
73+
74+
bsSlice = collection.slice(pattern.byte_size() * i, pattern.byte_size() * (i + 1));
75+
i++;
5076
}
77+
}else{
78+
for(let elem of collection){
79+
let r = Patterns.match_no_throw(pattern, elem);
80+
if(r){
81+
_into = into.concat(this._for(collections.slice(1), fun, filter, _into, previousValues.concat(r)));
82+
}
83+
}
5184
}
5285

5386
return _into;

src/javascript/tests/for.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Core from "../lib/core";
22
const Patterns = Core.Patterns;
33
const SpecialForms = Core.SpecialForms;
4+
const Tuple = Core.Tuple;
5+
const BitString = Core.BitString;
46

57
import Enum from "../lib/enum";
68

@@ -56,4 +58,34 @@ describe('for', () => {
5658

5759
expect(result).to.eql(["JOHN", "MEG"]);
5860
});
61+
62+
63+
it('for with bitstring', () => {
64+
//for <<r::8, g::8, b::8 <- <<213, 45, 132, 64, 76, 32, 76, 0, 0, 234, 32, 15>> >>, do: {r, g, b}
65+
66+
let collections = [
67+
[
68+
Patterns.bitStringMatch(BitString.integer({value: $}), BitString.integer({value: $}), BitString.integer({value: $})),
69+
new BitString(
70+
BitString.integer(213),
71+
BitString.integer(45),
72+
BitString.integer(132),
73+
BitString.integer(64),
74+
BitString.integer(76),
75+
BitString.integer(32),
76+
BitString.integer(76),
77+
BitString.integer(0),
78+
BitString.integer(0),
79+
BitString.integer(234),
80+
BitString.integer(32),
81+
BitString.integer(15)
82+
)
83+
]
84+
];
85+
86+
let result = SpecialForms._for(collections, (r, g, b) => new Tuple(r, g, b));
87+
88+
expect(result).to.eql([new Tuple(213, 45, 132), new Tuple(64, 76, 32), new Tuple(76, 0, 0), new Tuple(234, 32, 15)]);
89+
});
90+
5991
});

0 commit comments

Comments
 (0)