This commit is contained in:
blueloveTH 2024-06-15 21:02:06 +08:00
parent 59afaf8263
commit 225f634f33
7 changed files with 66 additions and 19 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
#include "pocketpy/common/str.h"
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -16,9 +17,11 @@ extern "C" {
#define SMALLMAP_T__HEADER #define SMALLMAP_T__HEADER
#define K const char* #define K c11_string
#define V uint16_t #define V int
#define TAG s2n #define TAG s2i
#define less(a, b) (c11_string__cmp((a.key), (b)) < 0)
#define equal(a, b) (c11_string__cmp((a), (b)) == 0)
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__HEADER #undef SMALLMAP_T__HEADER

View File

@ -14,6 +14,10 @@ typedef struct c11_string{
int size; int size;
} c11_string; } c11_string;
int c11_string__cmp(c11_string self, c11_string other);
int c11_string__cmp2(c11_string self, const char* other, int size);
int c11_string__cmp3(c11_string self, const char* other);
typedef struct pkpy_Str{ typedef struct pkpy_Str{
int size; int size;
bool is_ascii; bool is_ascii;
@ -48,6 +52,7 @@ pkpy_Str pkpy_Str__u8_slice(const pkpy_Str* self, int start, int stop, int step)
int pkpy_Str__u8_length(const pkpy_Str* self); int pkpy_Str__u8_length(const pkpy_Str* self);
int pkpy_Str__cmp(const pkpy_Str* self, const pkpy_Str* other); int pkpy_Str__cmp(const pkpy_Str* self, const pkpy_Str* other);
int pkpy_Str__cmp2(const pkpy_Str* self, const char* other, int size); int pkpy_Str__cmp2(const pkpy_Str* self, const char* other, int size);
int pkpy_Str__cmp3(const pkpy_Str* self, const char* other);
int pkpy_Str__unicode_index_to_byte(const pkpy_Str* self, int i); int pkpy_Str__unicode_index_to_byte(const pkpy_Str* self, int i);
int pkpy_Str__byte_index_to_unicode(const pkpy_Str* self, int n); int pkpy_Str__byte_index_to_unicode(const pkpy_Str* self, int n);
int pkpy_Str__index(const pkpy_Str* self, const pkpy_Str* sub, int start); int pkpy_Str__index(const pkpy_Str* self, const pkpy_Str* sub, int start);

View File

@ -347,6 +347,11 @@ struct SStream: pkpy_SStream {
return *this; return *this;
} }
SStream& operator<< (c11_string val){
pkpy_SStream__write_cstrn(this, val.data, val.size);
return *this;
}
SStream& operator<< (char val){ SStream& operator<< (char val){
pkpy_SStream__write_char(this, val); pkpy_SStream__write_char(this, val);
return *this; return *this;

View File

@ -1,5 +1,3 @@
#pragma once
#if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE) #if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE)
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
@ -14,6 +12,10 @@
#define less(a, b) ((a.key) < (b)) #define less(a, b) ((a.key) < (b))
#endif #endif
#ifndef equal
#define equal(a, b) ((a) == (b))
#endif
/* Temprary macros */ /* Temprary macros */
#define CONCAT(A, B) CONCAT_(A, B) #define CONCAT(A, B) CONCAT_(A, B)
#define CONCAT_(A, B) A##B #define CONCAT_(A, B) A##B
@ -53,7 +55,7 @@ void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value) {
int index; int index;
c11__lower_bound(KV, self->data, self->count, key, less, &index); c11__lower_bound(KV, self->data, self->count, key, less, &index);
KV* it = c11__at(KV, self, index); KV* it = c11__at(KV, self, index);
if(index != self->count && it->key == key) { if(index != self->count && equal(it->key, key)) {
it->value = value; it->value = value;
} else { } else {
KV kv = {key, value}; KV kv = {key, value};
@ -65,7 +67,7 @@ V* SMALLMAP_METHOD(try_get)(const SMALLMAP* self, K key) {
int index; int index;
c11__lower_bound(KV, self->data, self->count, key, less, &index); c11__lower_bound(KV, self->data, self->count, key, less, &index);
KV* it = c11__at(KV, self, index); KV* it = c11__at(KV, self, index);
if(index != self->count && it->key == key) { if(index != self->count && equal(it->key, key)) {
return &it->value; return &it->value;
} else { } else {
return NULL; return NULL;
@ -85,7 +87,7 @@ bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key) {
int index; int index;
c11__lower_bound(KV, self->data, self->count, key, less, &index); c11__lower_bound(KV, self->data, self->count, key, less, &index);
KV* it = c11__at(KV, self, index); KV* it = c11__at(KV, self, index);
if(index != self->count && it->key == key) { if(index != self->count && equal(it->key, key)) {
c11_vector__erase(KV, self, index); c11_vector__erase(KV, self, index);
return true; return true;
} }
@ -108,3 +110,4 @@ void SMALLMAP_METHOD(clear)(SMALLMAP* self) {
#undef V #undef V
#undef TAG #undef TAG
#undef less #undef less
#undef equal

View File

@ -1,4 +1,5 @@
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
#include "pocketpy/common/str.h"
#include <stdint.h> #include <stdint.h>
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
@ -10,8 +11,10 @@
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K const char* #define K c11_string
#define V uint16_t #define V int
#define TAG s2n #define TAG s2i
#define less(a, b) (c11_string__cmp((a.key), (b)) < 0)
#define equal(a, b) (c11_string__cmp((a), (b)) == 0)
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__SOURCE #undef SMALLMAP_T__SOURCE

View File

@ -238,6 +238,20 @@ pkpy_Str pkpy_Str__replace2(const pkpy_Str *self, const pkpy_Str *old, const pkp
return retval; return retval;
} }
int c11_string__cmp(c11_string self, c11_string other){
return c11_string__cmp2(self, other.data, other.size);
}
int c11_string__cmp2(c11_string self, const char *other, int size){
int res = strncmp(self.data, other, PK_MIN(self.size, size));
if(res != 0) return res;
return self.size - size;
}
int c11_string__cmp3(c11_string self, const char *other){
return c11_string__cmp2(self, other, strlen(other));
}
int pkpy_Str__cmp(const pkpy_Str *self, const pkpy_Str *other){ int pkpy_Str__cmp(const pkpy_Str *self, const pkpy_Str *other){
return pkpy_Str__cmp2(self, pkpy_Str__data(other), other->size); return pkpy_Str__cmp2(self, pkpy_Str__data(other), other->size);
} }
@ -248,6 +262,10 @@ int pkpy_Str__cmp2(const pkpy_Str *self, const char *other, int size){
return self->size - size; return self->size - size;
} }
int pkpy_Str__cmp3(const pkpy_Str *self, const char *other){
return strcmp(pkpy_Str__data(self), other);
}
pkpy_Str pkpy_Str__u8_getitem(const pkpy_Str *self, int i){ pkpy_Str pkpy_Str__u8_getitem(const pkpy_Str *self, int i){
i = pkpy_Str__unicode_index_to_byte(self, i); i = pkpy_Str__unicode_index_to_byte(self, i);
int size = c11__u8_header(pkpy_Str__data(self)[i], false); int size = c11__u8_header(pkpy_Str__data(self)[i], false);

View File

@ -1,6 +1,7 @@
#include "pocketpy/compiler/lexer.hpp" #include "pocketpy/compiler/lexer.hpp"
#include "pocketpy/common/config.h" #include "pocketpy/common/config.h"
#include "pocketpy/common/str.h" #include "pocketpy/common/str.h"
#include "pocketpy/common/smallmap.h"
#include <cstdarg> #include <cstdarg>
@ -603,29 +604,37 @@ Error* Lexer::precompile(Str* out) noexcept{
ss << "pkpy:" PK_VERSION << '\n'; // L1: version string ss << "pkpy:" PK_VERSION << '\n'; // L1: version string
ss << (int)src->mode << '\n'; // L2: mode ss << (int)src->mode << '\n'; // L2: mode
small_map<std::string_view, int> token_indices; c11_smallmap_s2i token_indices;
c11_smallmap_s2i__ctor(&token_indices);
for(auto token: nexts) { for(auto token: nexts) {
if(is_raw_string_used(token.type)) { if(is_raw_string_used(token.type)) {
if(!token_indices.contains(token.sv())) { c11_string token_sv = {token.start, token.length};
token_indices.insert(token.sv(), 0); if(!c11_smallmap_s2i__contains(&token_indices, token_sv)) {
c11_smallmap_s2i__set(&token_indices, token_sv, 0);
// assert no '\n' in token.sv() // assert no '\n' in token.sv()
for(char c: token.sv()) for(char c: token.sv())
assert(c != '\n'); assert(c != '\n');
} }
} }
} }
ss << "=" << (int)token_indices.size() << '\n'; // L3: raw string count ss << "=" << (int)token_indices.count << '\n'; // L3: raw string count
int index = 0; int index = 0;
for(auto& kv: token_indices) { for(int i=0; i<token_indices.count; i++){
ss << kv.first << '\n'; // L4: raw strings c11_smallmap_entry_s2i* kv = c11__at(c11_smallmap_entry_s2i, &token_indices, i);
kv.second = index++; ss << kv->key << '\n'; // L4: raw strings
kv->value = index++;
} }
ss << "=" << (int)nexts.size() << '\n'; // L5: token count ss << "=" << (int)nexts.size() << '\n'; // L5: token count
for(int i = 0; i < nexts.size(); i++) { for(int i = 0; i < nexts.size(); i++) {
const Token& token = nexts[i]; const Token& token = nexts[i];
ss << (int)token.type << ','; ss << (int)token.type << ',';
if(is_raw_string_used(token.type)) { ss << token_indices[token.sv()] << ','; } if(is_raw_string_used(token.type)) {
int index = c11_smallmap_s2i__get(&token_indices, {token.start, token.length}, -1);
assert(index >= 0);
ss << index << ',';
}
if(i > 0 && nexts[i - 1].line == token.line) if(i > 0 && nexts[i - 1].line == token.line)
ss << ','; ss << ',';
else else
@ -652,6 +661,7 @@ Error* Lexer::precompile(Str* out) noexcept{
token.value); token.value);
} }
*out = ss.str(); *out = ss.str();
c11_smallmap_s2i__dtor(&token_indices);
return NULL; return NULL;
} }