mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
some fix
This commit is contained in:
parent
c0a855ab7a
commit
52a3c74e43
@ -5,7 +5,6 @@
|
||||
#include "pocketpy/common/vector.hpp"
|
||||
|
||||
#include <string_view>
|
||||
#include <map>
|
||||
|
||||
namespace pkpy {
|
||||
|
||||
@ -113,9 +112,8 @@ struct StrName {
|
||||
|
||||
StrName(const Str& s) : index(get(s.sv()).index) {}
|
||||
|
||||
std::string_view sv() const { return _r_interned()[index]; }
|
||||
|
||||
const char* c_str() const { return _r_interned()[index].c_str(); }
|
||||
std::string_view sv() const;
|
||||
const char* c_str() const;
|
||||
|
||||
bool empty() const { return index == 0; }
|
||||
|
||||
@ -130,8 +128,6 @@ struct StrName {
|
||||
bool operator> (const StrName& other) const noexcept { return sv() > other.sv(); }
|
||||
|
||||
static StrName get(std::string_view s);
|
||||
static std::map<std::string_view, uint16_t>& _interned();
|
||||
static std::map<uint16_t, std::string>& _r_interned();
|
||||
static uint32_t _pesudo_random_index;
|
||||
};
|
||||
|
||||
|
@ -119,8 +119,8 @@ struct CodeEmitContext {
|
||||
int curr_iblock = 0;
|
||||
bool is_compiling_class = false;
|
||||
|
||||
std::map<PyVar, int> _co_consts_nonstring_dedup_map;
|
||||
std::map<std::string_view, int> _co_consts_string_dedup_map;
|
||||
small_map<PyVar, int> _co_consts_nonstring_dedup_map;
|
||||
small_map<std::string_view, int> _co_consts_string_dedup_map;
|
||||
|
||||
int get_loop() const;
|
||||
CodeBlock* enter_block(CodeBlockType type);
|
||||
|
@ -102,7 +102,7 @@ struct PyVar final {
|
||||
template <typename T>
|
||||
obj_get_t<T> obj_get();
|
||||
|
||||
// for std::map<>
|
||||
// std::less<> for map-like containers
|
||||
bool operator< (const PyVar& other) const { return memcmp(this, &other, sizeof(PyVar)) < 0; }
|
||||
};
|
||||
|
||||
|
@ -1,10 +1,12 @@
|
||||
#include "pocketpy/common/str.hpp"
|
||||
#include "pocketpy/common/gil.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cassert>
|
||||
#include <ostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
|
||||
namespace pkpy {
|
||||
|
||||
@ -356,19 +358,23 @@ int Str::count(const Str& sub) const {
|
||||
return cnt;
|
||||
}
|
||||
|
||||
std::map<std::string_view, uint16_t>& StrName::_interned() {
|
||||
std::map<std::string_view, uint16_t>& _interned() {
|
||||
static std::map<std::string_view, uint16_t> interned;
|
||||
return interned;
|
||||
}
|
||||
|
||||
std::map<uint16_t, std::string>& StrName::_r_interned() {
|
||||
std::map<uint16_t, std::string>& _r_interned() {
|
||||
static std::map<uint16_t, std::string> r_interned;
|
||||
return r_interned;
|
||||
}
|
||||
|
||||
std::string_view StrName::sv() const { return _r_interned()[index]; }
|
||||
const char* StrName::c_str() const { return _r_interned()[index].c_str(); }
|
||||
|
||||
uint32_t StrName::_pesudo_random_index = 0;
|
||||
|
||||
StrName StrName::get(std::string_view s) {
|
||||
PK_GLOBAL_SCOPE_LOCK()
|
||||
auto it = _interned().find(s);
|
||||
if(it != _interned().end()) return StrName(it->second);
|
||||
// generate new index
|
||||
|
@ -1191,12 +1191,11 @@ Str Compiler::precompile() {
|
||||
ss << "pkpy:" PK_VERSION << '\n'; // L1: version string
|
||||
ss << (int)mode() << '\n'; // L2: mode
|
||||
|
||||
std::map<std::string_view, int> token_indices;
|
||||
small_map<std::string_view, int> token_indices;
|
||||
for(auto token: tokens) {
|
||||
if(is_raw_string_used(token.type)) {
|
||||
auto it = token_indices.find(token.sv());
|
||||
if(it == token_indices.end()) {
|
||||
token_indices[token.sv()] = 0;
|
||||
if(!token_indices.contains(token.sv())) {
|
||||
token_indices.insert(token.sv(), 0);
|
||||
// assert no '\n' in token.sv()
|
||||
for(char c: token.sv())
|
||||
assert(c != '\n');
|
||||
|
@ -115,14 +115,14 @@ int CodeEmitContext::add_varname(StrName name) {
|
||||
}
|
||||
|
||||
int CodeEmitContext::add_const_string(std::string_view key) {
|
||||
auto it = _co_consts_string_dedup_map.find(key);
|
||||
if(it != _co_consts_string_dedup_map.end()) {
|
||||
return it->second;
|
||||
int* val = _co_consts_string_dedup_map.try_get(key);
|
||||
if(val) {
|
||||
return *val;
|
||||
} else {
|
||||
co->consts.push_back(VAR(key));
|
||||
int index = co->consts.size() - 1;
|
||||
key = co->consts.back().obj_get<Str>().sv();
|
||||
_co_consts_string_dedup_map[key] = index;
|
||||
_co_consts_string_dedup_map.insert(key, index);
|
||||
return index;
|
||||
}
|
||||
}
|
||||
@ -130,13 +130,13 @@ int CodeEmitContext::add_const_string(std::string_view key) {
|
||||
int CodeEmitContext::add_const(PyVar v) {
|
||||
assert(!is_type(v, VM::tp_str));
|
||||
// non-string deduplication
|
||||
auto it = _co_consts_nonstring_dedup_map.find(v);
|
||||
if(it != _co_consts_nonstring_dedup_map.end()) {
|
||||
return it->second;
|
||||
int* val = _co_consts_nonstring_dedup_map.try_get(v);
|
||||
if(val) {
|
||||
return *val;
|
||||
} else {
|
||||
co->consts.push_back(v);
|
||||
int index = co->consts.size() - 1;
|
||||
_co_consts_nonstring_dedup_map[v] = index;
|
||||
_co_consts_nonstring_dedup_map.insert(v, index);
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user