diff --git a/dylib/xmake.lua b/dylib/xmake.lua index f761114e..dafc3a18 100644 --- a/dylib/xmake.lua +++ b/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") diff --git a/include/pocketpy/opcodes.h b/include/pocketpy/opcodes.h index c04d7c96..0de0e080 100644 --- a/include/pocketpy/opcodes.h +++ b/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) diff --git a/src/ceval.cpp b/src/ceval.cpp index 5657fb33..9fce2eb3 100644 --- a/src/ceval.cpp +++ b/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 diff --git a/src/expr.cpp b/src/expr.cpp index 029089fe..301a1658 100644 --- a/src/expr.cpp +++ b/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); } } diff --git a/src2/pocketpy_c.cpp b/src2/pocketpy_c.c similarity index 100% rename from src2/pocketpy_c.cpp rename to src2/pocketpy_c.c diff --git a/tests/25_rawstring.py b/tests/25_rawstring.py index 6c889892..8b5d526c 100644 --- a/tests/25_rawstring.py +++ b/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' \ No newline at end of file