🎨 add namespace

This commit is contained in:
ValKmjolnir 2023-09-08 18:17:07 +08:00
parent cd4e0c1716
commit c157d8a9b1
27 changed files with 363 additions and 223 deletions

View File

@ -3,7 +3,8 @@
#include <iostream> #include <iostream>
#include "../src/nasal.h" #include "../src/nasal.h"
namespace nasal_fib_module { namespace nasal {
namespace fib_module {
double fibonaci(double x) { double fibonaci(double x) {
if (x<=2) { if (x<=2) {
@ -88,5 +89,7 @@ module_func_info func_tbl[] = {
} }
extern "C" module_func_info* get() { extern "C" module_func_info* get() {
return nasal_fib_module::func_tbl; return fib_module::func_tbl;
}
} }

View File

@ -12,6 +12,8 @@
#include <termios.h> #include <termios.h>
#endif #endif
namespace nasal {
class noecho_input { class noecho_input {
private: private:
#ifndef _WIN32 #ifndef _WIN32
@ -33,12 +35,14 @@ public:
tcsetattr(0, TCSANOW, &new_termios); tcsetattr(0, TCSANOW, &new_termios);
#endif #endif
} }
~noecho_input() { ~noecho_input() {
#ifndef _WIN32 #ifndef _WIN32
tcflush(0, TCIOFLUSH); tcflush(0, TCIOFLUSH);
tcsetattr(0, TCSANOW, &init_termios); tcsetattr(0, TCSANOW, &init_termios);
#endif #endif
} }
int noecho_kbhit() { int noecho_kbhit() {
#ifndef _WIN32 #ifndef _WIN32
unsigned char ch = 0; unsigned char ch = 0;
@ -59,6 +63,7 @@ public:
return kbhit(); return kbhit();
#endif #endif
} }
int noecho_getch() { int noecho_getch() {
#ifndef _WIN32 #ifndef _WIN32
int ch = 0; int ch = 0;
@ -102,3 +107,5 @@ module_func_info func_tbl[]={
extern "C" module_func_info* get() { extern "C" module_func_info* get() {
return func_tbl; return func_tbl;
} }
}

View File

@ -1,6 +1,8 @@
#include "../src/nasal.h" #include "../src/nasal.h"
#include <cmath> #include <cmath>
namespace nasal {
var nas_vec2(var* args, usize size, gc* ngc) { var nas_vec2(var* args, usize size, gc* ngc) {
var res = ngc->alloc(vm_vec); var res = ngc->alloc(vm_vec);
res.vec().elems.push_back(args[0]); res.vec().elems.push_back(args[0]);
@ -294,3 +296,5 @@ module_func_info func_tbl[]={
extern "C" module_func_info* get() { extern "C" module_func_info* get() {
return func_tbl; return func_tbl;
} }
}

View File

@ -28,6 +28,8 @@ static WSAmanager win;
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
namespace nasal {
var nas_socket(var* args, usize size, gc* ngc) { var nas_socket(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_num || args[1].type!=vm_num || args[2].type!=vm_num) if (args[0].type!=vm_num || args[1].type!=vm_num || args[2].type!=vm_num)
return nas_err("socket", "\"af\", \"type\", \"protocol\" should be number"); return nas_err("socket", "\"af\", \"type\", \"protocol\" should be number");
@ -89,7 +91,11 @@ var nas_connect(var* args, usize size, gc* ngc) {
addr.sin_port = htons(args[2].num()); addr.sin_port = htons(args[2].num());
hostent* entry = gethostbyname(args[1].str().c_str()); hostent* entry = gethostbyname(args[1].str().c_str());
memcpy(&addr.sin_addr, entry->h_addr, entry->h_length); memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
return var::num((double)connect(args[0].num(),(sockaddr*)&addr,sizeof(sockaddr_in))); return var::num((double)connect(
args[0].num(),
(sockaddr*)&addr,
sizeof(sockaddr_in)
));
} }
var nas_accept(var* args, usize size, gc* ngc) { var nas_accept(var* args, usize size, gc* ngc) {
@ -117,7 +123,12 @@ var nas_send(var* args, usize size, gc* ngc) {
return nas_err("send", "\"buff\" must be a string"); return nas_err("send", "\"buff\" must be a string");
if (args[2].type!=vm_num) if (args[2].type!=vm_num)
return nas_err("send", "\"flags\" muse be a number"); return nas_err("send", "\"flags\" muse be a number");
return var::num((double)send(args[0].num(),args[1].str().c_str(),args[1].str().length(),args[2].num())); return var::num((double)send(
args[0].num(),
args[1].str().c_str(),
args[1].str().length(),
args[2].num()
));
} }
var nas_sendto(var* args, usize size, gc* ngc) { var nas_sendto(var* args, usize size, gc* ngc) {
@ -137,7 +148,14 @@ var nas_sendto(var* args, usize size, gc* ngc) {
addr.sin_port = htons(args[2].num()); addr.sin_port = htons(args[2].num());
hostent* entry = gethostbyname(args[1].str().c_str()); hostent* entry = gethostbyname(args[1].str().c_str());
memcpy(&addr.sin_addr, entry->h_addr, entry->h_length); memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
return var::num((double)sendto(args[0].num(),args[3].str().c_str(),args[3].str().length(),args[4].num(),(sockaddr*)&addr,sizeof(sockaddr_in))); return var::num((double)sendto(
args[0].num(),
args[3].str().c_str(),
args[3].str().length(),
args[4].num(),
(sockaddr*)&addr,
sizeof(sockaddr_in)
));
} }
var nas_recv(var* args, usize size, gc* ngc) { var nas_recv(var* args, usize size, gc* ngc) {
@ -176,9 +194,23 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
auto& hash = res.hash().elems; auto& hash = res.hash().elems;
char* buf = new char[(int)args[1].num()+1]; char* buf = new char[(int)args[1].num()+1];
#ifdef _WIN32 #ifdef _WIN32
auto recvsize=recvfrom(args[0].num(),buf,args[1].num(),args[2].num(),(sockaddr*)&addr,&socklen); auto recvsize = recvfrom(
args[0].num(),
buf,
args[1].num(),
args[2].num(),
(sockaddr*)&addr,
&socklen
);
#else #else
auto recvsize=recvfrom(args[0].num(),buf,args[1].num(),args[2].num(),(sockaddr*)&addr,(socklen_t*)&socklen); auto recvsize = recvfrom(
args[0].num(),
buf,
args[1].num(),
args[2].num(),
(sockaddr*)&addr,
(socklen_t*)&socklen
);
#endif #endif
hash["size"] = var::num((double)recvsize); hash["size"] = var::num((double)recvsize);
buf[recvsize>=0? recvsize:0] = 0; buf[recvsize>=0? recvsize:0] = 0;
@ -212,3 +244,5 @@ module_func_info func_tbl[]={
extern "C" module_func_info* get() { extern "C" module_func_info* get() {
return func_tbl; return func_tbl;
} }
}

View File

@ -1,5 +1,7 @@
#include "bits_lib.h" #include "bits_lib.h"
namespace nasal {
var builtin_u32xor(var* local, gc& ngc) { var builtin_u32xor(var* local, gc& ngc) {
return var::num((f64)(u32(local[1].num())^u32(local[2].num()))); return var::num((f64)(u32(local[1].num())^u32(local[2].num())));
} }
@ -135,3 +137,5 @@ nasal_builtin_table bits_native[] = {
{"__buf", builtin_buf}, {"__buf", builtin_buf},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -4,6 +4,8 @@
#include "nasal_gc.h" #include "nasal_gc.h"
#include "nasal_builtin.h" #include "nasal_builtin.h"
namespace nasal {
var builtin_u32xor(var*, gc&); var builtin_u32xor(var*, gc&);
var builtin_u32and(var*, gc&); var builtin_u32and(var*, gc&);
var builtin_u32or(var*, gc&); var builtin_u32or(var*, gc&);
@ -15,3 +17,5 @@ var builtin_setfld(var*, gc&);
var builtin_buf(var*, gc&); var builtin_buf(var*, gc&);
extern nasal_builtin_table bits_native[]; extern nasal_builtin_table bits_native[];
}

View File

@ -1,5 +1,7 @@
#include "coroutine.h" #include "coroutine.h"
namespace nasal {
var builtin_cocreate(var* local, gc& ngc) { var builtin_cocreate(var* local, gc& ngc) {
// +-------------+ // +-------------+
// | old pc | <- top[0] // | old pc | <- top[0]
@ -114,3 +116,5 @@ nasal_builtin_table coroutine_native[] = {
{"__corun", builtin_corun}, {"__corun", builtin_corun},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -4,6 +4,8 @@
#include "nasal_gc.h" #include "nasal_gc.h"
#include "nasal_builtin.h" #include "nasal_builtin.h"
namespace nasal {
var builtin_cocreate(var*, gc&); var builtin_cocreate(var*, gc&);
var builtin_coresume(var*, gc&); var builtin_coresume(var*, gc&);
var builtin_coyield(var*, gc&); var builtin_coyield(var*, gc&);
@ -11,3 +13,5 @@ var builtin_costatus(var*, gc&);
var builtin_corun(var*, gc&); var builtin_corun(var*, gc&);
extern nasal_builtin_table coroutine_native[]; extern nasal_builtin_table coroutine_native[];
}

View File

@ -1,5 +1,7 @@
#include "dylib_lib.h" #include "dylib_lib.h"
namespace nasal {
const auto dylib_type_name = "dylib"; const auto dylib_type_name = "dylib";
const auto func_addr_type_name = "faddr"; const auto func_addr_type_name = "faddr";
@ -103,3 +105,5 @@ nasal_builtin_table dylib_lib_native[] = {
{"__dlcall", builtin_dlcall}, {"__dlcall", builtin_dlcall},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -11,6 +11,8 @@
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
namespace nasal {
void dylib_destructor(void*); void dylib_destructor(void*);
void func_addr_destructor(void*); void func_addr_destructor(void*);
@ -20,3 +22,5 @@ var builtin_dlcallv(var*, gc&);
var builtin_dlcall(var*, gc&); var builtin_dlcall(var*, gc&);
extern nasal_builtin_table dylib_lib_native[]; extern nasal_builtin_table dylib_lib_native[];
}

View File

@ -2,6 +2,8 @@
#include <fstream> #include <fstream>
namespace nasal {
var builtin_logprint(var* local, gc& ngc) { var builtin_logprint(var* local, gc& ngc) {
var level = local[1]; var level = local[1];
var elems = local[2]; var elems = local[2];
@ -34,3 +36,5 @@ nasal_builtin_table flight_gear_native[] = {
{"_logprint", builtin_logprint}, {"_logprint", builtin_logprint},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -4,6 +4,8 @@
#include "nasal_gc.h" #include "nasal_gc.h"
#include "nasal_builtin.h" #include "nasal_builtin.h"
namespace nasal {
#define SG_LOG_BULK 1 #define SG_LOG_BULK 1
#define SG_LOG_DEBUG 2 #define SG_LOG_DEBUG 2
#define SG_LOG_INFO 3 #define SG_LOG_INFO 3
@ -16,3 +18,5 @@
var builtin_logprint(var*, gc&); var builtin_logprint(var*, gc&);
extern nasal_builtin_table flight_gear_native[]; extern nasal_builtin_table flight_gear_native[];
}

View File

@ -1,5 +1,7 @@
#include "io_lib.h" #include "io_lib.h"
namespace nasal {
const auto file_type_name = "file"; const auto file_type_name = "file";
void filehandle_destructor(void* ptr) { void filehandle_destructor(void* ptr) {
@ -198,3 +200,5 @@ nasal_builtin_table io_lib_native[] = {
{"__eof", builtin_eof}, {"__eof", builtin_eof},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -10,6 +10,8 @@
#define F_OK 0 // fuck msc #define F_OK 0 // fuck msc
#endif #endif
namespace nasal {
void filehandle_destructor(void*); void filehandle_destructor(void*);
var builtin_readfile(var*, gc&); var builtin_readfile(var*, gc&);
@ -26,3 +28,5 @@ var builtin_stat(var*, gc&);
var builtin_eof(var*, gc&); var builtin_eof(var*, gc&);
extern nasal_builtin_table io_lib_native[]; extern nasal_builtin_table io_lib_native[];
}

View File

@ -1,5 +1,7 @@
#include "math_lib.h" #include "math_lib.h"
namespace nasal {
var builtin_pow(var* local, gc& ngc) { var builtin_pow(var* local, gc& ngc) {
var x = local[1]; var x = local[1];
var y = local[2]; var y = local[2];
@ -71,3 +73,5 @@ nasal_builtin_table math_lib_native[] = {
{"__isnan", builtin_isnan}, {"__isnan", builtin_isnan},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -4,6 +4,8 @@
#include "nasal_gc.h" #include "nasal_gc.h"
#include "nasal_builtin.h" #include "nasal_builtin.h"
namespace nasal {
var builtin_pow(var*, gc&); var builtin_pow(var*, gc&);
var builtin_sin(var*, gc&); var builtin_sin(var*, gc&);
var builtin_cos(var*, gc&); var builtin_cos(var*, gc&);
@ -16,3 +18,5 @@ var builtin_atan2(var*, gc&);
var builtin_isnan(var*, gc&); var builtin_isnan(var*, gc&);
extern nasal_builtin_table math_lib_native[]; extern nasal_builtin_table math_lib_native[];
}

View File

@ -11,6 +11,17 @@
#include <cmath> #include <cmath>
#include <vector> #include <vector>
using i32 = std::int32_t;
using i64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
using f64 = double;
namespace nasal {
bool is_windows(); bool is_windows();
bool is_linux(); bool is_linux();
bool is_macos(); bool is_macos();
@ -23,14 +34,7 @@ bool is_ia64();
bool is_powerpc(); bool is_powerpc();
bool is_superh(); bool is_superh();
using i32 = std::int32_t;
using i64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
using f64 = double;
const u32 STACK_DEPTH = 4096; const u32 STACK_DEPTH = 4096;
@ -50,4 +54,6 @@ i32 utf8_hdchk(const char);
std::string chrhex(const char); std::string chrhex(const char);
std::string rawstr(const std::string&, const usize maxlen=0); std::string rawstr(const std::string&, const usize maxlen=0);
}
#include "nasal_gc.h" #include "nasal_gc.h"

View File

@ -1,6 +1,8 @@
#include "nasal_builtin.h" #include "nasal_builtin.h"
#include <chrono> #include <chrono>
namespace nasal {
var builtin_print(var* local, gc& ngc) { var builtin_print(var* local, gc& ngc) {
for(auto& i : local[1].vec().elems) { for(auto& i : local[1].vec().elems) {
std::cout << i; std::cout << i;
@ -655,3 +657,5 @@ nasal_builtin_table builtin[] = {
{"__ghosttype", builtin_ghosttype}, {"__ghosttype", builtin_ghosttype},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -22,6 +22,8 @@
#define environ (*_NSGetEnviron()) #define environ (*_NSGetEnviron())
#endif #endif
namespace nasal {
var builtin_print(var*, gc&); var builtin_print(var*, gc&);
var builtin_println(var*, gc&); var builtin_println(var*, gc&);
var builtin_exit(var*, gc&); var builtin_exit(var*, gc&);
@ -75,3 +77,5 @@ struct nasal_builtin_table {
}; };
extern nasal_builtin_table builtin[]; extern nasal_builtin_table builtin[];
}

View File

@ -75,10 +75,12 @@ std::ostream& reset(std::ostream& s) {
void flstream::load(const std::string& f) { void flstream::load(const std::string& f) {
if (file==f) { // don't need to load a loaded file if (file==f) { // don't need to load a loaded file
return; return;
} else {
file = f;
} }
// update file name
file = f;
// REPL: load from memory
if (repl::info::instance()->in_repl_mode && if (repl::info::instance()->in_repl_mode &&
repl::info::instance()->repl_file_name==file) { repl::info::instance()->repl_file_name==file) {
const auto& source = repl::info::instance()->repl_file_source; const auto& source = repl::info::instance()->repl_file_source;

View File

@ -1,5 +1,7 @@
#include "nasal_gc.h" #include "nasal_gc.h"
namespace nasal {
var nas_vec::get_val(const i32 n) { var nas_vec::get_val(const i32 n) {
i32 size = elems.size(); i32 size = elems.size();
if (n<-size || n>=size) { if (n<-size || n>=size) {
@ -717,3 +719,5 @@ var nas_err(const std::string& error_function_name, const std::string& info) {
std::cerr << "[vm] " << error_function_name << ": " << info << "\n"; std::cerr << "[vm] " << error_function_name << ": " << info << "\n";
return var::none(); return var::none();
} }
}

View File

@ -32,6 +32,8 @@
#include "nasal.h" #include "nasal.h"
namespace nasal {
enum vm_type:u8 { enum vm_type:u8 {
/* none-gc object */ /* none-gc object */
vm_none = 0, vm_none = 0,
@ -388,3 +390,5 @@ struct module_func_info {
// module function "get" type // module function "get" type
typedef module_func_info* (*get_func_ptr)(); typedef module_func_info* (*get_func_ptr)();
}

View File

@ -1,5 +1,7 @@
#include "nasal.h" #include "nasal.h"
namespace nasal {
bool is_windows() { bool is_windows() {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
return true; return true;
@ -228,3 +230,5 @@ std::string rawstr(const std::string& str, const usize maxlen) {
} }
return ret; return ret;
} }
}

View File

@ -1,5 +1,7 @@
#include "nasal_opcode.h" #include "nasal_opcode.h"
namespace nasal {
const char* opname[] = { const char* opname[] = {
"exit ", "intg ", "intl ", "loadg ", "exit ", "intg ", "intl ", "loadg ",
"loadl ", "loadu ", "pnum ", "pnil ", "loadl ", "loadu ", "pnum ", "pnil ",
@ -116,3 +118,5 @@ std::ostream& operator<<(std::ostream& out, const codestream& ins) {
ins.dump(out); ins.dump(out);
return out; return out;
} }
}

View File

@ -5,6 +5,8 @@
#include <iostream> #include <iostream>
namespace nasal {
enum op_code_type:u8 { enum op_code_type:u8 {
op_exit, // stop the virtual machine op_exit, // stop the virtual machine
op_intg, // init global scope op_intg, // init global scope
@ -127,3 +129,5 @@ public:
std::ostream& operator<<(std::ostream&, const codestream&); std::ostream& operator<<(std::ostream&, const codestream&);
extern const char* opname[]; extern const char* opname[];
}

View File

@ -1,5 +1,7 @@
#include "unix_lib.h" #include "unix_lib.h"
namespace nasal {
const auto dir_type_name = "dir"; const auto dir_type_name = "dir";
void dir_entry_destructor(void* ptr) { void dir_entry_destructor(void* ptr) {
@ -149,3 +151,5 @@ nasal_builtin_table unix_lib_native[] = {
{"__getenv", builtin_getenv}, {"__getenv", builtin_getenv},
{nullptr, nullptr} {nullptr, nullptr}
}; };
}

View File

@ -20,6 +20,8 @@
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
namespace nasal {
void dir_entry_destructor(void*); void dir_entry_destructor(void*);
var builtin_pipe(var*, gc&); var builtin_pipe(var*, gc&);
@ -34,3 +36,5 @@ var builtin_getcwd(var*, gc&);
var builtin_getenv(var*, gc&); var builtin_getenv(var*, gc&);
extern nasal_builtin_table unix_lib_native[]; extern nasal_builtin_table unix_lib_native[];
}