Skip to content

Commit

Permalink
✨ add readline & os.arch
Browse files Browse the repository at this point in the history
delete test file mandel.nas

change io.fin => io.readfile
  • Loading branch information
ValKmjolnir committed Apr 28, 2023
1 parent c858afb commit f914678
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 4,177 deletions.
32 changes: 19 additions & 13 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
Expand All @@ -105,17 +105,17 @@ 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;
std::clog<<"process exited after "<<tm<<"s.\n\n";
}
}

i32 main(i32 argc,const char* argv[]) {
i32 main(i32 argc, const char* argv[]) {
// output version info
if (argc<=1) {
std::clog<<logo;
Expand All @@ -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();
}
Expand All @@ -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="";
Expand All @@ -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;
}
1 change: 0 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 61 additions & 1 deletion nasal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <vector>

bool is_windows() {
#if defined _WIN32 || defined _WIN64
#if defined(_WIN32) || defined(_WIN64)
return true;
#else
return false;
Expand All @@ -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;
Expand Down
28 changes: 25 additions & 3 deletions nasal_builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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},
Expand Down
42 changes: 25 additions & 17 deletions nasal_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion stl/csv.nas
Original file line number Diff line number Diff line change
@@ -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]);
Expand Down
13 changes: 10 additions & 3 deletions stl/lib.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/calc.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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 |',
Expand Down
4 changes: 2 additions & 2 deletions test/diff.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 2 additions & 2 deletions test/hexdump.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}();

Expand Down
10 changes: 5 additions & 5 deletions test/httptest.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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="";

Expand Down Expand Up @@ -302,19 +302,19 @@ 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"){
http.send(client,respond.ok("http server shutdown."));
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)){
Expand Down
2 changes: 1 addition & 1 deletion test/lexer.nas
Original file line number Diff line number Diff line change
@@ -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){
Expand Down
Loading

0 comments on commit f914678

Please sign in to comment.