mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
add pikaVM_runAsm, add pushArg for queue
This commit is contained in:
parent
aa1d34e5c8
commit
40dfcfa423
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -13,7 +13,7 @@
|
||||
"args": [
|
||||
// "--gtest_filter=object_test.test2"
|
||||
// "--gtest_filter=parser*"
|
||||
"--gtest_filter=VM*"
|
||||
// "--gtest_filter=VM*"
|
||||
// "--gtest_filter=block.ifrun2"
|
||||
// "--gtest_filter=sysObj.print"
|
||||
// "--gtest_filter=object_test.a_b"
|
||||
|
@ -5,23 +5,18 @@ extern "C" {
|
||||
#include "PikaStdLib_SysObj.h"
|
||||
#include "PikaVM.h"
|
||||
#include "dataMemory.h"
|
||||
#include "dataQueue.h"
|
||||
#include "dataStrs.h"
|
||||
}
|
||||
|
||||
TEST(VM, a_1) {
|
||||
char* line = (char*)"a = 1";
|
||||
char* line = (char*)"1";
|
||||
Args* buffs = New_strBuff();
|
||||
char* pikaAsm = pikaParseToAsm(buffs, line);
|
||||
printf("%s", pikaAsm);
|
||||
PikaObj* root = newRootObj((char*)"root", New_PikaStdLib_SysObj);
|
||||
|
||||
int lineAddr = 0;
|
||||
int size = strGetSize(pikaAsm);
|
||||
while (lineAddr < size) {
|
||||
lineAddr = pikaVM_run(root, pikaAsm, lineAddr);
|
||||
}
|
||||
|
||||
obj_deinit(root);
|
||||
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
|
||||
pikaVM_runAsm(self, pikaAsm);
|
||||
obj_deinit(self);
|
||||
args_deinit(buffs);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
@ -25,6 +25,21 @@ TEST(queue, INT) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(queue, arg) {
|
||||
Queue* q = New_queue();
|
||||
Arg* arg1 = arg_setInt(NULL, (char*)"", 1);
|
||||
Arg* arg2 = arg_setFloat(NULL, (char*)"", 1.2);
|
||||
Arg* arg3 = arg_setStr(NULL, (char*)"", (char*)"ij");
|
||||
queue_pushArg(q, arg1);
|
||||
queue_pushArg(q, arg2);
|
||||
queue_pushArg(q, arg3);
|
||||
EXPECT_EQ(arg_getInt(queue_popArg(q)), 1);
|
||||
EXPECT_FLOAT_EQ(arg_getFloat(queue_popArg(q)), 1.2);
|
||||
EXPECT_STREQ(arg_getStr(queue_popArg(q)), (char*)"ij");
|
||||
queue_deinit(q);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(queue, FLOAT) {
|
||||
Queue* q = New_queue();
|
||||
queue_pushFloat(q, 1.1f);
|
||||
|
43
src/PikaVM.c
43
src/PikaVM.c
@ -1,7 +1,9 @@
|
||||
#include "PikaVM.h"
|
||||
#include <string.h>
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "PikaParser.h"
|
||||
#include "dataQueue.h"
|
||||
#include "dataQueueObj.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
@ -43,17 +45,56 @@ static enum Instruct getInstruct(char* line) {
|
||||
return NON;
|
||||
}
|
||||
|
||||
Arg* pikaVM_getArg(PikaObj* self, enum Instruct instruct, char* data) {
|
||||
Arg* res = New_arg(NULL);
|
||||
if (instruct == NUM) {
|
||||
if (strIsContain(data, '.')) {
|
||||
return arg_setFloat(res, "", atof(data));
|
||||
}
|
||||
return arg_setInt(res, "", atoi(data));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int32_t pikaVM_run(PikaObj* self, char* pikaAsm, int32_t lineAddr) {
|
||||
Args* buffs = New_strBuff();
|
||||
char* code = pikaAsm + lineAddr;
|
||||
char* line = strs_getLine(buffs, code);
|
||||
int32_t nextAddr = lineAddr + strGetSize(line) + 1;
|
||||
|
||||
char deepth = line[0];
|
||||
char deepth[2] = {0};
|
||||
deepth[0] = line[0];
|
||||
enum Instruct instruct = getInstruct(line);
|
||||
char* data = line + 6;
|
||||
|
||||
Arg* res = pikaVM_getArg(self, instruct, data);
|
||||
Queue* queue = obj_getPtr(self, deepth);
|
||||
if (NULL == queue) {
|
||||
queue = New_queue();
|
||||
obj_setPtr(self, deepth, queue);
|
||||
}
|
||||
queue_pushArg(queue, res);
|
||||
goto nextLine;
|
||||
nextLine:
|
||||
args_deinit(buffs);
|
||||
return nextAddr;
|
||||
}
|
||||
|
||||
int32_t pikaVM_runAsm(PikaObj* self, char* pikaAsm) {
|
||||
int lineAddr = 0;
|
||||
int size = strGetSize(pikaAsm);
|
||||
while (lineAddr < size) {
|
||||
lineAddr = pikaVM_run(self, pikaAsm, lineAddr);
|
||||
}
|
||||
|
||||
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 0;
|
||||
}
|
@ -3,5 +3,6 @@
|
||||
#include "PikaObj.h"
|
||||
|
||||
int32_t pikaVM_run(PikaObj* self, char* pikaAsm, int32_t lineAddr);
|
||||
int32_t pikaVM_runAsm(PikaObj* self, char* pikaAsm);
|
||||
|
||||
#endif
|
@ -85,4 +85,26 @@ char* queue_popStr(Queue* queue) {
|
||||
/* add bottom */
|
||||
args_setInt(args, "bottom", bottom + 1);
|
||||
return args_getStr(args, bottomStr);
|
||||
}
|
||||
|
||||
int32_t queue_pushArg(Queue* queue, Arg* arg) {
|
||||
Args* args = queue;
|
||||
uint64_t top = args_getInt(args, "top");
|
||||
char topStr[32] = {0};
|
||||
sprintf(topStr, "%ld", top);
|
||||
/* add top */
|
||||
args_setInt(args, "top", top + 1);
|
||||
arg = arg_setName(arg, topStr);
|
||||
return args_setArg(args, arg);
|
||||
}
|
||||
|
||||
Arg* queue_popArg(Queue* queue) {
|
||||
Args* args = queue;
|
||||
uint64_t bottom = args_getInt(args, "bottom");
|
||||
char bottomStr[32] = {0};
|
||||
sprintf(bottomStr, "%ld", bottom);
|
||||
/* add bottom */
|
||||
args_setInt(args, "bottom", bottom + 1);
|
||||
Arg* res = args_getArg(args, bottomStr);
|
||||
return res;
|
||||
}
|
@ -14,8 +14,10 @@ Queue* queue_deinit(Queue* queue);
|
||||
int32_t queue_pushInt(Queue* queue, int val);
|
||||
int32_t queue_pushFloat(Queue* queue, float val);
|
||||
int32_t queue_pushStr(Queue* queue, char* str);
|
||||
int32_t queue_pushArg(Queue* queue, Arg* arg);
|
||||
|
||||
int64_t queue_popInt(Queue* queue);
|
||||
float queue_popFloat(Queue* queue);
|
||||
char* queue_popStr(Queue* queue);
|
||||
Arg* queue_popArg(Queue* queue);
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user