mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
fix parse for abc[a+1]*3
This commit is contained in:
parent
fc9562ccd6
commit
bb0631db1e
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
|||||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||||
"args": [
|
"args": [
|
||||||
"--gtest_filter=vm.test64_hex"
|
// "--gtest_filter=parser.modbus_1"
|
||||||
],
|
],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
|
@ -1,31 +1,32 @@
|
|||||||
class _ModBus:
|
class _ModBus:
|
||||||
def setSlave(self, slave: int): ...
|
def setSlave(self, slave: int): ...
|
||||||
def serializeReadBits(self, addr: int, nb: int) -> 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 serializeReadInputBits(self, addr: int, nb: int) -> int: ...
|
||||||
def deserializeReadInputBits(self, msgLength: int, dest: bytes) -> int: ...
|
|
||||||
def serializeReadRegisters(self, addr: int, nb: int) -> 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 serializeReadInputRegisters(self, addr: int, nb: int) -> int: ...
|
||||||
def deserializeReadInputRegisters(self, msgLength: int, dest: bytes) -> int: ...
|
|
||||||
def serializeWriteBit(self, addr: int, status: int) -> int: ...
|
def serializeWriteBit(self, addr: int, status: int) -> int: ...
|
||||||
def deserializeWriteBit(self, msgLength: int) -> int: ...
|
def deserializeWriteBit(self, msgLength: int) -> int: ...
|
||||||
def serializeWriteRegister(self, addr: int, value: int) -> int: ...
|
def serializeWriteRegister(self, addr: int, value: int) -> int: ...
|
||||||
def deserializeWriteRegister(self, msgLength: 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 deserializeWriteBits(self, msgLength: int) -> int: ...
|
||||||
def serializeWriteRegisters(self, addr: int, nb: int, src: bytes) -> int: ...
|
|
||||||
def deserializeWriteRegisters(self, msgLength: int) -> int: ...
|
def deserializeWriteRegisters(self, msgLength: int) -> int: ...
|
||||||
def serializeMaskWriteRegister(self, addr: int, andMask: int, orMask: int) -> int: ...
|
def serializeMaskWriteRegister(self, addr: int, andMask: int, orMask: int) -> int: ...
|
||||||
def deserializeMaskWriteRegister(self, msgLength: int) -> int: ...
|
def deserializeMaskWriteRegister(self, msgLength: int) -> int: ...
|
||||||
def serializeWriteAndReadRegisters(self, writeAddr: int, writeNb: int, src: bytes, readAddr: int, readNb: 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 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: ...
|
def deserializeReportSlaveId(self, msgLength: int, maxDest: int, dest: bytes) -> int: ...
|
||||||
|
|
||||||
class _ModBusRTU(_ModBus):
|
def getSendBuff(self) -> bytes: ...
|
||||||
def __init__(self, sendBuffSize: int, readBuffSize: int): ...
|
def getReadBuff(self) -> bytes: ...
|
||||||
|
def __init__rtu(self, sendBuffSize: int, readBuffSize: int): ...
|
||||||
|
def __init__tcp(self, sendBuffSize: int, readBuffSize: int): ...
|
||||||
class _ModBusTCP(_ModBus):
|
|
||||||
def __init__(self, sendBuffSize: int, readBuffSize: int): ...
|
|
||||||
|
@ -1,5 +1,59 @@
|
|||||||
import _modbus
|
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)
|
||||||
|
"""
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
#include "_modbus__ModBus.h"
|
#include "_modbus__ModBus.h"
|
||||||
#include "_modbus__ModBusRTU.h"
|
|
||||||
#include "_modbus__ModBusTCP.h"
|
|
||||||
#include "agile_modbus.h"
|
#include "agile_modbus.h"
|
||||||
|
|
||||||
void _modbus__ModBusRTU___init__(PikaObj* self,
|
void _modbus__ModBus___init__rtu(PikaObj* self,
|
||||||
int sendBUffSize,
|
int sendBUffSize,
|
||||||
int readBuffSize) {
|
int readBuffSize) {
|
||||||
agile_modbus_rtu_t ctx_rtu = {0};
|
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);
|
agile_modbus_set_slave(ctx, slave);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _modbus__ModBusTCP___init__(PikaObj* self,
|
void _modbus__ModBus___init__tcp(PikaObj* self,
|
||||||
int sendBuffSize,
|
int sendBuffSize,
|
||||||
int readBuffSize) {
|
int readBuffSize) {
|
||||||
agile_modbus_tcp_t ctx_tcp = {0};
|
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,
|
return agile_modbus_serialize_write_registers(ctx, addr, nb,
|
||||||
(uint16_t*)src);
|
(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);
|
||||||
|
}
|
||||||
|
@ -4311,4 +4311,26 @@ TEST(parser, pass_) {
|
|||||||
EXPECT_EQ(pikaMemNow(), 0);
|
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
|
#endif
|
||||||
|
@ -179,14 +179,21 @@ static char* __removeTokensBetween(Args* outBuffs,
|
|||||||
return output;
|
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) {
|
static enum StmtType Lexer_matchStmtType(char* right) {
|
||||||
Args buffs = {0};
|
Args buffs = {0};
|
||||||
enum StmtType stmtType = STMT_none;
|
enum StmtType stmtType = STMT_none;
|
||||||
char* rightWithoutSubStmt = __removeTokensBetween(&buffs, right, "(", ")");
|
char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, right);
|
||||||
rightWithoutSubStmt =
|
|
||||||
__removeTokensBetween(&buffs, rightWithoutSubStmt, "[", "]");
|
|
||||||
rightWithoutSubStmt =
|
|
||||||
__removeTokensBetween(&buffs, rightWithoutSubStmt, "{", "}");
|
|
||||||
|
|
||||||
PIKA_BOOL is_get_operator = PIKA_FALSE;
|
PIKA_BOOL is_get_operator = PIKA_FALSE;
|
||||||
PIKA_BOOL is_get_method = 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);
|
enum StmtType stmtType = Lexer_matchStmtType(right);
|
||||||
/* solve operator stmt */
|
/* solve operator stmt */
|
||||||
if (STMT_operator == stmtType) {
|
if (STMT_operator == stmtType) {
|
||||||
char* rightWithoutSubStmt = strsDeleteBetween(&buffs, right, '(', ')');
|
char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, right);
|
||||||
char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt);
|
char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt);
|
||||||
if (NULL == operator) {
|
if (NULL == operator) {
|
||||||
result = PIKA_RES_ERR_SYNTAX_ERROR;
|
result = PIKA_RES_ERR_SYNTAX_ERROR;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user