!58 Support read() builtin

* support read(-1)
* add example
This commit is contained in:
李昂 2022-07-15 02:06:23 +00:00
parent 5b1534b0b2
commit 45a07c5773
13 changed files with 323 additions and 42 deletions

11
examples/BuiltIn/file.py Normal file
View File

@ -0,0 +1,11 @@
f = open('test/python/main.py', 'r')
s = f.read(10)
print(s)
f.close()
f = open('test/assets/test.jpg', 'rb')
b = f.read(-1)
print(b)
f.close()
f = open('test/assets/write.txt', 'w')
f.write('Hello World!\n')
f.close()

View File

@ -0,0 +1,11 @@
import PikaMath
a=Quaternion()
a.set(0.592,0.158,0.592,0.525)
b=Quaternion()
a.add(b)
a.mul(b)

View File

@ -94,6 +94,13 @@ class ByteArray(TinyObj):
def decode(self) -> str: ...
class FILEIO:
def init(self, path: str, mode: str): ...
def read(self, size: int) -> any: ...
def write(self, s: any): ...
def close(self): ...
class Utils:
# convert a int to bytes
def int_to_bytes(self, val: int) -> bytes: ...

View File

@ -0,0 +1,68 @@
#include "PikaStdData_FILEIO.h"
#include <stdio.h>
void PikaStdData_FILEIO_init(PikaObj* self, char* mode, char* path) {
if (obj_isArgExist(self, "_f")) {
/* already initialized */
return;
}
FILE* f = __platform_fopen(path, mode);
if (f == NULL) {
printf("Error: can't open file %s\n", path);
return;
}
obj_setPtr(self, "_f", f);
obj_setStr(self, "_mode", mode);
}
void PikaStdData_FILEIO_close(PikaObj* self) {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {
return;
}
__platform_fclose(f);
obj_setPtr(self, "_f", NULL);
}
Arg* PikaStdData_FILEIO_read(PikaObj* self, int size) {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {
return NULL;
}
Arg* buf_arg = arg_setBytes(NULL, "", NULL, size);
uint8_t* buf = arg_getBytes(buf_arg);
/* read */
int n = __platform_fread(buf, 1, size, f);
if (n < size) {
/* EOF */
buf[n] = '\0';
}
char* mode = obj_getStr(self, "_mode");
if (strIsContain(mode, 'b')) {
/* binary */
return buf_arg;
} else {
/* text */
Arg* res = arg_setStr(NULL, "", (char*)buf);
arg_deinit(buf_arg);
return res;
}
}
void PikaStdData_FILEIO_write(PikaObj* self, Arg* s) {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {
obj_setErrorCode(self, PIKA_RES_ERR_IO);
__platform_printf("Error: can't write to file\n");
return;
}
char* mode = obj_getStr(self, "_mode");
if (strIsContain(mode, 'b')) {
/* binary */
__platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f);
} else {
/* text */
char* str = arg_getStr(s);
__platform_fwrite(str, 1, strlen(str), f);
}
}

View File

@ -7,27 +7,50 @@ class MemChecker:
class SysObj:
def type(self, arg: any) -> any: ...
def remove(self, argPath: str): ...
def int(self, arg: any) -> int: ...
def float(self, arg: any) -> float: ...
def str(self, arg: any) -> str: ...
def iter(self, arg: any) -> any: ...
def range(self, a1: int, a2: int) -> any: ...
def print(self, *val): ...
def printNoEnd(self, val: any): ...
def __set__(self, obj: any, key: any, val: any) -> any: ...
def __get__(self, obj: any, key: any) -> any: ...
def __slice__(self, obj: any, start: any, end: any, step: int) -> any: ...
def len(self, arg: any) -> int: ...
def list(self) -> any: ...
def dict(self) -> any: ...
def hex(self, val: int) -> str: ...
def ord(self, val: str) -> int: ...
def chr(self, val: int) -> str: ...
def bytes(self, val: any) -> bytes: ...
def cformat(self, fmt: str, *var) -> str: ...
def id(self, obj: any) -> int: ...
@staticmethod
def type(arg: any) -> any: ...
@staticmethod
def remove(argPath: str): ...
@staticmethod
def int(arg: any) -> int: ...
@staticmethod
def float(arg: any) -> float: ...
@staticmethod
def str(arg: any) -> str: ...
@staticmethod
def iter(arg: any) -> any: ...
@staticmethod
def range(a1: int, a2: int) -> any: ...
@staticmethod
def print(*val): ...
@staticmethod
def printNoEnd(val: any): ...
@staticmethod
def __set__(obj: any, key: any, val: any) -> any: ...
@staticmethod
def __get__(obj: any, key: any) -> any: ...
@staticmethod
def __slice__(obj: any, start: any, end: any, step: int) -> any: ...
@staticmethod
def len(arg: any) -> int: ...
@staticmethod
def list() -> any: ...
@staticmethod
def dict() -> any: ...
@staticmethod
def hex(val: int) -> str: ...
@staticmethod
def ord(val: str) -> int: ...
@staticmethod
def chr(val: int) -> str: ...
@staticmethod
def bytes(val: any) -> bytes: ...
@staticmethod
def cformat(fmt: str, *var) -> str: ...
@staticmethod
def id(obj: any) -> int: ...
@staticmethod
def open(path: str, mode: str) -> object: ...
class RangeObj:

