fix bug: windows ps/cmd output unicode in bytecodes abnormally.

so we use the hex format to print them.
This commit is contained in:
ValKmjolnir 2022-03-23 22:54:07 +08:00
parent 1dd3fd445c
commit cf722fd98c
3 changed files with 92 additions and 11 deletions

13
nasal.h
View File

@ -114,6 +114,18 @@ std::string rawstr(const std::string& str)
{
std::string ret("");
for(auto i:str)
{
#ifdef _WIN32
// windows ps or cmd doesn't output unicode normally
// if 'chcp65001' is not enabled, so we output the hex
if(i<=0)
{
ret+="\\x";
ret+="0123456789abcdef"[(i>>4)&15];
ret+="0123456789abcdef"[i&15];
continue;
}
#endif
switch(i)
{
case '\a': ret+="\\a";break;
@ -127,6 +139,7 @@ std::string rawstr(const std::string& str)
case '\0': ret+="\\0";break;
default: ret+=i; break;
}
}
return ret;
}
#include "nasal_err.h"

65
test/qrcode.nas Normal file
View File

@ -0,0 +1,65 @@
import("lib.nas");
var code=[
[1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1],
[1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
[1,0,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,0,1],
[1,0,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,0,1],
[1,0,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,0,1],
[1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
[1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
];
if(os.platform()=="windows")
system("chcp 65001");
var texture=[" ","██"];
for(var i=0;i<size(code);i+=1){
for(var j=0;j<size(code[i]);j+=1)
print(texture[code[i][j]]);
print('\n');
}
var transfer=func(s){
var mode=[0,1,0,0];
println(mode);
var len=size(s);
var vec=[0,0,0,0,0,0,0,0,0];
for(var i=8;i>=0;i-=1){
vec[i]=bits.bitand(1,len);
len=int(len/2);
}
println(vec);
var bitstr=[];
for(var i=0;i<size(s);i+=1){
var tmp=[0,0,0,0,0,0,0,0];
var c=s[i];
for(var j=7;j>=0;j-=1){
tmp[j]=bits.bitand(1,c);
c=int(c/2);
}
foreach(var j;tmp)
append(bitstr,j);
}
println(bitstr);
var end=[0,0,0,0];
println(end);
}
transfer("github.com/ValKmjolnir");

View File

@ -78,6 +78,7 @@ var game=func(x,y){
}
edge0~="╗\n";
edge1~="╝\n";
var vec=[];
setsize(vec,x);
for(var i=0;i<x;i+=1){
@ -117,7 +118,7 @@ var game=func(x,y){
}
s~='║\n';
}
print('\e[0;0H'~edge0~s~edge1);
print('\e[1;1H'~edge0~s~edge1);
},
next:func(){
var (fx,fy)=snake.front();
@ -126,28 +127,28 @@ var game=func(x,y){
snake.push_front([fx,fy-1]);
if(vec[fx][fy-1]==1)
gameover=1;
else if(vec[fx][fy-1]==2)
elsif(vec[fx][fy-1]==2)
eat=1;
vec[fx][fy-1]=1;
}else if(move=='a' and fx-1>=0){
}elsif(move=='a' and fx-1>=0){
snake.push_front([fx-1,fy]);
if(vec[fx-1][fy]==1)
gameover=1;
else if(vec[fx-1][fy]==2)
elsif(vec[fx-1][fy]==2)
eat=1;
vec[fx-1][fy]=1;
}else if(move=='s' and fy+1<y){
}elsif(move=='s' and fy+1<y){
snake.push_front([fx,fy+1]);
if(vec[fx][fy+1]==1)
gameover=1;
else if(vec[fx][fy+1]==2)
elsif(vec[fx][fy+1]==2)
eat=1;
vec[fx][fy+1]=1;
}else if(move=='d' and fx+1<x){
}elsif(move=='d' and fx+1<x){
snake.push_front([fx+1,fy]);
if(vec[fx+1][fy]==1)
gameover=1;
else if(vec[fx+1][fy]==2)
elsif(vec[fx+1][fy]==2)
eat=1;
vec[fx+1][fy]=1;
}else{
@ -161,6 +162,8 @@ var game=func(x,y){
if(eat and snake.length()!=x*y)
setapple();
elsif(snake.length()==x*y)
gameover=2;
},
move:func(c){
if(c=='w' or c=='a' or c=='s' or c=='d')
@ -188,14 +191,14 @@ var main=func(){
if(ch!=nil){
if(ch=='q'[0])
break;
else if(ch=='p'[0]){
elsif(ch=='p'[0]){
print("\rpress any key to continue...");
libkey.getch();
print("\r \r");
}
g.move(chr(ch));
}
unix.sleep(os.platform()=="windows"?0.01:0.02);
unix.sleep(0.02);
counter-=1;
if(counter==0){
counter=20;
@ -206,7 +209,7 @@ var main=func(){
}
}
libkey.close();
println("game over.");
println(g.gameover()<=1?"game over.":"you win!");
println("enter anything to quit.");
input();
}