restore simu

This commit is contained in:
pikastech 2021-11-16 21:29:27 +08:00
parent 3037a67c39
commit 71f487ab48
56 changed files with 3824 additions and 104 deletions

View File

@ -25,7 +25,6 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "pikaScript.h"
#include "PikaMain.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -57,19 +56,9 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define use_mem_pool 1
/* support mem pool */
#if use_mem_pool
/* use mem pool */
Pool pikaPool;
void* __impl_pikaMalloc(size_t size) {
void* mem = pool_malloc(&pikaPool, size);
return mem;
}
void __impl_pikaFree(void* ptrm, size_t size) {
pool_free(&pikaPool, ptrm, size);
}
#endif
/* USER CODE END 0 */
@ -78,7 +67,7 @@ void __impl_pikaFree(void* ptrm, size_t size) {
* @retval int
*/
int main(void)
{
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
@ -102,13 +91,9 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* init mem pool */
#if use_mem_pool
pikaPool = pool_init(0x4000, 8);
printf("[info]: pika memory poool init ok \r\n");
#endif
/* USER CODE BEGIN 2 */
PikaObj *pikaMain = pikaScriptInit();
@ -118,7 +103,7 @@ int main(void)
/* USER CODE BEGIN WHILE */
/* user input buff */
char inputBuff[256] = {0};
char inputBuff[256] = {0};
while (1)
{
static uint32_t r_mainloop_ncnt;
@ -127,10 +112,10 @@ int main(void)
fgets(inputBuff, sizeof(inputBuff), stdin);
printf(">>> %s", inputBuff);
/* run mimiScript and get res */
PikaObj *globals = obj_runDirect(pikaMain, inputBuff);
Args *resArgs = obj_runDirect(pikaMain, inputBuff);
/* get system output of mimiScript*/
char *sysOut = args_getSysOut(globals->list);
char *sysOut = args_getSysOut(resArgs);
if (!strEqu(sysOut, ""))
{
@ -139,7 +124,7 @@ int main(void)
}
/* deinit the res */
obj_deinit(globals);
args_deinit(resArgs);
/* USER CODE END WHILE */

View File

@ -133,7 +133,7 @@
<UseExternalTool>0</UseExternalTool>
<RunIndependent>0</RunIndependent>
<UpdateFlashBeforeDebugging>1</UpdateFlashBeforeDebugging>
<Capability>1</Capability>
<Capability>0</Capability>
<DriverSelection>4101</DriverSelection>
</Flash1>
<bUseTDR>1</bUseTDR>
@ -313,7 +313,7 @@
</ArmAdsMisc>
<Cads>
<interw>1</interw>
<Optim>1</Optim>
<Optim>5</Optim>
<oTime>0</oTime>
<SplitLS>0</SplitLS>
<OneElfS>1</OneElfS>
@ -381,33 +381,8 @@
</TargetOption>
<Groups>
<Group>
<GroupName>Booter</GroupName>
<GroupName>Application/MDK-ARM</GroupName>
<Files>
<File>
<FileName>stm32f1xx_hal_msp.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/stm32f1xx_hal_msp.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_it.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/stm32f1xx_it.c</FilePath>
</File>
<File>
<FileName>usart.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/usart.c</FilePath>
</File>
<File>
<FileName>gpio.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/gpio.c</FilePath>
</File>
<File>
<FileName>main.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/main.c</FilePath>
</File>
<File>
<FileName>startup_stm32f103xe.s</FileName>
<FileType>2</FileType>
@ -416,13 +391,38 @@
</Files>
</Group>
<Group>
<GroupName>Drivers</GroupName>
<GroupName>Application/User/Core</GroupName>
<Files>
<File>
<FileName>system_stm32f1xx.c</FileName>
<FileName>main.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/system_stm32f1xx.c</FilePath>
<FilePath>../Core/Src/main.c</FilePath>
</File>
<File>
<FileName>gpio.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/gpio.c</FilePath>
</File>
<File>
<FileName>usart.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/usart.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_it.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/stm32f1xx_it.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_hal_msp.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/stm32f1xx_hal_msp.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>Drivers/STM32F1xx_HAL_Driver</GroupName>
<Files>
<File>
<FileName>stm32f1xx_hal_gpio_ex.c</FileName>
<FileType>1</FileType>
@ -495,6 +495,16 @@
</File>
</Files>
</Group>
<Group>
<GroupName>Drivers/CMSIS</GroupName>
<Files>
<File>
<FileName>system_stm32f1xx.c</FileName>
<FileType>1</FileType>
<FilePath>../Core/Src/system_stm32f1xx.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>pikascript-core</GroupName>
<Files>
@ -528,21 +538,6 @@
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\dataMemory.c</FilePath>
</File>
<File>
<FileName>dataQueue.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\dataQueue.c</FilePath>
</File>
<File>
<FileName>dataQueueObj.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\dataQueueObj.c</FilePath>
</File>
<File>
<FileName>dataStack.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\dataStack.c</FilePath>
</File>
<File>
<FileName>dataString.c</FileName>
<FileType>1</FileType>
@ -553,45 +548,40 @@
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\dataStrs.c</FilePath>
</File>
<File>
<FileName>method.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\method.c</FilePath>
</File>
<File>
<FileName>PikaObj.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\PikaObj.c</FilePath>
</File>
<File>
<FileName>PikaParser.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\PikaParser.c</FilePath>
</File>
<File>
<FileName>PikaPlatform.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\PikaPlatform.c</FilePath>
</File>
<File>
<FileName>PikaVM.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\PikaVM.c</FilePath>
</File>
<File>
<FileName>TinyObj.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\TinyObj.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>pikascript-lib</GroupName>
<Files>
<File>
<FileName>PikaStdLib_MemChecker.c</FileName>
<FileName>PikaBlock.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\PikaStdLib\PikaStdLib_MemChecker.c</FilePath>
<FilePath>..\pikascript\pikascript-core\PikaBlock.c</FilePath>
</File>
<File>
<FileName>PikaStdLib_SysObj.c</FileName>
<FileName>PikaIf.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\PikaStdLib\PikaStdLib_SysObj.c</FilePath>
<FilePath>..\pikascript\pikascript-core\PikaIf.c</FilePath>
</File>
<File>
<FileName>PikaInvoke.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\PikaInvoke.c</FilePath>
</File>
<File>
<FileName>PikaWhile.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-core\PikaWhile.c</FilePath>
</File>
</Files>
</Group>
@ -603,6 +593,16 @@
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-api\PikaMain-api.c</FilePath>
</File>
<File>
<FileName>Device_LED-api.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-api\Device_LED-api.c</FilePath>
</File>
<File>
<FileName>Device_Uart-api.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-api\Device_Uart-api.c</FilePath>
</File>
<File>
<FileName>pikaScript.c</FileName>
<FileType>1</FileType>
@ -618,10 +618,30 @@
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-api\PikaStdLib_SysObj-api.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>pikascript-lib</GroupName>
<Files>
<File>
<FileName>PikaStdLib-api.c</FileName>
<FileName>PikaStdLib_SysObj.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-api\PikaStdLib-api.c</FilePath>
<FilePath>..\pikascript\pikascript-lib\PikaStdLib\PikaStdLib_SysObj.c</FilePath>
</File>
<File>
<FileName>PikaStdLib_MemChecker.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\PikaStdLib\PikaStdLib_MemChecker.c</FilePath>
</File>
<File>
<FileName>Device_Led.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\Device\Device_Led.c</FilePath>
</File>
<File>
<FileName>Device_Uart.c</FileName>
<FileType>1</FileType>
<FilePath>..\pikascript\pikascript-lib\Device\Device_Uart.c</FilePath>
</File>
</Files>
</Group>

View File

@ -0,0 +1,15 @@
from PikaObj import *
class LED(TinyObj):
def on():
pass
def off():
pass
class Uart(TinyObj):
def send(data:str):
pass
def setName(name:str):
pass
def printName():
pass

View File

@ -0,0 +1,14 @@
class TinyObj:
pass
class BaseObj(TinyObj):
pass
def print(val: any):
pass
def set(argPath: str, val: any):
pass

View File

@ -0,0 +1,27 @@
from PikaObj import *
class MemChecker(BaseObj):
def max():
pass
def now():
pass
def resetMax():
pass
class SysObj(BaseObj):
def type(argPath: str):
pass
def ls(objPath: str):
pass
def remove(argPath: str):
pass
def new(objPath: str, classPath: str):
pass

View File

@ -1,10 +1,16 @@
import PikaStdLib
from PikaObj import *
import Device
import PikaStdLib
led = Device.LED()
uart = Device.Uart()
mem = PikaStdLib.MemChecker()
print('hello pikascript!')
print('hello wrold')
uart.setName('com1')
uart.send('My name is:')
uart.printName()
print('mem used max:')
mem.max()
i = 0
while i < 100:
i = i + 1
print(i)
print('mem used now:')
mem.now()

View File

@ -0,0 +1,23 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#include "Device_LED.h"
#include "TinyObj.h"
#include <stdio.h>
#include <stdlib.h>
#include "BaseObj.h"
void Device_LED_offMethod(PikaObj *self, Args *args){
Device_LED_off(self);
}
void Device_LED_onMethod(PikaObj *self, Args *args){
Device_LED_on(self);
}
PikaObj *New_Device_LED(Args *args){
PikaObj *self = New_TinyObj(args);
class_defineMethod(self, "off()", Device_LED_offMethod);
class_defineMethod(self, "on()", Device_LED_onMethod);
return self;
}

View File

@ -0,0 +1,15 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#ifndef __Device_LED__H
#define __Device_LED__H
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"
PikaObj *New_Device_LED(Args *args);
void Device_LED_off(PikaObj *self);
void Device_LED_on(PikaObj *self);
#endif

View File

@ -0,0 +1,30 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#include "Device_Uart.h"
#include "TinyObj.h"
#include <stdio.h>
#include <stdlib.h>
#include "BaseObj.h"
void Device_Uart_printNameMethod(PikaObj *self, Args *args){
Device_Uart_printName(self);
}
void Device_Uart_sendMethod(PikaObj *self, Args *args){
char * data = args_getStr(args, "data");
Device_Uart_send(self, data);
}
void Device_Uart_setNameMethod(PikaObj *self, Args *args){
char * name = args_getStr(args, "name");
Device_Uart_setName(self, name);
}
PikaObj *New_Device_Uart(Args *args){
PikaObj *self = New_TinyObj(args);
class_defineMethod(self, "printName()", Device_Uart_printNameMethod);
class_defineMethod(self, "send(data:str)", Device_Uart_sendMethod);
class_defineMethod(self, "setName(name:str)", Device_Uart_setNameMethod);
return self;
}

View File

@ -0,0 +1,16 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#ifndef __Device_Uart__H
#define __Device_Uart__H
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"
PikaObj *New_Device_Uart(Args *args);
void Device_Uart_printName(PikaObj *self);
void Device_Uart_send(PikaObj *self, char * data);
void Device_Uart_setName(PikaObj *self, char * name);
#endif

View File

@ -0,0 +1,22 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#include "PikaMain.h"
#include "PikaStdLib_SysObj.h"
#include "Device_LED.h"
#include "PikaStdLib_MemChecker.h"
#include "Device_Uart.h"
#include <stdio.h>
#include <stdlib.h>
#include "BaseObj.h"
PikaObj *New_PikaMain(Args *args){
PikaObj *self = New_PikaStdLib_SysObj(args);
obj_import(self, "Device_LED", New_Device_LED);
obj_newObj(self, "led", "Device_LED");
obj_import(self, "PikaStdLib_MemChecker", New_PikaStdLib_MemChecker);
obj_newObj(self, "mem", "PikaStdLib_MemChecker");
obj_import(self, "Device_Uart", New_Device_Uart);
obj_newObj(self, "uart", "Device_Uart");
return self;
}

View File

@ -0,0 +1,13 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#ifndef __PikaMain__H
#define __PikaMain__H
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"
PikaObj *New_PikaMain(Args *args);
#endif

View File

@ -0,0 +1,28 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#include "PikaStdLib_MemChecker.h"
#include "BaseObj.h"
#include <stdio.h>
#include <stdlib.h>
#include "BaseObj.h"
void PikaStdLib_MemChecker_maxMethod(PikaObj *self, Args *args){
PikaStdLib_MemChecker_max(self);
}
void PikaStdLib_MemChecker_nowMethod(PikaObj *self, Args *args){
PikaStdLib_MemChecker_now(self);
}
void PikaStdLib_MemChecker_resetMaxMethod(PikaObj *self, Args *args){
PikaStdLib_MemChecker_resetMax(self);
}
PikaObj *New_PikaStdLib_MemChecker(Args *args){
PikaObj *self = New_BaseObj(args);
class_defineMethod(self, "max()", PikaStdLib_MemChecker_maxMethod);
class_defineMethod(self, "now()", PikaStdLib_MemChecker_nowMethod);
class_defineMethod(self, "resetMax()", PikaStdLib_MemChecker_resetMaxMethod);
return self;
}

View File

@ -0,0 +1,16 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#ifndef __PikaStdLib_MemChecker__H
#define __PikaStdLib_MemChecker__H
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"
PikaObj *New_PikaStdLib_MemChecker(Args *args);
void PikaStdLib_MemChecker_max(PikaObj *self);
void PikaStdLib_MemChecker_now(PikaObj *self);
void PikaStdLib_MemChecker_resetMax(PikaObj *self);
#endif

View File

@ -0,0 +1,38 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#include "PikaStdLib_SysObj.h"
#include "BaseObj.h"
#include <stdio.h>
#include <stdlib.h>
#include "BaseObj.h"
void PikaStdLib_SysObj_lsMethod(PikaObj *self, Args *args){
char * objPath = args_getStr(args, "objPath");
PikaStdLib_SysObj_ls(self, objPath);
}
void PikaStdLib_SysObj_newMethod(PikaObj *self, Args *args){
char * classPath = args_getStr(args, "classPath");
char * objPath = args_getStr(args, "objPath");
PikaStdLib_SysObj_new(self, classPath, objPath);
}
void PikaStdLib_SysObj_removeMethod(PikaObj *self, Args *args){
char * argPath = args_getStr(args, "argPath");
PikaStdLib_SysObj_remove(self, argPath);
}
void PikaStdLib_SysObj_typeMethod(PikaObj *self, Args *args){
char * argPath = args_getStr(args, "argPath");
PikaStdLib_SysObj_type(self, argPath);
}
PikaObj *New_PikaStdLib_SysObj(Args *args){
PikaObj *self = New_BaseObj(args);
class_defineMethod(self, "ls(objPath:str)", PikaStdLib_SysObj_lsMethod);
class_defineMethod(self, "new(objPath:str,classPath:str)", PikaStdLib_SysObj_newMethod);
class_defineMethod(self, "remove(argPath:str)", PikaStdLib_SysObj_removeMethod);
class_defineMethod(self, "type(argPath:str)", PikaStdLib_SysObj_typeMethod);
return self;
}

View File

@ -0,0 +1,17 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#ifndef __PikaStdLib_SysObj__H
#define __PikaStdLib_SysObj__H
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"
PikaObj *New_PikaStdLib_SysObj(Args *args);
void PikaStdLib_SysObj_ls(PikaObj *self, char * objPath);
void PikaStdLib_SysObj_new(PikaObj *self, char * classPath, char * objPath);
void PikaStdLib_SysObj_remove(PikaObj *self, char * argPath);
void PikaStdLib_SysObj_type(PikaObj *self, char * argPath);
#endif

View File

@ -0,0 +1 @@
Compiler { dist_path: "pikascript-api/", source_path: "", class_list: {"Device_LED": ClassInfo { this_class_name: "Device_LED", super_class_name: "TinyObj", method_list: {"off": MethodInfo { class_name: "Device_LED", name: "off", arg_list: None, return_type: None }, "on": MethodInfo { class_name: "Device_LED", name: "on", arg_list: None, return_type: None }}, object_list: {}, import_list: {}, script_list: Script { content: "" } }, "Device_Uart": ClassInfo { this_class_name: "Device_Uart", super_class_name: "TinyObj", method_list: {"printName": MethodInfo { class_name: "Device_Uart", name: "printName", arg_list: None, return_type: None }, "send": MethodInfo { class_name: "Device_Uart", name: "send", arg_list: Some(ArgList { py_arg_list: "data:str", list: {"data": PyArg { py_type: PyType { type_name: "str" }, name: "data" }} }), return_type: None }, "setName": MethodInfo { class_name: "Device_Uart", name: "setName", arg_list: Some(ArgList { py_arg_list: "name:str", list: {"name": PyArg { py_type: PyType { type_name: "str" }, name: "name" }} }), return_type: None }}, object_list: {}, import_list: {}, script_list: Script { content: "" } }, "PikaMain": ClassInfo { this_class_name: "PikaMain", super_class_name: "PikaStdLib_SysObj", method_list: {}, object_list: {"led": ObjectInfo { class_name: "PikaMain", name: "led", import_class_name: "Device_LED" }, "mem": ObjectInfo { class_name: "PikaMain", name: "mem", import_class_name: "PikaStdLib_MemChecker" }, "uart": ObjectInfo { class_name: "PikaMain", name: "uart", import_class_name: "Device_Uart" }}, import_list: {}, script_list: Script { content: " obj_run(pikaMain, \"print('hello wrold')\");\r\n obj_run(pikaMain, \"uart.setName('com1')\");\r\n obj_run(pikaMain, \"uart.send('My name is:')\");\r\n obj_run(pikaMain, \"uart.printName()\");\r\n obj_run(pikaMain, \"print('mem used max:')\");\r\n obj_run(pikaMain, \"mem.max()\");\r\n obj_run(pikaMain, \"print('mem used now:')\");\r\n obj_run(pikaMain, \"mem.now()\");\r\n" } }, "PikaStdLib_MemChecker": ClassInfo { this_class_name: "PikaStdLib_MemChecker", super_class_name: "BaseObj", method_list: {"max": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "max", arg_list: None, return_type: None }, "now": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "now", arg_list: None, return_type: None }, "resetMax": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "resetMax", arg_list: None, return_type: None }}, object_list: {}, import_list: {}, script_list: Script { content: "" } }, "PikaStdLib_SysObj": ClassInfo { this_class_name: "PikaStdLib_SysObj", super_class_name: "BaseObj", method_list: {"ls": MethodInfo { class_name: "PikaStdLib_SysObj", name: "ls", arg_list: Some(ArgList { py_arg_list: "objPath:str", list: {"objPath": PyArg { py_type: PyType { type_name: "str" }, name: "objPath" }} }), return_type: None }, "new": MethodInfo { class_name: "PikaStdLib_SysObj", name: "new", arg_list: Some(ArgList { py_arg_list: "objPath:str,classPath:str", list: {"classPath": PyArg { py_type: PyType { type_name: "str" }, name: "classPath" }, "objPath": PyArg { py_type: PyType { type_name: "str" }, name: "objPath" }} }), return_type: None }, "remove": MethodInfo { class_name: "PikaStdLib_SysObj", name: "remove", arg_list: Some(ArgList { py_arg_list: "argPath:str", list: {"argPath": PyArg { py_type: PyType { type_name: "str" }, name: "argPath" }} }), return_type: None }, "type": MethodInfo { class_name: "PikaStdLib_SysObj", name: "type", arg_list: Some(ArgList { py_arg_list: "argPath:str", list: {"argPath": PyArg { py_type: PyType { type_name: "str" }, name: "argPath" }} }), return_type: None }}, object_list: {}, import_list: {}, script_list: Script { content: "" } }}, class_now_name: Some("PikaMain") }

