some cleanup

This commit is contained in:
blueloveTH 2024-01-03 19:15:41 +08:00
parent e2f36d017b
commit ab458b4af4
8 changed files with 35 additions and 32 deletions

View File

@ -17,6 +17,8 @@ struct PrattRule{
};
class Compiler {
PK_ALWAYS_PASS_BY_POINTER(Compiler)
inline static PrattRule rules[kTokenCount];
std::unique_ptr<Lexer> lexer;
stack<CodeEmitContext> contexts;
@ -134,9 +136,6 @@ class Compiler {
public:
Compiler(VM* vm, const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
CodeObject_ compile();
Compiler(const Compiler&) = delete;
Compiler& operator=(const Compiler&) = delete;
};
} // namespace pkpy

View File

@ -24,16 +24,17 @@ enum CompileMode {
};
struct SourceData {
std::string source; // assume '\0' terminated
PK_ALWAYS_PASS_BY_POINTER(SourceData)
Str filename;
std::vector<const char*> line_starts;
CompileMode mode;
SourceData(const SourceData&) = delete;
SourceData& operator=(const SourceData&) = delete;
std::string source; // assume '\0' terminated
std::vector<const char*> line_starts;
SourceData(const Str& source, const Str& filename, CompileMode mode);
std::pair<const char*,const char*> get_line(int lineno) const;
SourceData(const Str& filename, CompileMode mode);
std::pair<const char*,const char*> _get_line(int lineno) const;
Str snapshot(int lineno, const char* cursor, std::string_view name) const;
};

View File

@ -452,7 +452,7 @@ public:
void _unpack_as_list(ArgsView args, List& list);
void _unpack_as_dict(ArgsView args, Dict& dict);
PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
CodeObject_ compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope=false);
CodeObject_ compile(const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
PyObject* py_negate(PyObject* obj);
bool py_bool(PyObject* obj);
i64 py_hash(PyObject* obj);

View File

@ -1,4 +1,5 @@
#include "pocketpy/collections.h"
namespace pkpy
{
struct PyDequeIter // Iterator for the deque type

View File

@ -1,5 +1,4 @@
#include "pocketpy/csv.h"
#include "pocketpy/config.h"
namespace pkpy{

View File

@ -2,7 +2,7 @@
namespace pkpy{
SourceData::SourceData(const Str& source, const Str& filename, CompileMode mode) {
SourceData::SourceData(const Str& source, const Str& filename, CompileMode mode): filename(filename), mode(mode) {
int index = 0;
// Skip utf8 BOM if there is any.
if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
@ -12,14 +12,16 @@ namespace pkpy{
if(source[index] != '\r') ss << source[index];
index++;
}
this->filename = filename;
this->source = ss.str().str();
line_starts.push_back(this->source.c_str());
this->mode = mode;
}
std::pair<const char*,const char*> SourceData::get_line(int lineno) const {
SourceData::SourceData(const Str& filename, CompileMode mode): filename(filename), mode(mode) {
line_starts.push_back(this->source.c_str());
}
std::pair<const char*,const char*> SourceData::_get_line(int lineno) const {
if(lineno == -1) return {nullptr, nullptr};
lineno -= 1;
if(lineno < 0) lineno = 0;
@ -34,19 +36,21 @@ namespace pkpy{
SStream ss;
ss << " " << "File \"" << filename << "\", line " << lineno;
if(!name.empty()) ss << ", in " << name;
ss << '\n';
std::pair<const char*,const char*> pair = get_line(lineno);
Str line = "<?>";
int removed_spaces = 0;
if(pair.first && pair.second){
line = Str(pair.first, pair.second-pair.first).lstrip();
removed_spaces = pair.second - pair.first - line.length();
if(line.empty()) line = "<?>";
}
ss << " " << line;
if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){
auto column = cursor - pair.first - removed_spaces;
if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
if(!source.empty()){
ss << '\n';
std::pair<const char*,const char*> pair = _get_line(lineno);
Str line = "<?>";
int removed_spaces = 0;
if(pair.first && pair.second){
line = Str(pair.first, pair.second-pair.first).lstrip();
removed_spaces = pair.second - pair.first - line.length();
if(line.empty()) line = "<?>";
}
ss << " " << line;
if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){
auto column = cursor - pair.first - removed_spaces;
if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
}
}
return ss.str();
}

View File

@ -1720,8 +1720,8 @@ void VM::post_init(){
#endif
}
CodeObject_ VM::compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope) {
Compiler compiler(this, std::move(source), filename, mode, unknown_global_scope);
CodeObject_ VM::compile(const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope) {
Compiler compiler(this, source, filename, mode, unknown_global_scope);
try{
return compiler.compile();
}catch(const Exception& e){

View File

@ -1,5 +1,4 @@
#include "pocketpy/vm.h"
#include "pocketpy/error.h"
namespace pkpy{