remove JSON_MODE and use EVAL_MODE

This commit is contained in:
blueloveTH 2024-07-09 23:44:38 +08:00
parent 554363e66f
commit f3a4473162
6 changed files with 12 additions and 38 deletions

View File

@ -12,7 +12,7 @@ extern "C" {
struct pk_SourceData { struct pk_SourceData {
RefCounted rc; RefCounted rc;
enum CompileMode mode; enum py_CompileMode mode;
bool is_precompiled; bool is_precompiled;
bool is_dynamic; // for exec() and eval() bool is_dynamic; // for exec() and eval()
@ -25,7 +25,7 @@ struct pk_SourceData {
typedef struct pk_SourceData* pk_SourceData_; typedef struct pk_SourceData* pk_SourceData_;
pk_SourceData_ pk_SourceData__rcnew(const char* source, const char* filename, enum CompileMode mode, bool is_dynamic); pk_SourceData_ pk_SourceData__rcnew(const char* source, const char* filename, enum py_CompileMode mode, bool is_dynamic);
bool pk_SourceData__get_line(const struct pk_SourceData* self, int lineno, const char** st, const char** ed); bool pk_SourceData__get_line(const struct pk_SourceData* self, int lineno, const char** st, const char** ed);
c11_string* pk_SourceData__snapshot(const struct pk_SourceData *self, int lineno, const char *cursor, const char *name); c11_string* pk_SourceData__snapshot(const struct pk_SourceData *self, int lineno, const char *cursor, const char *name);

View File

@ -44,7 +44,7 @@ enum BindType {
BindType_CLASSMETHOD, BindType_CLASSMETHOD,
}; };
enum CompileMode { EXEC_MODE, EVAL_MODE, REPL_MODE, JSON_MODE, CELL_MODE }; enum py_CompileMode { EXEC_MODE, EVAL_MODE, REPL_MODE, CELL_MODE };
/************* Global VMs *************/ /************* Global VMs *************/
void py_initialize(); void py_initialize();
@ -55,7 +55,7 @@ bool py_exec(const char* source);
/// Eval a simple expression. /// Eval a simple expression.
/// The result will be set to `py_retval()`. /// The result will be set to `py_retval()`.
bool py_eval(const char* source); bool py_eval(const char* source);
bool py_exec2(const char* source, const char* filename, enum CompileMode mode); bool py_exec2(const char* source, const char* filename, enum py_CompileMode mode);
/************* Values Creation *************/ /************* Values Creation *************/
void py_newint(py_Ref, py_i64); void py_newint(py_Ref, py_i64);

View File

@ -7,7 +7,7 @@
static void pk_SourceData__ctor(struct pk_SourceData* self, static void pk_SourceData__ctor(struct pk_SourceData* self,
const char* source, const char* source,
const char* filename, const char* filename,
enum CompileMode mode, enum py_CompileMode mode,
bool is_dynamic) { bool is_dynamic) {
self->filename = c11_string__new(filename); self->filename = c11_string__new(filename);
self->mode = mode; self->mode = mode;
@ -45,7 +45,7 @@ static void pk_SourceData__dtor(struct pk_SourceData* self) {
pk_SourceData_ pk_SourceData__rcnew(const char* source, pk_SourceData_ pk_SourceData__rcnew(const char* source,
const char* filename, const char* filename,
enum CompileMode mode, enum py_CompileMode mode,
bool is_dynamic) { bool is_dynamic) {
pk_SourceData_ self = malloc(sizeof(struct pk_SourceData)); pk_SourceData_ self = malloc(sizeof(struct pk_SourceData));
pk_SourceData__ctor(self, source, filename, mode, is_dynamic); pk_SourceData__ctor(self, source, filename, mode, is_dynamic);

View File

@ -22,7 +22,6 @@ typedef struct ExprVt {
bool (*emit_istore)(Expr*, Ctx*); bool (*emit_istore)(Expr*, Ctx*);
/* reflections */ /* reflections */
bool is_literal; bool is_literal;
bool is_json_object;
bool is_name; // NameExpr bool is_name; // NameExpr
bool is_tuple; // TupleExpr bool is_tuple; // TupleExpr
bool is_attrib; // AttribExpr bool is_attrib; // AttribExpr
@ -312,8 +311,7 @@ void LiteralExpr__emit_(Expr* self_, Ctx* ctx) {
LiteralExpr* LiteralExpr__new(int line, const TokenValue* value) { LiteralExpr* LiteralExpr__new(int line, const TokenValue* value) {
const static ExprVt Vt = {.emit_ = LiteralExpr__emit_, const static ExprVt Vt = {.emit_ = LiteralExpr__emit_,
.is_literal = true, .is_literal = true};
.is_json_object = true};
static_assert_expr_size(LiteralExpr); static_assert_expr_size(LiteralExpr);
LiteralExpr* self = PoolExpr_alloc(); LiteralExpr* self = PoolExpr_alloc();
self->vt = &Vt; self->vt = &Vt;
@ -342,7 +340,7 @@ void Literal0Expr__emit_(Expr* self_, Ctx* ctx) {
} }
Literal0Expr* Literal0Expr__new(int line, TokenIndex token) { Literal0Expr* Literal0Expr__new(int line, TokenIndex token) {
const static ExprVt Vt = {.emit_ = Literal0Expr__emit_, .is_json_object = true}; const static ExprVt Vt = {.emit_ = Literal0Expr__emit_};
static_assert_expr_size(Literal0Expr); static_assert_expr_size(Literal0Expr);
Literal0Expr* self = PoolExpr_alloc(); Literal0Expr* self = PoolExpr_alloc();
self->vt = &Vt; self->vt = &Vt;
@ -480,15 +478,13 @@ static SequenceExpr* SequenceExpr__new(int line, const ExprVt* vt, int count, Op
SequenceExpr* ListExpr__new(int line, int count) { SequenceExpr* ListExpr__new(int line, int count) {
const static ExprVt ListExprVt = {.dtor = SequenceExpr__dtor, const static ExprVt ListExprVt = {.dtor = SequenceExpr__dtor,
.emit_ = SequenceExpr__emit_, .emit_ = SequenceExpr__emit_};
.is_json_object = true};
return SequenceExpr__new(line, &ListExprVt, count, OP_BUILD_LIST); return SequenceExpr__new(line, &ListExprVt, count, OP_BUILD_LIST);
} }
SequenceExpr* DictExpr__new(int line, int count) { SequenceExpr* DictExpr__new(int line, int count) {
const static ExprVt DictExprVt = {.dtor = SequenceExpr__dtor, const static ExprVt DictExprVt = {.dtor = SequenceExpr__dtor,
.emit_ = SequenceExpr__emit_, .emit_ = SequenceExpr__emit_};
.is_json_object = true};
return SequenceExpr__new(line, &DictExprVt, count, OP_BUILD_DICT); return SequenceExpr__new(line, &DictExprVt, count, OP_BUILD_DICT);
} }
@ -2639,15 +2635,6 @@ Error* Compiler__compile(Compiler* self, CodeObject* out) {
Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE); Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
check(pop_context(self)); check(pop_context(self));
return NULL; return NULL;
} else if(mode() == JSON_MODE) {
check(EXPR(self));
Expr* e = Ctx__s_popx(ctx());
if(!e->vt->is_json_object) return SyntaxError("expect a JSON object, literal or array");
consume(TK_EOF);
vtemit_(e, ctx());
Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
check(pop_context(self));
return NULL;
} }
while(!match(TK_EOF)) { while(!match(TK_EOF)) {

View File

@ -241,19 +241,6 @@ static Error* eat_name(pk_Lexer* self){
if(length == 0) return SyntaxError("@id contains invalid char"); if(length == 0) return SyntaxError("@id contains invalid char");
c11_sv name = {self->token_start, length}; c11_sv name = {self->token_start, length};
if(self->src->mode == JSON_MODE) {
if(c11__sveq2(name, "true")) {
add_token(self, TK_TRUE);
} else if(c11__sveq2(name, "false")) {
add_token(self, TK_FALSE);
} else if(c11__sveq2(name, "null")) {
add_token(self, TK_NONE);
} else {
return SyntaxError("invalid JSON token");
}
return NULL;
}
const char** KW_BEGIN = pk_TokenSymbols + TK_FALSE; const char** KW_BEGIN = pk_TokenSymbols + TK_FALSE;
int KW_COUNT = TK__COUNT__ - TK_FALSE; int KW_COUNT = TK__COUNT__ - TK_FALSE;
#define less(a, b) (c11_sv__cmp2(b, a) > 0) #define less(a, b) (c11_sv__cmp2(b, a) > 0)

View File

@ -157,7 +157,7 @@ static void disassemble(CodeObject* co) {
} }
static bool static bool
pk_VM__exec(pk_VM* vm, const char* source, const char* filename, enum CompileMode mode) { pk_VM__exec(pk_VM* vm, const char* source, const char* filename, enum py_CompileMode mode) {
CodeObject co; CodeObject co;
pk_SourceData_ src = pk_SourceData__rcnew(source, filename, mode, false); pk_SourceData_ src = pk_SourceData__rcnew(source, filename, mode, false);
Error* err = pk_compile(src, &co); Error* err = pk_compile(src, &co);
@ -182,7 +182,7 @@ bool py_exec(const char* source) { return pk_VM__exec(pk_current_vm, source, "<e
bool py_eval(const char* source) { return pk_VM__exec(pk_current_vm, source, "<eval>", EVAL_MODE); } bool py_eval(const char* source) { return pk_VM__exec(pk_current_vm, source, "<eval>", EVAL_MODE); }
bool py_exec2(const char* source, const char* filename, enum CompileMode mode) { bool py_exec2(const char* source, const char* filename, enum py_CompileMode mode) {
return pk_VM__exec(pk_current_vm, source, filename, mode); return pk_VM__exec(pk_current_vm, source, filename, mode);
} }