View File

@ -0,0 +1,21 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#include "PikaMain.h"
#include <stdio.h>
#include <stdlib.h>
PikaObj * pikaScriptInit(){
PikaObj * pikaMain = newRootObj("pikaMain", New_PikaMain);
obj_run(pikaMain, "print('hello wrold')");
obj_run(pikaMain, "uart.setName('com1')");
obj_run(pikaMain, "uart.send('My name is:')");
obj_run(pikaMain, "uart.printName()");
obj_run(pikaMain, "print('mem used max:')");
obj_run(pikaMain, "mem.max()");
obj_run(pikaMain, "print('mem used now:')");
obj_run(pikaMain, "mem.now()");
obj_run(pikaMain, "");
return pikaMain;
}

View File

@ -0,0 +1,13 @@
/* ******************************** */
/* Warning! Don't modify this file! */
/* ******************************** */
#ifndef __pikaScript__H
#define __pikaScript__H
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"
#include "PikaMain.h"
PikaObj * pikaScriptInit();
#endif

View File

@ -0,0 +1,2 @@
*kite*
*.o

View File

@ -0,0 +1,160 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "BaseObj.h"
#include "PikaObj.h"
#include "TinyObj.h"
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
static void* getClassPtr(PikaObj* classObj, char* classPath) {
char* ptrPath = classPath;
return obj_getPtr(classObj, ptrPath);
}
int32_t obj_newObjByFun(PikaObj* self,
char* objName,
char* className,
void* newFunPtr) {
/* class means subprocess init */
Args* buffs = New_strBuff();
/* class means subprocess init */
char* mataObjName = strsAppend(buffs, "[mate]", objName);
obj_setPtr(self, mataObjName, newFunPtr);
/* add void process Ptr, no inited */
args_setObjectWithClass(self->attributeList, objName, className, NULL);
args_deinit(buffs);
return 0;
}
int32_t sysObj_setObjbyClass(PikaObj* self, char* objName, char* classPath) {
/* class means subprocess init */
Args* buffs = New_strBuff();
PikaObj* classLoader = obj_getObj(self, "_clsld", 0);
void* newFunPtr = getClassPtr(classLoader, classPath);
/* class means subprocess init */
char* mataObjName = strsAppend(buffs, "[mate]", objName);
obj_setPtr(self, mataObjName, newFunPtr);
/* add void process Ptr, no inited */
args_setObjectWithClass(self->attributeList, objName, classPath, NULL);
args_deinit(buffs);
return 0;
}
static int32_t storeClassInfo(PikaObj* self,
Args* buffs,
char* classPath,
void* classPtr) {
int32_t res = 0;
PikaObj* classHost = obj_getObj(self, classPath, 1);
if (NULL == classHost) {
res = 1;
goto exit;
}
char* className = strsGetLastToken(buffs, classPath, '.');
char* classStoreName = className;
obj_setPtr(classHost, classStoreName, classPtr);
res = 0;
goto exit;
exit:
return res;
}
int32_t obj_import(PikaObj* self, char* className, NewFun classPtr) {
PikaObj* classLoader = obj_getObj(self, "_clsld", 0);
Args* buffs = New_args(NULL);
int32_t res = storeClassInfo(classLoader, buffs, className, classPtr);
args_deinit(buffs);
return res;
}
int32_t obj_newObj(PikaObj* self, char* objPath, char* classPath) {
PikaObj* classLoader = obj_getObj(self, "_clsld", 0);
Args* buffs = New_args(NULL);
int res = 0;
void* NewObjPtr = getClassPtr(classLoader, classPath);
if (NULL == NewObjPtr) {
res = 1;
goto exit;
}
PikaObj* objHost = obj_getObj(self, objPath, 1);
if (NULL == objHost) {
res = 2;
goto exit;
}
char* objName = strsGetLastToken(buffs, objPath, '.');
obj_newObjByFun(objHost, objName, classPath, NewObjPtr);
res = 0;
goto exit;
exit:
args_deinit(buffs);
return res;
}
static void print(PikaObj* self, Args* args) {
obj_setErrorCode(self, 0);
char* res = args_print(args, "val");
if (NULL == res) {
obj_setSysOut(self, "[error] print: can not print val");
obj_setErrorCode(self, 1);
return;
}
/* not empty */
obj_setSysOut(self, res);
}
static void set(PikaObj* self, Args* args) {
obj_setErrorCode(self, 0);
char* argPath = method_getStr(args, "argPath");
if (obj_isArgExist(self, argPath)) {
/* update arg */
char* valStr = args_print(args, "val");
int32_t res = obj_set(self, argPath, valStr);
if (1 == res) {
obj_setSysOut(self, "[error] set: arg no found.");
obj_setErrorCode(self, 1);
return;
}
if (2 == res) {
obj_setSysOut(self, "[error] set: type not match.");
obj_setErrorCode(self, 1);
return;
}
if (3 == res) {
obj_setSysOut(self, "[error] set: object not found.");
obj_setErrorCode(self, 1);
return;
}
return;
}
/* new arg */
Arg* val = args_getArg(args, "val");
Arg* newArg = arg_copy(val);
char* argName = strsGetLastToken(args, argPath, '.');
newArg = arg_setName(newArg, argName);
int32_t res = obj_setArg(self, argPath, newArg);
if (res == 1) {
obj_setSysOut(self, "[error] set: object not found.");
obj_setErrorCode(self, 1);
}
arg_deinit(newArg);
newArg = NULL;
return;
}
PikaObj* New_BaseObj(Args* args) {
PikaObj* self = New_TinyObj(args);
obj_setObjWithoutClass(self, "_clsld", New_TinyObj);
obj_getObj(self, "_clsld", 0);
class_defineMethod(self, "print(val:any)", print);
class_defineMethod(self, "set(argPath:str, val:any)", set);
return self;
}

View File

@ -0,0 +1,19 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _PikaObj_baseObj__H
#define _PikaObj_baseObj__H
#include "PikaObj.h"
#include "TinyObj.h"
#include "dataMemory.h"
PikaObj* New_BaseObj(Args* args);
int32_t obj_newObj(PikaObj* self, char* objPath, char* classPath);
int32_t obj_import(PikaObj* self, char* className, NewFun classPtr);
int32_t obj_newObjByFun(PikaObj* self,
char* objName,
char* className,
void* newFunPtr);
#endif

View File

