add log_buff when printf

This commit is contained in:
lyon 2022-01-03 21:12:05 +08:00
parent 57eb87d6bf
commit 11115ded90
18 changed files with 237 additions and 173 deletions

0
port/linux/api-make-linux.sh Normal file → Executable file
View File

0
port/linux/api-make-win10.sh Normal file → Executable file
View File

0
port/linux/api-make.sh Normal file → Executable file
View File

0
port/linux/gtest.sh Normal file → Executable file
View File

0
port/linux/init.sh Normal file → Executable file
View File

0
port/linux/make.sh Normal file → Executable file
View File

0
port/linux/package/pikascript/rust-msc-latest-linux Normal file → Executable file
View File

0
port/linux/pkg-push.sh Normal file → Executable file
View File

0
port/linux/pull-core.sh Normal file → Executable file
View File

0
port/linux/push-core.sh Normal file → Executable file
View File

0
port/linux/test-banchmark.sh Normal file → Executable file
View File

View File

@ -1,4 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "dataMemory.h"
#include "mem_pool_config.h"
#define use_const_pool 0
#define use_dynamic_pool 1
@ -6,6 +9,21 @@
#define pika_aline 8
#define pika_pool_size 0x4000
char log_buff[LOG_BUFF_MAX][LOG_SIZE] = {0};
uint32_t log_index = 0;
/* save printf content to log_buff */
void __platform_printf(char* fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
for (int i = LOG_BUFF_MAX - 2; i >= 0; i--) {
memcpy(log_buff[i + 1], log_buff[i], LOG_SIZE);
}
memcpy(log_buff[0], fmt, LOG_SIZE);
}
/* quick_malloc is always open */
uint8_t __is_quick_malloc(void) {
// return 1;

View File

@ -0,0 +1,2 @@
#define LOG_BUFF_MAX 100
#define LOG_SIZE 64

View File

@ -5,6 +5,7 @@ extern "C" {
#include "dataArgs.h"
#include "dataMemory.h"
#include "pikaScript.h"
#include "mem_pool_config.h"
}
extern PikaMemInfo pikaMemInfo;
TEST(pikaMain, init) {
@ -448,3 +449,28 @@ TEST(pikaMain, for_if_continue) {
/* mem check */
EXPECT_EQ(pikaMemNow(), 0);
}
/* the log_buff of printf */
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
TEST(pikaMain, print_in_def) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
/* the test is used to fix too many print in def */
/* clear the log_buff */
memset(log_buff, 0, LOG_BUFF_MAX * LOG_SIZE);
obj_runDirect(pikaMain, (char*)
"def test_print():\n"
" print('test')\n"
"test_print()\n"
);
/* collect */
/* assert */
/* should only print once, so the second log (log_buff[1]) shuold be '\0' */
EXPECT_EQ(log_buff[1][0], 0);
/* deinit */
obj_deinit(pikaMain);
/* mem check */
EXPECT_EQ(pikaMemNow(), 0);
}

0
port/linux/update-compiler.sh Normal file → Executable file
View File

View File

@ -272,109 +272,128 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
if (type_arg1 == TYPE_INT) {
num1_i = arg_getInt(arg1);
num1_f = (float)num1_i;
} else if (type_arg1 == TYPE_FLOAT) {
}
if (type_arg1 == TYPE_FLOAT) {
num1_f = arg_getFloat(arg1);
num1_i = (int)num1_f;
}
if (type_arg2 == TYPE_INT) {
num2_i = arg_getInt(arg2);
num2_f = (float)num2_i;
} else if (type_arg2 == TYPE_FLOAT) {
}
if (type_arg2 == TYPE_FLOAT) {
num2_f = arg_getFloat(arg2);
num2_i = (int)num2_f;
}
//do {
if (strEqu("+", data)) {
if ((type_arg1 == TYPE_FLOAT) || type_arg2 == TYPE_FLOAT) {
outArg = arg_setFloat(outArg, "", num1_f + num2_f);
} else {
outArg = arg_setInt(outArg, "", num1_i + num2_i);
}
//break;
} else if (strEqu("-", data)) {
if ((type_arg1 == TYPE_FLOAT) || type_arg2 == TYPE_FLOAT) {
outArg = arg_setFloat(outArg, "", num1_f - num2_f);
} else {
outArg = arg_setInt(outArg, "", num1_i - num2_i);
}
//break;
} else if (strEqu("*", data)) {
if ((type_arg1 == TYPE_FLOAT) || type_arg2 == TYPE_FLOAT) {
outArg = arg_setFloat(outArg, "", num1_f * num2_f);
} else {
outArg = arg_setInt(outArg, "", num1_i * num2_i);
}
//break;
} else if (strEqu("/", data)) {
outArg = arg_setFloat(outArg, "", num1_f / num2_f);
//break;
} else if (strEqu("<", data)) {
outArg = arg_setInt(outArg, "", num1_f < num2_f);
//break;
} else if (strEqu(">", data)) {
outArg = arg_setInt(outArg, "", num1_f > num2_f);
//break;
} else if (strEqu("%", data)) {
outArg = arg_setInt(outArg, "", num1_i % num2_i);
//break;
} else if (strEqu("**", data)) {
float res = 1;
for (int i = 0; i < num2_i; i++) {
res = res * num1_f;
}
outArg = arg_setFloat(outArg, "", res);
//break;
} else if (strEqu("//", data)) {
outArg = arg_setInt(outArg, "", num1_i / num2_i);
//break;
} else if (strEqu("==", data)) {
outArg = arg_setInt(outArg, "",
(num1_f - num2_f) * (num1_f - num2_f) < 0.000001);
//break;
} else if (strEqu("!=", data)) {
outArg = arg_setInt(
outArg, "", !((num1_f - num2_f) * (num1_f - num2_f) < 0.000001));
//break;
} else if (strEqu(">=", data)) {
outArg =
arg_setInt(outArg, "",
(num1_f > num2_f) ||
((num1_f - num2_f) * (num1_f - num2_f) < 0.000001));
//break;
} else if (strEqu("<=", data)) {
outArg =
arg_setInt(outArg, "",
(num1_f < num2_f) ||
((num1_f - num2_f) * (num1_f - num2_f) < 0.000001));
//break;
} else if (strEqu("&", data)) {
outArg = arg_setInt(outArg, "", num1_i & num2_i);
//break;
} else if (strEqu("|", data)) {
outArg = arg_setInt(outArg, "", num1_i | num2_i);
//break;
} else if (strEqu("~", data)) {
outArg = arg_setInt(outArg, "", ~num1_i);
//break;
} else if (strEqu(">>", data)) {
outArg = arg_setInt(outArg, "", num1_i >> num2_i);
//break;
} else if (strEqu("<<", data)) {
outArg = arg_setInt(outArg, "", num1_i << num2_i);
//break;
} else if (strEqu(" and ", data)) {
outArg = arg_setInt(outArg, "", num1_i && num2_i);
//break;
} else if (strEqu(" or ", data)) {
outArg = arg_setInt(outArg, "", num1_i || num2_i);
//break;
} else if (strEqu(" not ", data)) {
outArg = arg_setInt(outArg, "", !num1_i);
//break;
if (strEqu("+", data)) {
if ((type_arg1 == TYPE_FLOAT) || type_arg2 == TYPE_FLOAT) {
outArg = arg_setFloat(outArg, "", num1_f + num2_f);
goto OPT_exit;
}
//} while(0);
outArg = arg_setInt(outArg, "", num1_i + num2_i);
goto OPT_exit;
}
if (strEqu("-", data)) {
if ((type_arg1 == TYPE_FLOAT) || type_arg2 == TYPE_FLOAT) {
outArg = arg_setFloat(outArg, "", num1_f - num2_f);
goto OPT_exit;
}
outArg = arg_setInt(outArg, "", num1_i - num2_i);
goto OPT_exit;
}
if (strEqu("*", data)) {
if ((type_arg1 == TYPE_FLOAT) || type_arg2 == TYPE_FLOAT) {
outArg = arg_setFloat(outArg, "", num1_f * num2_f);
goto OPT_exit;
}
outArg = arg_setInt(outArg, "", num1_i * num2_i);
goto OPT_exit;
}
if (strEqu("/", data)) {
outArg = arg_setFloat(outArg, "", num1_f / num2_f);
goto OPT_exit;
}
if (strEqu("<", data)) {
outArg = arg_setInt(outArg, "", num1_f < num2_f);
goto OPT_exit;
}
if (strEqu(">", data)) {
outArg = arg_setInt(outArg, "", num1_f > num2_f);
goto OPT_exit;
}
if (strEqu("%", data)) {
outArg = arg_setInt(outArg, "", num1_i % num2_i);
goto OPT_exit;
}
if (strEqu("**", data)) {
float res = 1;
for (int i = 0; i < num2_i; i++) {
res = res * num1_f;
}
outArg = arg_setFloat(outArg, "", res);
goto OPT_exit;
}
if (strEqu("//", data)) {
outArg = arg_setInt(outArg, "", num1_i / num2_i);
goto OPT_exit;
}
if (strEqu("==", data)) {
outArg = arg_setInt(outArg, "",
(num1_f - num2_f) * (num1_f - num2_f) < 0.000001);
goto OPT_exit;
}
if (strEqu("!=", data)) {
outArg = arg_setInt(
outArg, "", !((num1_f - num2_f) * (num1_f - num2_f) < 0.000001));
goto OPT_exit;
}
if (strEqu(">=", data)) {
outArg =
arg_setInt(outArg, "",
(num1_f > num2_f) ||
((num1_f - num2_f) * (num1_f - num2_f) < 0.000001));
goto OPT_exit;
}
if (strEqu("<=", data)) {
outArg =
arg_setInt(outArg, "",
(num1_f < num2_f) ||
((num1_f - num2_f) * (num1_f - num2_f) < 0.000001));
goto OPT_exit;
}
if (strEqu("&", data)) {
outArg = arg_setInt(outArg, "", num1_i & num2_i);
goto OPT_exit;
}
if (strEqu("|", data)) {
outArg = arg_setInt(outArg, "", num1_i | num2_i);
goto OPT_exit;
}
if (strEqu("~", data)) {
outArg = arg_setInt(outArg, "", ~num1_i);
goto OPT_exit;
}
if (strEqu(">>", data)) {
outArg = arg_setInt(outArg, "", num1_i >> num2_i);
goto OPT_exit;
}
if (strEqu("<<", data)) {
outArg = arg_setInt(outArg, "", num1_i << num2_i);
goto OPT_exit;
}
if (strEqu(" and ", data)) {
outArg = arg_setInt(outArg, "", num1_i && num2_i);
goto OPT_exit;
}
if (strEqu(" or ", data)) {
outArg = arg_setInt(outArg, "", num1_i || num2_i);
goto OPT_exit;
}
if (strEqu(" not ", data)) {
outArg = arg_setInt(outArg, "", !num1_i);
goto OPT_exit;
}
OPT_exit:
arg_deinit(arg1);
arg_deinit(arg2);
if (NULL != outArg) {
@ -612,36 +631,35 @@ int32_t pikaVM_runAsmLine(PikaObj* self,
char* data;
Arg* resArg;
do {
/* Found new script Line, clear the queues*/
if ('B' == line[0]) {
args_setErrorCode(locals->list, 0);
args_setSysOut(locals->list, (char*)"");
__clearInvokeQueues(locals);
break;
}
invokeDeepth0[0] = line[0];
invokeDeepth1[0] = line[0] + 1;
instruct = getInstruct(line);
data = line + 6;
vmState.q0 = args_getPtr(locals->list, invokeDeepth0);
vmState.q1 = args_getPtr(locals->list, invokeDeepth1);
if (NULL == vmState.q0) {
vmState.q0 = New_queue();
args_setPtr(locals->list, invokeDeepth0, vmState.q0);
}
if (NULL == vmState.q1) {
vmState.q1 = New_queue();
args_setPtr(locals->list, invokeDeepth1, vmState.q1);
}
/* run instruct */
resArg = VM_instruct_handler_table[instruct](self, &vmState, data);
if (NULL != resArg) {
queue_pushArg(vmState.q0, resArg);
}
} while(0);
/* Found new script Line, clear the queues*/
if ('B' == line[0]) {
args_setErrorCode(locals->list, 0);
args_setSysOut(locals->list, (char*)"");
__clearInvokeQueues(locals);
goto nextLine;
}
invokeDeepth0[0] = line[0];
invokeDeepth1[0] = line[0] + 1;
instruct = getInstruct(line);
data = line + 6;
vmState.q0 = args_getPtr(locals->list, invokeDeepth0);
vmState.q1 = args_getPtr(locals->list, invokeDeepth1);
if (NULL == vmState.q0) {
vmState.q0 = New_queue();
args_setPtr(locals->list, invokeDeepth0, vmState.q0);
}
if (NULL == vmState.q1) {
vmState.q1 = New_queue();
args_setPtr(locals->list, invokeDeepth1, vmState.q1);
}
/* run instruct */
resArg = VM_instruct_handler_table[instruct](self, &vmState, data);
if (NULL != resArg) {
queue_pushArg(vmState.q0, resArg);
}
goto nextLine;
nextLine:
args_deinit(buffs);
/* exit */
if (-999 == vmState.jmp) {
@ -722,16 +740,14 @@ VM_Parameters* pikaVM_run(PikaObj* self, char* multiLine) {
Args* buffs = New_strBuff();
VM_Parameters* globals = NULL;
char* pikaAsm = Parser_multiLineToAsm(buffs, multiLine);
do {
if (NULL == pikaAsm) {
__platform_printf("[error]: Syntax error.\r\n");
globals = NULL;
break;
}
globals = pikaVM_runAsm(self, pikaAsm);
} while (0);
if (NULL == pikaAsm) {
__platform_printf("[error]: Syntax error.\r\n");
globals = NULL;
goto exit;
}
globals = pikaVM_runAsm(self, pikaAsm);
goto exit;
exit:
if (NULL != buffs) {
args_deinit(buffs);
}

View File

@ -305,34 +305,40 @@ char* args_print(Args* self, char* name) {
char* res = NULL;
ArgType type = args_getType(self, name);
Args* buffs = New_strBuff();
do {
if (TYPE_NONE == type) {
/* can not get arg */
res = NULL;
break;
} else if (type == TYPE_INT) {
int32_t val = args_getInt(self, name);
res = getPrintStringFromInt(self, name, val);
break;
} else if (type == TYPE_FLOAT) {
float val = args_getFloat(self, name);
res = getPrintStringFromFloat(self, name, val);
break;
} else if (type == TYPE_STRING) {
res = args_getStr(self, name);
break;
} else if (type == TYPE_POINTER) {
void* val = args_getPtr(self, name);
res = getPrintStringFromPtr(self, name, val);
break;
}
if (TYPE_NONE == type) {
/* can not get arg */
res = NULL;
goto exit;
}
/* can not match type */
//res = NULL;
} while (0);
if (type == TYPE_INT) {
int32_t val = args_getInt(self, name);
res = getPrintStringFromInt(self, name, val);
goto exit;
}
if (type == TYPE_FLOAT) {
float val = args_getFloat(self, name);
res = getPrintStringFromFloat(self, name, val);
goto exit;
}
if (type == TYPE_STRING) {
res = args_getStr(self, name);
goto exit;
}
if (type == TYPE_POINTER) {
void* val = args_getPtr(self, name);
res = getPrintStringFromPtr(self, name, val);
goto exit;
}
/* can not match type */
res = NULL;
goto exit;
exit:
args_deinit(buffs);
return res;
}

View File

@ -28,8 +28,6 @@
#include "dataString.h"
#include "PikaPlatform.h"
#include <string.h>
char* strAppendWithSize_unlimited(char* strOut, char* pData, int32_t Size) {
int32_t strOut_i = strGetSize(strOut);
for (int32_t i = 0; i < Size; i++) {
@ -48,13 +46,13 @@ char* strCut(char* strOut, char* strIn, char startSign, char endSign) {
int32_t iEnd = Size;
uint8_t isStart = 0;
uint8_t isEnd = 0;
intptr_t tLocation = (intptr_t)strchr(strIn, startSign);
if (tLocation != (intptr_t)NULL) {
iStart = tLocation - (intptr_t)strIn;
isStart = 1;
for (int32_t i = 0; i < Size; i++) {
if (strIn[i] == startSign) {
iStart = i;
isStart = 1;
break;
}
}
for (int32_t i = Size - 1; i >= 0; i--) {
if (strIn[i] == endSign) {
iEnd = i;
@ -62,13 +60,11 @@ char* strCut(char* strOut, char* strIn, char startSign, char endSign) {
break;
}
}
int outi = iEnd - iStart - 1;
//! use memcpy to increase performance
__platform_memcpy(strOut, &strIn[iStart + 1], outi);
int outi = 0;
for (int32_t i = iStart + 1; i < iEnd; i++) {
strOut[outi] = strIn[i];
outi++;
}
/* add \0 */
strOut[outi] = 0;
if (isStart && isEnd) {