1#ifndef INCLUDED_TYPES_H_
2#define INCLUDED_TYPES_H_
33#include <source_location>
44#include <linux/limits.h>
56#include <sys/utsname.h>
62#ifndef DOXYGEN_SHOULD_SKIP_THIS
78 using IntArrayRef = c10::ArrayRef<int64_t>;
94#include "build_options.data"
99 static constexpr const char*
ws =
" \t\n\r\f\v";
101 inline std::string &
rtrim(std::string& s,
const char* t =
ws) {
102 s.erase(s.find_last_not_of(t) + 1);
106 inline std::string &
ltrim(std::string& s,
const char* t =
ws) {
107 s.erase(0, s.find_first_not_of(t));
111 inline std::string &
trim(std::string& s,
const char* t =
ws) {
115 template <
typename T>
121 }
else if (n == 1 || str.empty()) {
124 const auto period = str.size();
126 str.append(n - 1, str.front());
129 str.reserve(period * n);
131 for (; m < n; m *= 2) str += str;
132 str.append(str.c_str(), (n - (m / 2)) * period);
138 return path.substr(path.find_last_of(delims) + 1);
143 typename T::size_type
const p(filename.find_last_of(
'.'));
144 return p > 0 && p != T::npos ? filename.substr(0, p) : filename;
195 typename std::enable_if<!std::is_same<Variable, D>::value,
VariablePair>::type
214 typedef std::pair<std::string, VariablePair>
PairType;
230 return std::map<std::string, VariablePair>::size();
236 std::string
Dump(
const std::string_view filter=
"",
const char delim=
';');
296std::string
ParserMessage(std::string &buffer,
int row,
int col,
const char *format, ...)
302void ParserException(
const char *msg, std::string &buffer,
int row,
int col);
305#define LOG_LEVEL_ERROR_MESSAGE LOG_LEVEL_DEBUG
307#define LOG_LEVEL_ERROR_MESSAGE LOG_LEVEL_INFO
310#define NL_PARSER(term, format, ...) \
313 std::string message = \
314 newlang::ParserMessage(term->m_source ? *term->m_source : empty, term->m_line, term->m_col, format, ##__VA_ARGS__); \
315 LOG_THROW(ParserError, LOG_LEVEL_ERROR_MESSAGE, "", "%s", message.c_str()); \
318#define NL_MESSAGE(level, term, format, ...) \
321 std::string message = \
322 newlang::ParserMessage((term && term->m_source) ? *term->m_source : empty, term?term->m_line:1, term?term->m_col:0, format, ##__VA_ARGS__); \
323 LOG_MAKE(level, "", "%s", message.c_str()); \
326#define NL_CHECK(cond, format, ...) \
329 LOG_THROW(ParserError, LOG_LEVEL_ERROR_MESSAGE, "", format, ##__VA_ARGS__); \
333#define NL_TYPECHECK(term, from, to) \
335 if (!canCast(from, to)) { \
336 std::string message = "Incompatible data type '"; \
337 message += newlang::toString(from); \
338 message += "' and '"; \
339 message += newlang::toString(to); \
340 message += "' (" __FILE__ ":" TO_STR(__LINE__) ")"; \
342 ParserError, LOG_LEVEL_ERROR_MESSAGE, "", "%s", \
343 newlang::ParserMessage(*term->m_source, term->m_line, term->m_col, "%s", message.c_str()).c_str()); \
422 _(EVAL_FUNCTION, 110) \
427 _(BLOCK_MINUS, 114) \
456 _(ErrorParser, 241) \
457 _(ErrorRunTime, 242) \
525#define DEFINE_ENUM(name, value) name = static_cast<uint8_t>(value),
531#define MAKE_TYPE_NAME(type_name) type_name
534#define DEFINE_CASE(name, _) \
535 case ObjType::name: \
536 return MAKE_TYPE_NAME(":" #name);
542 LOG_RUNTIME(
"UNKNOWN type code %d",
static_cast<int> (type));
558 return int_bool ?
"int" :
"bool";
563 return "signed char";
565 return "unsigned char";
567 return "signed short";
569 return "unsigned short";
573 return "unsigned int";
575 return "signed long long int";
577 return "unsigned long long int";
586 LOG_RUNTIME(
"NewLang type '%s'(%d) can't be represented by C++ type!",
toString(type),
static_cast<int> (type));
602 inline const char *
toCXXRef(
const std::string_view ref) {
603 if (ref.compare(
"&*") == 0) {
605 }
else if (ref.compare(
"&") == 0) {
607 }
else if (ref.compare(
"&&") == 0) {
609 }
else if (ref.compare(
"&?") == 0) {
612 }
else if (ref.compare(
"&*^") == 0) {
614 }
else if (ref.compare(
"&^") == 0) {
616 }
else if (ref.compare(
"&&^") == 0) {
618 }
else if (ref.compare(
"&?^") == 0) {
622 LOG_RUNTIME(
"Unknown reference type '%s'!", ref.begin());
722 return static_cast<uint8_t
> (t) &&
static_cast<uint8_t
> (t) <=
static_cast<uint8_t
> (
ObjType::Tensor);
727 return (
static_cast<uint8_t
> (t) >
static_cast<uint8_t
> (
ObjType::Bool) &&
744 return static_cast<uint8_t
> (t) &&
static_cast<uint8_t
> (t) <=
static_cast<uint8_t
> (
ObjType::Tensor);
755 return static_cast<uint8_t
> (t) >=
static_cast<uint8_t
> (
ObjType::Bool) &&
990 inline bool canCast(
const std::string from,
const std::string to) {
1000 std::string temp = str;
1001 for (
size_t k = temp.find(
'_'); k != temp.npos; k = temp.find(
'_', k)) {
1004 int64_t
result = std::strtol(temp.c_str(), &ptr, 10);
1005 if (ptr == temp.c_str() || (ptr && *ptr !=
'\0')) {
1014 std::string temp = str;
1015 for (
size_t k = temp.find(
'_'); k != temp.npos; k = temp.find(
'_', k)) {
1018 double result = std::strtod(temp.c_str(), &ptr);
1019 if (ptr == temp.c_str() || (ptr && *ptr !=
'\0')) {
1040 if (name.size() > 3 || !(name[0] ==
'$' || name[0] ==
'@' || name[0] ==
'%')) {
1042 return name.compare(
"_") == 0;
1044 return name.compare(
"$") == 0 || name.compare(
"@") == 0 || name.compare(
"%") == 0
1045 || name.compare(
"$$") == 0 || name.compare(
"@$") == 0 || name.compare(
"$^") == 0 || name.compare(
"$?") == 0
1046 || name.compare(
"@::") == 0 || name.compare(
"$*") == 0 || name.compare(
"$#") == 0;
1050 return !name.empty() && (name.rbegin()[0] ==
':' || name.rbegin()[0] ==
'$' ||
isReservedName(name));
1054 return name.size() > 4 && name[0] ==
'_' && name[1] ==
'$';
1058 return !name.empty() && name[0] ==
'\\';
1062 return name.find(
"::") != std::string::npos;
1067 return name.find(
".") != std::string::npos;
1071 return name.find(
"$") == std::string::npos && name.find(
":") == std::string::npos && name.find(
"@") == std::string::npos;
1075 return !name.empty() && (name[0] ==
'$' || name[name.size() - 1] ==
'$');
1080 return name.size() > 1 && ((name[0] ==
':' && name[1] ==
':') || (name[0] ==
'$' && name[1] ==
'$'));
1084 size_t pos = name.find(
"::");
1085 return pos && pos != std::string::npos && name[0] !=
'@';
1089 return name.find(
":::") != std::string::npos || (name.size() > 1 && name[0] ==
':' && name[1] !=
':');
1094 return name.size() > 1 && name[0] ==
':' && name[1] ==
':';
1099 return !name.empty() && name[0] ==
'@';
1104 return !name.empty() && name[0] ==
'%';
1109 return !name.empty() && (name[0] ==
'$' || name[0] ==
'@' || name[0] ==
':' || name[0] ==
'%' || name[0] ==
'\\');
1123 return name.size() >= 4 && name.find(
"__") == 0 && name.rfind(
"__") == name.size() - 2;
1131 return name.size() >= 3 && name.find(
"__") == 0;
1143 return name.find(
"::") != name.npos;
1149 return !name.empty() && name[name.size() - 1] ==
'^';
1153 std::string
result(name.begin());
1181 if (!name.empty() && (name[0] ==
'\\' || name[0] ==
'$' || name[0] ==
'@' || name[0] ==
'%')) {
1183 return name.find(
"\\\\") == 0 ? name.substr(2) : name.substr(1);
1190 std::string
result(name.begin(), name.begin() + name.find(
"$_"));
1196 size_t pos = name.find(
"::");
1197 if (pos != std::string::npos) {
1199 return std::string(name.begin(), name.begin() + pos);
1201 return std::string(name.begin(), name.end());
1204 return std::string();
1208 size_t found = name.find(
"\\_");
1209 if (name.empty() || found == 0 || found == 1 || name[name.size() - 1] ==
'_') {
1213 for (
size_t i = 0; i < name.size(); i++) {
1215 if (!(name[i] ==
'\\' || name[i] ==
'_' || islower(name[i]) || isdigit(name[i]))) {
1224 size_t pos = name.rfind(
"::");
1225 if (pos != std::string::npos) {
1226 name = name.substr(pos + 2);
1230 return std::string();
1270 std::string
result(*
this);
1276 if (module.size() > 2) {
1277 result.insert(
result.begin(), module.begin(), module.end());
1278 std::replace(
result.begin(),
result.begin() + module.size(),
'\\',
'$');
1421 return static_cast<uint8_t
> (type) <=
static_cast<uint8_t
> (generic);
bool AppendModule(ModulePtr module)
std::pair< std::string, VariablePair > PairType
bool RegisterObject(bool only_new, const std::string_view name, TermPtr term, Variable var)
std::map< std::string, ModulePtr > m_modules
std::string Dump(const std::string_view filter="", const char delim=';')
VariablePair * FindObject(const std::string_view name)
std::string GetLocalName()
static std::string ExtractModuleName(const std::string_view name)
InternalName & operator=(const InternalName &name)
InternalName(const std::string_view str="")
InternalName & operator=(const std::string_view name)
std::string ExtractName()
std::string SetFromLocalName(std::string name)
InternalName(const InternalName &name)
std::string SetFromGlobalName(std::string name)
std::string getMangledName(const std::string_view module)
std::string ExtractModuleName()
#define DEFINE_CASE(name)
__attribute__((weak)) std
#define LOG_RUNTIME(format,...)
#define ASSERT(condition)
bool isConstName(const std::string_view name)
bool isFloatingType(ObjType t)
bool isStringChar(ObjType t)
bool isMangledName(const std::string_view name)
const VariablePtr getEllipsysVar()
bool isContainsType(ObjType generic, ObjType type)
bool isTypeName(ObjType t)
bool isStaticName(const std::string_view name)
std::shared_ptr< Module > ModulePtr
std::vector< ObjPtr > ArrayObjType
const TermPtr getDefaultType(const std::string_view text)
bool isStringWide(ObjType t)
std::string MakeName(std::string name)
const TermPtr getRequiredTerm()
std::vector< std::string > PostLexerType
std::string ParserMessage(std::string &buffer, int row, int col, const char *format,...)
bool isArithmeticType(ObjType t)
bool isTrivialName(const std::string_view name)
bool CheckCharModuleName(const std::string_view name)
std::shared_ptr< std::string > SourceType
bool isEllipsis(ObjType t)
bool isInterrupt(ObjType t)
std::string ExtractName(std::string name)
bool isLocalName(const std::string_view name)
const VariablePair & getNonePair()
const char * toCXXType(ObjType type, bool int_bool)
std::shared_ptr< Parser > ParserPtr
bool isNativeType(ObjType t)
bool isModuleName(const std::string_view name)
bool isIntegralType(ObjType t, bool includeBool)
bool isGenericType(ObjType t)
bool isDictionary(ObjType t)
bool isFieldName(const std::string_view name)
bool isPrivateName(const std::string_view name)
std::shared_ptr< Variable > VariablePtr
bool isLocalType(ObjType)
std::shared_ptr< Term > TermPtr
static constexpr const char * ws
bool isFullName(const std::string_view name)
int64_t parseInteger(const char *str)
std::shared_ptr< Obj > ObjPtr
T BaseFileName(T const &path, T const &delims="/\\")
bool isSystemName(const std::string_view name)
std::complex< double > parseComplex(const char *)
std::shared_ptr< RunTime > RuntimePtr
T RemoveFileExtension(T const &filename)
bool isVariableName(const std::string_view name)
std::shared_ptr< runtime::Buildin > BuildinPtr
std::vector< std::string > StringArray
void ParserException(const char *msg, std::string &buffer, int row, int col)
bool isHidenName(const std::string_view name)
bool isModuleScope(const std::string_view name)
std::string ExtractModuleName(const std::string_view name)
bool isGlobalScope(const std::string_view name)
double parseDouble(const char *str)
void NewLangSignalHandler(int signal)
const ObjPtr getNoneObj()
const char * toCXXRef(const std::string_view ref)
std::string & rtrim(std::string &s, const char *t=ws)
bool isReservedName(const std::string_view name)
const Variable & getNoneVar()
std::shared_ptr< Context > RunnerPtr
std::shared_ptr< Macro > MacroPtr
std::vector< TermPtr > BlockType
ObjType GetBaseTypeFromString(const std::string_view type_arg, bool *has_error=nullptr)
bool isInternalName(const std::string_view name)
bool isPlainDataType(ObjType t)
bool isNativeName(const std::string_view name)
ObjType typeFromString(TermPtr &term, RunTime *rt, bool *has_error)
bool isIndexingType(ObjType curr, ObjType fix)
bool isBooleanType(ObjType t)
std::shared_ptr< Diag > DiagPtr
bool isLocalAnyName(const std::string_view name)
T repeat(T str, const std::size_t n)
bool isBaseType(ObjType t)
bool isComplexType(ObjType t)
bool isSimpleType(ObjType t)
ObjPtr(* EvalFunction)(Context *ctx, const TermPtr &term, Obj *args, bool eval_block)
ObjType typeFromLimit(int64_t value, ObjType type_default=ObjType::Int64)
std::string NormalizeName(const std::string_view name)
bool isMacroName(const std::string_view name)
const char * toString(TermID type)
const TermPtr getNoneTerm()
ObjPtr FunctionType(Context *ctx, Obj &in)
bool isFunction(ObjType t)
bool isDefaultType(const TermPtr &term)
std::string & ltrim(std::string &s, const char *t=ws)
const ObjPtr getEllipsysObj()
std::string & trim(std::string &s, const char *t=ws)
bool isObjectType(ObjType t)
bool canCastLimit(const ObjType from, const ObjType to)
const TermPtr getEllipsysTerm()
bool canCast(const ObjType from, const ObjType to)
static VariablePair Create(TermPtr term, Variable d)
static std::enable_if<!std::is_same< Variable, D >::value, VariablePair >::type Create(TermPtr term, D d)