@ -0,0 +1,88 @@
/*
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");
}

View File

@ -0,0 +1,22 @@
/*
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

View File

@ -0,0 +1,39 @@
/*
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);
}
}
}

View File

@ -0,0 +1,13 @@
/*
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

View File

@ -0,0 +1,296 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include <stdarg.h>
#include "PikaObj.h"
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
static int32_t loadArgByType(PikaObj* self,
char* definedName,
char* definedType,
char* sourceArgPath,
Args* args) {
if (strEqu(definedType, "any")) {
if (0 == obj_getAnyArg(self, definedName, sourceArgPath, args)) {
return 0;
}
/* solve arg faild */
return 3;
}
if (strEqu(definedType, "str")) {
/* solve the string type */
char* directStr = strsGetDirectStr(args, sourceArgPath);
if (NULL != directStr) {
/* direct value */
args_setStr(args, definedName, directStr);
/* ok */
return 0;
}
/* reference value */
char* refStr = obj_getStr(self, sourceArgPath);
if (NULL == refStr) {
/* faild */
return 1;
}
args_setStr(args, definedName, refStr);
/* succeed */
return 0;
}
if (strEqu(definedType, "int")) {
/* solve the int32_t type */
args_setInt(args, definedName, 0);
if ((sourceArgPath[0] >= '0') && (sourceArgPath[0] <= '9')) {
/* direct value */
args_set(args, definedName, sourceArgPath);
/* succeed */
return 0;
}
/* reference value */
if (!obj_isArgExist(self, sourceArgPath)) {
/* can not get reference */
return 3;
}
int32_t referenceVal = obj_getInt(self, sourceArgPath);
args_setInt(args, definedName, referenceVal);
/* succeed */
return 0;
}
if (strEqu(definedType, "float")) {
/* solve the float type */
args_setFloat(args, definedName, 0);
if ((sourceArgPath[0] >= '0') && (sourceArgPath[0] <= '9')) {
/* direct value */
args_set(args, definedName, sourceArgPath);
/* succeed */
return 0;
}
/* reference value */
if (!obj_isArgExist(self, sourceArgPath)) {
/* can not get reference */
return 3;
}
float referenceVal = obj_getFloat(self, sourceArgPath);
args_setFloat(args, definedName, referenceVal);
/* succeed */
return 0;
}
if (strEqu(definedType, "pointer")) {
/* only support reference value */
if (!obj_isArgExist(self, sourceArgPath)) {
/* can not get reference */
return 3;
}
void* ptr = obj_getPtr(self, sourceArgPath);
args_setPtr(args, definedName, ptr);
return 0;
}
/* type match faild */
return 2;
}
char* getTypeVal(Args* buffs, char* typeToken) {
if (!strIsContain(typeToken, ':')) {
return strsCopy(buffs, "");
}
return strsGetLastToken(buffs, typeToken, ':');
}
static Args* getArgsByNameMatch(PikaObj* self, char* typeList, char* argList) {
Args* buffs = New_strBuff();
char* typeListBuff = strsCopy(buffs, typeList);
Args* args = New_args(NULL);
while (1) {
char* typeToken = strsPopToken(buffs, typeListBuff, ',');
/* poped all type from typeList */
if (0 == typeToken[0]) {
break;
}
char* typeName = strsGetFirstToken(buffs, typeToken, ':');
char* typeVal = getTypeVal(buffs, typeToken);
char* argListBuff = strsCopy(buffs, argList);
while (1) {
char* argToken = strsPopToken(buffs, argListBuff, ',');
char* argName = strsGetFirstToken(buffs, argToken, '=');
char* argVal = strsGetLastToken(buffs, argToken, '=');
if (0 == argToken[0]) {
/* arg poped finised */
break;
}
if (!strEqu(typeName, argName)) {
/* name not match */
continue;
}
if (0 != loadArgByType(self, typeName, typeVal, argVal, args)) {
args_deinit(args);
args_deinit(buffs);
return NULL;
}
}
}
args_deinit(buffs);
return args;
}
static Args* getArgsBySort(PikaObj* self, char* typeList, char* argList) {
Args* buffs = New_strBuff();
char* typeListBuff = strsCopy(buffs, typeList);
char* argListBuff = strsCopy(buffs, argList);
Args* args = New_args(NULL);
while (1) {
char* typeToken = strsPopToken(buffs, typeListBuff, ',');
char* argToken = strsPopToken(buffs, argListBuff, ',');
if ((0 == argToken[0]) || (0 == typeToken[0])) {
/* arg or type poped finised */
break;
}
char* typeName = strsGetFirstToken(buffs, typeToken, ':');
char* typeVal = getTypeVal(buffs, typeToken);
char* argPath = argToken;
if (0 != loadArgByType(self, typeName, typeVal, argPath, args)) {
args_deinit(args);
args_deinit(buffs);
return NULL;
}
}
args_deinit(buffs);
return args;
}
static Args* getArgsBySentence(PikaObj* self, char* typeList, char* argList) {
if (strIsContain(argList, '=')) {
return getArgsByNameMatch(self, typeList, argList);
}
return getArgsBySort(self, typeList, argList);
}
static char* getMethodDeclearation(PikaObj* obj, char* methodName) {
Args* buffs = New_strBuff();
char* methodDeclearationPath = strsAppend(buffs, "[md]", methodName);
char* res = obj_getStr(obj, methodDeclearationPath);
args_deinit(buffs);
return res;
}
static void* getMethodPtr(PikaObj* methodHost, char* methodName) {
Args* buffs = New_strBuff();
char* methodPtrPath = strsAppend(buffs, "[mp]", methodName);
void* res = obj_getPtr(methodHost, methodPtrPath);
args_deinit(buffs);
return res;
}
Args* obj_invoke(PikaObj* self, char* cmd) {
/* the Args returned need to be deinit */
Args* res = New_args(NULL);
args_setErrorCode(res, 0);
Args* buffs = New_strBuff();
char* methodToken = strsGetFirstToken(buffs, cmd, '(');
char* methodPath = methodToken;
Args* args = NULL;
PikaObj* methodHostObj = obj_getObj(self, methodPath, 1);
PikaObj* methodHostClass = NULL;
if (NULL == methodHostObj) {
/* error, not found object */
args_setErrorCode(res, 1);
args_setSysOut(res, "[error] runner: object no found.");
goto exit;
}
char* methodName = strsGetLastToken(buffs, methodPath, '.');
void* classPtr = obj_getPtr(methodHostObj, "_clsptr");
char* methodHostClassName =
strsAppend(buffs, "classObj-", obj_getName(methodHostObj));
methodHostClass =
obj_getClassObjByNewFun(methodHostObj, methodHostClassName, classPtr);
/* get method Ptr */
void (*methodPtr)(PikaObj * self, Args * args) =
getMethodPtr(methodHostClass, methodName);
char* methodDecInClass = getMethodDeclearation(methodHostClass, methodName);
/* assert method*/
if ((NULL == methodDecInClass) || (NULL == methodPtr)) {
/* error, method no found */
args_setErrorCode(res, 2);
args_setSysOut(res, "[error] runner: method no found.");
goto exit;
}
char* methodDec = strsCopy(buffs, methodDecInClass);
/* free method host class to save memory */
obj_deinit(methodHostClass);
methodHostClass = NULL;
/* get type list */
char* typeList = strsCut(buffs, methodDec, '(', ')');
if (typeList == NULL) {
/* typeList no found */
args_setErrorCode(res, 3);
args_setSysOut(res, "[error] runner: type list no found.");
goto exit;
}
/* get arg list */
char* argList = strsCut(buffs, cmd, '(', ')');
{
if (argList == NULL) {
/* argL List no found */
args_setErrorCode(res, 4);
args_setSysOut(res, "[error] runner: arg list no found.");
goto exit;
}
}
/* get return type */
char* returnType = strsGetLastToken(buffs, methodDec, ')');
/* get args */
args = getArgsBySentence(self, typeList, argList);
if (NULL == args) {
/* get args faild */
args_setErrorCode(res, 5);
args_setSysOut(res, "[error] runner: solve arg faild.");
goto exit;
}
obj_setErrorCode(methodHostObj, 0);
obj_setSysOut(methodHostObj, "");
/* run method */
methodPtr(methodHostObj, args);
/* transfer return type */
args_setStr(res, "returnType", returnType);
/* transfer return */
args_copyArgByName(args, "return", res);
/* transfer sysOut */
char* sysOut = obj_getSysOut(methodHostObj);
if (NULL != sysOut) {
args_setSysOut(res, sysOut);
}
/* transfer errCode */
if (0 != obj_getErrorCode(methodHostObj)) {
/* method error */
args_setErrorCode(res, 6);
}
goto exit;
exit:
if (NULL != buffs) {
args_deinit(buffs);
}
if (NULL != methodHostClass) {
obj_deinit(methodHostClass);
}
if (NULL != args) {
args_deinit(args);
}
return res;
}

View File

@ -0,0 +1,9 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _invoke__H
#define _invoke__H
Args* obj_invoke(PikaObj* self, char* cmd);
#endif

View File

