BLUELOVETH 2 yıl önce
ebeveyn
işleme
db321abdae
6 değiştirilmiş dosya ile 27 ekleme ve 10 silme
  1. 2 2
      dylib/xmake.lua
  2. 1 0
      include/pocketpy/opcodes.h
  3. 5 0
      src/ceval.cpp
  4. 15 8
      src/expr.cpp
  5. 0 0
      src2/pocketpy_c.c
  6. 4 0
      tests/25_rawstring.py

+ 2 - 2
dylib/xmake.lua

@@ -1,6 +1,6 @@
 add_rules("mode.debug", "mode.release")
 
-set_languages("c11", "c++17")
+set_languages("c11")
 
 root_dir = "../"
 
@@ -10,7 +10,7 @@ add_includedirs(root_dir .. "include")
 -- Define the shared library target for pocketpy
 target("pocketpy")
     set_kind("shared")
-    add_files(root_dir .. "src2/pocketpy_c.cpp")
+    add_files(root_dir .. "src2/pocketpy_c.c")
 
 -- Define the shared library target
 target("test")

+ 1 - 0
include/pocketpy/opcodes.h

@@ -88,6 +88,7 @@ OPCODE(LOOP_CONTINUE)
 OPCODE(LOOP_BREAK)
 OPCODE(GOTO)
 /**************************/
+OPCODE(EVAL)
 OPCODE(CALL)
 OPCODE(CALL_TP)
 OPCODE(RETURN_VALUE)

+ 5 - 0
src/ceval.cpp

@@ -499,6 +499,11 @@ __NEXT_STEP:;
         frame->jump_abs_break(index);
     } DISPATCH();
     /*****************************************/
+    TARGET(EVAL){
+        DEF_SNAME(eval);
+        _0 = builtins->attr(eval);
+        TOP() = call(_0, TOP());
+    } DISPATCH();
     TARGET(CALL)
         _0 = vectorcall(
             byte.arg & 0xFFFF,          // ARGC

+ 15 - 8
src/expr.cpp

@@ -344,15 +344,22 @@ namespace pkpy{
 
 
     void FStringExpr::_load_simple_expr(CodeEmitContext* ctx, Str expr){
-        // TODO: pre compile this into a function
-        int dot = expr.index(".");
-        if(dot < 0){
-            ctx->emit(OP_LOAD_NAME, StrName(expr.sv()).index, line);
+        // name or name.name
+        std::regex pattern(R"(^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*){0,1}$)");
+        if(std::regex_match(expr.str(), pattern)){
+            int dot = expr.index(".");
+            if(dot < 0){
+                ctx->emit(OP_LOAD_NAME, StrName(expr.sv()).index, line);
+            }else{
+                StrName name(expr.substr(0, dot).sv());
+                StrName attr(expr.substr(dot+1).sv());
+                ctx->emit(OP_LOAD_NAME, name.index, line);
+                ctx->emit(OP_LOAD_ATTR, attr.index, line);
+            }
         }else{
-            StrName name(expr.substr(0, dot).sv());
-            StrName attr(expr.substr(dot+1).sv());
-            ctx->emit(OP_LOAD_NAME, name.index, line);
-            ctx->emit(OP_LOAD_ATTR, attr.index, line);
+            int index = ctx->add_const(py_var(ctx->vm, expr));
+            ctx->emit(OP_LOAD_CONST, index, line);
+            ctx->emit(OP_EVAL, BC_NOARG, line);
         }
     }
 

+ 0 - 0
src2/pocketpy_c.cpp → src2/pocketpy_c.c


+ 4 - 0
tests/25_rawstring.py

@@ -57,3 +57,7 @@ assert f'{obj.b:10}' == '123       '
 assert f'{obj.b:>10}' == '       123'
 assert f'{obj.b:1}' == '123'
 assert f'{obj.b:10s}' == '123       '
+
+a = [(1,2), 3, obj]
+assert f'{a[0][1]}' == '2'
+assert f'abc{a[-1].b:10}==={1234}' == 'abc123       ===1234'