update hexdump.nas

This commit is contained in:
Li Haokun 2021-08-13 15:10:20 +08:00 committed by GitHub
parent e4c598cae6
commit b2be386be8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 77 additions and 11 deletions

View File

@ -1,5 +1,9 @@
# hexdump.nas by ValKmjolnir
# 2021/8/13
import("lib.nas");
# init
var hex_num=[
'0','1','2','3',
'4','5','6','7',
@ -9,17 +13,79 @@ var hex_num=[
var hex=[];
foreach(var i;hex_num)
foreach(var j;hex_num)
append(hex,'0x'~i~j);
var s=io.fin("lib.nas");
append(hex,i~j);
# read file
var s=func()
{
var filename=[
"nasal.h",
"main.cpp",
"nasal_ast.h",
"nasal_builtin.h",
"nasal_codegen.h",
"nasal_gc.h",
"nasal_import.h",
"nasal_lexer.h",
"nasal_parse.h",
"nasal_vm.h",
];
var ret="";
foreach(var elem;filename)
ret~=io.fin(elem);
return ret;
}();
# used to change line and control the spaces
var cnt=0;
print(0,'\t');
for(var i=0;i<size(s);i+=1){
if(cnt==8){
cnt=0;
print('\n',i,'\t');
}
cnt+=1;
print(hex[s[i]],' ');
# used to print hex index
var hex_index=[0,0,0,0];
# print binary in text format
var textprint=func(index)
{
print(' |');
for(var i=index-cnt;i<index;i+=1)
print((0<=s[i] and s[i]<32)?'.':chr(s[i]));
for(var i=cnt;i<16;i+=1)
print('.');
print('|\n');
}
print('\n');
# print index
var indexprint=func(index)
{
forindex(var i;hex_index)
{
hex_index[i]=index-int(index/256)*256;
index=int(index/256);
}
for(var i=3;i>=0;i-=1)
print(hex[hex_index[i]]);
print(' ');
return;
}
# main
func()
{
indexprint(0);
for(var i=0;i<size(s);i+=1)
{
if(cnt==16){
textprint(i);
cnt=0;
indexprint(i);
}elsif(cnt==8){
print(' ');
}
cnt+=1;
print(hex[s[i]],' ');
}
for(var l=cnt;l<16;l+=1)
print(' ');
if(cnt<8)
print(' ');
textprint(i);
}();