@ -0,0 +1,763 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "PikaObj.h"
#include <stdarg.h>
#include "PikaBlock.h"
#include "PikaIf.h"
#include "PikaInvoke.h"
#include "PikaWhile.h"
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
PikaObj* obj_getContext(PikaObj* self) {
return obj_getPtr(self, "_ctx");
}
void* getNewObjFunByClass(PikaObj* obj, char* classPath) {
PikaObj* classLoader = args_getPtr(obj->attributeList, "_clsld");
if (NULL == classLoader) {
return NULL;
}
void* (*newObjFun)(Args * initArgs) =
args_getPtr(classLoader->attributeList, classPath);
return newObjFun;
}
int32_t deinitEachSubObj(Arg* argEach, Args* handleArgs) {
if (NULL != handleArgs) {
/* error: tOhis handle not need handle args */
return 1;
}
char* type = arg_getType(argEach);
if (strIsStartWith(type, "_class")) {
PikaObj* subObj = arg_getPtr(argEach);
if (NULL != subObj) {
obj_deinit(subObj);
}
}
return 0;
}
void deinitAllSubObj(PikaObj* self) {
Args* args = self->attributeList;
args_foreach(args, deinitEachSubObj, NULL);
}
int32_t obj_deinit(PikaObj* self) {
deinitAllSubObj(self);
args_deinit(self->attributeList);
// DynMemPut(self->mem);
pikaFree(self, sizeof(PikaObj));
self = NULL;
return 0;
}
int32_t obj_enable(PikaObj* self) {
obj_setInt(self, "isEnable", 1);
return 0;
}
int32_t obj_disable(PikaObj* self) {
obj_setInt(self, "isEnable", 0);
return 0;
}
int32_t obj_setInt(PikaObj* self, char* argPath, int64_t val) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
/* [error] object no found */
return 1;
}
Args* buffs = New_strBuff();
char* name = strsGetLastToken(buffs, argPath, '.');
args_setInt(obj->attributeList, name, val);
args_deinit(buffs);
return 0;
}
int32_t obj_setPtr(PikaObj* self, char* argPath, void* pointer) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return 1;
}
Args* buffs = New_strBuff();
char* name = strsGetLastToken(buffs, argPath, '.');
args_setPtr(obj->attributeList, name, pointer);
args_deinit(buffs);
return 0;
}
int32_t obj_setFloat(PikaObj* self, char* argPath, float value) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return 1;
}
Args* buffs = New_strBuff();
char* name = strsGetLastToken(buffs, argPath, '.');
args_setFloat(obj->attributeList, name, value);
args_deinit(buffs);
return 0;
}
int32_t obj_setStr(PikaObj* self, char* argPath, char* str) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return 1;
}
Args* buffs = New_strBuff();
char* name = strsGetLastToken(buffs, argPath, '.');
args_setStr(obj->attributeList, name, str);
args_deinit(buffs);
return 0;
}
int64_t obj_getInt(PikaObj* self, char* argPath) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return -999999999;
}
Args* buffs = New_strBuff();
char* argName = strsGetLastToken(buffs, argPath, '.');
int res = args_getInt(obj->attributeList, argName);
args_deinit(buffs);
return res;
}
Arg* obj_getArg(PikaObj* self, char* argPath) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return NULL;
}
Args* buffs = New_strBuff();
char* argName = strsGetLastToken(buffs, argPath, '.');
Arg* res = args_getArg(obj->attributeList, argName);
args_deinit(buffs);
return res;
}
int32_t obj_setArg(PikaObj* self, char* argPath, Arg* arg) {
/* setArg would copy arg */
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
/* object no found */
return 1;
}
args_copyArg(obj->attributeList, arg);
return 0;
}
void* obj_getPtr(PikaObj* self, char* argPath) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return NULL;
}
Args* buffs = New_strBuff();
char* argName = strsGetLastToken(buffs, argPath, '.');
void* res = args_getPtr(obj->attributeList, argName);
args_deinit(buffs);
return res;
}
float obj_getFloat(PikaObj* self, char* argPath) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return -999.999;
}
Args* buffs = New_strBuff();
char* argName = strsGetLastToken(buffs, argPath, '.');
float res = args_getFloat(obj->attributeList, argName);
args_deinit(buffs);
return res;
}
char* obj_getStr(PikaObj* self, char* argPath) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
return NULL;
}
Args* buffs = New_strBuff();
char* argName = strsGetLastToken(buffs, argPath, '.');
char* res = args_getStr(obj->attributeList, argName);
args_deinit(buffs);
return res;
}
int32_t obj_load(PikaObj* self, Args* args, char* name) {
args_copyArgByName(args, name, self->attributeList);
return 0;
}
int32_t obj_setObjWithoutClass(PikaObj* self, char* objName, void* newFun) {
/* class means subprocess init */
Args* buffs = New_strBuff();
char* mataObjName = strsAppend(buffs, "[mate]", objName);
obj_setPtr(self, mataObjName, newFun);
/* add void object Ptr, no inited */
args_setObjectWithClass(self->attributeList, objName, "none", NULL);
args_deinit(buffs);
return 0;
}
int32_t obj_addOther(PikaObj* self, char* subObjectName, void* new_ObjectFun) {
Args* initArgs = New_args(NULL);
args_setPtr(initArgs, "_ctx", self);
void* (*new_Object)(Args * initArgs) = (void* (*)(Args*))new_ObjectFun;
void* subObject = new_Object(initArgs);
obj_setPtr(self, subObjectName, subObject);
args_deinit(initArgs);
return 0;
}
int32_t obj_freeObj(PikaObj* self, char* objPath) {
PikaObj* obj = obj_getPtr(self, objPath);
obj_deinit(obj);
return 0;
}
int32_t obj_bind(PikaObj* self, char* type, char* name, void* pointer) {
args_bind(self->attributeList, type, name, pointer);
return 0;
}
char* obj_print(PikaObj* self, char* name) {
if (NULL == self) {
return NULL;
}
return args_print(self->attributeList, name);
}
int32_t obj_bindInt(PikaObj* self, char* name, int32_t* valPtr) {
args_bindInt(self->attributeList, name, valPtr);
return 0;
}
int32_t obj_bindFloat(PikaObj* self, char* name, float* valPtr) {
args_bindFloat(self->attributeList, name, valPtr);
return 0;
}
int32_t obj_bindString(PikaObj* self, char* name, char** valPtr) {
args_bindStr(self->attributeList, name, valPtr);
return 0;
}
int32_t obj_set(PikaObj* self, char* argPath, char* valStr) {
PikaObj* obj = obj_getObj(self, argPath, 1);
if (NULL == obj) {
/* cant get object */
return 3;
}
Args* buffs = New_strBuff();
char* argName = strsGetLastToken(buffs, argPath, '.');
int32_t res = args_set(obj->attributeList, argName, valStr);
args_deinit(buffs);
if (res == 1) {
/* do not get arg */
return 1;
}
if (res == 2) {
/* type not match */
return 2;
}
/* succeed */
return 0;
}
PikaObj* obj_getClassObjByNewFun(PikaObj* context,
char* name,
NewFun newClassFun) {
Args* initArgs = New_args(NULL);
args_setPtr(initArgs, "_ctx", context);
args_setStr(initArgs, "_n", name);
PikaObj* thisClass = newClassFun(initArgs);
obj_setPtr(thisClass, "_clsptr", newClassFun);
args_deinit(initArgs);
return thisClass;
}
char* obj_getClassPath(PikaObj* objHost, Args* buffs, char* objName) {
Arg* objArg = obj_getArg(objHost, objName);
char* objType = arg_getType(objArg);
char* classPath = strsRemovePrefix(buffs, objType, "_class-");
return classPath;
}
void* getNewClassObjFunByName(PikaObj* obj, char* name) {
Args* buffs = New_strBuff();
char* classPath = strsAppend(buffs, "[mate]", name);
/* init the subprocess */
void* (*newClassObjFun)(Args * initArgs) =
args_getPtr(obj->attributeList, classPath);
args_deinit(buffs);
return newClassObjFun;
}
int32_t removeEachMethodInfo(Arg* argNow, Args* argList) {
if (strIsStartWith(arg_getName(argNow), "[md]")) {
args_removeArg(argList, arg_getName(argNow));
return 0;
}
if (strIsStartWith(arg_getName(argNow), "[mp]")) {
args_removeArg(argList, arg_getName(argNow));
return 0;
}
return 0;
}
PikaObj* removeMethodInfo(PikaObj* thisClass) {
args_foreach(thisClass->attributeList, removeEachMethodInfo,
thisClass->attributeList);
return thisClass;
}
static void removeClassLoader(PikaObj* obj) {
PikaObj* classLoader = args_getPtr(obj->attributeList, "_clsld");
if (NULL != classLoader) {
obj_deinit(classLoader);
args_removeArg(obj->attributeList, "_clsld");
}
}
PikaObj* newRootObj(char* name, NewFun newObjFun) {
PikaObj* thisClass = obj_getClassObjByNewFun(NULL, name, newObjFun);
PikaObj* newObj = removeMethodInfo(thisClass);
return newObj;
}
PikaObj* initObj(PikaObj* obj, char* name) {
PikaObj* res = NULL;
NewFun newObjFun = getNewClassObjFunByName(obj, name);
Args* buffs = New_args(NULL);
if (NULL == newObjFun) {
/* no such object */
res = NULL;
goto exit;
}
PikaObj* thisClass = obj_getClassObjByNewFun(obj, name, newObjFun);
PikaObj* newObj = removeMethodInfo(thisClass);
/* delete [mate]<objName> */
obj_removeArg(obj, strsAppend(buffs, "[mate]", name));
/* delete "_clsld" object */
removeClassLoader(newObj);
char* type = args_getType(obj->attributeList, name);
args_setPtrWithType(obj->attributeList, name, type, newObj);
res = obj_getPtr(obj, name);
goto exit;
exit:
args_deinit(buffs);
return res;
}
PikaObj* obj_getObjDirect(PikaObj* self, char* name) {
if (NULL == self) {
return NULL;
}
/* check subprocess */
if (NULL == args_getPtr(self->attributeList, name)) {
/* no inited subprocess, check subprocess init fun*/
return initObj(self, name);
}
/* finded subscribe, check type*/
char* type = args_getType(self->attributeList, name);
if (!strIsStartWith(type, "_class")) {
/* type error, could not found subprocess */
return NULL;
}
return obj_getPtr(self, name);
}
PikaObj* obj_getObj(PikaObj* self, char* objPath, int32_t keepDeepth) {
Args* buffs = New_strBuff();
char* objPathBuff = strsCopy(buffs, objPath);
int32_t tokenNum = strGetTokenNum(objPath, '.');
PikaObj* obj = self;
for (int32_t i = 0; i < tokenNum - keepDeepth; i++) {
char* token = strsPopToken(buffs, objPathBuff, '.');
obj = obj_getObjDirect(obj, token);
if (obj == NULL) {
goto exit;
}
}
goto exit;
exit:
args_deinit(buffs);
return obj;
}
char* obj_getName(PikaObj* self) {
return obj_getStr(self, "_n");
}
void loadMethodInfo(PikaObj* methodHost,
char* methodName,
char* methodDeclearation,
void* methodPtr) {
Args* buffs = New_strBuff();
char* methodPtrPath = strsAppend(buffs, "[mp]", methodName);
char* methodDeclearationPath = strsAppend(buffs, "[md]", methodName);
obj_setPtr(methodHost, methodPtrPath, methodPtr);
obj_setStr(methodHost, methodDeclearationPath, methodDeclearation);
args_deinit(buffs);
}
int32_t class_defineMethod(PikaObj* self,
char* declearation,
void (*methodPtr)(PikaObj* self, Args* args)) {
int32_t size = strGetSize(declearation);
int32_t res = 0;
Args* buffs = New_strBuff();
char* cleanDeclearation =
strDeleteChar(args_getBuff(buffs, size), declearation, ' ');
char* methodPath =
strGetFirstToken(args_getBuff(buffs, size), cleanDeclearation, '(');
PikaObj* methodHost = obj_getObj(self, methodPath, 1);
if (NULL == methodHost) {
/* no found method object */
res = 1;
goto exit;
}
char* methodName = strsGetLastToken(buffs, methodPath, '.');
loadMethodInfo(methodHost, methodName, cleanDeclearation, methodPtr);
res = 0;
goto exit;
exit:
args_deinit(buffs);
return res;
}
static void transferReturnVal(PikaObj* self,
char* returnType,
char* returnName,
Args* args) {
if (strEqu("->int", returnType)) {
int returnVal = args_getInt(args, "return");
obj_setInt(self, returnName, returnVal);
return;
}
if (strEqu("->float", returnType)) {
float returnVal = args_getFloat(args, "return");
obj_setFloat(self, returnName, returnVal);
return;
}
if (strEqu("->str", returnType)) {
char* returnVal = args_getStr(args, "return");
obj_setStr(self, returnName, returnVal);
return;
}
}
char* getRightCmd(Args* buffs, char* cmd) {
char* right = NULL;
char* cmdBuff = strsCopy(buffs, cmd);
if (strIsContain(cmd, '(')) {
cmdBuff = strsGetFirstToken(buffs, cmdBuff, '(');
}
if (strIsContain(cmdBuff, '=')) {
cmdBuff = strsCopy(buffs, cmd);
strsPopToken(buffs, cmdBuff, '=');
right = cmdBuff;
} else {
right = cmd;
}
return right;
}
uint8_t obj_getAnyArg(PikaObj* self,
char* targetArgName,
char* sourceArgPath,
Args* targetArgs) {
if (0 == args_setLiteral(targetArgs, targetArgName, sourceArgPath)) {
return 0;
}
if (0 == obj_getRefArg(self, targetArgName, sourceArgPath, targetArgs)) {
return 0;
}
/* solve arg faild */
return 3;
}
uint8_t obj_getRefArg(PikaObj* self,
char* targetArgName,
char* sourceArgPath,
Args* targetArgs) {
/* get reference arg */
Arg* arg = obj_getArg(self, sourceArgPath);
if (arg == NULL) {
/* can not get arg */
return 3;
}
Arg* argCopied = arg_copy(arg);
argCopied = arg_setName(argCopied, targetArgName);
args_setArg(targetArgs, argCopied);
return 0;
}
Args* getRightRes(PikaObj* self, char* cmd) {
Args* buffs = New_strBuff();
Args* res = NULL;
if (strIsContain(cmd, '(') && strIsContain(cmd, ')')) {
res = obj_invoke(self, cmd);
goto exit;
}
res = New_args(NULL);
args_setSysOut(res, "");
int err = obj_getAnyArg(self, "return", cmd, res);
if (err != 0) {
args_setSysOut(res, "[error] get value faild.");
args_setErrorCode(res, 1);
goto exit;
}
char* returnType = args_getType(res, "return");
returnType = strsAppend(buffs, "->", returnType);
args_setStr(res, "returnType", returnType);
goto exit;
exit:
args_deinit(buffs);
return res;
}
Args* obj_runScript(PikaObj* self, char* cmd) {
Args* buffs = New_strBuff();
Args* res = NULL;
cmd = strsGetCleanCmd(buffs, cmd);
/* get right cmd */
char* right = getRightCmd(buffs, cmd);
/* get res from right cmd */
res = getRightRes(self, right);
if (NULL != res) {
if (0 != args_getErrorCode(res)) {
goto exit;
}
}
/* check res */
if (NULL == res) {
res = New_args(NULL);
args_setErrorCode(res, 1);
args_setSysOut(res, "[error] solve script format faild!");
goto exit;
}
/* transfer return */
if (strIsContain(cmd, '=')) {
char* returnName = strsGetFirstToken(buffs, cmd, '=');
returnName = strsDeleteChar(buffs, returnName, ' ');
char* returnType = args_getStr(res, "returnType");
transferReturnVal(self, returnType, returnName, res);
}
exit:
args_deinit(buffs);
return res;
}
Args* obj_runDirect(PikaObj* self, char* cmd) {
Args* buffs = New_strBuff();
Args* res = NULL;
cmd = strsDeleteChar(buffs, cmd, '\n');
/* 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);
goto exit;
}
if (strEqu(block_getMode(block), "while")) {
while_pushLine(block, cmd);
goto exit;
}
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 */
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 */
goto exit;
}
/* run script */
if (strIsContain(cmd, '(') && strIsContain(cmd, ')')) {
res = obj_runScript(self, cmd);
goto exit;
}
/* run script */
if (strIsContain(cmd, '=')) {
res = obj_runScript(self, cmd);
goto exit;
}
exit:
/* check res */
if (NULL == res) {
res = New_args(NULL);
args_setErrorCode(res, 0);
args_setSysOut(res, "");
goto exit;
}
args_deinit(buffs);
return res;
}
int32_t obj_removeArg(PikaObj* self, char* argPath) {
PikaObj* objHost = obj_getObj(self, argPath, 1);
PikaObj* obj = obj_getObj(self, argPath, 0);
Args* buffs = New_strBuff();
if (NULL != obj) {
obj_deinit(obj);
}
int32_t err = 0;
if (NULL == objHost) {
/* [error] object no found */
err = 1;
goto exit;
}
char* argName = strsGetLastToken(buffs, argPath, '.');
int32_t res = args_removeArg(objHost->attributeList, argName);
if (1 == res) {
/*[error] not found arg*/
err = 2;
goto exit;
}
goto exit;
exit:
args_deinit(buffs);
return err;
}
int32_t obj_isArgExist(PikaObj* self, char* argPath) {
PikaObj* obj = obj_getObj(self, argPath, 1);
Args* buffs = New_strBuff();
int32_t res = 0;
if (NULL == obj) {
/* [error] object no found */
res = 1;
goto exit;
}
char* argName = strsGetLastToken(buffs, argPath, '.');
Arg* arg = args_getArg(obj->attributeList, argName);
if (NULL == arg) {
/* no found arg */
res = 0;
goto exit;
}
/* found arg */
res = 1;
goto exit;
exit:
args_deinit(buffs);
return res;
}
void obj_runNoRes(PikaObj* slef, char* cmd) {
/* unsafe, nothing would happend when error occord */
args_deinit(obj_runDirect(slef, cmd));
}
void obj_run(PikaObj* self, char* cmd) {
/* safe, stop when error occord and error info would be print32_t */
Args* res = obj_runDirect(self, cmd);
char* sysOut = args_getSysOut(res);
uint8_t errcode = args_getErrorCode(res);
obj_setSysOut(self, sysOut);
obj_setErrorCode(self, errcode);
if (!strEqu("", sysOut)) {
printf("%s\r\n", sysOut);
}
if (0 != errcode) {
printf("[info] input commond: %s\r\n", cmd);
while (1)
;
}
if (NULL != res) {
args_deinit(res);
}
}
void obj_setErrorCode(PikaObj* self, int32_t errCode) {
obj_setInt(self, "__errCode", errCode);
}
int32_t obj_getErrorCode(PikaObj* self) {
if (!obj_isArgExist(self, "__errCode")) {
return 0;
}
return obj_getInt(self, "__errCode");
}
void args_setErrorCode(Args* args, int32_t errCode) {
args_setInt(args, "__errCode", errCode);
}
int32_t args_getErrorCode(Args* args) {
if (!args_isArgExist(args, "__errCode")) {
return 0;
}
return args_getInt(args, "__errCode");
}
void obj_setSysOut(PikaObj* self, char* str) {
obj_setStr(self, "__sysOut", str);
}
char* obj_getSysOut(PikaObj* self) {
return obj_getStr(self, "__sysOut");
}
char* args_getSysOut(Args* args) {
return args_getStr(args, "__sysOut");
}
void args_setSysOut(Args* args, char* str) {
args_setStr(args, "__sysOut", str);
}
void obj_sysPrintf(PikaObj* self, char* fmt, ...) {
va_list args;
va_start(args, fmt);
char sysOut[128] = {0};
vsprintf(sysOut, fmt, args);
obj_setSysOut(self, sysOut);
va_end(args);
}