View File

@ -1,4 +1,5 @@
#include "PikaStdLib_SysObj.h"
#include "PikaStdData_FILEIO.h"
#include "PikaStdLib_RangeObj.h"
#include "PikaStdLib_StringObj.h"
#include "PikaVM.h"
@ -452,3 +453,15 @@ int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
}
return ptr & (0x7FFFFFFF);
}
PikaObj* PikaStdLib_SysObj_open(PikaObj* self, char* mode, char* path) {
#if PIKA_FILEIO_ENABLE
PikaObj* file = newNormalObj(New_PikaStdData_FILEIO);
PikaStdData_FILEIO_init(file, mode, path);
return file;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_FILEIO_ENABLE is not enabled.\r\n");
return NULL;
#endif
}

View File

@ -94,6 +94,13 @@ class ByteArray(TinyObj):
def decode(self) -> str: ...
class FILEIO:
def init(self, path: str, mode: str): ...
def read(self, size: int) -> any: ...
def write(self, s: any): ...
def close(self): ...
class Utils:
# convert a int to bytes
def int_to_bytes(self, val: int) -> bytes: ...

View File

@ -7,27 +7,50 @@ class MemChecker:
class SysObj:
def type(self, arg: any) -> any: ...
def remove(self, argPath: str): ...
def int(self, arg: any) -> int: ...
def float(self, arg: any) -> float: ...
def str(self, arg: any) -> str: ...
def iter(self, arg: any) -> any: ...
def range(self, a1: int, a2: int) -> any: ...
def print(self, *val): ...
def printNoEnd(self, val: any): ...
def __set__(self, obj: any, key: any, val: any) -> any: ...
def __get__(self, obj: any, key: any) -> any: ...
def __slice__(self, obj: any, start: any, end: any, step: int) -> any: ...
def len(self, arg: any) -> int: ...
def list(self) -> any: ...
def dict(self) -> any: ...
def hex(self, val: int) -> str: ...
def ord(self, val: str) -> int: ...
def chr(self, val: int) -> str: ...
def bytes(self, val: any) -> bytes: ...
def cformat(self, fmt: str, *var) -> str: ...
def id(self, obj: any) -> int: ...
@staticmethod
def type(arg: any) -> any: ...
@staticmethod
def remove(argPath: str): ...
@staticmethod
def int(arg: any) -> int: ...
@staticmethod
def float(arg: any) -> float: ...
@staticmethod
def str(arg: any) -> str: ...
@staticmethod
def iter(arg: any) -> any: ...
@staticmethod
def range(a1: int, a2: int) -> any: ...
@staticmethod
def print(*val): ...
@staticmethod
def printNoEnd(val: any): ...
@staticmethod
def __set__(obj: any, key: any, val: any) -> any: ...
@staticmethod
def __get__(obj: any, key: any) -> any: ...
@staticmethod
def __slice__(obj: any, start: any, end: any, step: int) -> any: ...
@staticmethod
def len(arg: any) -> int: ...
@staticmethod
def list() -> any: ...
@staticmethod
def dict() -> any: ...
@staticmethod
def hex(val: int) -> str: ...
@staticmethod
def ord(val: str) -> int: ...
@staticmethod
def chr(val: int) -> str: ...
@staticmethod
def bytes(val: any) -> bytes: ...
@staticmethod
def cformat(fmt: str, *var) -> str: ...
@staticmethod
def id(obj: any) -> int: ...
@staticmethod
def open(path: str, mode: str) -> object: ...
class RangeObj:

View File

