add readline & os.arch

delete test file mandel.nas

change io.fin => io.readfile
This commit is contained in:
ValKmjolnir 2023-04-28 23:11:17 +08:00
parent c858afbb76
commit f914678311
17 changed files with 158 additions and 4177 deletions

View File

@ -70,7 +70,7 @@ void err() {
std::exit(1);
}
void execute(const string& file,const std::vector<string>& argv,const u32 cmd) {
void execute(const string& file, const std::vector<string>& argv, const u32 cmd) {
using clk=std::chrono::high_resolution_clock;
const auto den=clk::duration::period::den;
@ -88,7 +88,7 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd) {
parse.compile(lex).chkerr();
// linker gets parser's ast and load import files to this ast
ld.link(parse,file,cmd&VM_DETAIL).chkerr();
ld.link(parse, file, cmd&VM_DETAIL).chkerr();
// optimizer does simple optimization on ast
optimize(parse.tree());
@ -105,9 +105,9 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd) {
// run
auto start=clk::now();
if (cmd&VM_DEBUG) {
dbg(err).run(gen,ld,argv);
dbg(err).run(gen, ld, argv);
} else if (cmd&VM_TIME || cmd&VM_EXEC) {
ctx.run(gen,ld,argv,cmd&VM_DETAIL);
ctx.run(gen, ld, argv, cmd&VM_DETAIL);
}
if (cmd&VM_TIME) {
f64 tm=(clk::now()-start).count()*1.0/den;
@ -115,7 +115,7 @@ void execute(const string& file,const std::vector<string>& argv,const u32 cmd) {
}
}
i32 main(i32 argc,const char* argv[]) {
i32 main(i32 argc, const char* argv[]) {
// output version info
if (argc<=1) {
std::clog<<logo;
@ -128,7 +128,7 @@ i32 main(i32 argc,const char* argv[]) {
if (s=="-h" || s=="--help") {
std::clog<<help;
} else if (s[0]!='-') {
execute(s,{},VM_EXEC);
execute(s, {}, VM_EXEC);
} else {
err();
}
@ -137,12 +137,18 @@ i32 main(i32 argc,const char* argv[]) {
// execute with arguments
const std::unordered_map<string,u32> cmdlst={
{"--ast",VM_AST},{"-a",VM_AST},
{"--code",VM_CODE},{"-c",VM_CODE},
{"--exec",VM_EXEC},{"-e",VM_EXEC},
{"--time",VM_TIME|VM_EXEC},{"-t",VM_TIME|VM_EXEC},
{"--detail",VM_DETAIL|VM_EXEC},{"-d",VM_DETAIL|VM_EXEC},
{"--debug",VM_DEBUG},{"-dbg",VM_DEBUG}
{"--ast",VM_AST},
{"-a",VM_AST},
{"--code",VM_CODE},
{"-c",VM_CODE},
{"--exec",VM_EXEC},
{"-e",VM_EXEC},
{"--time",VM_TIME|VM_EXEC},
{"-t",VM_TIME|VM_EXEC},
{"--detail",VM_DETAIL|VM_EXEC},
{"-d",VM_DETAIL|VM_EXEC},
{"--debug",VM_DEBUG},
{"-dbg",VM_DEBUG}
};
u32 cmd=0;
string filename="";
@ -159,6 +165,6 @@ i32 main(i32 argc,const char* argv[]) {
if (!filename.length()) {
err();
}
execute(filename,vm_argv,cmd?cmd:VM_EXEC);
execute(filename, vm_argv, cmd?cmd:VM_EXEC);
return 0;
}

View File

@ -54,7 +54,6 @@ test:nasal
@ ./nasal -d test/lexer.nas
@ ./nasal -d test/life.nas
@ ./nasal -t test/loop.nas
@ ./nasal -t -d test/mandel.nas
@ ./nasal -t test/mandelbrot.nas
@ ./nasal -t test/md5.nas
@ ./nasal -t -d test/md5compare.nas

62
nasal.h
View File

@ -12,7 +12,7 @@
#include <vector>
bool is_windows() {
#if defined _WIN32 || defined _WIN64
#if defined(_WIN32) || defined(_WIN64)
return true;
#else
return false;
@ -35,6 +35,66 @@ bool is_macos() {
#endif
}
bool is_x86() {
#if defined(__i386__) || defined(_M_IX86)
return true;
#else
return false;
#endif
}
bool is_amd64() {
#if defined(__amd64__) || defined(_M_X64)
return true;
#else
return false;
#endif
}
bool is_x86_64() {
return is_amd64();
}
bool is_arm() {
#if defined(__arm__) || defined(_M_ARM)
return true;
#else
return false;
#endif
}
bool is_aarch64() {
#if defined(__aarch64__) || defined(_M_ARM64)
return true;
#else
return false;
#endif
}
bool is_ia64() {
#if defined(__ia64__)
return true;
#else
return false;
#endif
}
bool is_powerpc() {
#if defined(__powerpc__)
return true;
#else
return false;
#endif
}
bool is_superh() {
#if defined(__sh__)
return true;
#else
return false;
#endif
}
using i32=std::int32_t;
using i64=std::int64_t;
using u8=std::uint8_t;

View File

@ -102,10 +102,10 @@ var builtin_input(var* local,gc& ngc) {
return ret;
}
var builtin_fin(var* local,gc& ngc) {
var builtin_readfile(var* local,gc& ngc) {
var val=local[1];
if (val.type!=vm_str) {
return nas_err("io::fin","\"filename\" must be string");
return nas_err("io::readfile","\"filename\" must be string");
}
std::ifstream in(val.str(),std::ios::binary);
std::stringstream rd;
@ -1008,6 +1008,27 @@ var builtin_platform(var* local,gc& ngc) {
return ngc.newstr("unknown");
}
var builtin_arch(var* local,gc& ngc) {
if (is_x86()) {
return ngc.newstr("x86");
} else if (is_x86_64()) {
return ngc.newstr("x86-64");
} else if (is_amd64()) {
return ngc.newstr("amd64");
} else if (is_arm()) {
return ngc.newstr("arm");
} else if (is_aarch64()) {
return ngc.newstr("aarch64");
} else if (is_ia64()) {
return ngc.newstr("ia64");
} else if (is_powerpc()) {
return ngc.newstr("powerpc");
} else if (is_superh()) {
return ngc.newstr("superh");
}
return ngc.newstr("unknown");
}
// md5 related functions
string tohex(u32 num) {
const char str16[]="0123456789abcdef";
@ -1272,7 +1293,7 @@ struct {
{"__setsize", builtin_setsize },
{"__system", builtin_system },
{"__input", builtin_input },
{"__fin", builtin_fin },
{"__readfile",builtin_readfile},
{"__fout", builtin_fout },
{"__split", builtin_split },
{"__rand", builtin_rand },
@ -1343,6 +1364,7 @@ struct {
{"__dlcallv", builtin_dlcallv },
{"__dlcall", builtin_dlcall },
{"__platform",builtin_platform},
{"__arch", builtin_arch },
{"__md5", builtin_md5 },
{"__cocreate",builtin_cocreate},
{"__coresume",builtin_coresume},

View File

@ -181,23 +181,10 @@ struct nas_obj {
private:
/* RAII constructor, new object is initialized when creating */
void file_dtor() {
fclose((FILE*)ptr);
}
void dir_dtor() {
#ifndef _MSC_VER
closedir((DIR*)ptr);
#else
FindClose(ptr);
#endif
}
void dylib_dtor() {
#ifdef _WIN32
FreeLibrary((HMODULE)ptr);
#else
dlclose(ptr);
#endif
}
void file_dtor();
void dir_dtor();
void dylib_dtor();
public:
nas_obj():type(obj_type::null),ptr(nullptr) {}
~nas_obj() {clear();}
@ -359,6 +346,27 @@ void nas_obj::clear() {
ptr=nullptr;
}
void nas_obj::file_dtor() {
if ((FILE*)ptr==stdin) {
return;
}
fclose((FILE*)ptr);
}
void nas_obj::dir_dtor() {
#ifndef _MSC_VER
closedir((DIR*)ptr);
#else
FindClose(ptr);
#endif
}
void nas_obj::dylib_dtor() {
#ifdef _WIN32
FreeLibrary((HMODULE)ptr);
#else
dlclose(ptr);
#endif
}
void nas_co::clear() {
for(u32 i=0;i<STACK_DEPTH;++i) {
stack[i]=var::nil();

View File

@ -1,7 +1,7 @@
# lib csv.nas
# ValKmjolnir 2022/10/15
var read_csv=func(path,delimeter=",",endline="\n"){
var context=io.fin(path);
var context=io.readfile(path);
context=split(endline,context);
forindex(var i;context){
context[i]=split(delimeter,context[i]);

View File

@ -29,6 +29,12 @@ var input=func(end=nil){
return __input(end);
}
# readline
var readline=func(prompt="> ") {
print(prompt);
return input("\n");
}
# split a string by separator for example:
# split("ll","hello world") -> ["he","o world"]
# this function will return a vector.
@ -291,7 +297,7 @@ var io={
SEEK_CUR:1,
SEEK_END:2,
# get content of a file by filename. returns a string.
fin: func(filename){return __fin(filename);},
readfile: func(filename){return __readfile(filename);},
# input a string as the content of a file.
fout: func(filename,str){return __fout(filename,str);},
# use C access
@ -462,8 +468,9 @@ var dylib={
# windows/macOS/linux are supported.
var os={
# get a string that tell which os it runs on.
platform: func(){return __platform;},
time: func(){return __logtime; }
platform: func() {return __platform;},
time: func() {return __logtime;},
arch: func() {return __arch;}
};
# runtime gives us some functions that we could manage it manually.

View File

@ -58,7 +58,7 @@ var calc=func(codetype,files,path=""){
println(codetype);
var (bytes,ctx,line,semi,line_cnt,semi_cnt)=(0,"",0,0,0,0);
forindex(var i;files){
var s=io.exists(path~files[i])?io.fin(path~files[i]):"";
var s=io.exists(path~files[i])?io.readfile(path~files[i]):"";
(line_cnt,semi_cnt)=(count(s,'\n'),count(s,';'));
println(rightpad(files[i],padding_length),'|',
column(line_cnt),' line |',

View File

@ -118,7 +118,7 @@ func(diff){
);
print("\n");
diff(
io.fin("test/bf.nas"),
io.fin("test/bfconvertor.nas")
io.readfile("test/bf.nas"),
io.readfile("test/bfconvertor.nas")
);
}(myers);

View File

@ -44,11 +44,11 @@ var s=func(){
println(" nasal hexdump.nas [file] | get single file's hexdump.");
return "";
}
return io.fin(argv[0]);
return io.readfile(argv[0]);
}
var ret="";
foreach(var elem;filename)
ret~=io.fin(elem);
ret~=io.readfile(elem);
return ret;
}();

View File

@ -73,7 +73,7 @@ var html_read_file=func(filename){
var timer=maketimestamp();
timer.stamp();
var keyword=["var","func","for","while","foreach","forindex","break","continue","return","if","else","elsif","nil"];
var file_text=io.fin(filename);
var file_text=io.readfile(filename);
var (s,index,len)=("",-1,size(file_text));
var content="";
@ -302,7 +302,7 @@ while(1){
var page_back="</pre>\n</body>\n</html>\n";
http.send(client,respond.ok(page~html_read_file("./test/"~filename)~page_back));
}else{
http.send(client,respond.ok(io.fin("./doc/nasal-http-test-web.html")));
http.send(client,respond.ok(io.readfile("./doc/nasal-http-test-web.html")));
}
}
elsif(path=="/shutdown"){
@ -310,11 +310,11 @@ while(1){
break;
}
elsif(path=="/favicon.ico")
http.send(client,respond.ok(io.fin("./doc/pic/favicon.ico")));
http.send(client,respond.ok(io.readfile("./doc/pic/favicon.ico")));
elsif(path=="/license")
http.send(client,respond.ok(io.fin("./LICENSE")));
http.send(client,respond.ok(io.readfile("./LICENSE")));
elsif(path=="/doc/pic/nasal.png" or path=="/doc/pic/benchmark.png" or path=="/doc/pic/mandelbrot.png")
http.send(client,respond.ok(io.fin("."~path)));
http.send(client,respond.ok(io.readfile("."~path)));
else{
var filename=substr(path,1,size(path)-1);
if(contains(files,filename)){

View File

@ -1,7 +1,7 @@
var lexer=func(file)
{
var (ptr,token)=(0,[]);
var s=io.fin(file);
var s=io.readfile(file);
var len=size(s);
var line=0;
var gen=func(tok){

View File

@ -70,7 +70,7 @@ var ppm_gen=func(width,height){
var init=func(){
var res=new_map();
if(io.exists(".life_data")) {
var vec=split("\n",io.fin(".life_data"));
var vec=split("\n",io.readfile(".life_data"));
if (num(vec[0])!=width or num(vec[1])!=height) {
die("incorrect width or height: "~vec[0]~":"~str(width)~" / "~vec[1]~":"~str(height))
}

File diff suppressed because it is too large Load Diff

View File

@ -67,7 +67,7 @@ var filechecksum=func(){
timestamp.stamp();
var bar=process_bar.high_resolution_bar(40);
forindex(var i;files){
var f=io.fin(files[i]);
var f=io.readfile(files[i]);
var res=md5(f);
byte+=size(f);
if(cmp(res,_md5(f))){

View File

@ -11,7 +11,7 @@ if(is_windows_platform){
var cpu_stat=func(){
if(is_windows_platform or is_macos_platform)
return nil;
var cpu=split("\n",io.fin("/proc/stat"))[0];
var cpu=split("\n",io.readfile("/proc/stat"))[0];
cpu=split(" ",cpu);
cpu={
name:cpu[0],
@ -54,7 +54,7 @@ var cpu_occupation=func(){
var mem_occupation=func(){
if(is_windows_platform or is_macos_platform)
return {MemTotal:math.inf,MemFree:math.inf};
var meminfo=split("\n",io.fin("/proc/meminfo"));
var meminfo=split("\n",io.readfile("/proc/meminfo"));
var mem_res={};
forindex(var i;meminfo){
var tmp=split(" ",meminfo[i])[0:1];
@ -149,7 +149,7 @@ func(){
cpu_occupation_log[-1]=cpu_occ;
mem_occupation_log[-1]=mem_occ;
println("\e[1;1H\e[1m Operating system : \e[0m",is_windows_platform?"\e[31m":"\e[36m",os.platform(),"\e[0m");
println("\e[1;1H\e[1m Operating system : \e[0m",is_windows_platform?"\e[31m":"\e[36m",os.platform()," ",os.arch(),"\e[0m");
println("\e[2;1H\e[1m Current time : \e[0m\e[36m",os.time(),"\e[0m");
println("\e[3;1H\e[1m Memory total(GB) : \e[0m\e[36m",mem.MemTotal/1024/1024,"\e[0m");
println("\e[4;1H\e[1m Memory free(GB) : \e[0m\e[36m",mem.MemFree/1024/1024,"\e[0m");

View File

@ -55,7 +55,7 @@ func(argv){
}
var file_content="";
foreach(var f;argv)
file_content~=io.fin(f)~" ";
file_content~=io.readfile(f)~" ";
var vec=keys(spliter(file_content));
sort(vec,func(a,b){return cmp(a,b)<=0;});
println(vec);