View File

@ -0,0 +1,99 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _Process__H
#define _Process__H
#include "dataArgs.h"
#include "dataLink.h"
#include "dataMemory.h"
#include "method.h"
typedef struct PikaObj_t PikaObj;
typedef PikaObj* (*NewFun)(Args* args);
typedef PikaObj* (*InitFun)(PikaObj* self, Args* args);
struct PikaObj_t {
/* list */
Args* attributeList;
};
/* operation */
int32_t obj_deinit(PikaObj* self);
int32_t obj_init(PikaObj* self, Args* args);
int32_t obj_update(PikaObj* self);
int32_t obj_enable(PikaObj* self);
int32_t obj_disable(PikaObj* self);
// arg type operations
int32_t obj_setInt(PikaObj* self, char* argPath, int64_t val);
int32_t obj_setPtr(PikaObj* self, char* argPath, void* pointer);
int32_t obj_setFloat(PikaObj* self, char* argPath, float value);
int32_t obj_setStr(PikaObj* self, char* argPath, char* str);
int32_t obj_setArg(PikaObj* self, char* argPath, Arg* arg);
void* obj_getPtr(PikaObj* self, char* argPath);
float obj_getFloat(PikaObj* self, char* argPath);
char* obj_getStr(PikaObj* self, char* argPath);
int64_t obj_getInt(PikaObj* self, char* argPath);
Arg* obj_getArg(PikaObj* self, char* argPath);
char* obj_getName(PikaObj* self);
// arg general operations
int32_t obj_bind(PikaObj* self, char* type, char* name, void* pointer);
int32_t obj_set(PikaObj* self, char* name, char* valStr);
int32_t obj_bindInt(PikaObj* self, char* name, int32_t* valPtr);
int32_t obj_bindFloat(PikaObj* self, char* name, float* valPtr);
int32_t obj_bindString(PikaObj* self, char* name, char** valPtr);
char* obj_print(PikaObj* self, char* name);
// args operations
int32_t obj_load(PikaObj* self, Args* args, char* name);
// subObject
int32_t obj_addOther(PikaObj* self, char* subObjectName, void* new_projcetFun);
int32_t obj_setObjWithoutClass(PikaObj* self,
char* subObjectName,
void* new_projcetFun);
PikaObj* obj_getObjDirect(PikaObj* self, char* name);
PikaObj* obj_getObj(PikaObj* self, char* processPathectory, int32_t deepth);
// subProcess
int32_t obj_freeObj(PikaObj* self, char* subObjectName);
/* method */
int32_t class_defineMethod(PikaObj* self,
char* declearation,
void (*methodPtr)(PikaObj* self, Args* args));
void obj_runNoRes(PikaObj* slef, char* cmd);
void obj_run(PikaObj* self, char* cmd);
Args* obj_runDirect(PikaObj* self, char* cmd);
int32_t obj_removeArg(PikaObj* self, char* argPath);
int32_t obj_isArgExist(PikaObj* self, char* argPath);
PikaObj* obj_getClassObjByNewFun(PikaObj* self, char* name, NewFun newClassFun);
PikaObj* newRootObj(char* name, NewFun newObjFun);
void obj_setErrorCode(PikaObj* self, int32_t errCode);
int32_t obj_getErrorCode(PikaObj* self);
void obj_setSysOut(PikaObj* self, char* str);
char* args_getSysOut(Args* args);
void args_setErrorCode(Args* args, int32_t errCode);
int32_t args_getErrorCode(Args* args);
void args_setSysOut(Args* args, char* str);
char* obj_getSysOut(PikaObj* self);
void obj_sysPrintf(PikaObj* self, char* fmt, ...);
PikaObj* obj_getContext(PikaObj* self);
uint8_t obj_getRefArg(PikaObj* self,
char* targetArgName,
char* sourceArgPath,
Args* targetArgs);
uint8_t obj_getAnyArg(PikaObj* self,
char* targetArgName,
char* sourceArgPath,
Args* targetArgs);
#endif

View File

@ -0,0 +1,47 @@
/*
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);
}

View File

@ -0,0 +1,14 @@
/*
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

View File

@ -0,0 +1 @@
# PikaScript 运行时内核

View File

@ -0,0 +1,34 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "PikaObj.h"
void _UpdateHandle(PikaObj* self) {
// override the handle function here
}
void _beforDinit(PikaObj* self) {
/* override in user code */
}
PikaObj* New_TinyObj(Args* args) {
PikaObj* self = pikaMalloc(sizeof(PikaObj));
/* List */
self->attributeList = New_args(NULL);
/* attribute */
obj_setPtr(self, "_ctx", self);
obj_setStr(self, "_n", "root");
/* load */
if (NULL != args) {
obj_load(self, args, "_n");
obj_load(self, args, "_ctx");
}
/* hard attribute */
return self;
}

View File

@ -0,0 +1,10 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef __TYNYOBJ__H
#define __TYNYOBJ__H
#include "PikaObj.h"
PikaObj* New_TinyObj(Args* args);
#endif

View File

@ -0,0 +1,327 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataArg.h"
#include <stdint.h>
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataString.h"
#include "stdlib.h"
void arg_deinit(Arg* self) {
arg_freeContent(self);
}
uint16_t arg_getTotleSize(Arg* self) {
return content_totleSize(self);
}
uint16_t content_sizeOffset(uint8_t* self) {
const uint8_t nextLength = sizeof(uint8_t*);
return nextLength;
}
uint16_t content_getSize(uint8_t* self) {
uint16_t size = 0;
// add 0x30 to avoid 0
size += self[content_sizeOffset(self) + 1];
size = (size << 8);
size += self[content_sizeOffset(self)];
return size;
}
void content_setNext(uint8_t* self, uint8_t* next) {
uint8_t* nextDir = self + content_nextOffset(self);
uint64_t pointerTemp = (uint64_t)next;
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
// aboid \0
nextDir[i] = pointerTemp;
pointerTemp = pointerTemp >> 8;
}
}
uint8_t* content_init(char* name,
char* type,
uint8_t* content,
uint16_t size,
uint8_t* next) {
const uint8_t nextLength = sizeof(uint8_t*);
const uint8_t sizeLength = 2;
uint16_t nameSize = strGetSize(name);
uint16_t typeSize = strGetSize(type);
uint8_t* self = (uint8_t*)pikaMalloc(nextLength + sizeLength + nameSize + 1 +
size + typeSize + 1);
uint8_t* nextDir = self;
uint8_t* sizeDir = nextDir + nextLength;
uint8_t* nameDir = sizeDir + sizeLength;
uint8_t* contentDir = nameDir + nameSize + 1;
uint8_t* typeDir = contentDir + size;
memcpy(nameDir, name, nameSize + 1);
memcpy(typeDir, type, typeSize + 1);
sizeDir[0] = size;
sizeDir[1] = size >> 8;
if (NULL != content) {
memcpy(contentDir, content, size);
} else {
memset(contentDir, 0, size);
}
uint64_t pointerTemp = (uint64_t)next;
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
// aboid \0
nextDir[i] = pointerTemp;
pointerTemp = pointerTemp >> 8;
}
return self;
}
uint16_t content_totleSize(uint8_t* self) {
char* name = content_getName(self);
char* type = content_getType(self);
const uint8_t sizeLenth = 2;
const uint8_t nextLength = sizeof(uint8_t*);
uint16_t size = content_getSize(self);
return size + strGetSize(name) + 1 + strGetSize(type) + 1 + sizeLenth +
nextLength;
}
void arg_freeContent(Arg* self) {
if (NULL != self) {
content_deinit(self);
}
}
uint8_t content_nameOffset(uint8_t* self) {
const uint8_t nextLength = sizeof(uint8_t*);
const uint8_t sizeLength = 2;
return nextLength + sizeLength;
}
char* content_getName(uint8_t* self) {
return (char*)self + content_nameOffset(self);
}
uint8_t* content_deinit(uint8_t* self) {
uint16_t totleSize = content_totleSize(self);
pikaFree(self, totleSize);
return 0;
}
uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
if (NULL == self) {
return content_init("", "", content, size, NULL);
}
char* name = content_getName(self);
char* type = content_getType(self);
uint8_t* next = content_getNext(self);
uint8_t* newContent = content_init(name, type, content, size, next);
content_deinit(self);
return newContent;
}
uint8_t* content_setName(uint8_t* self, char* name) {
if (NULL == self) {
return content_init(name, "", NULL, 0, NULL);
}
char* type = content_getType(self);
uint8_t* content = content_getContent(self);
uint16_t size = content_getSize(self);
uint8_t* next = content_getNext(self);
uint8_t* newContent = content_init(name, type, content, size, next);
content_deinit(self);
return newContent;
}
uint8_t* content_setType(uint8_t* self, char* type) {
if (NULL == self) {
return content_init("", type, NULL, 0, NULL);
}
char* name = content_getName(self);
uint8_t* content = content_getContent(self);
uint16_t size = content_getSize(self);
uint8_t* next = content_getNext(self);
uint8_t* newContent = content_init(name, type, content, size, next);
content_deinit(self);
return newContent;
}
Arg* arg_newContent(Arg* self, uint32_t size) {
uint8_t* newContent = content_init("", "", NULL, size, NULL);
arg_freeContent(self);
return newContent;
}
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size) {
return content_setContent(self, content, size);
}
Arg* arg_setName(Arg* self, char* name) {
return content_setName(self, name);
}
Arg* arg_setType(Arg* self, char* type) {
return content_setType(self, type);
}
char* content_getType(uint8_t* self) {
return (char*)self + content_typeOffset(self);
}
uint16_t content_contentOffset(uint8_t* self) {
const uint8_t nextLength = sizeof(uint8_t*);
const uint8_t sizeLength = 2;
char* name = content_getName(self);
return nextLength + sizeLength + strGetSize(name) + 1;
}
uint16_t content_nextOffset(uint8_t* self) {
return 0;
}
uint8_t* content_getNext(uint8_t* self) {
uint8_t* nextDir = self + content_nextOffset(self);
uint8_t* next = NULL;
uint64_t pointerTemp = 0;
for (int32_t i = sizeof(uint8_t*); i > -1; i--) {
// avoid \0
uint8_t val = nextDir[i];
pointerTemp = (pointerTemp << 8);
pointerTemp += val;
}
next = (uint8_t*)pointerTemp;
return next;
}
uint8_t* content_getContent(uint8_t* self) {
return self + content_contentOffset(self);
}
uint8_t* arg_getContent(Arg* self) {
return content_getContent(self);
}
Arg* arg_setInt(Arg* self, char* name, int64_t val) {
int64_t int64Temp = val;
uint8_t contentBuff[8];
for (uint32_t i = 0; i < 4; i++) {
// add 0x30 to void \0
contentBuff[i] = int64Temp;
int64Temp = int64Temp >> 8;
}
return content_init(name, "int", contentBuff, 4, NULL);
}
Arg* arg_setFloat(Arg* self, char* name, float val) {
uint8_t contentBuff[4];
uint8_t* valPtr = (uint8_t*)&val;
for (uint32_t i = 0; i < 4; i++) {
// add 0x30 to void \0
contentBuff[i] = valPtr[i];
}
return content_init(name, "float", contentBuff, 4, NULL);
}
float arg_getFloat(Arg* self) {
if (NULL == arg_getContent(self)) {
return -999.999;
}
float valOut = 0;
uint8_t* valOutPtr = (uint8_t*)(&valOut);
uint8_t* valPtr = arg_getContent(self);
for (uint32_t i = 0; i < 4; i++) {
valOutPtr[i] = valPtr[i];
}
return valOut;
}
Arg* arg_setPtr(Arg* self, char* name, char* type, void* pointer) {
uint64_t pointerTemp = (uint64_t)pointer;
uint8_t contentBuff[8];
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
// aboid \0
contentBuff[i] = pointerTemp;
pointerTemp = pointerTemp >> 8;
}
return content_init(name, type, contentBuff, sizeof(uint8_t*), NULL);
}
Arg* arg_setStr(Arg* self, char* name, char* string) {
return content_init(name, "str", (uint8_t*)string, strGetSize(string) + 1,
NULL);
}
int64_t arg_getInt(Arg* self) {
if (NULL == arg_getContent(self)) {
return -999999;
}
int64_t int64Temp = 0;
for (int32_t i = 3; i > -1; i--) {
// add 0x30 to avoid 0
int64Temp = (int64Temp << 8);
int64Temp += arg_getContent(self)[i];
}
return int64Temp;
}
void* arg_getPtr(Arg* self) {
void* pointer = NULL;
uint64_t pointerTemp = 0;
if (NULL == arg_getContent(self)) {
return NULL;
}
uint8_t* content = arg_getContent(self);
for (int32_t i = sizeof(uint8_t*) - 1; i > -1; i--) {
// avoid \0
uint8_t val = content[i];
pointerTemp = (pointerTemp << 8);
pointerTemp += val;
}
pointer = (void*)pointerTemp;
return pointer;
}
char* arg_getStr(Arg* self) {
return (char*)arg_getContent(self);
}
uint16_t content_typeOffset(uint8_t* self) {
const uint8_t nextLength = sizeof(uint8_t*);
const uint8_t sizeLength = 2;
uint16_t size = content_getSize(self);
char* name = content_getName(self);
uint16_t nameSize = strGetSize(name);
return nextLength + sizeLength + nameSize + 1 + size;
}
char* arg_getName(Arg* self) {
return content_getName(self);
}
char* arg_getType(Arg* self) {
return content_getType(self);
}
uint16_t arg_getContentSize(Arg* self) {
return content_getSize(self);
}
Arg* New_arg(void* voidPointer) {
return NULL;
}
Arg* arg_copy(Arg* argToBeCopy) {
Arg* argCopied = New_arg(NULL);
argCopied = arg_setContent(argCopied, arg_getContent(argToBeCopy),
arg_getContentSize(argToBeCopy));
argCopied = arg_setName(argCopied, arg_getName(argToBeCopy));
argCopied = arg_setType(argCopied, arg_getType(argToBeCopy));
return argCopied;
}

