fix parse for abc[a+1]*3

This commit is contained in:
pikastech 2022-09-19 00:44:03 +08:00
parent fc9562ccd6
commit bb0631db1e
6 changed files with 119 additions and 27 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
"--gtest_filter=vm.test64_hex"
// "--gtest_filter=parser.modbus_1"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -1,31 +1,32 @@
class _ModBus:
def setSlave(self, slave: int): ...
def serializeReadBits(self, addr: int, nb: int) -> int: ...
def deserializeReadBits(self, msgLength: int, dest: bytes) -> int: ...
def serializeReadInputBits(self, addr: int, nb: int) -> int: ...
def deserializeReadInputBits(self, msgLength: int, dest: bytes) -> int: ...
def serializeReadRegisters(self, addr: int, nb: int) -> int: ...
def deserializeReadRegisters(self, msgLength: int, dest: bytes) -> int: ...
def serializeReadInputRegisters(self, addr: int, nb: int) -> int: ...
def deserializeReadInputRegisters(self, msgLength: int, dest: bytes) -> int: ...
def serializeWriteBit(self, addr: int, status: int) -> int: ...
def deserializeWriteBit(self, msgLength: int) -> int: ...
def serializeWriteRegister(self, addr: int, value: int) -> int: ...
def deserializeWriteRegister(self, msgLength: int) -> int: ...
def serializeWriteBits(self, addr: int, nb: int, src: bytes) -> int: ...
def deserializeWriteBits(self, msgLength: int) -> int: ...
def serializeWriteRegisters(self, addr: int, nb: int, src: bytes) -> int: ...
def deserializeWriteRegisters(self, msgLength: int) -> int: ...
def serializeMaskWriteRegister(self, addr: int, andMask: int, orMask: int) -> int: ...
def deserializeMaskWriteRegister(self, msgLength: int) -> int: ...
def serializeWriteAndReadRegisters(self, writeAddr: int, writeNb: int, src: bytes, readAddr: int, readNb: int) -> int: ...
def deserializeWriteAndReadRegisters(self, msgLength: int, dest: bytes) -> int: ...
def serializeReportSlaveId(self) -> int: ...
def deserializeReadBits(self, msgLength: int, dest: bytes) -> int: ...
def deserializeReadInputBits(self, msgLength: int, dest: bytes) -> int: ...
def deserializeReadRegisters(self, msgLength: int, dest: bytes) -> int: ...
def deserializeReadInputRegisters(self, msgLength: int, dest: bytes) -> int: ...
def serializeWriteBits(self, addr: int, nb: int, src: bytes) -> int: ...
def serializeWriteRegisters(self, addr: int, nb: int, src: bytes) -> int: ...
def deserializeWriteAndReadRegisters(self, msgLength: int, dest: bytes) -> int: ...
def deserializeReportSlaveId(self, msgLength: int, maxDest: int, dest: bytes) -> int: ...
class _ModBusRTU(_ModBus):
def __init__(self, sendBuffSize: int, readBuffSize: int): ...
class _ModBusTCP(_ModBus):
def __init__(self, sendBuffSize: int, readBuffSize: int): ...
def getSendBuff(self) -> bytes: ...
def getReadBuff(self) -> bytes: ...
def __init__rtu(self, sendBuffSize: int, readBuffSize: int): ...
def __init__tcp(self, sendBuffSize: int, readBuffSize: int): ...

View File

@ -1,5 +1,59 @@
import _modbus
"""
class ModBus(_modbus._ModBus):
def deserializeReadBits(self, msgLength: int):
dest = bytes(msgLength)
super().deserializeReadBits(msgLength, dest)
return list(dest)
class ModBus(_modbus._Modbus):
...
def deserializeReadInputBits(self, msgLength: int):
dest = bytes(msgLength)
super().deserializeReadInputBits(msgLength, dest)
return list(dest)
def deserializeReadRegisters(self, msgLength: int):
dest = bytes(2 * msgLength)
super().deserializeReadRegisters(msgLength, dest)
ret = []
for i in range(0, len(dest), 2):
ret.append(dest[i] + dest[i + 1] * 256)
return ret
def deserializeReadInputRegisters(self, msgLength: int) -> list:
dest = bytes(2 * msgLength)
super().deserializeReadInputRegisters(msgLength, dest)
ret = []
for i in range(0, len(dest), 2):
ret.append(dest[i] + dest[i + 1] * 256)
return ret
def serializeWriteBits(self, addr: int, nb: int, src: list):
src = bytes(src)
return super().serializeWriteBits(addr, nb, src)
def serializeWriteRegisters(self, addr: int, nb: int, src: list):
_src = bytes(2 * len(src))
for i in range(len(src)):
_src[2 * i] = src[i] % 256
_src[2 * i + 1] = src[i] // 256
return super().serializeWriteRegisters(addr, nb, _src)
def deserializeWriteAndReadRegisters(self, msgLength: int) -> list:
dest = bytes(2 * msgLength)
super().deserializeWriteAndReadRegisters(msgLength, dest)
ret = []
for i in range(0, len(dest), 2):
ret.append(dest[i] + dest[i + 1] * 256)
return ret
class ModBusRTU(ModBus):
def __init__(self, sendBuffSize: int, readBuffSize: int):
self.__init__rtu(sendBuffSize, readBuffSize)
class ModBusTCP(ModBus):
def __init__(self, sendBuffSize: int, readBuffSize: int):
self.__init__tcp(sendBuffSize, readBuffSize)
"""

