test stack for str is ok

This commit is contained in:
lyon1998 2021-10-16 18:59:46 +08:00
parent 46bf755673
commit a119401b65
5 changed files with 59 additions and 12 deletions

View File

@ -0,0 +1,26 @@
#include "gtest/gtest.h"
extern "C" {
#include "BaseObj.h"
#include "dataMemory.h"
#include "dataStack.h"
}
static int mem;
TEST(stack, NEW) {
Stack* s = New_Stack();
stack_deinit(s);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stack, str) {
Stack* s = New_Stack();
stack_pushStr(s, (char*)"abc");
stack_pushStr(s, (char*)"123");
stack_pushStr(s, (char*)"xyz");
char buff[32] = {0};
EXPECT_STREQ(stack_popStr(s, buff), (char*)"xyz");
EXPECT_STREQ(stack_popStr(s, buff), (char*)"123");
EXPECT_STREQ(stack_popStr(s, buff), (char*)"abc");
stack_deinit(s);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -2,6 +2,7 @@
#include "PikaObj.h"
#include "dataQueue.h"
#include "dataQueueObj.h"
#include "dataStack.h"
#include "dataStrs.h"
char* strsPopStmts(Args* buffs, char* stmts) {
@ -138,7 +139,7 @@ static int32_t getPyLineBlockDeepth(char* line) {
}
}
AST* pikaParseLine(char* line, Args* blockStack) {
AST* pikaParseLine(char* line, Stack* blockStack) {
AST* ast = New_queueObj();
Args* buffs = New_strBuff();
uint8_t blockDeepth = getPyLineBlockDeepth(line);

View File

@ -1,9 +1,10 @@
#ifndef __PIKA__PARSER__H
#define __PIKA__PARSER__H
#include "dataQueueObj.h"
#include "dataStack.h"
typedef QueueObj AST;
AST* pikaParseLine(char* line, Args* blockStack);
AST* pikaParseLine(char* line, Stack* blockStack);
char* pikaParseToAsm(Args* buffs, char* line);
int32_t AST_deinit(AST* ast);
char* AST_toPikaAsm(AST* ast, Args* buffs);

View File

@ -1,15 +1,34 @@
#include "dataQueue.h"
#include "dataStack.h"
#include "dataQueue.h"
Stack* New_Stack(){
Stack* New_Stack() {
Args* args = New_args(NULL);
args_setInt(args, "top", 0);
Stack* stack = args;
return stack;
}
int32_t stack_deinit(Stack* stack){
int32_t stack_deinit(Stack* stack) {
Args* args = stack;
args_deinit(args);
return 0;
}
int32_t stack_pushStr(Stack* stack, char* str){
int32_t stack_pushStr(Stack* stack, char* str) {
Args* args = stack;
uint64_t top = args_getInt(args, "top");
char buff[11];
/* add top */
char* topStr = fast_itoa(buff, top);
args_setInt(args, "top", top + 1);
return args_setStr(args, topStr, str);
}
char* stack_popStr(Stack* queue, char* buff){
char* stack_popStr(Stack* queue, char* outBuff) {
Args* args = queue;
uint64_t top = args_getInt(args, "top") - 1;
char buff[11];
/* sub top */
args_setInt(args, "top", top);
char* topStr = fast_itoa(buff, top);
strcpy(outBuff, args_getStr(args, topStr));
args_removeArg(args, topStr);
return outBuff;
}

View File

@ -12,5 +12,5 @@ Stack* New_Stack();
int32_t stack_deinit(Stack* stack);
int32_t stack_pushStr(Stack* stack, char* str);
char* stack_popStr(Stack* queue, char* buff);
char* stack_popStr(Stack* queue, char* outBuff);
#endif