View File

@ -0,0 +1,65 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _arg__H
#define _arg__H
#include "dataMemory.h"
typedef uint8_t Arg;
uint16_t content_typeOffset(uint8_t* content);
uint16_t content_contentOffset(uint8_t* content);
uint16_t content_sizeOffset(uint8_t* self);
uint16_t content_contentOffset(uint8_t* self);
uint16_t content_nextOffset(uint8_t* self);
uint8_t content_nameOffset(uint8_t* self);
char* content_getName(uint8_t* content);
char* content_getType(uint8_t* content);
uint8_t* content_getNext(uint8_t* self);
uint16_t content_getSize(uint8_t* self);
uint8_t* content_getContent(uint8_t* content);
uint16_t content_totleSize(uint8_t* self);
uint8_t* content_init(char* name,
char* type,
uint8_t* content,
uint16_t size,
uint8_t* next);
uint8_t* content_deinit(uint8_t* self);
uint8_t* content_setName(uint8_t* self, char* name);
uint8_t* content_setType(uint8_t* self, char* type);
uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size);
void content_setNext(uint8_t* self, uint8_t* next);
uint16_t arg_getTotleSize(Arg* self);
void arg_freeContent(Arg* self);
Arg* arg_setName(Arg* self, char* name);
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size);
Arg* arg_newContent(Arg* self, uint32_t size);
Arg* arg_setType(Arg* self, char* type);
char* arg_getName(Arg* self);
char* arg_getType(Arg* self);
uint8_t* arg_getContent(Arg* self);
uint16_t arg_getContentSize(Arg* self);
Arg* arg_setInt(Arg* self, char* name, int64_t val);
Arg* arg_setFloat(Arg* self, char* name, float val);
Arg* arg_setPtr(Arg* self, char* name, char* type, void* pointer);
Arg* arg_setStr(Arg* self, char* name, char* string);
int64_t arg_getInt(Arg* self);
float arg_getFloat(Arg* self);
void* arg_getPtr(Arg* self);
char* arg_getStr(Arg* self);
Arg* arg_copy(Arg* argToBeCopy);
Arg* arg_init(Arg* self, void* voidPointer);
void arg_deinit(Arg* self);
Arg* New_arg(void* voidPointer);
#endif

View File

@ -0,0 +1,502 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataArgs.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dataLink.h"
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
void args_deinit(Args* self) {
link_deinit(self);
}
int32_t args_setFloat(Args* self, char* name, float argFloat) {
Arg* argNew = New_arg(NULL);
argNew = arg_setFloat(argNew, name, argFloat);
args_setArg(self, argNew);
return 0;
}
void* args_getPtr(Args* self, char* name) {
void* pointer = NULL;
Arg* arg = args_getArg(self, name);
if (NULL == arg) {
return NULL;
}
pointer = arg_getPtr(arg);
return pointer;
}
int32_t args_setPtr(Args* self, char* name, void* argPointer) {
int32_t errCode = 0;
Arg* argNew = New_arg(NULL);
argNew = arg_setPtr(argNew, name, "pointer", argPointer);
args_setArg(self, argNew);
return errCode;
}
int32_t args_setStr(Args* self, char* name, char* strIn) {
int32_t errCode = 0;
Arg* argNew = New_arg(NULL);
argNew = arg_setStr(argNew, name, strIn);
args_setArg(self, argNew);
return errCode;
}
void setArgDirect(Args* self, Arg* arg) {
link_addNode(self, arg);
}
char* args_getBuff(Args* self, int32_t size) {
Arg* argNew = New_arg(NULL);
argNew = arg_newContent(argNew, size + 1);
setArgDirect(self, argNew);
return (char*)arg_getContent(argNew);
}
char* args_getStr(Args* self, char* name) {
if (NULL == self) {
return NULL;
}
Arg* arg = args_getArg(self, name);
if (NULL == arg) {
return NULL;
}
if (NULL == arg_getContent(arg)) {
return NULL;
}
return (char*)arg_getContent(arg);
}
int32_t args_setInt(Args* self, char* name, int64_t int64In) {
Arg* argNew = New_arg(NULL);
argNew = arg_setInt(argNew, name, int64In);
args_setArg(self, argNew);
return 0;
}
int64_t args_getInt(Args* self, char* name) {
Arg* arg = args_getArg(self, name);
if (NULL == arg) {
return -999999999;
}
return arg_getInt(arg);
}
int32_t args_getSize(Args* self) {
return link_getSize(self);
}
char* args_getType(Args* self, char* name) {
Arg* arg = NULL;
arg = args_getArg(self, name);
if (NULL == arg) {
return NULL;
}
return arg_getType(arg);
}
float args_getFloat(Args* self, char* name) {
Arg* arg = args_getArg(self, name);
if (NULL == arg) {
return -999999999.0;
}
return arg_getFloat(arg);
}
int32_t args_copyArg(Args* self, Arg* argToBeCopy) {
if (NULL == argToBeCopy) {
return 1;
}
Arg* argCopied = arg_copy(argToBeCopy);
args_setArg(self, argCopied);
return 0;
}
int32_t args_copyArgByName(Args* self, char* name, Args* directArgs) {
Arg* argToBeCopy = args_getArg(self, name);
args_copyArg(directArgs, argToBeCopy);
return 0;
}
int32_t args_isArgExist(Args* self, char* name) {
if (NULL == name) {
return 0;
}
if (NULL != args_getArg(self, name)) {
return 1;
}
return 0;
}
int32_t updateArg(Args* self, Arg* argNew) {
LinkNode* nodeToUpdate = NULL;
LinkNode* nodeNow = self->firstNode;
LinkNode* priorNode = NULL;
char* name = arg_getName(argNew);
while (1) {
if (strEqu(content_getName(nodeNow), name)) {
nodeToUpdate = nodeNow;
break;
}
if (content_getNext(nodeNow) == NULL) {
// error, node no found
goto exit;
}
priorNode = nodeNow;
nodeNow = content_getNext(nodeNow);
}
nodeToUpdate = arg_setContent(nodeToUpdate, arg_getContent(argNew),
arg_getContentSize(argNew));
// update privior link, because arg_getContent would free origin pointer
if (NULL == priorNode) {
self->firstNode = nodeToUpdate;
goto exit;
}
content_setNext(priorNode, nodeToUpdate);
goto exit;
exit:
arg_deinit(argNew);
return 0;
}
int32_t args_setArg(Args* self, Arg* arg) {
char* name = arg_getName(arg);
if (!args_isArgExist(self, name)) {
setArgDirect(self, arg);
return 0;
}
updateArg(self, arg);
return 0;
}
LinkNode* args_getNode(Args* self, char* name) {
LinkNode* nodeNow = self->firstNode;
if (NULL == nodeNow) {
return NULL;
}
while (1) {
Arg* arg = nodeNow;
char* thisName = arg_getName(arg);
if (strEqu(name, thisName)) {
return nodeNow;
}
if (NULL == content_getNext(nodeNow)) {
return NULL;
}
nodeNow = content_getNext(nodeNow);
}
}
Arg* args_getArg(Args* self, char* name) {
LinkNode* node = args_getNode(self, name);
if (NULL == node) {
return NULL;
}
return node;
}
void args_bind(Args* self, char* type, char* name, void* pointer) {
Args* buffs = New_strBuff();
char* typeWithBind = strsAppend(buffs, "_bind-", type);
Arg* argNew = New_arg(NULL);
argNew = arg_setPtr(argNew, name, typeWithBind, pointer);
args_setArg(self, argNew);
args_deinit(buffs);
return;
}
void args_bindInt(Args* self, char* name, int32_t* intPtr) {
args_bind(self, "int", name, intPtr);
}
void args_bindFloat(Args* self, char* name, float* floatPtr) {
args_bind(self, "float", name, floatPtr);
}
void args_bindStr(Args* self, char* name, char** stringPtr) {
args_bind(self, "str", name, stringPtr);
}
char* getPrintSring(Args* self, char* name, char* valString) {
Args* buffs = New_strBuff();
char* printName = strsFormat(buffs, 128, "[printBuff]%s", name);
char* printString = strsCopy(buffs, valString);
args_setStr(self, printName, printString);
char* res = args_getStr(self, printName);
args_deinit(buffs);
return res;
}
char* getPrintStringFromInt(Args* self, char* name, int32_t val) {
Args* buffs = New_strBuff();
char* res = NULL;
char* valString = strsFormat(buffs, 32, "%d", val);
res = getPrintSring(self, name, valString);
args_deinit(buffs);
return res;
}
char* getPrintStringFromFloat(Args* self, char* name, float val) {
Args* buffs = New_strBuff();
char* res = NULL;
char* valString = strsFormat(buffs, 32, "%f", val);
res = getPrintSring(self, name, valString);
args_deinit(buffs);
return res;
}
char* getPrintStringFromPtr(Args* self, char* name, void* val) {
Args* buffs = New_strBuff();
char* res = NULL;
uint64_t intVal = (uint64_t)val;
char* valString = strsFormat(buffs, 32, "0x%llx", intVal);
res = getPrintSring(self, name, valString);
args_deinit(buffs);
return res;
}
char* args_print(Args* self, char* name) {
char* res = NULL;
char* type = args_getType(self, name);
Args* buffs = New_strBuff();
if (NULL == type) {
/* can not get arg */
res = NULL;
goto exit;
}
if (strEqu(type, "int")) {
int32_t val = args_getInt(self, name);
res = getPrintStringFromInt(self, name, val);
goto exit;
}
if (strEqu(type, "float")) {
float val = args_getFloat(self, name);
res = getPrintStringFromFloat(self, name, val);
goto exit;
}
if (strEqu(type, "str")) {
res = args_getStr(self, name);
goto exit;
}
if (strEqu(type, "pointer")) {
void* val = args_getPtr(self, name);
res = getPrintStringFromPtr(self, name, val);
goto exit;
}
char* bindTypePrefix = strsCopy(self, "_bind-");
if (strIsStartWith(type, bindTypePrefix)) {
char* typeWithoutBind = strsRemovePrefix(buffs, type, bindTypePrefix);
if (strEqu(typeWithoutBind, "int")) {
int32_t* valPtr = args_getPtr(self, name);
int32_t val = *valPtr;
res = getPrintStringFromInt(self, name, val);
goto exit;
}
if (strEqu(typeWithoutBind, "float")) {
float* valPtr = args_getPtr(self, name);
float val = *valPtr;
res = getPrintStringFromFloat(self, name, val);
goto exit;
}
if (strEqu(typeWithoutBind, "str")) {
// the value of &string is equal to string it self
char* string = args_getPtr(self, name);
res = string;
goto exit;
}
}
/* can not match type */
res = NULL;
goto exit;
exit:
args_deinit(buffs);
return res;
}
uint8_t args_setLiteral(Args* self, char* targetArgName, char* literal) {
Args* buffs = New_strBuff();
literal = strsGetCleanCmd(buffs, literal);
uint8_t err = 0;
char* directStr = strsGetDirectStr(self, literal);
/* get direct Str ok */
if (NULL != directStr) {
/* direct string value */
args_setStr(self, targetArgName, directStr);
/* ok */
err = 0;
goto exit;
}
/* match bool */
if (strEqu(literal, "True")) {
args_setInt(self, targetArgName, 1);
err = 0;
goto exit;
}
if (strEqu(literal, "False")) {
args_setInt(self, targetArgName, 0);
err = 0;
goto exit;
}
/* match num */
if ((literal[0] >= '0') && (literal[0] <= '9')) {
/* match float num */
if (strIsContain(literal, '.')) {
args_setFloat(self, targetArgName, 0);
args_set(self, targetArgName, literal);
/* succeed */
err = 0;
goto exit;
}
/* match int num */
args_setInt(self, targetArgName, 0);
args_set(self, targetArgName, literal);
/* succeed */
err = 0;
goto exit;
}
err = 1;
goto exit;
exit:
args_deinit(buffs);
return err;
}
int32_t args_set(Args* self, char* name, char* valStr) {
char* type = args_getType(self, name);
Args* buffs = New_strBuff();
int32_t err = 0;
if (NULL == type) {
/* do not get arg */
err = 1;
goto exit;
}
if (strEqu("int", type)) {
int32_t val = atoi(valStr);
args_setInt(self, name, val);
// operation succeed
err = 0;
goto exit;
}
if (strEqu("float", type)) {
float val = atof(valStr);
args_setFloat(self, name, val);
// operation succeed
err = 0;
goto exit;
}
if (strEqu("str", type)) {
args_setStr(self, name, valStr);
// operation succeed
err = 0;
goto exit;
}
char* bindTypePrefix = strsCopy(self, "_bind-");
if (strIsStartWith(type, bindTypePrefix)) {
char* typeWithoutBind = strsRemovePrefix(buffs, type, bindTypePrefix);
if (strEqu(typeWithoutBind, "int")) {
int32_t* valPtr = args_getPtr(self, name);
int32_t val = atoi(valStr);
*valPtr = val;
// operation succeed
err = 0;
goto exit;
}
if (strEqu(typeWithoutBind, "float")) {
float* valPtr = args_getPtr(self, name);
float val = atof(valStr);
*valPtr = val;
// operation succeed
err = 0;
goto exit;
}
if (strEqu(typeWithoutBind, "str")) {
char* stringBinded = args_getPtr(self, name);
/* size add 1 to copy the '\0' */
memcpy(stringBinded, valStr, strGetSize(valStr) + 1);
// operation succeed
err = 0;
goto exit;
}
}
/* type not match */
err = 2;
goto exit;
exit:
args_deinit(buffs);
return err;
}
int32_t args_setPtrWithType(Args* self, char* name, char* type, void* objPtr) {
Arg* argNew = New_arg(NULL);
argNew = arg_setPtr(argNew, name, type, objPtr);
args_setArg(self, argNew);
return 0;
}
int32_t args_setObjectWithClass(Args* self,
char* objName,
char* className,
void* objPtr) {
Args* buffs = New_strBuff();
char* typeWithClass = strsAppend(buffs, "_class-", className);
args_setPtrWithType(self, objName, typeWithClass, objPtr);
args_deinit(buffs);
return 0;
}
int32_t args_foreach(Args* self,
int32_t (*eachHandle)(Arg* argEach, Args* handleArgs),
Args* handleArgs) {
LinkNode* nodeNow = self->firstNode;
while (1) {
Arg* argNow = nodeNow;
if (NULL == argNow) {
continue;
}
LinkNode* nextNode = content_getNext(nodeNow);
eachHandle(argNow, handleArgs);
if (NULL == nextNode) {
break;
}
nodeNow = nextNode;
}
return 0;
}
int32_t args_removeArg(Args* self, char* name) {
Arg* argNow = args_getArg(self, name);
if (NULL == argNow) {
/* can not found arg */
return 1;
}
link_removeNode(self, argNow);
return 0;
}
Args* New_args(Args* args) {
Args* self = New_link(NULL);
return self;
}

