This commit is contained in:
blueloveTH 2024-06-01 18:53:44 +08:00
parent 197d8414bb
commit fc84e657e3
5 changed files with 27 additions and 49 deletions

View File

@ -49,18 +49,6 @@ struct SourceData {
Str snapshot(int lineno, const char* cursor, std::string_view name) const; Str snapshot(int lineno, const char* cursor, std::string_view name) const;
}; };
struct ExceptionLine{
std::shared_ptr<SourceData> src;
int lineno;
const char* cursor;
std::string name;
Str snapshot() const { return src->snapshot(lineno, cursor, name); }
ExceptionLine(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
src(src), lineno(lineno), cursor(cursor), name(name) {}
};
struct Exception { struct Exception {
StrName type; StrName type;
Str msg; Str msg;
@ -70,8 +58,20 @@ struct Exception {
void* _code_on_error; void* _code_on_error;
PyObject* _self; // weak reference PyObject* _self; // weak reference
stack<ExceptionLine> stacktrace; struct Frame{
std::shared_ptr<SourceData> src;
int lineno;
const char* cursor;
std::string name;
Str snapshot() const { return src->snapshot(lineno, cursor, name); }
Frame(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
src(src), lineno(lineno), cursor(cursor), name(name) {}
};
stack<Frame> stacktrace;
Exception(StrName type): type(type), is_re(true), _ip_on_error(-1), _code_on_error(nullptr), _self(nullptr) {} Exception(StrName type): type(type), is_re(true), _ip_on_error(-1), _code_on_error(nullptr), _self(nullptr) {}
PyObject* self() const{ PyObject* self() const{

View File

@ -54,9 +54,7 @@ typedef unique_ptr_128<Expr> Expr_;
typedef small_vector<Expr_, 4> Expr_vector; typedef small_vector<Expr_, 4> Expr_vector;
template<> template<>
struct TriviallyRelocatable<Expr_>{ constexpr inline bool is_trivially_relocatable_v<Expr_> = true;
constexpr static bool value = true;
};
struct Expr{ struct Expr{
int line = 0; int line = 0;

View File

@ -123,11 +123,12 @@ struct Lexer {
bool lex_one_token(); bool lex_one_token();
/***** Error Reporter *****/ /***** Error Reporter *****/
void throw_err(StrName type, Str msg); [[noreturn]] void throw_err(StrName type, Str msg);
void throw_err(StrName type, Str msg, int lineno, const char* cursor); [[noreturn]] void throw_err(StrName type, Str msg, int lineno, const char* cursor);
void SyntaxError(Str msg){ throw_err("SyntaxError", msg); } [[noreturn]] void SyntaxError(Str msg){ throw_err("SyntaxError", msg); }
void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); } [[noreturn]] void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); }
void IndentationError(Str msg){ throw_err("IndentationError", msg); } [[noreturn]] void IndentationError(Str msg){ throw_err("IndentationError", msg); }
Lexer(VM* vm, std::shared_ptr<SourceData> src); Lexer(VM* vm, std::shared_ptr<SourceData> src);
vector<Token> run(); vector<Token> run();
}; };

View File

@ -9,6 +9,9 @@ struct explicit_copy_t {
explicit explicit_copy_t() = default; explicit explicit_copy_t() = default;
}; };
template<typename T>
constexpr inline bool is_trivially_relocatable_v = std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>;
template<typename T> template<typename T>
struct array{ struct array{
T* _data; T* _data;
@ -124,7 +127,7 @@ struct vector{
if(cap < 4) cap = 4; // minimum capacity if(cap < 4) cap = 4; // minimum capacity
if(cap <= capacity()) return; if(cap <= capacity()) return;
T* new_data = (T*)malloc(sizeof(T) * cap); T* new_data = (T*)malloc(sizeof(T) * cap);
if constexpr(std::is_trivially_copyable_v<T>){ if constexpr(is_trivially_relocatable_v<T>){
memcpy(new_data, _data, sizeof(T) * _size); memcpy(new_data, _data, sizeof(T) * _size);
}else{ }else{
for(int i=0; i<_size; i++){ for(int i=0; i<_size; i++){
@ -249,29 +252,7 @@ public:
} // namespace pkpy } // namespace pkpy
namespace pkpy namespace pkpy {
{
// explicitly mark a type as trivially relocatable for better performance
template<typename T>
struct TriviallyRelocatable
{
constexpr static bool value =
std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>;
};
template<typename T>
constexpr inline bool is_trivially_relocatable_v =
TriviallyRelocatable<T>::value;
template<typename T>
struct TriviallyRelocatable<std::shared_ptr<T>>
{
constexpr static bool value = true;
};
// the implementation of small_vector
template<typename T, std::size_t N> template<typename T, std::size_t N>
class small_vector class small_vector
{ {

View File

@ -410,9 +410,7 @@ int utf8len(unsigned char c, bool suppress){
buffer[buffer.size()] = '\0'; // set '\0' buffer[buffer.size()] = '\0'; // set '\0'
return Str(buffer.detach()); return Str(buffer.detach());
#else #else
#warning "SStream::str() needs to be optimized" return Str(buffer.data(), buffer.size());
buffer.push_back('\0');
return Str(buffer.data(), buffer.size()-1);
#endif #endif
} }