support parse for from <module> import *

This commit is contained in:
Lyon 2023-09-24 20:57:01 +08:00
parent d5e0bd40a7
commit 5e8bfbf2c2
6 changed files with 117 additions and 70 deletions

View File

@ -12,7 +12,7 @@
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=vm.keyword_2"
"--gtest_filter=except.raise_type"
"--gtest_filter=parser.import_p"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -189,6 +189,7 @@ static enum StmtType Lexer_matchStmtType(char* right) {
pika_bool bSlice = pika_false;
pika_bool bDict = pika_false;
pika_bool bImport = pika_false;
pika_bool bInhert = pika_false;
pika_bool bChain = pika_false;
Cursor_forEach(cs, sTopStmt) {
Cursor_iterStart(&cs);
@ -197,6 +198,10 @@ static enum StmtType Lexer_matchStmtType(char* right) {
bImport = pika_true;
goto __iter_continue;
}
if (strEqu(cs.token1.pyload, "@inh ")) {
bInhert = pika_true;
goto __iter_continue;
}
if (strEqu(cs.token2.pyload, "[")) {
/* (symble | iteral | <]> | <)>) + <[> */
if (TOKEN_symbol == cs.token1.type ||
@ -268,6 +273,10 @@ static enum StmtType Lexer_matchStmtType(char* right) {
__iter_continue:
Cursor_iterEnd(&cs);
}
if (bInhert) {
eStmtType = STMT_inhert;
goto __exit;
}
if (bImport) {
eStmtType = STMT_import;
goto __exit;
@ -751,6 +760,17 @@ char* Lexer_getTokenStream(Args* outBuffs, char* sStmt) {
continue;
}
}
/* @inh */
if ('@' == c0) {
if (('i' == c1) && ('n' == c2) && ('h' == c3) && (' ' == c4)) {
aTokenStream =
Lexer_setSymbel(aTokenStream, sStmt, i, &iSymbolStartIndex);
aTokenStream =
Lexer_setToken(aTokenStream, TOKEN_operator, "@inh ");
i = i + 3;
continue;
}
}
__after_match_string_operator:
/* skip spaces */
if (' ' == c0) {
@ -1757,6 +1777,7 @@ AST* AST_parseStmt(AST* ast, char* sStmt) {
char* sLeft = NULL;
char* sRight = NULL;
char* sImport = NULL;
char* sInhert = NULL;
PIKA_RES eResult = PIKA_RES_OK;
sRight = sStmt;
@ -1930,6 +1951,12 @@ AST* AST_parseStmt(AST* ast, char* sStmt) {
AST_setNodeAttr(ast, (char*)"import", sImport);
goto __exit;
}
/* solve @inh stmt (from <module> import *) */
if (STMT_inhert == eStmtType) {
sInhert = strsGetLastToken(&buffs, sRight, ' ');
AST_setNodeAttr(ast, (char*)"inhert", sInhert);
goto __exit;
}
/* solve str/bytes stmt */
if (STMT_string == eStmtType || STMT_bytes == eStmtType) {
sStr = strsCopy(&buffs, sRight);
@ -2591,6 +2618,14 @@ static char* Suger_from_import_as(Args* buffs_p, char* sLine) {
goto __exit;
}
/* solve from module import * */
if (strEqu(sClass, "*")) {
sLineOut =
strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "@inh %s\n", sModule);
sLineOut = strsCopy(buffs_p, sLineOut);
goto __exit;
}
while (1) {
char* sClassItem = Cursor_popToken(&buffs, &sClass, ",");
if (sClassItem[0] == '\0') {
@ -3124,6 +3159,7 @@ char* AST_genAsm(AST* oAST, AST* subAst, Args* outBuffs, char* sPikaAsm) {
{.ins = "BYT", .type = VAL_DYNAMIC, .ast = "bytes"},
{.ins = "NUM", .type = VAL_DYNAMIC, .ast = "num"},
{.ins = "IMP", .type = VAL_DYNAMIC, .ast = "import"},
{.ins = "INH", .type = VAL_DYNAMIC, .ast = "inhert"},
{.ins = "REF", .type = VAL_DYNAMIC, .ast = "ref"},
{.ins = "STR", .type = VAL_DYNAMIC, .ast = "string"},
{.ins = "SLC", .type = VAL_NONEVAL, .ast = "slice"},

View File

@ -54,6 +54,7 @@ enum StmtType {
STMT_method,
STMT_chain,
STMT_operator,
STMT_inhert,
STMT_import,
STMT_list,
STMT_slice,

View File

@ -3374,6 +3374,13 @@ static Arg* VM_instruction_handler_IMP(PikaObj* self,
return NULL;
}
static Arg* VM_instruction_handler_INH(PikaObj* self,
PikaVMFrame* vm,
char* data,
Arg* arg_ret_reg) {
return NULL;
}
#if PIKA_INSTRUCT_EXTENSION_ENABLE
const VMInstructionSet VM_default_instruction_set = {
#define __INS_OPCODE

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 12
#define PIKA_VERSION_MICRO 6
#define PIKA_EDIT_TIME "2023/09/24 16:00:18"
#define PIKA_EDIT_TIME "2023/09/24 20:56:53"

View File

@ -30,73 +30,76 @@
//! just append ins to the end, insert ins would brake the pre-compiled
//! bytecode.
/* clang-format off */
/* none */
def_ins(NON)
/* get referance */
def_ins(REF)
/* run function */
def_ins(RUN)
/* string */
def_ins(STR)
/* output */
def_ins(OUT)
/* number */
def_ins(NUM)
/* jump */
def_ins(JMP)
/* jump qual zero */
def_ins(JEZ)
/* operator */
def_ins(OPT)
/* define */
def_ins(DEF)
/* return */
def_ins(RET)
/* not equal */
def_ins(NEL)
/* delete */
def_ins(DEL)
/* exist */
def_ins(EST)
/* break */
def_ins(BRK)
/* continue */
def_ins(CTN)
/* global */
def_ins(GLB)
/* run as */
def_ins(RAS)
/* new */
def_ins(NEW)
/* class */
def_ins(CLS)
/* bytes */
def_ins(BYT)
/* list */
def_ins(LST)
/* import */
def_ins(IMP)
/* try */
def_ins(TRY)
/* not try */
def_ins(NTR)
/* raise */
def_ins(RIS)
/* get error code */
def_ins(GER)
/* set error code */
def_ins(SER)
/* dict */
def_ins(DCT)
/* slice */
def_ins(SLC)
/* assert */
def_ins(ASS)
/* expect */
def_ins(EXP)
/* jump no zero */
def_ins(JNZ)
/* tuple */
def_ins(TPL)
/* no list */
def_ins(NLS)
/* get referance */
def_ins(REF)
/* run function */
def_ins(RUN)
/* string */
def_ins(STR)
/* output */
def_ins(OUT)
/* number */
def_ins(NUM)
/* jump */
def_ins(JMP)
/* jump qual zero */
def_ins(JEZ)
/* operator */
def_ins(OPT)
/* define */
def_ins(DEF)
/* return */
def_ins(RET)
/* not equal */
def_ins(NEL)
/* delete */
def_ins(DEL)
/* exist */
def_ins(EST)
/* break */
def_ins(BRK)
/* continue */
def_ins(CTN)
/* global */
def_ins(GLB)
/* run as */
def_ins(RAS)
/* new */
def_ins(NEW)
/* class */
def_ins(CLS)
/* bytes */
def_ins(BYT)
/* list */
def_ins(LST)
/* import */
def_ins(IMP)
/* try */
def_ins(TRY)
/* not try */
def_ins(NTR)
/* raise */
def_ins(RIS)
/* get error code */
def_ins(GER)
/* set error code */
def_ins(SER)
/* dict */
def_ins(DCT)
/* slice */
def_ins(SLC)
/* assert */
def_ins(ASS)
/* expect */
def_ins(EXP)
/* jump no zero */
def_ins(JNZ)
/* tuple */
def_ins(TPL)
/* no list */
def_ins(NLS)
/* inhert */
def_ins(INH)