View File

@ -0,0 +1,71 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _dataArgs__H
#define _dataArgs__H
#include "dataArg.h"
#include "dataLink.h"
#include "dataMemory.h"
#include "dataString.h"
typedef Link Args;
/* operation */
void args_deinit(Args* self);
void args_init(Args* self, Args* args);
int32_t args_getSize(Args* self);
LinkNode* args_getNode(Args* self, char* name);
Arg* args_getArgByIndex(Args* self, int32_t index);
Arg* args_getArg(Args* self, char* name);
int32_t args_removeArg(Args* self, char* name);
int32_t args_setArg(Args* self, Arg* arg);
int32_t args_copyArgByName(Args* self, char* name, Args* directList);
int32_t args_copyArg(Args* self, Arg* argToBeCopy);
char* args_getType(Args* self, char* name);
int32_t args_isArgExist(Args* self, char* name);
int32_t args_setStr(Args* self, char* name, char* strIn);
int32_t args_setStrWithDefaultName(Args* self, char* strIn);
char* args_getStr(Args* self, char* name);
int32_t args_setFloatWithDefaultName(Args* self, float argFloat);
int32_t args_setFloat(Args* self, char* name, float argFloat);
float args_getFloat(Args* self, char* name);
int32_t args_setPtr(Args* self, char* name, void* argPointer);
void* args_getPtr(Args* self, char* name);
int32_t args_setInt(Args* self, char* name, int64_t int64In);
int64_t args_getInt(Args* self, char* name);
void args_bindInt(Args* self, char* name, int32_t* intPtr);
void args_bindFloat(Args* self, char* name, float* floatPtr);
void args_bindStr(Args* self, char* name, char** stringPtr);
/* arg general opeartion */
void args_bind(Args* self, char* type, char* name, void* pointer);
char* args_print(Args* self, char* name);
int32_t args_set(Args* self, char* name, char* valueStr);
int32_t args_setObjectWithClass(Args* self,
char* objectName,
char* className,
void* objectPtr);
int32_t args_setPtrWithType(Args* self,
char* objName,
char* type,
void* objPtr);
int32_t args_foreach(Args* self,
int32_t (*eachHandle)(Arg* argEach, Args* handleArgs),
Args* handleArgs);
char* args_getBuff(Args* self, int32_t size);
uint8_t args_setLiteral(Args* self, char* targetArgName, char* literal);
Args* New_args(Args* args);
#endif

View File

@ -0,0 +1,87 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataLink.h"
#include "dataArg.h"
#include "dataLinkNode.h"
#include "dataMemory.h"
void link_deinit(Link* self) {
LinkNode* nowNode = self->firstNode;
while (NULL != nowNode) {
LinkNode* nodeNext = content_getNext(nowNode);
linkNode_deinit(nowNode);
nowNode = nodeNext;
}
// DynMemPut(self->mem);
pikaFree(self, sizeof(Link));
self = NULL;
}
void link_addNode(Link* self, void* content) {
// old first node become new second node
LinkNode* secondNode = self->firstNode;
self->firstNode = content;
// change the first node to new node
content_setNext(content, secondNode);
}
void link_removeNode(Link* self, void* content) {
LinkNode* nodeToDelete = NULL;
LinkNode* nodeNow = self->firstNode;
LinkNode* priorNode = NULL;
while (1) {
if (nodeNow == content) {
nodeToDelete = nodeNow;
break;
}
if (nodeNow == NULL) {
// error, node no found
goto exit;
}
priorNode = nodeNow;
nodeNow = content_getNext(nodeNow);
}
LinkNode* nextNode = content_getNext(nodeToDelete);
if (nodeToDelete == self->firstNode) {
self->firstNode = content_getNext(nodeToDelete);
}
if (NULL == priorNode) {
self->firstNode = nextNode;
goto exit;
}
content_setNext(priorNode, nextNode);
goto exit;
// deinit the node
exit:
linkNode_deinit(nodeToDelete);
return;
}
int32_t link_getSize(Link* self) {
LinkNode* NowNode;
int32_t size = 0;
NowNode = self->firstNode;
while (NULL != NowNode) {
size++;
NowNode = content_getNext(NowNode);
}
return size;
}
void link_init(Link* self, void* args) {
self->firstNode = NULL;
}
Link* New_link(void* args) {
Link* self = pikaMalloc(sizeof(Link));
link_init(self, args);
return self;
}

View File

@ -0,0 +1,24 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _link2__H
#define _link2__H
#include "dataLinkNode.h"
#include "dataMemory.h"
typedef struct Class_link Link;
struct Class_link {
LinkNode* firstNode;
};
void link_deinit(Link* self);
void link_init(Link* self, void* args);
void link_addNode(Link* self, void* content);
void link_removeNode(Link* self, void* content);
LinkNode* link_getNode(Link* self, int64_t id);
int32_t link_getSize(Link* self);
Link* New_link(void* args);
#endif

View File

@ -0,0 +1,20 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataLinkNode.h"
#include "dataArg.h"
#include "dataMemory.h"
void linkNode_deinit(LinkNode* self) {
arg_deinit(self);
}
void linkNode_init(LinkNode* self, void* args) {
/* attribute */
}
LinkNode* New_linkNode(void* args) {
return NULL;
}

View File

@ -0,0 +1,15 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef _linkNode__H
#define _linkNode__H
#include "dataMemory.h"
typedef uint8_t LinkNode;
void linkNode_deinit(LinkNode* self);
void linkNode_init(LinkNode* self, void* args);
LinkNode* New_linkNode(void* args);
#endif

View File

@ -0,0 +1,41 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataMemory.h"
#include <stdint.h>
#include <stdlib.h>
PikaMemInfo pikaMemInfo = {0};
void* pikaMalloc(uint32_t size) {
pikaMemInfo.heapUsed += size;
if (pikaMemInfo.heapUsedMax < pikaMemInfo.heapUsed) {
pikaMemInfo.heapUsedMax = pikaMemInfo.heapUsed;
}
void* mem = malloc(size);
if (NULL == mem) {
printf("[error]: No heap space!\r\n");
while (1) {
}
}
return mem;
}
void pikaFree(void* mem, uint32_t size) {
free(mem);
pikaMemInfo.heapUsed -= size;
}
uint16_t pikaMemNow(void) {
return pikaMemInfo.heapUsed;
}
uint16_t pikaMemMax(void) {
return pikaMemInfo.heapUsedMax;
}
void pikaMemMaxReset(void) {
pikaMemInfo.heapUsedMax = 0;
}

View File

@ -0,0 +1,23 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef __MEMORY_H__
#define __MEMORY_H__
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef struct {
uint32_t heapUsed;
uint32_t heapUsedMax;
} PikaMemInfo;
void pikaFree(void* mem, uint32_t size);
void* pikaMalloc(uint32_t size);
uint16_t pikaMemNow(void);
uint16_t pikaMemMax(void);
void pikaMemMaxReset(void);
#endif

View File

@ -0,0 +1,249 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataString.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.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++) {
strOut[strOut_i + i] = pData[i];
}
strOut_i += Size;
// add \0 to the end of strOut
strOut[strOut_i] = 0;
return strOut;
}
char* strCut(char* strOut, char* strIn, char startSign, char endSign) {
int32_t Size = strGetSize(strIn);
int32_t isStart = 0;
int32_t isEnd = 0;
int32_t iOut = 0;
for (int32_t i = 0; i < Size; i++) {
if (!isStart && (strIn[i] == startSign)) {
isStart = 1;
continue;
}
if (isStart && (strIn[i] == endSign)) {
isEnd = 1;
break;
}
if (isStart) {
strOut[iOut] = strIn[i];
iOut++;
}
}
/* add \0 */
strOut[iOut] = 0;
if (isStart && isEnd) {
/* succeed */
return strOut;
}
/* faild */
return NULL;
}
char* strDeleteChar(char* strOut, char* strIn, char ch) {
int32_t iOut = 0;
for (int32_t i = 0; i < strGetSize(strIn); i++) {
if (ch == strIn[i]) {
continue;
}
strOut[iOut] = strIn[i];
iOut++;
}
/* add \0 */
strOut[iOut] = 0;
return strOut;
}
char* strDeleteEnter(char* str) {
return strDeleteChar(str, str, '\n');
}
char* strAppendWithSize(char* strOut, char* pData, int32_t Size) {
int32_t strOut_i = strGetSize(strOut);
for (int32_t i = 0; i < Size; i++) {
strOut[strOut_i + i] = pData[i];
}
strOut_i += Size;
// add \0 to the end of strOut
strOut[strOut_i] = 0;
return strOut;
}
int32_t strCountSign(char* strIn, char sign) {
int32_t count = 0;
for (int32_t i = 0; i < strGetSize(strIn); i++) {
if (sign == strIn[i]) {
count++;
}
}
return count;
}
int32_t strGetTokenNum(char* strIn, char sign) {
return strCountSign(strIn, sign) + 1;
}
uint32_t strGetSize(char* pData) {
return strlen(pData);
}
char* strAppend_unlimited(char* strOut, char* pData) {
uint32_t Size = 0;
Size = strGetSize(pData);
return strAppendWithSize_unlimited(strOut, pData, Size);
}
char* strGetLastToken(char* strOut, char* strIn, char sign) {
int32_t size = strGetSize(strIn);
int32_t isGetSign = 0;
int32_t buffSize = 0;
for (int32_t i = size - 1; i > -1; i--) {
if (strIn[i] != sign) {
strOut[size - i - 1] = strIn[i];
buffSize++;
}
if (strIn[i] == sign) {
isGetSign = 1;
break;
}
}
int32_t i = 0;
for (i = 0; i < buffSize / 2; i++) {
char buff = strOut[i];
strOut[i] = strOut[buffSize - i - 1];
strOut[buffSize - i - 1] = buff;
}
strOut[buffSize] = 0;
return strOut;
}
char* strPopToken(char* strOut, char* strIn, char sign) {
int32_t getSign = 0;
int32_t iPoped = 0;
int32_t iOut = 0;
int32_t size = strGetSize(strIn);
int32_t i = 0;
for (i = 0; i < size; i++) {
if (getSign) {
strIn[iPoped++] = strIn[i];
continue;
}
if (strIn[i] != sign) {
strOut[iOut++] = strIn[i];
continue;
}
if (strIn[i] == sign) {
getSign = 1;
continue;
}
}
strOut[iOut] = 0;
strIn[iPoped] = 0;
return strOut;
}
char* strGetFirstToken(char* strOut, char* strIn, char sign) {
int32_t size = strGetSize(strIn);
for (int32_t i = 0; i < size; i++) {
if (strIn[i] != sign) {
strOut[i] = strIn[i];
}
if (strIn[i] == sign) {
break;
}
}
return strOut;
}
int32_t strGetToken(char* string, char** argv, char sign) {
int32_t argc = 0;
int32_t i = 0;
// arg_i point32_t to the arg operated now
int32_t arg_i = 0;
// if not found ' ', then put chars from CMD to argv_tem
int32_t char_i = 0;
for (i = 0; (i < strGetSize(string)); i++) {
if (string[i] != sign) {
argv[arg_i][char_i] = string[i];
char_i++;
}
if (string[i] == sign) {
// write '\0' to the end of argv
argv[arg_i][char_i] = 0;
arg_i++;
char_i = 0;
}
// write '\0' to the end of last argv
argv[arg_i][char_i] = 0;
}
argc = arg_i + 1;
return argc;
}
char* strAppend(char* strOut, char* pData) {
uint32_t Size = 0;
Size = strGetSize(pData);
return strAppendWithSize(strOut, pData, Size);
}
int32_t strIsStartWith(char* str, char* strStart) {
if (NULL == str || NULL == strStart) {
/* input is null */
return 0;
}
uint32_t size = strGetSize(strStart);
uint32_t CMDName_get = 1;
for (int32_t i = 0; i < size; i++) {
if (str[i] != strStart[i]) {
CMDName_get = 0;
}
}
return CMDName_get;
}
int32_t strEqu(char* str1, char* str2) {
return !strcmp(str1, str2);
}
char* strRemovePrefix(char* inputStr, char* prefix, char* outputStr) {
if (!strIsStartWith(inputStr, prefix)) {
return NULL;
}
for (int32_t i = strGetSize(prefix); i < strGetSize(inputStr); i++) {
outputStr[i - strGetSize(prefix)] = inputStr[i];
}
return outputStr;
}
char* strClear(char* str) {
for (int32_t i = 0; i < sizeof(str); i++) {
str[i] = 0;
}
return str;
}
int32_t strIsContain(char* str, char ch) {
for (int32_t i = 0; i < strGetSize(str); i++) {
if (str[i] == ch) {
return 1;
}
}
return 0;
}
char* strCopy(char* strBuff, char* strIn) {
memcpy(strBuff, strIn, strGetSize(strIn));
return strBuff;
}

