mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
supporting contral in pikaParser
This commit is contained in:
parent
aa5c6cebdc
commit
e6c8e23f56
@ -1,88 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
|
||||
#include "PikaBlock.h"
|
||||
#include <stdarg.h>
|
||||
#include "PikaObj.h"
|
||||
#include "TinyObj.h"
|
||||
#include "dataArgs.h"
|
||||
#include "dataMemory.h"
|
||||
#include "dataString.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
void block_deinit(PikaObj* self) {
|
||||
obj_deinit(self);
|
||||
}
|
||||
|
||||
PikaObj* block_init(Args* args) {
|
||||
PikaObj* self = New_TinyObj(args);
|
||||
obj_setStr(self, "mode", "");
|
||||
obj_setStr(self, "assert", "");
|
||||
obj_setStr(self, "body", "");
|
||||
obj_setInt(self, "lineSize", 0);
|
||||
obj_setStr(self, "lineNow", "");
|
||||
return self;
|
||||
}
|
||||
|
||||
char* block_getBody(PikaObj* self) {
|
||||
return obj_getStr(self, "body");
|
||||
}
|
||||
|
||||
void block_setBody(PikaObj* self, char* body) {
|
||||
obj_setStr(self, "body", body);
|
||||
}
|
||||
|
||||
uint8_t block_checkAssert(PikaObj* self) {
|
||||
Args* buffs = New_strBuff();
|
||||
PikaObj* host = obj_getContext(self);
|
||||
char* assert = block_getAssert(self);
|
||||
obj_run(host, strsFormat(buffs, 32, "_res = %s", assert));
|
||||
int res = obj_getInt(host, "_res");
|
||||
obj_removeArg(host, "_res");
|
||||
args_deinit(buffs);
|
||||
return res;
|
||||
}
|
||||
|
||||
uint16_t block_getLineSize(PikaObj* self) {
|
||||
return obj_getInt(self, "lineSize");
|
||||
}
|
||||
|
||||
void block_setAssert(PikaObj* self, char* assert) {
|
||||
obj_setStr(self, "assert", assert);
|
||||
}
|
||||
|
||||
char* block_getAssert(PikaObj* self) {
|
||||
return obj_getStr(self, "assert");
|
||||
}
|
||||
|
||||
void block_setMode(PikaObj* self, char* mode) {
|
||||
obj_setStr(self, "mode", mode);
|
||||
}
|
||||
|
||||
char* block_getMode(PikaObj* self) {
|
||||
return obj_getStr(self, "mode");
|
||||
}
|
||||
|
||||
void block_pushLine(PikaObj* self, char* line) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* body = obj_getStr(self, "body");
|
||||
body = strsAppend(buffs, body, line);
|
||||
body = strsAppend(buffs, body, "\n");
|
||||
obj_setStr(self, "body", body);
|
||||
obj_setInt(self, "lineSize", obj_getInt(self, "lineSize") + 1);
|
||||
args_deinit(buffs);
|
||||
}
|
||||
|
||||
char* block_popLine(PikaObj* self) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* body = obj_getStr(self, "body");
|
||||
char* line = strsPopToken(buffs, body, '\n');
|
||||
|
||||
obj_setStr(self, "body", body);
|
||||
obj_setStr(self, "lineNow", line);
|
||||
obj_setInt(self, "lineSize", obj_getInt(self, "lineSize") - 1);
|
||||
args_deinit(buffs);
|
||||
return obj_getStr(self, "lineNow");
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
|
||||
#ifndef _pikaBlock__H
|
||||
#define _pikaBlock__H
|
||||
#include "PikaObj.h"
|
||||
|
||||
void block_deinit(PikaObj* self);
|
||||
PikaObj* block_init(Args* args);
|
||||
char* block_popLine(PikaObj* self);
|
||||
void block_pushLine(PikaObj* self, char* line);
|
||||
char* block_getBody(PikaObj* self);
|
||||
char* block_getAssert(PikaObj* self);
|
||||
void block_setAssert(PikaObj* self, char* assert);
|
||||
uint8_t block_checkAssert(PikaObj* self);
|
||||
uint16_t block_getLineSize(PikaObj* self);
|
||||
void block_setMode(PikaObj* self, char* mode);
|
||||
char* block_getMode(PikaObj* self);
|
||||
void block_setBody(PikaObj* self, char* body);
|
||||
#endif
|
@ -1,39 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
|
||||
#include "PikaIf.h"
|
||||
#include <stdarg.h>
|
||||
#include "PikaBlock.h"
|
||||
#include "TinyObj.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
void if_setAssert(PikaObj* self, char* line) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* assert = strsRemovePrefix(buffs, line, "if ");
|
||||
assert = strsGetFirstToken(buffs, assert, ':');
|
||||
block_setAssert(self, assert);
|
||||
block_setMode(self, "if");
|
||||
args_deinit(buffs);
|
||||
}
|
||||
|
||||
void if_pushLine(PikaObj* self, char* line) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* bodyLine = strsRemovePrefix(buffs, line, " ");
|
||||
block_pushLine(self, bodyLine);
|
||||
goto exit;
|
||||
exit:
|
||||
args_deinit(buffs);
|
||||
return;
|
||||
}
|
||||
|
||||
void if_run(PikaObj* self) {
|
||||
if (block_checkAssert(self)) {
|
||||
PikaObj* host = obj_getContext(self);
|
||||
while (0 != block_getLineSize(self)) {
|
||||
char* line = block_popLine(self);
|
||||
obj_run(host, line);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
|
||||
#ifndef _pikaIf__H
|
||||
#define _pikaIf__H
|
||||
#include "PikaObj.h"
|
||||
|
||||
void if_pushLine(PikaObj* self, char* line);
|
||||
void if_setAssert(PikaObj* self, char* line);
|
||||
void if_run(PikaObj* self);
|
||||
#endif
|
@ -1,73 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
#include "PikaObj.h"
|
||||
#include <stdarg.h>
|
||||
#include "BaseObj.h"
|
||||
#include "PikaBlock.h"
|
||||
#include "PikaIf.h"
|
||||
#include "PikaWhile.h"
|
||||
#include "dataArgs.h"
|
||||
#include "dataMemory.h"
|
||||
#include "dataString.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
int __runExtern_contral(PikaObj* self, char* cmd) {
|
||||
int isExit = 0;
|
||||
/* in block */
|
||||
if (NULL != obj_getArg(self, "_isInBlock")) {
|
||||
PikaObj* block = obj_getObj(self, "_block", 0);
|
||||
if (strIsStartWith(cmd, " ")) {
|
||||
if (strEqu(block_getMode(block), "if")) {
|
||||
if_pushLine(block, cmd);
|
||||
isExit = 1;
|
||||
goto exit;
|
||||
}
|
||||
if (strEqu(block_getMode(block), "while")) {
|
||||
while_pushLine(block, cmd);
|
||||
isExit = 1;
|
||||
goto exit;
|
||||
}
|
||||
isExit = 1;
|
||||
goto exit;
|
||||
}
|
||||
/* the block is end */
|
||||
else {
|
||||
obj_removeArg(self, "_isInBlock");
|
||||
if (strEqu(block_getMode(block), "if")) {
|
||||
if_run(block);
|
||||
}
|
||||
if (strEqu(block_getMode(block), "while")) {
|
||||
while_run(block);
|
||||
}
|
||||
obj_removeArg(self, "_block");
|
||||
/* not finished */
|
||||
}
|
||||
}
|
||||
|
||||
/* if block */
|
||||
if (strIsStartWith(cmd, "if ")) {
|
||||
obj_setInt(self, "_isInBlock", 1);
|
||||
obj_setObjWithoutClass(self, "_block", block_init);
|
||||
PikaObj* block = obj_getObj(self, "_block", 0);
|
||||
if_setAssert(block, cmd);
|
||||
/* this line processed ok */
|
||||
isExit = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* while block */
|
||||
if (strIsStartWith(cmd, "while ")) {
|
||||
obj_setInt(self, "_isInBlock", 1);
|
||||
obj_setObjWithoutClass(self, "_block", block_init);
|
||||
PikaObj* block = obj_getObj(self, "_block", 0);
|
||||
while_setAssert(block, cmd);
|
||||
/* this line processed ok */
|
||||
isExit = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
return isExit;
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include "PikaBlock.h"
|
||||
#include "PikaObj.h"
|
||||
#include "dataArgs.h"
|
||||
#include "dataMemory.h"
|
||||
#include "dataString.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
void while_setAssert(PikaObj* self, char* line) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* assert = strsRemovePrefix(buffs, line, "while ");
|
||||
assert = strsGetFirstToken(buffs, assert, ':');
|
||||
block_setAssert(self, assert);
|
||||
block_setMode(self, "while");
|
||||
args_deinit(buffs);
|
||||
}
|
||||
|
||||
void while_pushLine(PikaObj* self, char* line) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* bodyLine = strsRemovePrefix(buffs, line, " ");
|
||||
block_pushLine(self, bodyLine);
|
||||
goto exit;
|
||||
exit:
|
||||
args_deinit(buffs);
|
||||
return;
|
||||
}
|
||||
|
||||
void while_run(PikaObj* self) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* body = strsCopy(buffs, block_getBody(self));
|
||||
uint16_t lineSize = block_getLineSize(self);
|
||||
while (block_checkAssert(self)) {
|
||||
PikaObj* host = obj_getContext(self);
|
||||
for (int i = 0; i < lineSize; i++) {
|
||||
char* line = block_popLine(self);
|
||||
obj_run(host, line);
|
||||
}
|
||||
block_setBody(self, body);
|
||||
}
|
||||
exit:
|
||||
args_deinit(buffs);
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
/*
|
||||
Author: lyon
|
||||
Tencent QQ: 645275593
|
||||
*/
|
||||
|
||||
#ifndef _pikaWhile__H
|
||||
#define _pikaWhile__H
|
||||
#include "PikaObj.h"
|
||||
|
||||
void while_setAssert(PikaObj* self, char* line);
|
||||
void while_pushLine(PikaObj* self, char* line);
|
||||
void while_run(PikaObj* self);
|
||||
|
||||
#endif
|
@ -1,115 +0,0 @@
|
||||
#include "gtest/gtest.h"
|
||||
extern "C" {
|
||||
#include "BaseObj.h"
|
||||
#include "PikaBlock.h"
|
||||
#include "PikaIf.h"
|
||||
#include "PikaObj.h"
|
||||
#include "TinyObj.h"
|
||||
#include "dataStrs.h"
|
||||
}
|
||||
|
||||
TEST(block, init) {
|
||||
PikaObj* block = block_init(NULL);
|
||||
block_deinit(block);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(block, push) {
|
||||
PikaObj* block = block_init(NULL);
|
||||
block_pushLine(block, (char*)"line1");
|
||||
block_pushLine(block, (char*)"line2");
|
||||
block_deinit(block);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(block, pop) {
|
||||
PikaObj* block = block_init(NULL);
|
||||
block_pushLine(block, (char*)"line1");
|
||||
block_pushLine(block, (char*)"line2");
|
||||
char* line1 = block_popLine(block);
|
||||
ASSERT_STREQ(line1, (char*)"line1");
|
||||
char* line2 = block_popLine(block);
|
||||
ASSERT_STREQ(line2, (char*)"line2");
|
||||
char* line3 = block_popLine(block);
|
||||
ASSERT_STREQ(line3, (char*)"");
|
||||
block_deinit(block);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(block, assert1) {
|
||||
PikaObj* block = block_init(NULL);
|
||||
{
|
||||
block_setAssert(block, (char*)"1");
|
||||
int res = block_checkAssert(block);
|
||||
ASSERT_EQ(1, res);
|
||||
}
|
||||
|
||||
{
|
||||
block_setAssert(block, (char*)"0");
|
||||
int res = block_checkAssert(block);
|
||||
ASSERT_EQ(0, res);
|
||||
}
|
||||
block_deinit(block);
|
||||
}
|
||||
|
||||
TEST(block, if1) {
|
||||
PikaObj* block = block_init(NULL);
|
||||
if_setAssert(block, (char*)"if 1 :");
|
||||
if_pushLine(block, (char*)" print('hello')");
|
||||
if_pushLine(block, (char*)" print('hello2')");
|
||||
char* assert = block_getAssert(block);
|
||||
ASSERT_STREQ((char*)"1 ", assert);
|
||||
int res = block_checkAssert(block);
|
||||
char* line1 = block_popLine(block);
|
||||
ASSERT_STREQ((char*)"print('hello')", line1);
|
||||
char* line2 = block_popLine(block);
|
||||
ASSERT_STREQ((char*)"print('hello2')", line2);
|
||||
ASSERT_EQ(1, res);
|
||||
block_deinit(block);
|
||||
}
|
||||
|
||||
TEST(block, if2) {
|
||||
PikaObj* obj = New_TinyObj(NULL);
|
||||
obj_run(obj, (char*)"if 1 :");
|
||||
obj_deinit(obj);
|
||||
}
|
||||
|
||||
TEST(block, if3) {
|
||||
PikaObj* obj = New_TinyObj(NULL);
|
||||
obj_run(obj, (char*)"if 1 :");
|
||||
obj_run(obj, (char*)" print('hello')");
|
||||
obj_run(obj, (char*)" print('hello2')");
|
||||
PikaObj* block = obj_getObj(obj, (char*)"_block", 0);
|
||||
char* mode = block_getMode(block);
|
||||
ASSERT_STREQ((char*)"if", mode);
|
||||
char* assert = block_getAssert(block);
|
||||
ASSERT_STREQ((char*)"1 ", assert);
|
||||
char* line1 = block_popLine(block);
|
||||
ASSERT_STREQ((char*)"print('hello')", line1);
|
||||
char* line2 = block_popLine(block);
|
||||
ASSERT_STREQ((char*)"print('hello2')", line2);
|
||||
obj_deinit(obj);
|
||||
}
|
||||
|
||||
TEST(block, ifrun1) {
|
||||
PikaObj* obj = newRootObj((char*)"root", New_BaseObj);
|
||||
obj_run(obj, (char*)"if 1 :");
|
||||
obj_run(obj, (char*)" a = 1");
|
||||
obj_run(obj, (char*)" b = 2");
|
||||
obj_run(obj, (char*)"");
|
||||
int a = obj_getInt(obj, (char*)"a");
|
||||
int b = obj_getInt(obj, (char*)"b");
|
||||
ASSERT_EQ(a, 1);
|
||||
ASSERT_EQ(b, 2);
|
||||
obj_deinit(obj);
|
||||
}
|
||||
|
||||
TEST(block, ifrun2) {
|
||||
PikaObj* obj = newRootObj((char*)"root", New_BaseObj);
|
||||
obj_run(obj, (char*)"if 0 :");
|
||||
PikaObj* block = obj_getObj(obj, (char*)"_block", 0);
|
||||
char* assert = block_getAssert(block);
|
||||
uint8_t res = block_checkAssert(block);
|
||||
ASSERT_EQ(res, 0);
|
||||
obj_deinit(obj);
|
||||
}
|
@ -141,8 +141,17 @@ static int32_t getPyLineBlockDeepth(char* line) {
|
||||
AST* pikaParseLine(char* line) {
|
||||
AST* ast = New_queueObj();
|
||||
Args* buffs = New_strBuff();
|
||||
obj_setInt(ast, "blockDeepth", getPyLineBlockDeepth(line));
|
||||
char* stmt = strsGetCleanCmd(buffs, line);
|
||||
uint8_t blockDeepth = getPyLineBlockDeepth(line);
|
||||
obj_setInt(ast, "blockDeepth", blockDeepth);
|
||||
char* lineStart = line + blockDeepth * 4;
|
||||
char* stmt = lineStart;
|
||||
if (0 == strncmp(lineStart, (char*)"while ", 6)) {
|
||||
stmt = strsCut(buffs, lineStart, ' ', ':');
|
||||
}
|
||||
if (0 == strncmp(lineStart, (char*)"if ", 3)) {
|
||||
stmt = strsCut(buffs, lineStart, ' ', ':');
|
||||
}
|
||||
stmt = strsGetCleanCmd(buffs, stmt);
|
||||
ast = AST_parseStmt(ast, stmt);
|
||||
goto exit;
|
||||
exit:
|
||||
|
@ -238,6 +238,7 @@ int32_t pikaVM_runAsmLine(PikaObj* self,
|
||||
/* Found new script Line, clear the queues*/
|
||||
if ('B' == line[0]) {
|
||||
clearQueues(self);
|
||||
uint8_t blockDeepth = line[1] - '0';
|
||||
goto nextLine;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user