mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-07 10:40:16 +00:00
some cleanup
This commit is contained in:
parent
e2f36d017b
commit
ab458b4af4
@ -17,6 +17,8 @@ struct PrattRule{
|
|||||||
};
|
};
|
||||||
|
|
||||||
class Compiler {
|
class Compiler {
|
||||||
|
PK_ALWAYS_PASS_BY_POINTER(Compiler)
|
||||||
|
|
||||||
inline static PrattRule rules[kTokenCount];
|
inline static PrattRule rules[kTokenCount];
|
||||||
std::unique_ptr<Lexer> lexer;
|
std::unique_ptr<Lexer> lexer;
|
||||||
stack<CodeEmitContext> contexts;
|
stack<CodeEmitContext> contexts;
|
||||||
@ -134,9 +136,6 @@ class Compiler {
|
|||||||
public:
|
public:
|
||||||
Compiler(VM* vm, const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
|
Compiler(VM* vm, const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
|
||||||
CodeObject_ compile();
|
CodeObject_ compile();
|
||||||
|
|
||||||
Compiler(const Compiler&) = delete;
|
|
||||||
Compiler& operator=(const Compiler&) = delete;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
@ -24,16 +24,17 @@ enum CompileMode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SourceData {
|
struct SourceData {
|
||||||
std::string source; // assume '\0' terminated
|
PK_ALWAYS_PASS_BY_POINTER(SourceData)
|
||||||
|
|
||||||
Str filename;
|
Str filename;
|
||||||
std::vector<const char*> line_starts;
|
|
||||||
CompileMode mode;
|
CompileMode mode;
|
||||||
|
|
||||||
SourceData(const SourceData&) = delete;
|
std::string source; // assume '\0' terminated
|
||||||
SourceData& operator=(const SourceData&) = delete;
|
std::vector<const char*> line_starts;
|
||||||
|
|
||||||
SourceData(const Str& source, const Str& filename, CompileMode mode);
|
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;
|
Str snapshot(int lineno, const char* cursor, std::string_view name) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -452,7 +452,7 @@ public:
|
|||||||
void _unpack_as_list(ArgsView args, List& list);
|
void _unpack_as_list(ArgsView args, List& list);
|
||||||
void _unpack_as_dict(ArgsView args, Dict& dict);
|
void _unpack_as_dict(ArgsView args, Dict& dict);
|
||||||
PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
|
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);
|
PyObject* py_negate(PyObject* obj);
|
||||||
bool py_bool(PyObject* obj);
|
bool py_bool(PyObject* obj);
|
||||||
i64 py_hash(PyObject* obj);
|
i64 py_hash(PyObject* obj);
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
#include "pocketpy/collections.h"
|
#include "pocketpy/collections.h"
|
||||||
|
|
||||||
namespace pkpy
|
namespace pkpy
|
||||||
{
|
{
|
||||||
struct PyDequeIter // Iterator for the deque type
|
struct PyDequeIter // Iterator for the deque type
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
#include "pocketpy/csv.h"
|
#include "pocketpy/csv.h"
|
||||||
#include "pocketpy/config.h"
|
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
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;
|
int index = 0;
|
||||||
// Skip utf8 BOM if there is any.
|
// Skip utf8 BOM if there is any.
|
||||||
if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
|
if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
|
||||||
@ -12,14 +12,16 @@ namespace pkpy{
|
|||||||
if(source[index] != '\r') ss << source[index];
|
if(source[index] != '\r') ss << source[index];
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->filename = filename;
|
|
||||||
this->source = ss.str().str();
|
this->source = ss.str().str();
|
||||||
|
|
||||||
line_starts.push_back(this->source.c_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};
|
if(lineno == -1) return {nullptr, nullptr};
|
||||||
lineno -= 1;
|
lineno -= 1;
|
||||||
if(lineno < 0) lineno = 0;
|
if(lineno < 0) lineno = 0;
|
||||||
@ -34,8 +36,9 @@ namespace pkpy{
|
|||||||
SStream ss;
|
SStream ss;
|
||||||
ss << " " << "File \"" << filename << "\", line " << lineno;
|
ss << " " << "File \"" << filename << "\", line " << lineno;
|
||||||
if(!name.empty()) ss << ", in " << name;
|
if(!name.empty()) ss << ", in " << name;
|
||||||
|
if(!source.empty()){
|
||||||
ss << '\n';
|
ss << '\n';
|
||||||
std::pair<const char*,const char*> pair = get_line(lineno);
|
std::pair<const char*,const char*> pair = _get_line(lineno);
|
||||||
Str line = "<?>";
|
Str line = "<?>";
|
||||||
int removed_spaces = 0;
|
int removed_spaces = 0;
|
||||||
if(pair.first && pair.second){
|
if(pair.first && pair.second){
|
||||||
@ -48,6 +51,7 @@ namespace pkpy{
|
|||||||
auto column = cursor - pair.first - removed_spaces;
|
auto column = cursor - pair.first - removed_spaces;
|
||||||
if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
|
if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1720,8 +1720,8 @@ void VM::post_init(){
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
CodeObject_ VM::compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope) {
|
CodeObject_ VM::compile(const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope) {
|
||||||
Compiler compiler(this, std::move(source), filename, mode, unknown_global_scope);
|
Compiler compiler(this, source, filename, mode, unknown_global_scope);
|
||||||
try{
|
try{
|
||||||
return compiler.compile();
|
return compiler.compile();
|
||||||
}catch(const Exception& e){
|
}catch(const Exception& e){
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
#include "pocketpy/vm.h"
|
#include "pocketpy/vm.h"
|
||||||
#include "pocketpy/error.h"
|
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user