mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-08 20:50:16 +00:00
make it compile
This commit is contained in:
parent
8e106f5227
commit
c36d429584
4
.gitignore
vendored
4
.gitignore
vendored
@ -30,4 +30,6 @@ pocketpy.dSYM
|
||||
libpocketpy.dylib.dSYM/
|
||||
main.dSYM/
|
||||
|
||||
docs/references.md
|
||||
docs/references.md
|
||||
|
||||
.xmake
|
||||
|
||||
@ -15,7 +15,7 @@ typedef struct {
|
||||
} pkpy_Rcptr_header;
|
||||
|
||||
void pkpy_Rcptr__ctor(void *self);
|
||||
void pkpy_Rcptr__ctor_withd(void *self, void (*dtor)(void *));
|
||||
void pkpy_Rcptr__ctor_withd(void *self, void *dtor);
|
||||
void pkpy_Rcptr__ref(void *self);
|
||||
void pkpy_Rcptr__unref(void *self);
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ void pkpy_SStream__ctor(pkpy_SStream* self);
|
||||
void pkpy_SStream__dtor(pkpy_SStream* self);
|
||||
void pkpy_SStream__append_cstr(pkpy_SStream* self, const char* str);
|
||||
void pkpy_SStream__append_cstrn(pkpy_SStream* self, const char* str, int n);
|
||||
void pkpy_SStream__append_Str(pkpy_SStream* self, pkpy_Str* str);
|
||||
void pkpy_SStream__append_Str(pkpy_SStream* self, const pkpy_Str* str);
|
||||
void pkpy_SStream__append_char(pkpy_SStream* self, char c);
|
||||
void pkpy_SStream__append_int(pkpy_SStream* self, int i);
|
||||
void pkpy_SStream__append_int64(pkpy_SStream* self, int64_t i);
|
||||
|
||||
@ -13,6 +13,10 @@ typedef struct c11_string{
|
||||
int size;
|
||||
} c11_string;
|
||||
|
||||
#ifndef isascii
|
||||
#define isascii(c) ((unsigned)(c) < 0x80)
|
||||
#endif
|
||||
|
||||
typedef struct pkpy_Str{
|
||||
int size;
|
||||
bool is_ascii;
|
||||
|
||||
@ -28,13 +28,8 @@ c11_vector c11_vector__copy(const c11_vector* self);
|
||||
void* c11_vector__at(c11_vector* self, int index);
|
||||
void c11_vector__reserve(c11_vector* self, int capacity);
|
||||
|
||||
inline int c11_vector__size(c11_vector* self) {
|
||||
return self->count;
|
||||
}
|
||||
|
||||
inline void* c11_vector__data(c11_vector* self) {
|
||||
return self->data;
|
||||
}
|
||||
#define c11_vector__size(self) ((self)->count)
|
||||
#define c11_vector__data(self) ((self)->data)
|
||||
|
||||
#define c11__getitem(T, self, index) ((T*)(self)->data)[index]
|
||||
#define c11__setitem(T, self, index, value) ((T*)(self)->data)[index] = value;
|
||||
|
||||
@ -41,7 +41,7 @@ struct Compiler {
|
||||
#if PK_DEBUG_COMPILER
|
||||
if(__i>=0 && __i<lexer.nexts.size()){
|
||||
printf("%s:%d %s %s\n",
|
||||
lexer.src->filename.c_str(),
|
||||
lexer.src.filename().c_str(),
|
||||
curr().line,
|
||||
TK_STR(curr().type),
|
||||
curr().str().escape().c_str()
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include "pocketpy/objects/error.hpp"
|
||||
|
||||
#include <variant>
|
||||
#include <cstdarg>
|
||||
|
||||
namespace pkpy {
|
||||
|
||||
|
||||
@ -14,13 +14,13 @@ void pkpy_Rcptr__ctor(void *self) {
|
||||
pkpy_Rcptr__ctor_withd(self, NULL);
|
||||
}
|
||||
|
||||
void pkpy_Rcptr__ctor_withd(void *self_, void (*dtor)(void *)) {
|
||||
void pkpy_Rcptr__ctor_withd(void *self_, void *dtor) {
|
||||
pkpy_Rcptr_header *self = self_;
|
||||
#if PK_DEBUG_DATASTRUCTURE
|
||||
self->magic = RCPTR_MAGIC;
|
||||
#endif
|
||||
self->ref_c = 1;
|
||||
self->dtor = dtor;
|
||||
self->dtor = (void (*)(void *))dtor;
|
||||
}
|
||||
|
||||
void pkpy_Rcptr__ref(void *self_) {
|
||||
|
||||
@ -29,8 +29,8 @@ void pkpy_SourceData__ctor(struct pkpy_SourceData* self,
|
||||
}
|
||||
pkpy_Str__take_buf(&self->source, buf, len);
|
||||
|
||||
self->is_precompiled = (strncmp(pkpy_Str_data(&self->source), "pkpy:", 5) == 0);
|
||||
c11_vector__push_back(const char*, &self->line_starts, pkpy_Str_data(&self->source));
|
||||
self->is_precompiled = (strncmp(pkpy_Str__data(&self->source), "pkpy:", 5) == 0);
|
||||
c11_vector__push_back(const char*, &self->line_starts, pkpy_Str__data(&self->source));
|
||||
}
|
||||
|
||||
void pkpy_SourceData__dtor(struct pkpy_SourceData* self) {
|
||||
@ -51,6 +51,7 @@ bool pkpy_SourceData__get_line(const struct pkpy_SourceData* self, int lineno, c
|
||||
i++;
|
||||
*st = _start;
|
||||
*ed = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
pkpy_Str pkpy_SourceData__snapshot(const struct pkpy_SourceData* self, int lineno, const char* cursor, const char* name) {
|
||||
@ -63,14 +64,14 @@ pkpy_Str pkpy_SourceData__snapshot(const struct pkpy_SourceData* self, int linen
|
||||
|
||||
if(name) {
|
||||
pkpy_SStream__append_cstr(&ss, ", in ");
|
||||
pkpy_SStream__append_Str(&ss, &name);
|
||||
pkpy_SStream__append_cstr(&ss, name);
|
||||
}
|
||||
|
||||
if(!self->is_precompiled) {
|
||||
pkpy_SStream__append_char(&ss, '\n');
|
||||
const char *st = NULL, *ed;
|
||||
if(pkpy_SourceData__get_line(self, lineno, &st, &ed)) {
|
||||
while(st < ed && isblank(st))
|
||||
while(st < ed && isblank(*st))
|
||||
++st;
|
||||
if(st < ed) {
|
||||
pkpy_SStream__append_cstr(&ss, " ");
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#include "pocketpy/common/sstream.h"
|
||||
#include <stdio.h>
|
||||
#include "sstream.h"
|
||||
|
||||
void pkpy_SStream__ctor(pkpy_SStream* self) {
|
||||
c11_vector__ctor(&self->data, sizeof(char));
|
||||
@ -11,18 +10,18 @@ void pkpy_SStream__dtor(pkpy_SStream* self) {
|
||||
}
|
||||
|
||||
void pkpy_SStream__append_cstr(pkpy_SStream* self, const char* str) {
|
||||
for (size_t i = 0; str[i] != '\0'; i++) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
c11_vector__push_back(char, &self->data, str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void pkpy_SStream__append_cstrn(pkpy_SStream* self, const char* str, int n) {
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
c11_vector__push_back(char, &self->data, str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void pkpy_SStream__append_Str(pkpy_SStream* self, pkpy_Str* str) {
|
||||
void pkpy_SStream__append_Str(pkpy_SStream* self, const pkpy_Str* str) {
|
||||
pkpy_SStream__append_cstr(self, pkpy_Str__data(str));
|
||||
}
|
||||
|
||||
@ -31,13 +30,13 @@ void pkpy_SStream__append_char(pkpy_SStream* self, char c) {
|
||||
}
|
||||
|
||||
void pkpy_SStream__append_int(pkpy_SStream* self, int i) {
|
||||
char str[11]; // 10 digits + null terminator
|
||||
char str[12]; // sign + 10 digits + null terminator
|
||||
sprintf(str, "%d", i);
|
||||
pkpy_SStream__append_cstr(self, str);
|
||||
}
|
||||
|
||||
void pkpy_SStream__append_int64(pkpy_SStream* self, int64_t i) {
|
||||
char str[21]; // 20 digits + null terminator
|
||||
char str[23]; // sign + 21 digits + null terminator
|
||||
sprintf(str, "%lld", i);
|
||||
pkpy_SStream__append_cstr(self, str);
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ NameScope Compiler::name_scope() const noexcept{
|
||||
}
|
||||
|
||||
CodeObject_ Compiler::push_global_context() noexcept{
|
||||
CodeObject_ co = std::make_shared<CodeObject>(lexer.src, lexer.src->filename);
|
||||
CodeObject_ co = std::make_shared<CodeObject>(lexer.src, lexer.src.filename());
|
||||
co->start_line = __i == 0 ? 1 : prev().line;
|
||||
contexts.push_back(CodeEmitContext(vm, co, contexts.size()));
|
||||
return co;
|
||||
@ -1293,8 +1293,8 @@ Error* Compiler::compile(CodeObject_* out) noexcept{
|
||||
Error* err;
|
||||
check(lexer.run());
|
||||
|
||||
// if(lexer.src->filename[0] != '<'){
|
||||
// printf("%s\n", lexer.src->filename.c_str());
|
||||
// if(lexer.src.filename()[0] != '<'){
|
||||
// printf("%s\n", lexer.src.filename().c_str());
|
||||
// for(int i=0; i<lexer.nexts.size(); i++){
|
||||
// printf("%s: %s\n", TK_STR(tk(i).type), tk(i).str().escape().c_str());
|
||||
// }
|
||||
|
||||
@ -20,7 +20,7 @@ void LineProfiler::begin() { frames.clear(); }
|
||||
void LineProfiler::_step(int callstack_size, Frame* frame) {
|
||||
auto line_info = frame->co->lines[frame->ip()];
|
||||
if(line_info.is_virtual) return;
|
||||
std::string_view filename = frame->co->src->filename.sv();
|
||||
std::string_view filename = frame->co->src.filename().sv();
|
||||
int line = line_info.lineno;
|
||||
|
||||
if(frames.empty()) {
|
||||
@ -87,7 +87,7 @@ Str LineProfiler::stats() {
|
||||
int start_line = decl->code->start_line;
|
||||
int end_line = decl->code->end_line;
|
||||
if(start_line == -1 || end_line == -1) continue;
|
||||
std::string_view filename = decl->code->src->filename.sv();
|
||||
std::string_view filename = decl->code->src.filename().sv();
|
||||
const _LineRecord* file_records = records[filename];
|
||||
clock_t total_time = 0;
|
||||
for(int line = start_line; line <= end_line; line++) {
|
||||
|
||||
@ -1706,7 +1706,7 @@ void VM::__breakpoint() {
|
||||
SStream ss;
|
||||
Frame* frame = &frames[i]->frame;
|
||||
int lineno = frame->curr_lineno();
|
||||
ss << "File \"" << frame->co->src->filename << "\", line " << lineno;
|
||||
ss << "File \"" << frame->co->src.filename() << "\", line " << lineno;
|
||||
if(frame->_callable) {
|
||||
ss << ", in ";
|
||||
ss << frame->_callable->as<Function>().decl->code->name;
|
||||
|
||||
@ -512,13 +512,21 @@ bool pkpy_check_error(pkpy_vm* vm_handle) {
|
||||
return vm->__c.error != nullptr;
|
||||
}
|
||||
|
||||
// strdup() is in C23, make a polyfill
|
||||
static char* pkpy_strdup(const char *src) {
|
||||
int len = strlen(src);
|
||||
char *res = (char*)std::malloc(len + 1);
|
||||
strcpy(res, src);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool pkpy_clear_error(pkpy_vm* vm_handle, char** message) {
|
||||
VM* vm = (VM*)vm_handle;
|
||||
// no error
|
||||
if(vm->__c.error == nullptr) return false;
|
||||
Exception& e = vm->__c.error->as<Exception>();
|
||||
if(message != nullptr)
|
||||
*message = strdup(e.summary().c_str());
|
||||
*message = pkpy_strdup(e.summary().c_str());
|
||||
else
|
||||
std::cout << e.summary() << std::endl;
|
||||
vm->__c.error = nullptr;
|
||||
|
||||
30
xmake.lua
Normal file
30
xmake.lua
Normal file
@ -0,0 +1,30 @@
|
||||
set_project("pocketpy")
|
||||
set_basename("pocketpy")
|
||||
set_version("2.0.0")
|
||||
|
||||
set_languages("cxx17", "c11")
|
||||
set_targetdir("build")
|
||||
set_policy("build.warning", true)
|
||||
|
||||
option("pk_enable_profiler")
|
||||
set_default(false)
|
||||
add_defines("PK_ENABLE_PROFILER=1")
|
||||
|
||||
target("main")
|
||||
set_default(true)
|
||||
set_kind("binary")
|
||||
add_files("src/**.cpp", "src/**.c", "src2/*.cpp")
|
||||
add_includedirs("include/")
|
||||
|
||||
set_warnings("allextra")
|
||||
if is_mode("release") then
|
||||
set_strip("all")
|
||||
set_optimize("faster")
|
||||
add_defines("NDEBUG")
|
||||
end
|
||||
|
||||
if is_mode("debug") then
|
||||
set_optimize("none")
|
||||
set_symbols("debug")
|
||||
add_defines("DEBUG")
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user