Skip to content

Commit 947d9b8

Browse files
committed
add some data processing scripts
1 parent 3de0a20 commit 947d9b8

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

scripts/freqdump.g

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; output byte frequency of input file
2+
; fasmg freqdump.g -i "FileName equ 'freqdump.g'"
3+
;
4+
; performance: bytes seconds
5+
; 1,126,964 1.2
6+
; 20,968,922 45.4
7+
; 510,018,886 534.9
8+
9+
10+
match =FileName,FileName
11+
display 10,'usage: fasmg freqdump.g -i "FileName equ ''your_file''"',10
12+
err 'missing FileName expected'
13+
else
14+
virtual at 0
15+
source::
16+
file FileName
17+
end virtual
18+
19+
AreaFrequency source
20+
21+
; we can verify total equals length
22+
total = 0
23+
postpone
24+
assert total = sizeof source
25+
end postpone
26+
27+
; display array of values
28+
repeat 256, i:0
29+
repeat 1, N:source.i
30+
display `N,','
31+
total = total + N
32+
dd N ; and store to output
33+
end repeat
34+
end repeat
35+
end match
36+
37+
calminstruction AreaFrequency area*
38+
local offset, var, val, i
39+
compute offset, 0
40+
41+
; set all byte frequency counts to zero
42+
compute i, 256
43+
clear:
44+
compute i, i - 1
45+
arrange var, area.i
46+
publish var, offset
47+
check i
48+
jyes clear
49+
50+
; increment frequency counts for each byte
51+
jump zero
52+
more:
53+
load val, area:offset, 1
54+
compute val, 0+val ; convert string to number
55+
arrange var, area.val
56+
compute val, var + 1
57+
publish var, val
58+
compute offset, offset + 1
59+
zero:
60+
check offset < sizeof area
61+
jyes more
62+
end calminstruction
63+
64+
; text AreaContent Some_Area
65+
; display text
66+
calminstruction (target) AreaContent area*
67+
local string
68+
load string, area: 0, sizeof area
69+
publish target, string
70+
end calminstruction

scripts/json.format.g

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
; strip whitespace from JSON file and re-format with tab
2+
3+
if definite FILE
4+
virtual
5+
DAT::
6+
file FILE
7+
DAT_END := $ - $$
8+
end virtual
9+
10+
define _db
11+
calminstruction json_prettier i:0,instring:0,depth:0
12+
local awk,char
13+
more:
14+
arrange awk, =load =_db:1 =from =DAT:i
15+
assemble awk
16+
compute i, i + 1
17+
compute char, 0+_db
18+
19+
check instring
20+
jyes string_contents
21+
22+
check _db = '"'
23+
jyes string_inout
24+
check _db = ','
25+
jyes output_indent
26+
27+
check _db = '{'
28+
jyes increase_depth
29+
check _db = '}'
30+
jyes decrease_depth
31+
32+
check _db = '['
33+
jyes increase_depth
34+
check _db = ']'
35+
jyes decrease_depth
36+
37+
; consume existing whitespace
38+
check _db = ' '
39+
jyes skip_ws
40+
check _db = 10
41+
jyes skip_ws
42+
check _db = 13
43+
jyes skip_ws
44+
check _db = 9
45+
jyes skip_ws
46+
47+
output_char:
48+
arrange awk, =db char
49+
assemble awk
50+
skip_ws:
51+
check i < DAT_END
52+
jyes more
53+
exit
54+
55+
decrease_depth:
56+
compute depth, depth - 1
57+
indent_output:
58+
arrange awk, =db 10,depth =dup 9,char
59+
assemble awk
60+
jump skip_ws
61+
62+
increase_depth:
63+
compute depth, depth + 1
64+
output_indent:
65+
arrange awk, =db char,10,depth =dup 9
66+
assemble awk
67+
jump skip_ws
68+
69+
string_contents:
70+
check instring = 3
71+
jyes string_esc
72+
check _db = '"'
73+
jyes string_inout
74+
check _db = '\'
75+
jno output_char
76+
string_esc:
77+
compute instring, instring xor 2
78+
jump output_char
79+
string_inout:
80+
compute instring, instring xor 1
81+
jump output_char
82+
end calminstruction
83+
json_prettier
84+
85+
else
86+
display 'usage: fasmg -i "define FILE ''<filename>''" ',__FILE__,' <outfile>'
87+
end if

scripts/json.pack.g

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
match =FileName,FileName
2+
display 10,'usage: fasmg json.pack.g -i "FileName equ ''metaModel.json''"',10
3+
err 'missing FileName expected'
4+
else
5+
virtual at 0
6+
source::
7+
file FileName
8+
end virtual
9+
end match
10+
11+
calminstruction JSON_Pack area*
12+
local offset, outset, val
13+
compute outset, -1
14+
compute offset, -1
15+
value:
16+
compute offset, offset + 1
17+
check offset = sizeof area
18+
jyes done
19+
load val, area:offset, 1
20+
check val = ' '
21+
jyes value
22+
check val = 9
23+
jyes value
24+
check val = 10
25+
jyes value
26+
check val = 13
27+
jyes value
28+
; store verbatim
29+
compute outset, outset + 1
30+
store area:outset, 1, val
31+
check val = '"'
32+
jno value
33+
quoted: ; assume valid JSON - all quotes
34+
compute offset, offset + 1
35+
load val, area:offset, 1
36+
; store verbatim
37+
compute outset, outset + 1
38+
store area:outset, 1, val
39+
check val = '"'
40+
jno quoted
41+
; was it an escaped quote?
42+
load val, area:offset-1, 1
43+
check val = '\'
44+
jyes quoted
45+
jump value
46+
done:
47+
; define new length to area, SIZEOF no good!
48+
compute outset, outset + 1
49+
arrange val, area.=packed =:== outset
50+
assemble val
51+
end calminstruction
52+
53+
format binary as 'json'
54+
JSON_Pack source
55+
load package:source.packed from source:0
56+
db package

scripts/makedb.g

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
; Make data statements from file (don't use this).
3+
4+
if definite FILE
5+
virtual at 0
6+
DAT::
7+
file FILE
8+
BYTES := $
9+
end virtual
10+
11+
db '; ',FILE,13,10
12+
repeat 1,D:BYTES
13+
db 'label dat:',`D,13,10
14+
end repeat
15+
db 'db \',13,10
16+
data_dump BYTES,,db
17+
18+
calminstruction hex_nibble digit*, command: display
19+
compute digit, 0FFh and '0123456789ABCDEF' shr (digit*8)
20+
arrange command, command digit
21+
assemble command
22+
end calminstruction
23+
24+
define _db
25+
calminstruction data_dump bytes*, i:0, cmd:display
26+
local awk, digit
27+
jj: arrange awk, =load =_db:1 =from =DAT:i
28+
assemble awk
29+
arrange awk, cmd '0x'
30+
assemble awk
31+
compute digit, (_db shr 4) and 0Fh
32+
call hex_nibble, digit, cmd
33+
compute digit, _db and 0Fh
34+
call hex_nibble, digit, cmd
35+
compute i, i + 1
36+
check i = bytes
37+
jyes done
38+
arrange awk, cmd ','
39+
assemble awk
40+
check i and 0Fh
41+
jyes jj
42+
arrange awk, cmd '\',13,10
43+
assemble awk
44+
jump jj
45+
done: arrange awk, cmd 13,10
46+
assemble awk
47+
end calminstruction
48+
49+
else
50+
display 'usage: fasmg -i "define FILE ''<filename>''" ',__FILE__,' <outfile>'
51+
end if

0 commit comments

Comments
 (0)