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;
};
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 {
StrName type;
Str msg;
@ -71,7 +59,19 @@ struct Exception {
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) {}
PyObject* self() const{

View File

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

View File

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

View File

@ -9,6 +9,9 @@ struct explicit_copy_t {
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>
struct array{
T* _data;
@ -124,7 +127,7 @@ struct vector{
if(cap < 4) cap = 4; // minimum capacity
if(cap <= capacity()) return;
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);
}else{
for(int i=0; i<_size; i++){
@ -249,29 +252,7 @@ public:
} // 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
namespace pkpy {
template<typename T, std::size_t N>
class small_vector
{

View File

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