View File

@ -1,9 +1,7 @@
#include "_modbus__ModBus.h"
#include "_modbus__ModBusRTU.h"
#include "_modbus__ModBusTCP.h"
#include "agile_modbus.h"
void _modbus__ModBusRTU___init__(PikaObj* self,
void _modbus__ModBus___init__rtu(PikaObj* self,
int sendBUffSize,
int readBuffSize) {
agile_modbus_rtu_t ctx_rtu = {0};
@ -22,7 +20,7 @@ void _modbus__ModBus_setSlave(PikaObj* self, int slave) {
agile_modbus_set_slave(ctx, slave);
}
void _modbus__ModBusTCP___init__(PikaObj* self,
void _modbus__ModBus___init__tcp(PikaObj* self,
int sendBuffSize,
int readBuffSize) {
agile_modbus_tcp_t ctx_tcp = {0};
@ -181,3 +179,13 @@ int _modbus__ModBus_serializeWriteRegisters(PikaObj* self,
return agile_modbus_serialize_write_registers(ctx, addr, nb,
(uint16_t*)src);
}
Arg* _modbus__ModBus_getSendBuff(PikaObj* self) {
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
return arg_newBytes(ctx->send_buf, ctx->send_bufsz);
}
Arg* _modbus__ModBus_getReadBuff(PikaObj* self) {
agile_modbus_t* ctx = obj_getPtr(self, "ctx");
return arg_newBytes(ctx->read_buf, ctx->read_bufsz);
}

View File

@ -4311,4 +4311,26 @@ TEST(parser, pass_) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, modbus_1) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = "dest[i + 1] * 256\n";
__platform_printf("%s\n", lines);
char* pikaAsm = Parser_linesToAsm(buffs, lines);
__platform_printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 REF dest\n"
"3 REF i\n"
"3 NUM 1\n"
"2 OPT +\n"
"1 SLC \n"
"1 NUM 256\n"
"0 OPT *\n"
"B0\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif

View File

@ -179,14 +179,21 @@ static char* __removeTokensBetween(Args* outBuffs,
return output;
}
char* _remove_sub_stmt(Args* outBuffs, char* stmt) {
Args buffs = {0};
stmt = strsCopy(&buffs, stmt);
stmt = __removeTokensBetween(&buffs, stmt, "(", ")");
stmt = __removeTokensBetween(&buffs, stmt, "[", "]");
stmt = __removeTokensBetween(&buffs, stmt, "{", "}");
char* res = args_cacheStr(outBuffs, stmt);
strsDeinit(&buffs);
return res;
}
static enum StmtType Lexer_matchStmtType(char* right) {
Args buffs = {0};
enum StmtType stmtType = STMT_none;
char* rightWithoutSubStmt = __removeTokensBetween(&buffs, right, "(", ")");
rightWithoutSubStmt =
__removeTokensBetween(&buffs, rightWithoutSubStmt, "[", "]");
rightWithoutSubStmt =
__removeTokensBetween(&buffs, rightWithoutSubStmt, "{", "}");
char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, right);
PIKA_BOOL is_get_operator = PIKA_FALSE;
PIKA_BOOL is_get_method = PIKA_FALSE;
@ -1528,7 +1535,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
enum StmtType stmtType = Lexer_matchStmtType(right);
/* solve operator stmt */
if (STMT_operator == stmtType) {
char* rightWithoutSubStmt = strsDeleteBetween(&buffs, right, '(', ')');
char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, right);
char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt);
if (NULL == operator) {
result = PIKA_RES_ERR_SYNTAX_ERROR;