54 va_start(args, format);
60 if (log->
m_func !=
nullptr) {
70 printf(
"CALL: RunMain(const int arg, const char** argv, const char** penv)");
1123 bool is_error =
false;
1145 return result && !is_error;
1204 LOG_RUNTIME(
"Fail compile module '%s' form file '%s'.", output, mod);
1223 return std::make_shared<RunTime>(args);
1312 if (!analysis.
Analyze(ast, &stack)) {
1319 if (std::filesystem::exists(filename)) {
1320 filename = std::filesystem::absolute(filename);
1329 if (c ==
'A')
return 'C';
1330 if (c ==
'C')
return 'G';
1331 if (c ==
'G')
return 'T';
1332 if (c ==
'T')
return 'A';
1417 ASSERT(!term->m_normalized.empty());
1435 return RegisterObject(
true, term->m_normalized, term,
CreateNative(term,
nullptr,
false, ast->m_right->m_text.substr(1).c_str()));
1440 std::string type_name(
toString(type));
1443 func_name.insert(0,
"::");
1454 ASSERT(!type_name.empty() &&
result->m_class_name.compare(type_name) == 0);
1462 for (
auto &parent : parents) {
1464 if (iter == end()) {
1468 for (
auto &elem :
result->m_class_parents) {
1471 if (!elem->m_class_name.empty() && elem->m_class_name.compare(
NormalizeName(parent)) == 0) {
1472 LOG_DEBUG(
"The type '%s' already exists in the parents of '%s'!", parent.c_str(), type_name.c_str());
1477 result->m_class_parents.push_back(iter->second.var.GetValueAsObject());
1481 type_term->m_normalized = type_name;
1488 if (type_name.compare(
"None:::") == 0) {
1500 LOG_RUNTIME(
"Type name '%s' not found!", type.begin());
1504 if (result_types != end()) {
1505 return result_types->second.var.GetValueAsObject();
1522 LOG_RUNTIME(
"Type name '%s' not found!", type.begin());
1527 bool local_has_error =
false;
1531 if (!local_has_error) {
1536 *has_error = local_has_error;
1539 if (local_has_error && !rt) {
1540 LOG_RUNTIME(
"Type name '%s' is not calculated statically!", type_name.begin());
1545 if (obj_type ==
nullptr) {
1550 LOG_RUNTIME(
"Type name '%s' not found!", type_name.begin());
1552 return obj_type->m_var_type_fixed;
1597 std::vector<std::wstring>
result;
1599 bool find_local =
false;
1600 bool find_global =
false;
1601 bool find_types =
false;
1602 bool find_macro =
false;
1608 start = start.substr(1);
1612 start = start.substr(1);
1627 for (
auto &elem : *
this) {
1630 if (
result.size() > overage_count + 1) {
1679 for (
auto &elem : *
this) {
1682 if (
result.size() > overage_count + 1) {
1743 result->m_sequence = block;
1760 term = p.
Parse(proto);
1761 }
catch (std::exception &) {
1763 std::string func(proto);
1766 term = p2.
Parse(func)->m_left;
1767 }
catch (std::exception &e) {
1769 LOG_RUNTIME(
"Fail parsing prototype '%s'!", e.what());
1778 void *addr =
GetNativeAddress(
nullptr, mangle_name ? mangle_name : proto->m_text.c_str());
1780 NL_CHECK(addr,
"Error getting address '%s' from '%s'!", proto->toString().c_str(), module);
1788 NL_CHECK(proto,
"Fail prototype native function!");
1794 if (proto->isCall()) {
1798 for (
int i = 0; i < proto->size(); i++) {
1800 TermPtr proto_arg = (*proto)[i].second;
1810 if (i + 1 != proto->size()) {
1811 NL_PARSER(proto_arg,
"Ellipsys must end of argument!");
1814 if (!proto_arg->m_type) {
1815 NL_PARSER(proto_arg,
"Argument type must be specified!");
1820 NL_PARSER(proto_arg->GetType(),
"Argument must be machine type! Creating a variable with type '%s' is not supported!", proto_arg->m_type->m_text.c_str());
1827 if (!proto->m_type) {
1833 NL_PARSER(proto,
"Creating a variable with type '%s' is not supported!", proto->m_type->m_text.c_str());
1850 void * ptr =
nullptr;
1854 ASSERT(std::holds_alternative<std::monostate>(
result->m_var));
1860 result->m_var =
static_cast<bool *
> (ptr);
1861 result->m_var_is_init =
true;
1866 result->m_var =
static_cast<int8_t *
> (ptr);
1867 result->m_var_is_init =
true;
1871 result->m_var =
static_cast<int16_t *
> (ptr);
1872 result->m_var_is_init =
true;
1876 result->m_var =
static_cast<int32_t *
> (ptr);
1877 result->m_var_is_init =
true;
1881 result->m_var =
static_cast<int64_t *
> (ptr);
1882 result->m_var_is_init =
true;
1886 result->m_var =
static_cast<float *
> (ptr);
1887 result->m_var_is_init =
true;
1891 result->m_var =
static_cast<double *
> (ptr);
1892 result->m_var_is_init =
true;
1903 NL_CHECK(std::get<void *>(
result->m_var),
"Error getting address '%s'!", proto->toString().c_str());
1904 }
else if (ptr &&
result->is_tensor_type()) {
1907 result->m_var_is_init =
true;
1909 NL_PARSER(proto,
"Fail CreateNative object");
1912 result->m_var_is_init =
true;
1918 std::string filename(str);
1919 std::string::size_type idx = filename.rfind(
'.');
1920 if (idx != std::string::npos) {
1922 return filename.substr(idx);
1924 return std::string(
"");
1928 std::string filename(str);
1930 if (file_ext.empty() && !filename.empty() && filename.compare(
".") != 0) {
1932 filename.append(ext_default);
1938 std::string filename(str);
1940 if (file_ext.compare(ext_old) == 0) {
1941 filename = filename.substr(0, filename.length() - file_ext.length());
1944 if (file_ext.compare(
".") != 0 && file_ext.compare(ext_new) != 0 && !filename.empty() &&
1945 filename.compare(
".") != 0) {
1947 filename.append(ext_new);
1953 std::ifstream f(fileName);
1954 std::stringstream ss;
1973 if (elem.compare(name) == 0) {
1974 NL_PARSER(term,
"Load module '%s' recursion!", term->m_text.c_str());
2015static void zip_save(zip_t *zipper,
const std::string_view file,
const std::string &data) {
2017 zip_source_t *zip_data =
nullptr;
2018 if ((zip_data = zip_source_buffer_create(data.data(), data.size(), ZIP_CREATE | ZIP_TRUNCATE, &error)) == NULL) {
2019 LOG_RUNTIME(
"can't create source: %s", zip_error_strerror(&error));
2022 if (zip_file_add(zipper, file.begin(), zip_data, ZIP_FL_ENC_UTF_8) < 0) {
2024 zip_source_free(zip_data);
2025 LOG_RUNTIME(
"Failed to add file '%s' to zip: %s", file.begin(), zip_strerror(zipper));
2029 zip_source_keep(zip_data);
2034 zip_t *zipper = zip_open(filepath.begin(), ZIP_CREATE | ZIP_EXCL, &errorp);
2035 if (zipper ==
nullptr) {
2036 zip_error_t ziperror;
2037 zip_error_init_with_code(&ziperror, errorp);
2038 LOG_RUNTIME(
"Failed to open output file '%s': %s", filepath.begin(), zip_error_strerror(&ziperror));
2041 std::string module_name(name);
2042 if (module_name.empty()) {
2046 std::string temp_name;
2047 temp_name = module_name;
2048 temp_name +=
".inc";
2052 if (!data.
source.empty()) {
2053 temp_name = module_name;
2054 temp_name +=
".src";
2060 temp_name = module_name;
2070static bool zip_read(zip* z,
const std::string_view name, std::string &data) {
2074 zip_stat(z, name.begin(), 0, &st);
2080 data.resize(st.size);
2083 zip_file* f = zip_fopen(z, name.begin(), 0);
2084 zip_fread(f, data.data(), st.size);
2097 zip* z = zip_open(filepath.begin(), 0, &errorp);
2099 zip_error_t ziperror;
2100 zip_error_init_with_code(&ziperror, errorp);
2101 LOG_RUNTIME(
"Failed to open file '%s': %s", filepath.begin(), zip_error_strerror(&ziperror));
2104 data.
name.assign(modulename);
2105 if (data.
name.empty()) {
2109 std::string temp_name = data.
name;
2111 temp_name = data.
name;
2112 temp_name +=
".inc";
2115 LOG_RUNTIME(
"Name '%s' in file module '%s' not exist!", temp_name.c_str(), filepath.begin());
2118 temp_name = data.
name;
2119 temp_name +=
".src";
2122 LOG_INFO(
"Name '%s' in file module '%s' not exist!", temp_name.c_str(), filepath.begin());
2125 temp_name = data.
name;
2129 LOG_INFO(
"Name '%s' in file module '%s' not exist!", temp_name.c_str(), filepath.begin());
2133 LOG_RUNTIME(
"In file module '%s' not source and bytecode not exist!", filepath.begin());
2188 for (
auto &c : str) {
2191 }
else if (c ==
'\t') {
2193 }
else if (c ==
'\r') {
2234 return dlsym(handle, name.begin());
2240 return std::string(name.begin() + 1);
2253 return "Help system not implemented!!!!!";
2258 std::string
result(name.substr(1));
2260 result += name.substr(name.rfind(
":"));
2271 return std::string(strerror(errno));
2273 wchar_t buffer[256];
2274 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
2275 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer,
sizeof (buffer), NULL);
2295m_diag(std::make_shared<
Diag>()) {
2311 m_work_dir = std::filesystem::current_path();
2312 m_temp_dir = std::filesystem::temp_directory_path();
2326 std::wstring sys_file;
2327 std::string sys_init;
2331 sys_file = L
"cygwin1.dll";
2332 sys_init =
"cygwin_dll_init";
2425#if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) || defined(__amd64)
2428#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
2432#error Target architecture not defined!
bool Analyze(TermPtr term, NameLookupStack *stack_ptr=nullptr)
bool RegisterObject(bool only_new, const std::string_view name, TermPtr term, Variable var)
static Logger * Instance()
static ObjPtr CreateBaseType(ObjType type)
static ObjPtr CreateNone(Sync *sync=nullptr)
static ObjPtr CreateType(ObjType type, ObjType fixed=ObjType::None, bool is_init=false, Sync *sync=nullptr)
TermPtr Parse(const std::string str, bool expand_module=false)
static ffi_type * m_ffi_type_uint64
static StringArray MakeMainArgs(int argc, const char **argv, const char **penv)
static ffi_type * m_ffi_type_float
static ffi_prep_cif_type * m_ffi_prep_cif
ObjPtr GetTypeFromString(const std::string_view type, bool *has_error=nullptr)
static ffi_prep_cif_var_type * m_ffi_prep_cif_var
bool RegisterBuildinType(ObjType type, std::vector< std::string > parents)
static std::string Escape(const std::string_view str)
static ObjType BaseTypeFromString(RunTime *rt, const std::string_view text, bool *has_error=nullptr)
static ffi_type * m_ffi_type_uint32
static ffi_type * m_wide_char_type_ffi
static std::string GetLastErrorMessage()
static ffi_call_type * m_ffi_call
void GlobalNameBuildinRegister()
static ffi_type * m_ffi_type_sint64
static ffi_type * m_ffi_type_pointer
static ffi_type * m_ffi_type_sint8
ObjPtr ExecModule(const char *module, const char *output, bool cached, Context *ctx)
static ffi_type * m_ffi_type_uint16
static ObjType m_integer_type
bool ParseArgs(StringArray args)
void ffi_call_type(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
static ffi_type * m_ffi_type_void
static void * GetNativeAddress(void *handle, const std::string_view name)
ObjPtr CreateFunction(TermPtr proto, TermPtr block)
static bool pred_compare(const std::string &find, const std::string &str)
static ObjType m_wide_char_type
bool RegisterSystemFunc(const char *source)
static ffi_type * m_ffi_type_uint8
TermPtr ParseBuildin(const std::string_view src)
bool LoadModuleFromFile(const char *name_str, bool init)
static void * m_ffi_handle
ffi_status ffi_prep_cif_var_type(ffi_cif *cif, ffi_abi abi, unsigned int nfixedargs, unsigned int ntotalargs, ffi_type *rtype, ffi_type **atypes)
static bool ModuleRead(FileModule &data, const std::string_view filename, const std::string_view modulename="")
ObjPtr OpLoadModule(TermPtr term)
static ffi_type * m_ffi_type_double
StringArray m_module_loader
static std::string NativeNameMangling(const Term *term, RunTime *rt)
static RuntimePtr Init(StringArray args)
static ObjPtr CreateNative(const char *proto, const char *module=nullptr, bool lazzy=false, const char *mangle_name=nullptr)
static std::string ffi_file
RunTime(const StringArray &args={})
static bool ExpandFileName(std::string &filename)
void InitInternal(const StringArray args)
static bool ModuleSave(const FileModule &data, const std::string_view filename, const std::string_view module_name="")
ffi_status ffi_prep_cif_type(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes)
static ffi_type * m_ffi_type_sint32
static ffi_type * m_ffi_type_sint16
bool RegisterBuildinFunc(std::string proto, void *func)
bool UnLoadModule(const char *name_str, bool deinit)
std::vector< std::wstring > SelectPredict(std::wstring wstart, size_t overage_count=0)
std::map< std::string, StorageTerm > ModuleMapType
static TermPtr Create(TermID id, const char *text, parser::token_type lex_type=parser::token_type::END, size_t len=std::string::npos, location *loc=nullptr, std::shared_ptr< std::string > source=nullptr)
static TermPtr CreateName(std::string name, TermID id=TermID::NAME)
static newlang::ObjPtr __len__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __assert_abort__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __thread_get_id__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __timeit__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __thread_hardware_concurrency__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __thread_join__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __thread_this_id__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __thread_joinable__(newlang::Context *ctx, newlang::Obj &in)
static newlang::ObjPtr __thread_start__(newlang::Context *ctx, newlang::Obj &in)
__attribute__((weak)) std
#define LOG_RUNTIME(format,...)
#define STATIC_ASSERT(expr)
#define ASSERT(condition)
bool isTypeName(ObjType t)
std::wstring utf8_decode(const std::string str)
std::string GetDoc(std::string name)
bool isLocalName(const std::string_view name)
bool isNativeType(ObjType t)
bool isModuleName(const std::string_view name)
std::shared_ptr< Term > TermPtr
std::shared_ptr< Obj > ObjPtr
T BaseFileName(T const &path, T const &delims="/\\")
std::shared_ptr< RunTime > RuntimePtr
T RemoveFileExtension(T const &filename)
std::vector< std::string > StringArray
std::string ExtractModuleName(const std::string_view name)
ObjType GetBaseTypeFromString(const std::string_view type_arg, bool *has_error=nullptr)
std::string ReadFile(const char *fileName)
bool isNativeName(const std::string_view name)
ObjType typeFromString(TermPtr &term, RunTime *rt, bool *has_error)
std::string ReplaceFileExt(const char *str, const char *ext_old, const char *ext_new)
int RunMain(const int arg, const char **argv, const char **penv)
std::string NormalizeName(const std::string_view name)
bool isMacroName(const std::string_view name)
const char * toString(TermID type)
std::string GetFileExt(const char *str)
std::string utf8_encode(const std::wstring wstr)
std::string MakeConstructorName(std::string name)
std::string AddDefaultFileExt(const char *str, const char *ext_default)
static void zip_save(zip_t *zipper, const std::string_view file, const std::string &data)
static bool zip_read(zip *z, const std::string_view name, std::string &data)
static char nlc_prinft_sub_buffer[4096 *8]
int nlc_prinft_sub_(char const *format,...)
#define NL_PARSER(term, format,...)
#define NL_CHECK(cond, format,...)