#include #include "parser.hpp" #if defined(HAVE_LIBCURL) #include #endif #if defined(HAVE_LIBSDL2) #include #endif #if defined(GEKKO) //#include #else #include #endif #if defined(HW_RVL) #include #include #endif #include string GetLabelText(BoxScript::Expression* exp) { if(exp->GetNodeName()=="VariableGetValueNode") { return ((BoxScript::VariableGetValueNode*)exp)->GetName(); } throw exception(); } int64_t runtime_gt(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) > expressions[1]->Evaluate(state); } int64_t runtime_gte(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) >= expressions[1]->Evaluate(state); } int64_t runtime_lt(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) < expressions[1]->Evaluate(state); } int64_t runtime_lte(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) <= expressions[1]->Evaluate(state); } int64_t runtime_eq(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) == expressions[1]->Evaluate(state); } int64_t runtime_not(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); return !expressions[0]->Evaluate(state); } int64_t runtime_xor(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) ^ expressions[1]->Evaluate(state); } int64_t runtime_bor(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) | expressions[1]->Evaluate(state); } int64_t runtime_cor(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) || expressions[1]->Evaluate(state); } int64_t runtime_band(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) & expressions[1]->Evaluate(state); } int64_t runtime_cand(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) && expressions[1]->Evaluate(state); } int64_t runtime_lshift(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) << (int)(expressions[1]->Evaluate(state)); } int64_t runtime_rshift(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); return expressions[0]->Evaluate(state) >> (int)(expressions[1]->Evaluate(state)); } int64_t runtime_printint(BoxScript::IApplicationState* state,std::vector expressions) { for(auto val : expressions) { printf("%i",(int)val->Evaluate(state)); } return 0; } int64_t runtime_printintln(BoxScript::IApplicationState* state,std::vector expressions) { for(auto val : expressions) { printf("%i\n",(int)val->Evaluate(state)); } return 0; } int64_t runtime_printchars(BoxScript::IApplicationState* state,std::vector expressions) { for(auto val : expressions) { printf("%c",(char)val->Evaluate(state)); } return 0; } int64_t runtime_udpclient(BoxScript::IApplicationState* state,std::vector expressions) { BoxScript::Stream* strm=BoxScript::UdpClient(state->BoxToString(expressions[0]->Evaluate(state)),expressions[1]->Evaluate(state)); return state->CreateStream(strm); } int64_t runtime_tcpclient(BoxScript::IApplicationState* state,std::vector expressions) { BoxScript::Stream* strm=BoxScript::TcpClient(state->BoxToString(expressions[0]->Evaluate(state)),expressions[1]->Evaluate(state)); return state->CreateStream(strm); } class threadctx_t { public: BoxScript::Expression* expression; BoxScript::IApplicationState* appstate; }; class clientctx_t { public: BoxScript::Expression* expression; BoxScript::IApplicationState* appstate; int64_t stream; int64_t ip; } ; static void* __me_create_thread(void* t) { fprintf(stdout,"\t\tMECREATETHREAD\n"); fflush(stdout); threadctx_t* tInfo = static_cast(t); //this is a thread tInfo->expression->Evaluate(tInfo->appstate); tInfo->appstate->RemoveThreadOwner(); delete tInfo; return NULL; } static void* __me_create_thread2(void* t) { clientctx_t* tInfo = static_cast(t); //this is a thread tInfo->expression->Evaluate(tInfo->appstate); tInfo->appstate->CloseStream(tInfo->stream); tInfo->appstate->DestroyBox(tInfo->ip); tInfo->appstate->RemoveThreadOwner(); delete tInfo; return NULL; } int64_t runtime_threadcreate(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); threadctx_t* tInfo = new threadctx_t(); tInfo->expression = expressions[0]; tInfo->appstate=state->NewScope(); tInfo->appstate->AddThreadOwner(); #if defined(GEKKO) lwp_t pthread = LWP_THREAD_NULL; auto res = LWP_CreateThread (&pthread, __me_create_thread, tInfo, NULL, 8196, 80); // return (int64_t)pthread; #else std::thread* t = new std::thread(__me_create_thread,tInfo); return (int64_t)t; #endif return 0; } class serverctx_t { public: BoxScript::Expression* expression; BoxScript::IApplicationState* appstate; string variable_name; string ip_name; string ip; int64_t port; }; static void* __udp_server_thread(void* t) { serverctx_t* tInfo = (serverctx_t*)t; //this is a thread #if defined(_WIN32) || defined(WIN32) SOCKET fd=BoxScript::UdpServerInit(tInfo->ip,tInfo->port); #else int fd=BoxScript::UdpServerInit(tInfo->ip,tInfo->port); #endif while(true) { string ip_addr=""; BoxScript::Stream* strm= BoxScript::AcceptClient(fd,&ip_addr); BoxScript::IApplicationState* state2=tInfo->appstate->NewScope(); state2->AddThreadOwner(); clientctx_t* tInfo2=new clientctx_t(); tInfo2->expression = tInfo->expression; tInfo2->appstate=state2; tInfo2->ip=state2->CreateBox(ip_addr); tInfo2->stream = state2->CreateStream(strm); state2->SetVariable(tInfo->ip_name,tInfo2->ip); state2->SetVariable(tInfo->variable_name,tInfo2->stream); //start for thread #if defined(GEKKO) lwp_t pthread = LWP_THREAD_NULL; LWP_CreateThread (&pthread, __me_create_thread2, tInfo2, NULL, 8196, 80); #else std::thread t(__me_create_thread2,tInfo2); #endif } /*BoxScript::IApplicationState* state=tInfo->appstate->NewScope(); state->AddThreadOwner(); tInfo->expression->Evaluate(state); state->RemoveThreadOwner();*/ tInfo->appstate->RemoveThreadOwner(); delete tInfo; return NULL; } static void* __tcp_server_thread(void* t) { serverctx_t* tInfo = (serverctx_t*)t; //this is a thread #if defined(_WIN32) || defined(WIN32) SOCKET fd=BoxScript::TcpServerInit(tInfo->ip,tInfo->port); #else int fd=BoxScript::TcpServerInit(tInfo->ip,tInfo->port); #endif while(true) { string ip_addr=""; BoxScript::Stream* strm= BoxScript::AcceptClient(fd,&ip_addr); BoxScript::IApplicationState* state2=tInfo->appstate->NewScope(); state2->AddThreadOwner(); clientctx_t* tInfo2=new clientctx_t(); tInfo2->expression = tInfo->expression; tInfo2->appstate=state2; tInfo2->ip=state2->CreateBox(ip_addr); tInfo2->stream = state2->CreateStream(strm); state2->SetVariable(tInfo->ip_name,tInfo2->ip); state2->SetVariable(tInfo->variable_name,tInfo2->stream); //start for thread #if defined(GEKKO) lwp_t pthread = LWP_THREAD_NULL; LWP_CreateThread (&pthread, __me_create_thread2, tInfo2, NULL, 8196, 80); #else std::thread t(__me_create_thread2,tInfo2); #endif } /*BoxScript::IApplicationState* state=tInfo->appstate->NewScope(); state->AddThreadOwner(); tInfo->expression->Evaluate(state); state->RemoveThreadOwner();*/ tInfo->appstate->RemoveThreadOwner(); delete tInfo; return NULL; } int64_t runtime_tcpserver(BoxScript::IApplicationState* state,std::vector expressions) { //tcpserver("0.0.0.0",4202,strm,ip,{}) if(expressions.size() < 5) throw exception(); serverctx_t* tInfo = new serverctx_t(); state->AddThreadOwner(); tInfo->appstate=state; tInfo->ip=""; tInfo->ip += state->BoxToString(expressions[0]->Evaluate(state)); tInfo->port = expressions[1]->Evaluate(state); tInfo->variable_name = GetLabelText(expressions[2]); tInfo->ip_name = GetLabelText(expressions[3]); tInfo->expression = expressions[4]; #if defined(GEKKO) lwp_t pthread = LWP_THREAD_NULL; LWP_CreateThread (&pthread, __tcp_server_thread, tInfo, NULL, 8196, 80); return (int64_t)pthread; #else std::thread* t = new std::thread(__tcp_server_thread,tInfo); return (int64_t)t; #endif } int64_t runtime_udpserver(BoxScript::IApplicationState* state,std::vector expressions) { //udpserver("0.0.0.0",4202,strm,ip,{}) if(expressions.size() < 5) throw exception(); serverctx_t* tInfo = new serverctx_t(); state->AddThreadOwner(); tInfo->appstate=state; tInfo->ip = state->BoxToString(expressions[0]->Evaluate(state)); tInfo->port = expressions[1]->Evaluate(state); tInfo->variable_name = GetLabelText(expressions[2]); tInfo->ip_name = GetLabelText(expressions[3]); tInfo->expression = expressions[4]; #if defined(GEKKO) lwp_t pthread = LWP_THREAD_NULL; LWP_CreateThread (&pthread, __udp_server_thread, tInfo, NULL, 8196, 80); return (int64_t)pthread; #else std::thread* t = new std::thread(__udp_server_thread,tInfo); return (int64_t)t; #endif } int64_t runtime_clearscreen(BoxScript::IApplicationState* state,std::vector expressions) { #if defined(_WIN32) || defined(WIN32) clrscr(); #else printf("\e[3J\033c"); #endif #if defined(GEKKO) clear_screen_ogc(); printf ("\x1b[%d;%dH",2,2); #endif return 0; } int64_t runtime_setpos(BoxScript::IApplicationState* state,std::vector expressions) { int x=0,y=0; if(expressions.size() >= 2) { x = (int)expressions[0]->Evaluate(state); y = (int)expressions[1]->Evaluate(state); } #if defined(_WIN32) || defined(WIN32) #else printf ("\x1b[%d;%dH",y ,x); #endif return 0; } int64_t runtime_exit(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() > 0) { DestroyApp((int)expressions[0]->Evaluate(state)); }else{ DestroyApp(0); } return 0; } int64_t runtime_updatescreen(BoxScript::IApplicationState* state,std::vector expressions) { #if defined(GEKKO) VIDEO_WaitVSync(); #endif return 0; } #if defined(HAVE_LIBCURL) int64_t runtime_httpclient_create(BoxScript::IApplicationState* state,std::vector expressions) { CURL* curl = curl_easy_init(); return (int64_t)curl; } int64_t runtime_httpclient_seturl(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); CURL* curl=(CURL*)expressions[0]->Evaluate(state); curl_easy_setopt(curl, CURLOPT_URL,state->BoxToString(expressions[1]->Evaluate(state)).c_str()); return 0; } #else int64_t runtime_httpclient_create(BoxScript::IApplicationState* state,std::vector expressions) { return 0; } #endif #if defined(HW_RVL) int64_t runtime_wpadscanpads(BoxScript::IApplicationState* state,std::vector expressions) { return (int64_t)WPAD_ScanPads(); } int64_t runtime_wpadbuttonsdown(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return (int64_t)WPAD_ButtonsDown(expressions[0]->Evaluate(state)); } int64_t runtime_wpadbuttonsup(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return (int64_t)WPAD_ButtonsUp(expressions[0]->Evaluate(state)); } int64_t runtime_wpadbuttona(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_A; } int64_t runtime_wpadbuttonb(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_B; } int64_t runtime_wpadbutton1(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_1; } int64_t runtime_wpadbutton2(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_2; } int64_t runtime_wpadbuttonhome(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_HOME; } int64_t runtime_wpadbuttonplus(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_PLUS; } int64_t runtime_wpadbuttonminus(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_MINUS; } int64_t runtime_wpadbuttonup(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_UP; } int64_t runtime_wpadbuttondown(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_DOWN; } int64_t runtime_wpadbuttonleft(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_LEFT; } int64_t runtime_wpadbuttonright(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; uint32_t btn= (uint32_t)expressions[0]->Evaluate(state); return btn & WPAD_BUTTON_RIGHT; } #endif #if defined(USE_LIBSDL2) int64_t runtime_sdl2init(BoxScript::IApplicationState* state,std::vector expressions) { return SDL_Init(SDL_INIT_EVERYTHING); } int64_t runtime_sdl2createwindow(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 6) throw exception(); string title=state->BoxToString(expressions[0]->Evaluate(state)); return (int64_t)SDL_CreateWindow(title.c_str(),(int)expressions[1]->Evaluate(state),(int)expressions[2]->Evaluate(state),(int)expressions[3]->Evaluate(state),(int)expressions[4]->Evaluate(state),(Uint32)expressions[5]->Evaluate(state)); } #endif int64_t runtime_for(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 4) throw exception(); string variableName=GetLabelText(expressions[0]); int64_t max = expressions[1]->Evaluate(state); BoxScript::Expression* increment = expressions[2]; increment->DisableScope(); BoxScript::Expression* runInFor = expressions[3]; runInFor->DisableScope(); BoxScript::IApplicationState* scope = state->NewScope(); scope->AddThreadOwner(); for(;state->GetVariable(variableName) < max;state->SetVariable(variableName,increment->Evaluate(scope))) { scope->ExitIfNotRunning(); runInFor->Evaluate(scope); } scope->RemoveThreadOwner(); return 0; } int64_t runtime_if(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 3) throw exception(); BoxScript::Expression* condition = expressions[0]; condition->DisableScope(); BoxScript::Expression* truth = expressions[1]; truth->DisableScope(); BoxScript::Expression* falsey = expressions[2]; truth->DisableScope(); bool num=condition->Evaluate(state) != 0; if(num) { truth->Evaluate(state); }else{ falsey->Evaluate(state); } return 0; } int64_t runtime_e2n(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() > 0) { return state->e2n(expressions[0]); } throw exception(); } int64_t runtime_calln(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() > 0) { return state->Calln(expressions[0]->Evaluate(state))->Evaluate(state); } throw exception(); } int64_t runtime_readchar(BoxScript::IApplicationState* state,std::vector expressions) { return getchar(); } int64_t runtime_mean(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() == 0) return 0; int64_t mean=0; for(auto exp : expressions) { mean += exp->Evaluate(state); } mean /= (int64_t)expressions.size(); return mean; } int64_t runtime_boxcreate(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() > 0) { int64_t esize = expressions[0]->Evaluate(state); if(esize > 2147483647) throw exception(); int size = (int)esize; return state->CreateBox(size); } return state->CreateBox(100); } int64_t runtime_boxgetvalue(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t bId = expressions[0]->Evaluate(state); int64_t eindex = expressions[1]->Evaluate(state); if(eindex > 2147483647) throw exception(); int index = (int)eindex; return state->GetBoxValue(bId,index); } int64_t runtime_boxsetvalue(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 3) throw exception(); int64_t bId = expressions[0]->Evaluate(state); int64_t eindex = expressions[1]->Evaluate(state); if(eindex > 2147483647) throw exception(); int index = (int)eindex; int64_t value = expressions[2]->Evaluate(state); state->SetBoxValue(bId,index,value); return 0; } int64_t runtime_boxprint(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t bId = expressions[0]->Evaluate(state); cout << state->BoxToString(bId); return 0; } int64_t runtime_boxprintln(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t bId = expressions[0]->Evaluate(state); cout << state->BoxToString(bId) << endl; return 0; } int64_t runtime_boxlen(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t bId = expressions[0]->Evaluate(state); return state->BoxLength(bId); } int64_t runtime_boxadd(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t bId = expressions[0]->Evaluate(state); int64_t value = expressions[1]->Evaluate(state); state->AddToBox(bId,value); return 0; } int64_t runtime_boxremove(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t bId = expressions[0]->Evaluate(state); int64_t value = expressions[1]->Evaluate(state); state->RemoveFromBox(bId,value); return 0; } int64_t runtime_boxinsert(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 3) throw exception(); int64_t bId = expressions[0]->Evaluate(state); int64_t eindex = expressions[1]->Evaluate(state); if(eindex > 2147483647) throw exception(); int index = (int)eindex; int64_t value = expressions[2]->Evaluate(state); state->InsertToBox(bId,index,value); return 0; } int64_t runtime_boxremoveat(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t bId = expressions[0]->Evaluate(state); int64_t eindex = expressions[1]->Evaluate(state); if(eindex > 2147483647) throw exception(); int index = (int)eindex; state->RemoveAtFromBox(bId,index); return 0; } int64_t runtime_streamcreate(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 10) throw exception(); return state->CreateStream(new BoxScript::ExpressionStream(state,expressions)); } int64_t runtime_boxclear(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t bId = expressions[0]->Evaluate(state); state->ClearBox(bId); return 0; } int64_t runtime_streamcopy(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t sId1 = expressions[0]->Evaluate(state); int64_t sId2 = expressions[1]->Evaluate(state); state->CopyStream(sId1,sId2); return 0; } int64_t runtime_streamread(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 4) throw exception(); int64_t sId = expressions[0]->Evaluate(state); int64_t bId = expressions[1]->Evaluate(state); int64_t offset = expressions[2]->Evaluate(state); int64_t len = expressions[3]->Evaluate(state); return state->ReadFromStream(sId,bId,offset,len); } int64_t runtime_streamwrite(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 4) throw exception(); int64_t sId = expressions[0]->Evaluate(state); int64_t bId = expressions[1]->Evaluate(state); int64_t offset = expressions[2]->Evaluate(state); int64_t len = expressions[3]->Evaluate(state); state->WriteToStream(sId,bId,offset,len); return 0; } int64_t runtime_closestream(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t sId = expressions[0]->Evaluate(state); state->CloseStream(sId); return 0; } int64_t runtime_boxreadline(BoxScript::IApplicationState* state,std::vector expressions) { string text; int chr; while((chr = getchar()) != '\n') { if(chr == EOF) break; text += (char)chr; } return state->CreateBox(text); } int64_t runtime_createfunction(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); string func_name = GetLabelText(expressions[0]); BoxScript::Expression* func_body = expressions[expressions.size()-1]; vector args; for(int i =1;iAddFunction(func_name,args,func_body); return 0; } int64_t runtime_filecreate(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() <1) throw exception(); string name = state->BoxToString(expressions[0]->Evaluate(state)); FILE* f = fopen(name.c_str(),"wb"); return state->CreateStream(new BoxScript::FileStream(f,false,true,true,true)); } int64_t runtime_fileopenread(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() <1) throw exception(); string name = state->BoxToString(expressions[0]->Evaluate(state)); FILE* f = fopen(name.c_str(),"rb"); return state->CreateStream(new BoxScript::FileStream(f,true,false,true,true)); } int64_t runtime_fileopenreadwrite(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() <1) throw exception(); string name = state->BoxToString(expressions[0]->Evaluate(state)); FILE* f = fopen(name.c_str(),"rb+"); return state->CreateStream(new BoxScript::FileStream(f,true,true,true,true)); } int64_t runtime_stdin(BoxScript::IApplicationState* state,std::vector expressions) { return state->CreateStream(new BoxScript::FileStream(stdin,true,false,false,false)); } int64_t runtime_stdout(BoxScript::IApplicationState* state,std::vector expressions) { return state->CreateStream(new BoxScript::FileStream(stdout,false,true,false,false)); } int64_t runtime_stderr(BoxScript::IApplicationState* state,std::vector expressions) { return state->CreateStream(new BoxScript::FileStream(stderr,false,true,false,false)); } int64_t runtime_streamflush(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; state->FlushStream(expressions[0]->Evaluate(state)); return 0; } int64_t runtime_streamsetpos(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) return 0; state->SetStreamPosition(expressions[0]->Evaluate(state),expressions[1]->Evaluate(state)); return 0; } int64_t runtime_streamgetpos(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return state->GetStreamPosition(expressions[0]->Evaluate(state)); } int64_t runtime_streamgetlen(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return state->GetStreamLength(expressions[0]->Evaluate(state)); } int64_t runtime_streamcanread(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return state->CanRead(expressions[0]->Evaluate(state)) ? 1 : 0; } int64_t runtime_streamcanwrite(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return state->CanWrite(expressions[0]->Evaluate(state)) ? 1 : 0; } int64_t runtime_streamcanseek(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; return state->CanSeek(expressions[0]->Evaluate(state)) ? 1 : 0; } int64_t runtime_isplatform(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) return 0; string platform=state->BoxToString(expressions[0]->Evaluate(state)); if(platform == "c++" || platform == "C++" || platform == "cpp" || platform == "CPP") { return 1; } #if defined(HW_RVL) if(platform == "wii" || platform == "Wii" || platform == "WII" || platform == "RVL" || platform == "rvl") { return 1; } #endif #if defined(HW_DOL) if(platform == "GameCube" || platform == "GC" || platform == "Gamecube" || platform == "cube" || platform == "gcn" || platform == "ngc" || platform == "GCN" || platform == "NGC" || platform == "Cube" || platform == "gamecube") { return 1; } #endif #if __unix__ if(platform == "linux" || platform == "unix" || platform == "mac" || platform == "Mac" || platform == "Linux" || platform == "Unix") { return 1; } #endif #if defined(_WIN32) || defined(WIN32) if(platform == "win" || platform == "windows" || platform == "Windows" || platform == "win32" || platform == "WINDOWS" || platform == "Win32" || platform=="Win") { return 1; } #endif #if defined(HAVE_LIBCURL) if(platform == "curl" || platform == "libcurl") { return 1; } #endif #if defined(HAVE_LIBSDL2) if(platform == "sdl2" || platform == "libsdl2") { return 1; } #endif #if defined(USE_MBED) if(platform == "mbed") { return 1; } #endif return 0; } int64_t runtime_bufferedstreamcreate(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); return state->CreateBufferedStream(num); } int64_t runtime_readbyte(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); return state->ReadByteFromStream(num); } int64_t runtime_d2i(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); return (int64_t) val; } int64_t runtime_i2d(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); double num= (double)expressions[0]->Evaluate(state); int64_t val = *((int64_t*)&num); return val; } int64_t runtime_dadd(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); num= expressions[1]->Evaluate(state); val += *((double*)&num); return *((int64_t*)&num); } int64_t runtime_dsub(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); num= expressions[1]->Evaluate(state); val -= *((double*)&num); return *((int64_t*)&num); } int64_t runtime_dmul(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); num= expressions[1]->Evaluate(state); val *= *((double*)&num); return *((int64_t*)&num); } int64_t runtime_ddiv(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); num= expressions[1]->Evaluate(state); val /= *((double*)&num); return *((int64_t*)&num); } int64_t runtime_dfloor(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); val = floorl(val); return *((int64_t*)&val); } int64_t runtime_dceil(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); val = ceill(val); return *((int64_t*)&val); } int64_t runtime_dfloorint(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); val = floorl(val); return (int64_t)val; } int64_t runtime_dceilint(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t num= expressions[0]->Evaluate(state); double val = *((double*)&num); val = ceill(val); return (int64_t)val; } double __zero=0; int64_t runtime_dmean(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() == 0) return *((int64_t*)&__zero); double mean=0; for(auto exp : expressions) { int64_t num= exp->Evaluate(state); mean += *((double*)&num); } mean /= (int64_t)expressions.size(); return *((int64_t*)&mean); } int64_t runtime_boxdestroy(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 1) throw exception(); int64_t boxId = expressions[0]->Evaluate(state); state->DestroyBox(boxId); return 0; } int64_t runtime_getbytesnumber(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t boxId = expressions[0]->Evaluate(state); int64_t number = expressions[1]->Evaluate(state); for(int64_t i = state->BoxLength(boxId);i<8;i++) { state->AddToBox(boxId,0); } state->SetBoxValue(boxId,0,number >> 56); state->SetBoxValue(boxId,1,number >> 48 & 0x00000000000000FF); state->SetBoxValue(boxId,2,number >> 40 & 0x00000000000000FF); state->SetBoxValue(boxId,3,number >> 32 & 0x00000000000000FF); state->SetBoxValue(boxId,4,number >> 24 & 0x00000000000000FF); state->SetBoxValue(boxId,5,number >> 16 & 0x00000000000000FF); state->SetBoxValue(boxId,6,number >> 8 & 0x00000000000000FF); state->SetBoxValue(boxId,7,number & 0x00000000000000FF); } int64_t runtime_setbytesnumber(BoxScript::IApplicationState* state,std::vector expressions) { if(expressions.size() < 2) throw exception(); int64_t boxId = expressions[0]->Evaluate(state); switch(state->BoxLength(boxId)) { case 0: return 0; case 1: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF); case 2: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF); case 3: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 16 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,2) & 0x00000000000000FF); case 4: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 24 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF) << 16 | (state->GetBoxValue(boxId,2) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,3) & 0x00000000000000FF); case 5: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 32 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF) << 24 | (state->GetBoxValue(boxId,2) & 0x00000000000000FF) << 16 | (state->GetBoxValue(boxId,3) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,4) & 0x00000000000000FF); case 6: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 40 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF) << 32 | (state->GetBoxValue(boxId,2) & 0x00000000000000FF) << 24 | (state->GetBoxValue(boxId,3) & 0x00000000000000FF) << 16 | (state->GetBoxValue(boxId,4) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,5) & 0x00000000000000FF); case 7: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 48 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF) << 40 | (state->GetBoxValue(boxId,2) & 0x00000000000000FF) << 32 | (state->GetBoxValue(boxId,3) & 0x00000000000000FF) << 24 | (state->GetBoxValue(boxId,4) & 0x00000000000000FF) << 16 | (state->GetBoxValue(boxId,5) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,6) & 0x00000000000000FF); default: return (state->GetBoxValue(boxId,0) & 0x00000000000000FF) << 56 | (state->GetBoxValue(boxId,1) & 0x00000000000000FF) << 48 | (state->GetBoxValue(boxId,2) & 0x00000000000000FF) << 40 | (state->GetBoxValue(boxId,3) & 0x00000000000000FF) << 32 | (state->GetBoxValue(boxId,4) & 0x00000000000000FF) << 24 | (state->GetBoxValue(boxId,5) & 0x00000000000000FF) << 16 | (state->GetBoxValue(boxId,6) & 0x00000000000000FF) << 8 | (state->GetBoxValue(boxId,7) & 0x00000000000000FF); } //(uint64_t)data[0] << 56 | (uint64_t)data[1] << 48 | (uint64_t)(data[2] & 0x00000000000000FF) << 40 | (ulong)data[3] << 32 | (ulong)data[4] << 24 | (ulong)data[5] << 16 | (ulong)data[6] << 8 | (ulong)data[7]; } void RegisterDoubleFunctions(BoxScript::IApplicationState* state) { state->AddRuntimeFunction("i2d",runtime_i2d); state->AddRuntimeFunction("d2i",runtime_d2i); state->AddRuntimeFunction("dadd",runtime_dadd); state->AddRuntimeFunction("dsub",runtime_dsub); state->AddRuntimeFunction("dmul",runtime_dmul); state->AddRuntimeFunction("ddiv",runtime_ddiv); state->AddRuntimeFunction("dfloor",runtime_dfloor); state->AddRuntimeFunction("dfloorint",runtime_dfloorint); state->AddRuntimeFunction("dceil",runtime_dceil); state->AddRuntimeFunction("dceilint",runtime_dceilint); state->AddRuntimeFunction("dmean",runtime_dmean); /*state->AddRuntimeFunction("printdbl",runtime_printdbl); state->AddRuntimeFunction("printdblln",runtime_printdblln); state->AddRuntimeFunction("box_adddblstr",runtime_boxadddblstr); state->AddRuntimeFunction("box_addintstr",runtime_addintstr); state->AddRuntimeFunction("box_toint",runtime_boxtoint); state->AddRuntimeFunction("box_todbl",runtime_boxtodbl);*/ } void RegisterMainRuntimeFunctions(BoxScript::IApplicationState* state) { state->AddRuntimeFunction("gt",runtime_gt); state->AddRuntimeFunction("gte",runtime_gte); state->AddRuntimeFunction("lt",runtime_lt); state->AddRuntimeFunction("lte",runtime_lte); state->AddRuntimeFunction("eq",runtime_eq); state->AddRuntimeFunction("not",runtime_not); state->AddRuntimeFunction("xor",runtime_xor); state->AddRuntimeFunction("bor",runtime_bor); state->AddRuntimeFunction("cor",runtime_cor); state->AddRuntimeFunction("band",runtime_band); state->AddRuntimeFunction("cand",runtime_cand); state->AddRuntimeFunction("lshift",runtime_lshift); state->AddRuntimeFunction("rshirt",runtime_rshift); state->AddRuntimeFunction("printchars",runtime_printchars); state->AddRuntimeFunction("printint",runtime_printint); state->AddRuntimeFunction("printintln",runtime_printintln); state->AddRuntimeFunction("tcp_client",runtime_tcpclient); state->AddRuntimeFunction("udp_client",runtime_tcpclient); state->AddRuntimeFunction("tcp_server",runtime_tcpserver); state->AddRuntimeFunction("udp_server",runtime_udpserver); state->AddRuntimeFunction("clear",runtime_clearscreen); state->AddRuntimeFunction("exit",runtime_exit); state->AddRuntimeFunction("setpos",runtime_setpos); state->AddRuntimeFunction("update_screen",runtime_updatescreen); //state->AddRuntimeFunction("httpclient_create",runtime_httpclient_create); //state->AddRuntimeFunction("httpclient_seturl",runtime_httpclient_seturl); state->AddRuntimeFunction("thread_create",runtime_threadcreate); state->AddRuntimeFunction("is_platform",runtime_isplatform); state->AddRuntimeFunction("for",runtime_for); state->AddRuntimeFunction("if",runtime_if); state->AddRuntimeFunction("e2n",runtime_e2n); state->AddRuntimeFunction("calln",runtime_calln); state->AddRuntimeFunction("readchar",runtime_readchar); state->AddRuntimeFunction("mean",runtime_mean); state->AddRuntimeFunction("box_create",runtime_boxcreate); state->AddRuntimeFunction("box_getvalue",runtime_boxgetvalue); state->AddRuntimeFunction("box_setvalue",runtime_boxsetvalue); state->AddRuntimeFunction("box_print",runtime_boxprint); state->AddRuntimeFunction("box_println",runtime_boxprintln); state->AddRuntimeFunction("box_len",runtime_boxlen); state->AddRuntimeFunction("box_add",runtime_boxadd); state->AddRuntimeFunction("box_remove",runtime_boxremove); state->AddRuntimeFunction("box_clear",runtime_boxclear); state->AddRuntimeFunction("box_insert",runtime_boxinsert); state->AddRuntimeFunction("box_removeat",runtime_boxremoveat); state->AddRuntimeFunction("box_readline",runtime_boxreadline); state->AddRuntimeFunction("box_destroy",runtime_boxdestroy); state->AddRuntimeFunction("file_create",runtime_filecreate); state->AddRuntimeFunction("file_open_read",runtime_fileopenread); state->AddRuntimeFunction("file_open_write",runtime_fileopenreadwrite); state->AddRuntimeFunction("stdin",runtime_stdin); state->AddRuntimeFunction("stdout",runtime_stdout); state->AddRuntimeFunction("stderr",runtime_stderr); state->AddRuntimeFunction("stream_create",runtime_streamcreate); state->AddRuntimeFunction("stream_copy",runtime_streamcopy); state->AddRuntimeFunction("stream_read",runtime_streamread); state->AddRuntimeFunction("stream_write",runtime_streamwrite); state->AddRuntimeFunction("stream_flush",runtime_streamflush); state->AddRuntimeFunction("stream_setpos",runtime_streamsetpos); state->AddRuntimeFunction("stream_getpos",runtime_streamgetpos); state->AddRuntimeFunction("stream_getlen",runtime_streamgetlen); state->AddRuntimeFunction("stream_canread",runtime_streamcanread); state->AddRuntimeFunction("stream_canwrite",runtime_streamcanwrite); state->AddRuntimeFunction("stream_canseek",runtime_streamcanseek); state->AddRuntimeFunction("stream_close",runtime_closestream); state->AddRuntimeFunction("stream_createbuffered",runtime_bufferedstreamcreate); state->AddRuntimeFunction("stream_readbyte",runtime_readbyte); state->AddRuntimeFunction("create_function",runtime_createfunction); state->AddRuntimeFunction("get_bytes_number",runtime_getbytesnumber); state->AddRuntimeFunction("set_bytes_number",runtime_setbytesnumber); /* state->AddRuntimeFunction("dictionary_box_create",runtime_createdictionarybox); state->AddRuntimeFunction("dictionary_val_create",runtime_createdictionaryval); state->AddRuntimeFunction("dictionary_add",runtime_addtodictionary); state->AddRuntimeFunction("dictionary_getvalue",runtime_getvaluedict); state->AddRuntimeFunction("dictionary_setvalue",runtime_setvaluedict); state->AddRuntimeFunction("dictionary_contains",runtime_dictcontains); state->AddRuntimeFunction("dictionary_clear",runtime_dictclear); state->AddRuntimeFunction("dictionary_ittr",runtime_dictittr); state->AddRuntimeFunction("dictionary_destroy",runtime_dictdestroy);*/ #if defined(HW_RVL) state->AddRuntimeFunction("wpad_scanpads",runtime_wpadscanpads); state->AddRuntimeFunction("wpad_buttonsdown",runtime_wpadbuttonsdown); state->AddRuntimeFunction("wpad_buttonsup",runtime_wpadbuttonsup); state->AddRuntimeFunction("wpad_button_a",runtime_wpadbuttona); state->AddRuntimeFunction("wpad_button_b",runtime_wpadbuttonb); state->AddRuntimeFunction("wpad_button_1",runtime_wpadbutton1); state->AddRuntimeFunction("wpad_button_2",runtime_wpadbutton2); state->AddRuntimeFunction("wpad_button_home",runtime_wpadbuttonhome); state->AddRuntimeFunction("wpad_button_plus",runtime_wpadbuttonplus); state->AddRuntimeFunction("wpad_button_minus",runtime_wpadbuttonminus); state->AddRuntimeFunction("wpad_button_up",runtime_wpadbuttonup); state->AddRuntimeFunction("wpad_button_down",runtime_wpadbuttondown); state->AddRuntimeFunction("wpad_button_left",runtime_wpadbuttonleft); state->AddRuntimeFunction("wpad_button_right",runtime_wpadbuttonright); #endif #if defined(HAVE_LIBSDL2) state->AddRuntimeFunction("sdl_init",runtime_sdl2init); state->AddRuntimeFunction("sdl_createwindow",runtime_sdl2createwindow); #endif #if defined(USE_MBED) #endif //state->AddRuntimeFunction("httpclient_followurl",runtime_httpclient_followurl); }