@ -0,0 +1,74 @@
#include "PikaStdData_FILEIO.h"
#include <stdio.h>
void PikaStdData_FILEIO_init(PikaObj* self, char* mode, char* path) {
if (obj_isArgExist(self, "_f")) {
/* already initialized */
return;
}
FILE* f = __platform_fopen(path, mode);
if (f == NULL) {
printf("Error: can't open file %s\n", path);
return;
}
obj_setPtr(self, "_f", f);
obj_setStr(self, "_mode", mode);
}
void PikaStdData_FILEIO_close(PikaObj* self) {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {
return;
}
__platform_fclose(f);
obj_setPtr(self, "_f", NULL);
}
Arg* PikaStdData_FILEIO_read(PikaObj* self, int size) {
if (size <= 0) {
/* read all */
size = PIKA_READ_FILE_BUFF_SIZE;
}
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {
return NULL;
}
Arg* buf_arg = arg_setBytes(NULL, "", NULL, size);
uint8_t* buf = arg_getBytes(buf_arg);
/* read */
int n = __platform_fread(buf, 1, size, f);
if (n < size) {
/* EOF */
buf[n] = '\0';
}
char* mode = obj_getStr(self, "_mode");
if (strIsContain(mode, 'b')) {
/* binary */
Arg* res = arg_setBytes(NULL, "", buf, n);
arg_deinit(buf_arg);
return res;
} else {
/* text */
Arg* res = arg_setStr(NULL, "", (char*)buf);
arg_deinit(buf_arg);
return res;
}
}
void PikaStdData_FILEIO_write(PikaObj* self, Arg* s) {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {
obj_setErrorCode(self, PIKA_RES_ERR_IO);
__platform_printf("Error: can't write to file\n");
return;
}
char* mode = obj_getStr(self, "_mode");
if (strIsContain(mode, 'b')) {
/* binary */
__platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f);
} else {
/* text */
char* str = arg_getStr(s);
__platform_fwrite(str, 1, strlen(str), f);
}
}

View File

@ -1,4 +1,5 @@
#include "PikaStdLib_SysObj.h"
#include "PikaStdData_FILEIO.h"
#include "PikaStdLib_RangeObj.h"
#include "PikaStdLib_StringObj.h"
#include "PikaVM.h"
@ -452,3 +453,15 @@ int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
}
return ptr & (0x7FFFFFFF);
}
PikaObj* PikaStdLib_SysObj_open(PikaObj* self, char* mode, char* path) {
#if PIKA_FILEIO_ENABLE
PikaObj* file = newNormalObj(New_PikaStdData_FILEIO);
PikaStdData_FILEIO_init(file, mode, path);
return file;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_FILEIO_ENABLE is not enabled.\r\n");
return NULL;
#endif
}

View File

@ -84,3 +84,21 @@ TEST(stddata, encode_decode) {
obj_deinit(pikaMain);
}
#endif
#if PIKA_FILEIO_ENABLE
TEST(stddata, fileio) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_runSingleFile(pikaMain, "../../examples/BuiltIn/file.py");
Arg* s = obj_getArg(pikaMain, "s");
Arg* b = obj_getArg(pikaMain, "b");
/* assert */
EXPECT_EQ(arg_getType(s), ARG_TYPE_STRING);
EXPECT_EQ(arg_getType(b), ARG_TYPE_BYTES);
/* deinit */
obj_deinit(pikaMain);
}
#endif

View File

@ -93,6 +93,7 @@ typedef enum {
PIKA_RES_ERR_OPERATION_FAILED,
PIKA_RES_ERR_UNKNOWN,
PIKA_RES_ERR_SYNTAX_ERROR,
PIKA_RES_ERR_IO,
} PIKA_RES;
/* clang-format off */

View File

@ -102,6 +102,10 @@
#define PIKA_EVENT_ENABLE 0
#endif
#ifndef PIKA_FILEIO_ENABLE
#define PIKA_FILEIO_ENABLE 0
#endif
#elif PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MAXIMAL
#ifndef PIKA_SYNTAX_SLICE_ENABLE
#define PIKA_SYNTAX_SLICE_ENABLE 1
@ -130,6 +134,10 @@
#ifndef PIKA_EVENT_ENABLE
#define PIKA_EVENT_ENABLE 1
#endif
#ifndef PIKA_FILEIO_ENABLE
#define PIKA_FILEIO_ENABLE 1
#endif
#endif
@ -226,6 +234,10 @@
#define PIKA_DEBUG_ENABLE 0
#endif
#ifndef PIKA_FILEIO_ENABLE
#define PIKA_FILEIO_ENABLE 1
#endif
#ifndef PIKA_ARG_NUM_MAX
#define PIKA_ARG_NUM_MAX 16
#endif