Browse Source

remove `JSON_MODE` and use `EVAL_MODE`

blueloveTH 1 year ago
parent
commit
f3a4473162

+ 2 - 2
include/pocketpy/objects/sourcedata.h

@@ -12,7 +12,7 @@ extern "C" {
 
 struct pk_SourceData {
     RefCounted rc;
-    enum CompileMode mode;
+    enum py_CompileMode mode;
     bool is_precompiled;
     bool is_dynamic;    // for exec() and eval()
 
@@ -25,7 +25,7 @@ struct 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);
 c11_string* pk_SourceData__snapshot(const struct pk_SourceData *self, int lineno, const char *cursor, const char *name);
 

+ 2 - 2
include/pocketpy/pocketpy.h

@@ -44,7 +44,7 @@ enum BindType {
     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 *************/
 void py_initialize();
@@ -55,7 +55,7 @@ bool py_exec(const char* source);
 /// Eval a simple expression.
 /// The result will be set to `py_retval()`.
 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 *************/
 void py_newint(py_Ref, py_i64);

+ 2 - 2
src/common/sourcedata.c

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

+ 4 - 17
src/compiler/compiler.c

@@ -22,7 +22,6 @@ typedef struct ExprVt {
     bool (*emit_istore)(Expr*, Ctx*);
     /* reflections */
     bool is_literal;
-    bool is_json_object;
     bool is_name;     // NameExpr
     bool is_tuple;    // TupleExpr
     bool is_attrib;   // AttribExpr
@@ -312,8 +311,7 @@ void LiteralExpr__emit_(Expr* self_, Ctx* ctx) {
 
 LiteralExpr* LiteralExpr__new(int line, const TokenValue* value) {
     const static ExprVt Vt = {.emit_ = LiteralExpr__emit_,
-                              .is_literal = true,
-                              .is_json_object = true};
+                              .is_literal = true};
     static_assert_expr_size(LiteralExpr);
     LiteralExpr* self = PoolExpr_alloc();
     self->vt = &Vt;
@@ -342,7 +340,7 @@ void Literal0Expr__emit_(Expr* self_, Ctx* ctx) {
 }
 
 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);
     Literal0Expr* self = PoolExpr_alloc();
     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) {
     const static ExprVt ListExprVt = {.dtor = SequenceExpr__dtor,
-                                      .emit_ = SequenceExpr__emit_,
-                                      .is_json_object = true};
+                                      .emit_ = SequenceExpr__emit_};
     return SequenceExpr__new(line, &ListExprVt, count, OP_BUILD_LIST);
 }
 
 SequenceExpr* DictExpr__new(int line, int count) {
     const static ExprVt DictExprVt = {.dtor = SequenceExpr__dtor,
-                                      .emit_ = SequenceExpr__emit_,
-                                      .is_json_object = true};
+                                      .emit_ = SequenceExpr__emit_};
     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);
         check(pop_context(self));
         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)) {

+ 0 - 13
src/compiler/lexer.c

@@ -241,19 +241,6 @@ static Error* eat_name(pk_Lexer* self){
     if(length == 0) return SyntaxError("@id contains invalid char");
     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;
     int KW_COUNT = TK__COUNT__ - TK_FALSE;
     #define less(a, b) (c11_sv__cmp2(b, a) > 0)

+ 2 - 2
src/public/vm.c

@@ -157,7 +157,7 @@ static void disassemble(CodeObject* co) {
 }
 
 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;
     pk_SourceData_ src = pk_SourceData__rcnew(source, filename, mode, false);
     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_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);
 }