View File

@ -0,0 +1,37 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef __MY_TEST_TOOLS_H
#define __MY_TEST_TOOLS_H
#include <stdint.h>
/* size */
uint32_t strGetSize(char* pData);
/* append */
char* strAppend(char* strOut, char* pData);
char* strAppend_unlimited(char* strOut, char* pData);
char* strAppendWithSize(char* strOut, char* pData, int32_t Size);
/* cut */
char* strCut(char* strOut, char* strIn, char startSign, char endSign);
/* assert */
int32_t strIsStartWith(char* str, char* strStart);
int32_t strEqu(char* str1, char* str2);
/* delete */
char* strDeleteEnter(char* str);
char* strDeleteChar(char* strOut, char* strIn, char ch);
/* prefix */
char* strRemovePrefix(char* inputStr, char* prefix, char* outputStr);
/* token */
int32_t strGetToken(char* string, char** argv, char sign);
char* strPopToken(char* strOut, char* strIn, char sign);
int32_t strCountSign(char* strIn, char sign);
int32_t strGetTokenNum(char* strIn, char sign);
char* strGetFirstToken(char* strOut, char* strIn, char sign);
char* strGetLastToken(char* strOut, char* strIn, char sign);
char* strClear(char* str);
int32_t strIsContain(char* str, char ch);
char* strCopy(char* strBuff, char* strIn);
#endif

View File

@ -0,0 +1,105 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "dataStrs.h"
#include <stdarg.h>
#include <stdio.h>
#include "dataString.h"
Args* New_strBuff(void) {
return New_args(NULL);
}
char* strsRemovePrefix(Args* buffs, char* inputStr, char* prefix) {
int32_t size = strGetSize(inputStr);
char* buff = args_getBuff(buffs, size);
return strRemovePrefix(inputStr, prefix, buff);
}
char* strsGetDirectStr(Args* buffs, char* argPath) {
char* directStr = NULL;
directStr = strsCut(buffs, argPath, '"', '"');
if (NULL != directStr) {
return directStr;
}
directStr = strsCut(buffs, argPath, '\'', '\'');
if (NULL != directStr) {
return directStr;
}
return NULL;
}
char* strsAppend(Args* buffs, char* strOrigin, char* strToAppend) {
int32_t size = strGetSize(strOrigin) + strGetSize(strToAppend);
char* buff = args_getBuff(buffs, size);
char* strOut = strCopy(buff, strOrigin);
strAppend(strOut, strToAppend);
return strOut;
}
char* strsGetLastToken(Args* buffs, char* argPath, char sign) {
int32_t size = strGetSize(argPath);
char* buff = args_getBuff(buffs, size);
return strGetLastToken(buff, argPath, sign);
}
char* strsCut(Args* buffs, char* strIn, char startSign, char endSign) {
int32_t size = strGetSize(strIn);
char* buff = args_getBuff(buffs, size);
return strCut(buff, strIn, startSign, endSign);
}
char* strsDeleteChar(Args* buffs, char* strIn, char ch) {
int32_t size = strGetSize(strIn);
return strDeleteChar(args_getBuff(buffs, size), strIn, ch);
}
char* strsGetFirstToken(Args* buffs, char* strIn, char sign) {
int32_t size = strGetSize(strIn);
return strGetFirstToken(args_getBuff(buffs, size), strIn, sign);
}
char* strsPopToken(Args* buffs, char* tokens, char sign) {
int32_t size = strGetSize(tokens);
char* buff = args_getBuff(buffs, size);
return strPopToken(buff, tokens, sign);
}
char* strsCopy(Args* buffs, char* source) {
int32_t size = strGetSize(source);
char* buff = args_getBuff(buffs, size);
return strCopy(buff, source);
}
char* strsFormat(Args* buffs, uint16_t buffSize, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* res = args_getBuff(buffs, buffSize);
vsnprintf(res, buffSize, fmt, args);
va_end(args);
return res;
}
char* strsGetCleanCmd(Args* buffs, char* cmd) {
int32_t size = strGetSize(cmd);
char* strOut = args_getBuff(buffs, size);
int32_t iOut = 0;
char delChar = ' ';
int32_t isInStr = 0;
for (int32_t i = 0; i < strGetSize(cmd); i++) {
if ('\'' == cmd[i] || '\"' == cmd[i]) {
isInStr = !isInStr;
}
if ((delChar == cmd[i]) && (!isInStr)) {
/* do not load char */
continue;
}
strOut[iOut] = cmd[i];
iOut++;
}
/* add \0 */
strOut[iOut] = 0;
return strOut;
}

View File

@ -0,0 +1,21 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef __STR_ARGS__H
#define __STR_ARGS__H
#include "dataArgs.h"
Args* New_strBuff(void);
char* strsGetFirstToken(Args* buffs, char* strIn, char sign);
char* strsGetLastToken(Args* buffs, char* arg_Path, char sign);
char* strsPopToken(Args* buffs, char* tokens, char sign);
char* strsCopy(Args* buffs, char* source);
char* strsDeleteChar(Args* buff, char* strIn, char ch);
char* strsCut(Args* buffs, char* strIn, char startSign, char endSign);
char* strsRemovePrefix(Args* buffs, char* inputStr, char* prefix);
char* strsAppend(Args* buffs, char* strOrigin, char* strAppend);
char* strsFormat(Args* buffs, uint16_t buffSize, const char* fmt, ...);
char* strsGetDirectStr(Args* buffs, char* argPath);
char* strsGetCleanCmd(Args* buffs, char* cmd);
#endif

View File

@ -0,0 +1,30 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#include "PikaObj.h"
#include "dataArgs.h"
void method_returnStr(Args* args, char* val) {
args_setStr(args, "return", val);
}
void method_returnInt(Args* args, int32_t val) {
args_setInt(args, "return", val);
}
void method_returnFloat(Args* args, float val) {
args_setFloat(args, "return", val);
}
void method_returnPtr(Args* args, void* val) {
args_setPtr(args, "return", val);
}
int32_t method_getInt(Args* args, char* argName) {
return args_getInt(args, argName);
}
float method_getFloat(Args* args, char* argName) {
return args_getFloat(args, argName);
}
char* method_getStr(Args* args, char* argName) {
return args_getStr(args, argName);
}

View File

@ -0,0 +1,16 @@
/*
Author: lyon
Tencent QQ: 645275593
*/
#ifndef METHOD__H
#define METHOD__H
#include "dataArgs.h"
void method_returnStr(Args* args, char* val);
void method_returnInt(Args* args, int32_t val);
void method_returnFloat(Args* args, float val);
void method_returnPtr(Args* args, void* val);
int32_t method_getInt(Args* args, char* argName);
float method_getFloat(Args* args, char* argName);
char* method_getStr(Args* args, char* argName);
#endif

View File

@ -0,0 +1,13 @@
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "pikaScript.h"
void Device_LED_off(PikaObj *self)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
}
void Device_LED_on(PikaObj *self)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}

View File

@ -0,0 +1,26 @@
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "pikaScript.h"
void Device_Uart_setName(PikaObj *self, char *name)
{
obj_setStr(self, "name", name);
}
void Device_Uart_send(PikaObj *self, char *data)
{
printf("[uart1]: %s\r\n", data);
}
void Device_Uart_printName(PikaObj *self)
{
char *name = obj_getStr(self, "name");
if (NULL == name)
{
printf("[error] Uart: can't find name.\r\n");
return;
}
printf("%s\r\n", name);
}

View File

@ -0,0 +1,14 @@
#include "BaseObj.h"
#include "dataStrs.h"
void PikaStdLib_MemChecker_max(PikaObj* self) {
obj_sysPrintf(self, "%0.2f kB", pikaMemMax() / 1024.0);
}
void PikaStdLib_MemChecker_now(PikaObj* self) {
obj_sysPrintf(self, "%0.2f kB", pikaMemNow() / 1024.0);
}
void PikaStdLib_MemChecker_resetMax(PikaObj* self) {
pikaMemMaxReset();
}

View File

@ -0,0 +1,98 @@
#include "BaseObj.h"
#include "dataStrs.h"
static int32_t __foreach_listEachArg(Arg* argEach, Args* handleArgs) {
Args* buffs = handleArgs;
if (NULL == handleArgs) {
/* error: not handleArgs input */
return 1;
}
char* argName = strsCopy(buffs, arg_getName(argEach));
if (strIsStartWith(argName, "[")) {
/* skip */
return 0;
}
char* stringOut = args_getStr(handleArgs, "stringOut");
if (NULL == stringOut) {
// stringOut no found
return 1;
}
stringOut = strsAppend(buffs, stringOut, argName);
stringOut = strsAppend(buffs, stringOut, " ");
args_setStr(handleArgs, "stringOut", stringOut);
return 0;
}
void PikaStdLib_SysObj_ls(PikaObj* self, char* objPath) {
obj_setErrorCode(self, 0);
Args* args = New_args(NULL);
args_setStr(args, "stringOut", "");
obj_setSysOut(self, "");
if (NULL == objPath) {
/* no input obj path, use current obj */
args_foreach(self->attributeList, __foreach_listEachArg, args);
obj_setSysOut(self, args_getStr(args, "stringOut"));
goto exit;
}
PikaObj* obj = obj_getObj(self, objPath, 0);
if (NULL == obj) {
/* do not find obj */
obj_setSysOut(self, "[error] list: object no found.");
obj_setErrorCode(self, 1);
goto exit;
}
/* list args */
args_foreach(obj->attributeList, __foreach_listEachArg, args);
obj_setSysOut(self, args_getStr(args, "stringOut"));
exit:
args_deinit(args);
}
void PikaStdLib_SysObj_new(PikaObj* self, char* classPath, char* objPath) {
int32_t res = obj_newObj(self, objPath, classPath);
if (1 == res) {
obj_setSysOut(self, "[error] new: class not found .");
obj_setErrorCode(self, 1);
return;
}
}
void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
obj_setErrorCode(self, 0);
int32_t res = obj_removeArg(self, argPath);
if (1 == res) {
obj_setSysOut(self, "[error] del: object no found.");
obj_setErrorCode(self, 1);
return;
}
if (2 == res) {
obj_setSysOut(self, "[error] del: arg not match.");
obj_setErrorCode(self, 2);
return;
}
}
void PikaStdLib_SysObj_type(PikaObj* self, char* argPath) {
if (NULL == argPath) {
/* no input obj path, use current obj */
PikaObj* objHost = obj_getContext(self);
Arg* objArg = obj_getArg(objHost, obj_getStr(self, "_n"));
if (NULL == objArg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
}
obj_setSysOut(self, arg_getType(objArg));
return;
}
Arg* arg = obj_getArg(self, argPath);
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
}
obj_setSysOut(self, arg_getType(arg));
}