1- import chunk from 'lodash/chunk' ;
2- import flatten from 'lodash/flatten' ;
3-
41const sizeLookup = {
52 'b' : 1 ,
63 'w' : 2 ,
@@ -13,72 +10,72 @@ export function asmToBin(buffer) {
1310 . replace ( / \$ | e v e n | ( ; ( .* ?) $ ) / gm, '' ) // remove comments / even / $ (assume no decimal)
1411 . replace ( / ( ^ \s * $ ) / gm, '' ) ; // remove empty lines
1512
16- const sections = asm . split ( / ^ ( .* ?) : / gm) ;
17- // header contains a - symbol (assume no negatives in data sections)
18- const headerIndex = sections . findIndex ( ( f ) => f . indexOf ( '-' ) != - 1 ) ;
19- const headers = sections [ headerIndex ] ;
13+ // split into labels/data
14+ // double comment char used to indicate start of line
15+ const sections = ( asm
16+ . replace ( / ^ \S / gm, ( d ) => `;;${ d } ` )
17+ . replace ( / \n / gm, '' ) + ';' )
18+ . match ( / ; .* ?: .* ?; / g)
19+ . map ( ( d ) => d . replace ( / ; / g, '' ) . split ( ':' ) ) ;
20+
21+ // calculate pointer for each label
22+ let dataIndex = 0 ;
23+ const pointerMap = { } ;
24+ sections . forEach ( ( [ label , data ] ) => {
25+ pointerMap [ label ] = dataIndex ;
26+ // insert newlines to split on
27+ const lines = data . split ( 'dc' ) . join ( '\ndc' ) . split ( '\n' ) ;
28+ lines . forEach ( ( line ) => {
29+ const sizeMatch = line . match ( / d c \. ( b | w | l ) / ) ;
30+ if ( sizeMatch ) {
31+ const size = sizeLookup [ sizeMatch [ 1 ] ] ;
32+ const fragments = line . split ( ',' ) ;
33+ dataIndex += size * fragments . length ;
34+ }
35+ } ) ;
36+ } ) ;
2037
21- const dataSections = chunk ( sections . splice ( headerIndex + 1 ) , 2 )
22- . map ( ( [ label , data ] ) => {
23- const lines = data . split ( '\n' ) ;
24- let bytes = [ ] ;
25- lines . forEach ( ( line ) => {
26- const sizeMatch = line . match ( / d c \. ( b | w | l ) / ) ;
27- if ( sizeMatch ) {
28- const size = sizeLookup [ sizeMatch [ 1 ] ] ;
29- const fragments = line . replace ( / d c \. ( b | w | l ) | \s / g, '' ) . split ( ',' ) ;
30- // save each fragment into byte array based on size
31- fragments . forEach ( ( fragment ) => {
38+ const bytes = [ ] ;
39+
40+ // now just convert the data sections
41+ asm . replace ( / ^ ( .* ?) : / gm, '' )
42+ . split ( '\n' )
43+ . forEach ( ( line ) => {
44+ const sizeMatch = line . match ( / d c \. ( b | w | l ) / ) ;
45+ if ( sizeMatch ) {
46+ const size = sizeLookup [ sizeMatch [ 1 ] ] ;
47+ const fragments = line . replace ( / d c \. ( b | w | l ) | \s / g, '' ) . split ( ',' ) ;
48+
49+ // save each fragment into byte array based on size
50+ fragments . forEach ( ( fragment ) => {
51+ if ( ~ fragment . indexOf ( '-' ) ) {
52+ // if data is calculated from labels
53+ const [ lVal , rVal ] = fragment . split ( '-' ) ;
54+ let pointer = ( pointerMap [ lVal ] - pointerMap [ rVal ] ) ;
55+ let pointerBytes = [ ] ;
56+ for ( let i = 0 ; i < size ; i ++ ) {
57+ pointerBytes . unshift ( pointer & 0xFF ) ;
58+ pointer = pointer >> 8 ;
59+ }
60+ bytes . push ( ...pointerBytes ) ;
61+ }
62+ else {
3263 let hex = parseInt ( fragment , 16 ) ;
3364 let fragmentBytes = [ ] ;
3465 for ( let i = 0 ; i < size ; i ++ ) {
3566 fragmentBytes . unshift ( hex & 0xFF ) ;
3667 hex = hex >> 8 ;
3768 }
3869 bytes . push ( ...fragmentBytes ) ;
39- } ) ;
40- }
41- } ) ;
70+ }
71+ } ) ;
4272
43- return {
44- label,
45- bytes,
46- } ;
47- } ) ;
48-
49- // assume word sized headers
50- const headersList = headers
51- . replace ( / d c \. w / gm, '' ) // remove data annotation
52- . replace ( / \n / gm, ',' ) // change \n to comma
53- . replace ( / \s / gm, '' ) // strip whitespace
54- . replace ( / , $ | ^ , / g, '' ) // strip trailing/beginning comma
55- . split ( ',' ) ; // split by comma
56-
57- const headerSize = headersList . length * 2 ; // bytes
58-
59- const headerBytes = [ ] ;
60-
61- headersList . forEach ( ( header ) => {
62- const dashIndex = header . indexOf ( '-' ) ;
63- if ( dashIndex == - 1 ) {
64- const value = parseInt ( header , 16 ) ;
65- headerBytes . push ( value >> 8 , value & 0xFF ) ;
66- }
67- else {
68- const label = header . slice ( 0 , dashIndex ) ;
69- let value = headerSize ;
70- for ( let i = 0 ; i < dataSections . length ; i ++ ) {
71- if ( label == dataSections [ i ] . label ) {
72- break ;
73- }
74- value += dataSections [ i ] . bytes . length ;
7573 }
74+ } ) ;
7675
77- headerBytes . push ( value >> 8 , value & 0xFF ) ;
78- }
79- } ) ;
80-
81- const dataBytes = flatten ( dataSections . map ( ( d ) => d . bytes ) ) ;
76+ return bytes ;
77+ }
8278
83- return [ ...headerBytes , ...dataBytes ] ;
79+ export function stuffToAsm ( headers , frames , name ) {
80+ return '' ;
8481}
0 commit comments