add Bx blockDeepth for each py line

This commit is contained in:
lyon1998 2021-10-16 17:32:02 +08:00
parent eeae6b0392
commit aa5c6cebdc
4 changed files with 87 additions and 56 deletions

View File

@ -7,7 +7,7 @@ extern "C" {
}
TEST(parser, NEW) {
AST* ast = pikaParse((char*)"add(a,b)");
AST* ast = pikaParseLine((char*)"add(a,b)");
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
@ -17,35 +17,36 @@ TEST(parser, NEW) {
}
TEST(parser, add_a_b) {
AST* ast = pikaParse((char*)"add( a , b)");
AST* ast = pikaParseLine((char*)"add( a , b)");
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "1 REF a\n1 REF b\n0 RUN add\n");
EXPECT_STREQ(pikaAsm, "B0\n1 REF a\n1 REF b\n0 RUN add\n");
args_deinit(buffs);
AST_deinit(ast);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, add_a_b_c) {
AST* ast = pikaParse((char*)"d = add(add(a,b) , c)");
AST* ast = pikaParseLine((char*)"d = add(add(a,b) , c)");
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"2 REF a\n2 REF b\n1 RUN add\n1 REF c\n0 RUN add\n0 OUT d\n");
EXPECT_STREQ(
pikaAsm,
"B0\n2 REF a\n2 REF b\n1 RUN add\n1 REF c\n0 RUN add\n0 OUT d\n");
args_deinit(buffs);
AST_deinit(ast);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, method1) {
AST* ast = pikaParse((char*)"d.p = a.add(b.add(a,se.b) , pmw.c)");
AST* ast = pikaParseLine((char*)"d.p = a.add(b.add(a,se.b) , pmw.c)");
Args* buffs = New_strBuff();
char* pikaAsm = AST_toPikaAsm(ast, buffs);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"2 REF a\n2 REF se.b\n1 RUN b.add\n1 REF pmw.c\n0 RUN "
"B0\n2 REF a\n2 REF se.b\n1 RUN b.add\n1 REF pmw.c\n0 RUN "
"a.add\n0 OUT d.p\n");
args_deinit(buffs);
AST_deinit(ast);
@ -57,8 +58,9 @@ TEST(parser, method2) {
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"2 REF a\n2 REF se.b\n3 REF pp\n2 RUN diek\n1 RUN b.add\n1 "
EXPECT_STREQ(
pikaAsm,
"B0\n2 REF a\n2 REF se.b\n3 REF pp\n2 RUN diek\n1 RUN b.add\n1 "
"RUN pmw.c\n0 RUN a.add\n0 OUT d.p\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
@ -69,7 +71,7 @@ TEST(parser, str1) {
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "1 STR 2.322\n0 RUN literal\n");
EXPECT_STREQ(pikaAsm, "B0\n1 STR 2.322\n0 RUN literal\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -80,7 +82,7 @@ TEST(parser, str2) {
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"1 REF a\n2 STR 1\n1 RUN literal\n0 RUN add\n0 OUT b\n");
"B0\n1 REF a\n2 STR 1\n1 RUN literal\n0 RUN add\n0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -90,7 +92,7 @@ TEST(parser, num1) {
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "1 REF a\n1 NUM 1\n0 RUN add\n0 OUT b\n");
EXPECT_STREQ(pikaAsm, "B0\n1 REF a\n1 NUM 1\n0 RUN add\n0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -100,7 +102,7 @@ TEST(parser, add_str) {
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "1 REF a\n1 STR 1\n0 RUN add\n0 OUT b\n");
EXPECT_STREQ(pikaAsm, "B0\n1 REF a\n1 STR 1\n0 RUN add\n0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -110,7 +112,10 @@ TEST(parser, deep4) {
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "4 NUM 1\n4 NUM 2\n3 RUN add\n3 NUM 3\n2 RUN add\n2 NUM 4\n1 RUN add\n1 NUM 5\n0 RUN add\n0 OUT b\n");
EXPECT_STREQ(
pikaAsm,
"B0\n4 NUM 1\n4 NUM 2\n3 RUN add\n3 NUM 3\n2 RUN add\n2 NUM 4\n1 "
"RUN add\n1 NUM 5\n0 RUN add\n0 OUT b\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -120,7 +125,7 @@ TEST(parser, a_1) {
Args* buffs = New_strBuff();
char* pikaAsm = pikaParseToAsm(buffs, line);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm, "0 NUM 1\n0 OUT a\n");
EXPECT_STREQ(pikaAsm, "B0\n0 NUM 1\n0 OUT a\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -1,5 +1,6 @@
#include "PikaParser.h"
#include "PikaObj.h"
#include "dataQueue.h"
#include "dataQueueObj.h"
#include "dataStrs.h"
@ -123,9 +124,24 @@ exit:
return ast;
}
AST* pikaParse(char* line) {
static int32_t getPyLineBlockDeepth(char* line) {
uint32_t size = strGetSize(line);
for (int i = 0; i < line; i++) {
if (line[i] != ' ') {
uint32_t spaceNum = i;
if (0 == spaceNum % 4) {
return spaceNum / 4;
}
/* space Num is not 4N, error*/
return -1;
}
}
}
AST* pikaParseLine(char* line) {
AST* ast = New_queueObj();
Args* buffs = New_strBuff();
obj_setInt(ast, "blockDeepth", getPyLineBlockDeepth(line));
char* stmt = strsGetCleanCmd(buffs, line);
ast = AST_parseStmt(ast, stmt);
goto exit;
@ -135,7 +151,7 @@ exit:
}
char* pikaParseToAsm(Args* buffs, char* line) {
AST* ast = pikaParse(line);
AST* ast = pikaParseLine(line);
char* pikaAsm = AST_toPikaAsm(ast, buffs);
AST_deinit(ast);
return pikaAsm;
@ -189,6 +205,11 @@ char* AST_appandPikaAsm(AST* ast, AST* subAst, Args* buffs, char* pikaAsm) {
char* AST_toPikaAsm(AST* ast, Args* buffs) {
Args* runBuffs = New_strBuff();
char* pikaAsm = strsCopy(runBuffs, "");
pikaAsm = strsAppend(buffs, pikaAsm, (char*)"B");
char buff[11];
pikaAsm = strsAppend(buffs, pikaAsm,
fast_itoa(buff, obj_getInt(ast, "blockDeepth")));
pikaAsm = strsAppend(buffs, pikaAsm, (char*)"\n");
obj_setInt(ast, "deepth", 0);
pikaAsm = AST_appandPikaAsm(ast, ast, runBuffs, pikaAsm);
pikaAsm = strsCopy(buffs, pikaAsm);

View File

@ -3,7 +3,7 @@
#include "dataQueueObj.h"
typedef QueueObj AST;
AST* pikaParse(char* line);
AST* pikaParseLine(char* line);
char* pikaParseToAsm(Args* buffs, char* line);
int32_t AST_deinit(AST* ast);
char* AST_toPikaAsm(AST* ast, Args* buffs);

View File

@ -61,33 +61,29 @@ static char* getMethodDeclearation(PikaObj* obj, char* methodName) {
return res;
}
int fast_atoi(char *src)
{
const char *p = src;
static const uint64_t a[][10] =
{
0, 1e0, 2e0, 3e0, 4e0, 5e0, 6e0, 7e0, 8e0, 9e0,
0, 1e1, 2e1, 3e1, 4e1, 5e1, 6e1, 7e1, 8e1, 9e1,
0, 1e2, 2e2, 3e2, 4e2, 5e2, 6e2, 7e2, 8e2, 9e2,
0, 1e3, 2e3, 3e3, 4e3, 5e3, 6e3, 7e3, 8e3, 9e3,
0, 1e4, 2e4, 3e4, 4e4, 5e4, 6e4, 7e4, 8e4, 9e4,
0, 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5,
0, 1e6, 2e6, 3e6, 4e6, 5e6, 6e6, 7e6, 8e6, 9e6,
0, 1e7, 2e7, 3e7, 4e7, 5e7, 6e7, 7e7, 8e7, 9e7,
0, 1e8, 2e8, 3e8, 4e8, 5e8, 6e8, 7e8, 8e8, 9e8,
0, 1e9, 2e9, 3e9, 4e9, 5e9, 6e9, 7e9, 8e9, 9e9,
int fast_atoi(char* src) {
const char* p = src;
static const uint64_t a[][10] = {
0, 1e0, 2e0, 3e0, 4e0, 5e0, 6e0, 7e0, 8e0, 9e0, 0, 1e1, 2e1,
3e1, 4e1, 5e1, 6e1, 7e1, 8e1, 9e1, 0, 1e2, 2e2, 3e2, 4e2, 5e2,
6e2, 7e2, 8e2, 9e2, 0, 1e3, 2e3, 3e3, 4e3, 5e3, 6e3, 7e3, 8e3,
9e3, 0, 1e4, 2e4, 3e4, 4e4, 5e4, 6e4, 7e4, 8e4, 9e4, 0, 1e5,
2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 0, 1e6, 2e6, 3e6, 4e6,
5e6, 6e6, 7e6, 8e6, 9e6, 0, 1e7, 2e7, 3e7, 4e7, 5e7, 6e7, 7e7,
8e7, 9e7, 0, 1e8, 2e8, 3e8, 4e8, 5e8, 6e8, 7e8, 8e8, 9e8, 0,
1e9, 2e9, 3e9, 4e9, 5e9, 6e9, 7e9, 8e9, 9e9,
};
uint16_t size = strGetSize(src);
p = p + size - 1;
if (*p){
if (*p) {
int s = 0;
const uint64_t *n = a[0];
const uint64_t* n = a[0];
while (p != src) {
s += n[(*p - '0')];
n += 10;
p--;
}
if(*p == '-'){
if (*p == '-') {
return -s;
}
return s + n[(*p - '0')];
@ -121,10 +117,10 @@ Arg* pikaVM_runAsmInstruct(PikaObj* self,
return NULL;
}
if (instruct == REF) {
if( strEqu(data, (char *)"True") ){
if (strEqu(data, (char*)"True")) {
return arg_setInt(NULL, "", 1);
}
if( strEqu(data, (char *)"False") ){
if (strEqu(data, (char*)"False")) {
return arg_setInt(NULL, "", 0);
}
return arg_copy(obj_getArg(self, data));
@ -219,6 +215,18 @@ Arg* pikaVM_runAsmInstruct(PikaObj* self,
return NULL;
}
static int32_t clearQueues(PikaObj* self) {
for (char deepthChar = '0'; deepthChar < '9'; deepthChar++) {
char deepth[2] = {0};
deepth[0] = deepthChar;
Queue* queue = (Queue*)obj_getPtr(self, deepth);
if (NULL != queue) {
args_deinit(queue);
obj_removeArg(self, deepth);
}
}
}
int32_t pikaVM_runAsmLine(PikaObj* self,
char* pikaAsm,
int32_t lineAddr,
@ -227,6 +235,11 @@ int32_t pikaVM_runAsmLine(PikaObj* self,
char* code = pikaAsm + lineAddr;
char* line = strs_getLine(buffs, code);
int32_t nextAddr = lineAddr + strGetSize(line) + 1;
/* Found new script Line, clear the queues*/
if ('B' == line[0]) {
clearQueues(self);
goto nextLine;
}
char d0[2] = {0}, d1[2] = {0};
d0[0] = line[0];
@ -272,15 +285,7 @@ Args* pikaVM_runAsm(PikaObj* self, char* pikaAsm) {
while (lineAddr < size) {
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
}
clearQueues(self);
for (char deepthChar = '0'; deepthChar < '9'; deepthChar++) {
char deepth[2] = {0};
deepth[0] = deepthChar;
Queue* queue = (Queue*)obj_getPtr(self, deepth);
if (NULL != queue) {
args_deinit(queue);
obj_removeArg(self, deepth);
}
}
return sysRes;
}