- Rebuilt TinyPy

- Non-working trash is cleaned.
- Updated from latest git version. 
- Fixed modules pygame math and others. 
- Removed old modules added new ones.
- All samples work except "net"

git-svn-id: svn://kolibrios.org@8535 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
superturbocat2001
2021-01-12 23:18:45 +00:00
parent 44a5c1b211
commit b29cc6670d
59 changed files with 11516 additions and 6128 deletions

View File

@@ -0,0 +1,255 @@
#include <string.h>
#include "tinypy.h"
#define call70(par, st) __asm__ __volatile__("int $0x40":"=a"(st):"a"(70), "b"(par))
#define call70_rw(par, st, cnt) __asm__ __volatile__ ("int $0x40":"=a"(st), "=b"(cnt):"a"(70), "b"(par))
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
#pragma pack(push, 1)
typedef struct {
uint32_t subfnc;
uint32_t res1;
uint32_t res2;
uint32_t res3;
uint8_t *data;
uint8_t res4;
char *fn;
}info_params_t;
#pragma pack(pop)
#pragma pack(push,1)
typedef struct {
uint32_t subfnc;
uint32_t pos;
uint32_t res1;
uint32_t cnt;
char* data;
uint8_t res2;
char* fn;
}rw_params_t;
#pragma pack(pop)
static tp_obj kolibri_close(TP)
{
tp_obj self = TP_TYPE(TP_DICT);
uint32_t size = tp_get(tp, self, tp_string("size")).number.val;
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
tp_set(tp, self, tp_string("closed"), tp_True);
return tp_None;
}
static tp_obj kolibri_read(TP)
{
uint32_t status, num;
tp_obj self = TP_TYPE(TP_DICT);
uint32_t pos = tp_get(tp, self, tp_string("pos")).number.val;
uint32_t size = tp_get(tp, self, tp_string("size")).number.val;
uint32_t cnt;
char *buf = (char *)malloc(size - pos);
rw_params_t params = {0, pos, 0, size - pos, buf, 0,
(char *)tp_get(tp, self, tp_string("name")).string.val};
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
if (*mode != 'r')
tp_raise_f(tp_None, "IOError: file not open for reading", tp_None);
if (!buf)
return tp_None;
call70_rw((&params), status, cnt);
buf[cnt] = '\0';
return tp_string(buf);
}
#if 0
static tp_obj kolibri_readline(TP)
{
return tp_string("Line");
}
static tp_obj kolibri_readlines(TP)
{
tp_obj result = tp_list(tp);
int i;
for(i=0; i < 5; i++)
tp_add(result, tp_string("Line"));
return result;
}
#endif
/* Write object to file.
*
* tp TinyPy virtual machine structure.
*
* returns tp_True
*/
static tp_obj kolibri_write(TP)
{
tp_obj self = TP_TYPE(TP_DICT);
tp_obj obj = TP_OBJ(); /* What to write. */
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
uint32_t pos = (uint32_t)tp_get(tp, self, tp_string("pos")).number.val;
uint32_t size = (uint32_t)tp_get(tp, self, tp_string("size")).number.val;
if (*mode != 'w' && *mode != 'a')
{
tp_raise_f(tp_None, "IOError: file not open for writing", tp_None);
}
else
{
char *data = (char *)TP_CSTR(obj);
rw_params_t params = {3, pos, 0, strlen(data), data, 0,
(char *)tp_get(tp, self, tp_string("name")).string.val};
uint32_t status;
uint32_t cnt;
call70_rw((&params), status, cnt);
if (status)
{
tp_raise_f(tp_None, "IOError: writing failed with status %d", status);
}
pos += cnt;
tp_set(tp, self, tp_string("pos"), tp_number(pos));
if (pos > size)
{
/* If writing has come beyond the file, increase its size. */
tp_set(tp, self, tp_string("size"), tp_number(pos));
}
return tp_True;
}
}
/* Write line list into file.
*
* tp TinyPy virtual machine structure.
*
* returns tp_None.
*/
static tp_obj kolibri_writelines(TP)
{
tp_obj result = tp_None;
tp_obj self = TP_TYPE(TP_DICT);
tp_obj list = TP_TYPE(TP_LIST); /* What to write. */
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
long pos = (long)tp_get(tp, self, tp_string("pos")).number.val;
uint32_t size = (uint32_t)tp_get(tp, self, tp_string("size")).number.val;
if (*mode != 'w' && *mode != 'a')
{
tp_raise_f(tp_None, "IOError: file not open for writing", tp_None);
}
else
{
int i;
char *fn = (char *)tp_get(tp, self, tp_string("name")).string.val;
rw_params_t params = {3, 0, 0, 0, NULL, 0, fn};
for (i = 0; i < list.list.val->len; i++)
{
char *data = (char *)TP_CSTR(list.list.val->items[i]);
uint32_t status;
uint32_t cnt;
params.data = data;
params.cnt = strlen(data);
params.pos = pos;
call70_rw((&params), status, cnt);
if (status)
tp_raise_f(tp_None, "IOError: writing failed with status %d", status);
pos += cnt;
}
tp_set(tp, self, tp_string("pos"), tp_number(pos));
if (pos > size)
{
/* If writing has come beyond the file, increase its size. */
tp_set(tp, self, tp_string("size"), tp_number(pos));
}
return tp_True;
}
}
/* Get file size.
*
* fn ASCIIZ absolute file path.
*/
long long int kolibri_filesize(const char *fn)
{
uint8_t data[40];
uint32_t status;
long long int result;
info_params_t params = {5, 0, 0, 0, data, 0, (char *)fn};
call70((&params), status);
/* File size is at offset 32. */
if (status == 0)
result = *(long long*)(&data[32]);
else
result = -status;
return result;
}
/* Open file.
*
* tp TinyPy virtual machine structure.
*
* returns file object.
*/
tp_obj kolibri_open(TP) {
tp_obj obj;
char *fn = (char *)(TP_TYPE(TP_STRING).string.val);
tp_obj mode_obj = TP_OBJ();
long long int size;
long long int pos = 0;
uint32_t status;
info_params_t params = {2, 0, 0, 0, NULL, 0, fn};
if (mode_obj.type == TP_NONE)
mode_obj = tp_string("r");
else if (mode_obj.type != TP_STRING)
tp_raise_f(tp_None, "ValueError: bad file access mode %s", TP_CSTR(mode_obj));
switch(mode_obj.string.val[0])
{
case 'w':
call70((&params), status);
if (status)
tp_raise_f(tp_None, "IOError: cannot open file for writing", tp_None);
size = 0;
break;
case 'a':
pos = size;
break;
case 'r':
break;
default:
tp_raise_f(tp_None, "ValueError: mode string must begin with 'r', 'w', or 'a'", tp_None);
}
if ((size = kolibri_filesize(fn)) < 0)
tp_raise_f(tp_None, "IOError: filesize returned %d", tp_number(size));
obj = tp_dict(tp);
tp_set(tp, obj, tp_string("name"), tp_string(fn));
tp_set(tp, obj, tp_string("size"), tp_number(size));
tp_set(tp, obj, tp_string("pos"), tp_number(pos));
tp_set(tp, obj, tp_string("mode"), mode_obj);
#if 0
tp_set(tp, obj, tp_string("__doc__"),
tp_string("File object.\nAttributes:\n name: file name\n"
" closed: boolean indicating whether file was closed\n"
"Methods:\n read: read the entire file into string\n"
" readlines: get list of lines\n"
" close: close the file\n"
));
#endif
tp_set(tp, obj, tp_string("closed"), tp_False);
tp_set(tp, obj, tp_string("close"), tp_method(tp, obj, kolibri_close));
tp_set(tp, obj, tp_string("read"), tp_method(tp, obj, kolibri_read));
tp_set(tp, obj, tp_string("write"), tp_method(tp, obj, kolibri_write));
tp_set(tp, obj, tp_string("writelines"), tp_method(tp, obj, kolibri_writelines));
return obj;
}

View File

@@ -0,0 +1,32 @@
#include "tinypy.h"
#include "syscalls.c"
#include "fs.c"
#define EXPORT(MOD_NAME, F_NAME, F_POINT) tp_set(tp, MOD_NAME , tp_string(F_NAME), tp_fnc(tp, F_POINT))
extern tp_obj tp_dict(TP);
extern tp_obj tp_fnc(TP,tp_obj v(TP));
void ksys_init(TP)
{
tp_obj ksys_mod = tp_dict(tp);
// syscalls
EXPORT(ksys_mod, "debug_print" , _debug_print);
EXPORT(ksys_mod, "start_draw" , _start_draw);
EXPORT(ksys_mod, "end_draw" , _end_draw);
EXPORT(ksys_mod, "create_window", _create_window);
EXPORT(ksys_mod, "create_button", _create_button);
EXPORT(ksys_mod, "draw_text" , _draw_text);
EXPORT(ksys_mod, "get_event" , _get_event);
EXPORT(ksys_mod, "get_button" ,_get_button);
EXPORT(ksys_mod, "get_sys_colors",_get_sys_colors);
// filesystem
EXPORT(ksys_mod, "open", kolibri_open);
tp_set(tp, ksys_mod, tp_string("__doc__"), tp_string("KolibriOS system specific functions."));
tp_set(tp, ksys_mod, tp_string("__name__"), tp_string("kolibri"));
tp_set(tp, ksys_mod, tp_string("__file__"), tp_string(__FILE__));
tp_set(tp, tp->modules, tp_string("ksys"), ksys_mod);
}

View File

@@ -0,0 +1,296 @@
#include <sys/socket.h>
// #include <menuet/net.h>
#include "tp.h"
extern tp_obj tp_dict(TP);
extern tp_obj tp_method(TP,tp_obj self,tp_obj v(TP));
extern tp_obj tp_fnc(TP,tp_obj v(TP));
extern tp_obj tp_get(TP,tp_obj self, tp_obj k);
tp_obj tp_has(TP,tp_obj self, tp_obj k);
// #define _cdecl __attribute__((cdecl))
extern int (* _cdecl con_printf)(const char* format,...);
#define PRECISION 0.000001
#define GET_SOCKET_DESCRIPTOR(_obj, _sock) do{ \
if (fabs(tp_has(tp, _obj, tp_string("socket")).number.val) < PRECISION)\
tp_raise(tp_None, "Socket not open", tp_None); \
_sock = (__u32)(tp_get(tp, _obj, tp_string("socket")).number.val + PRECISION);\
} while(0)
/* Socket close method.
*
* Example:
* s.close() # s must be a socket object created by socket.
*
* Raises exception if socket was not opened. Otherwise returns True.
*/
static tp_obj kolibri_close_socket(TP)
{
// tp_obj self = TP_TYPE(TP_DICT);
// __u32 socktype;
// __u32 s;
// GET_SOCKET_DESCRIPTOR(self, s);
// socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
// GET_SOCKET_DESCRIPTOR(self, s);
// if (socktype == SOCK_STREAM)
// __menuet__close_TCP_socket(s);
// else if (socktype == SOCK_DGRAM)
// __menuet__close_UDP_socket(s);
return tp_True;
}
/* Socket send method.
*
* Example:
* data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
* s.send(data)
* or:
* s.send(data, 20) # Send just 20 bytes
*/
static tp_obj kolibri_send(TP)
{
// tp_obj self = TP_TYPE(TP_DICT);
// tp_obj data_obj = TP_TYPE(TP_STRING);
// __u32 datalen = TP_DEFAULT(tp_False).number.val;
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
// __u32 s;
// int result;
// GET_SOCKET_DESCRIPTOR(self, s);
// if (datalen < 0 || datalen > data_obj.string.len)
// datalen = data_obj.string.len;
// if (socktype == SOCK_STREAM)
// result = __menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val);
// else if (socktype == SOCK_DGRAM)
// result = __menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val);
// return tp_number(!(result != 0));
return tp_number(0);
}
#define __u32 unsigned int
#define __u8 unsigned char
/* Socket recv method.
*
* data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
* s.recv(data)
* or:
* s.recv(data, 20) # Send just 20 bytes
*/
static tp_obj kolibri_recv(TP)
{
tp_obj self = TP_TYPE(TP_DICT);
__u32 datalen = TP_DEFAULT(tp_False).number.val;
__u32 s;
__u8 c;
__u8 *buf, *p;
__u32 buf_size;
__u32 bytes_read = 0;
int i;
// GET_SOCKET_DESCRIPTOR(self, s);
// if (datalen)
// buf_size = datalen;
// else
// buf_size = 2048;
// if (!(buf = malloc(datalen)))
// tp_raise(tp_None, "Cannot allocate buffer for received data", tp_None);
// p = buf;
// while (__menuet__read_socket(s, &c) && bytes_read < buf_size)
// {
// *p++ = c;
// bytes_read++;
// if (bytes_read >= buf_size && !datalen)
// {
// buf_size += 1024;
// buf = realloc(buf, buf_size);
// }
// }
return tp_string_n(buf, bytes_read);
}
static void inet_pton(TP, const char *buf, int len, __u32 *addr)
{
char *p = (char *)buf;
int i = 0;
__u32 val = 0;
*addr = 0;
while (*p && p < buf + len && i < 4)
{
if (*p == '.' || !*p)
{
if (val > 255)
tp_raise(tp_None, "ValueError: number > 255 in IP address", tp_None);
*addr += (val << ((i++) << 3));
val = 0;
}
else
{
if (*p < '0' || *p > '9')
tp_raise(tp_None, "ValueError: bad char in IP address, digit expected", tp_None);
val = val * 10 + *p - '0';
}
p++;
}
if (!*p)
{
if (i == 3)
*addr += (val << ((i++) << 3));
else
tp_raise(tp_None, "ValueError: bad IP address", tp_None);
}
}
/* Converter from string presentation to binary address. */
static tp_obj kolibri_inet_pton(TP)
{
tp_obj obj;
__u32 addr;
obj = TP_TYPE(TP_STRING);
// inet_pton(tp, (char *)obj.string.val, (int)obj.string.len, &addr);
return tp_number(addr);
}
/* Socket bind method.
*
* In KolibriOS it just sets local address and port.
*
* Example:
* s.bind('10.10.1.2', 6000) #Connects to 10.10.1.2:6000
*/
tp_obj kolibri_bind(TP)
{
// tp_obj self = TP_TYPE(TP_DICT);
// tp_obj local_addr_obj = TP_OBJ();
// __u32 local_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
// __u32 local_addr;
// if (local_addr_obj.type == TP_NUMBER)
// local_addr = local_addr_obj.number.val;
// else if (local_addr_obj.type == TP_STRING)
// inet_pton(tp, (const char *)local_addr_obj.string.val, local_addr_obj.string.len, &local_addr);
// tp_set(tp, self, tp_string("local_addr"), tp_number(local_addr));
// tp_set(tp, self, tp_string("local_port"), tp_number(local_port));
return tp_None;
}
/* Socket connect method.
*
* Example:
* s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000
*/
tp_obj kolibri_connect(TP)
{
// tp_obj self = TP_TYPE(TP_DICT);
// tp_obj remote_addr_obj = TP_OBJ();
// __u32 remote_addr;
// __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
// __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val;
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
// int s = -1; /* Socket descriptor */
// if (remote_addr_obj.type == TP_NUMBER)
// remote_addr = remote_addr_obj.number.val;
// else if (remote_addr_obj.type == TP_STRING)
// inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);
// if (socktype == SOCK_STREAM)
// s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1);
// else if (socktype == SOCK_DGRAM)
// s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr);
// if (s >= 0)
// {
// tp_set(tp, self, tp_string("socket"), tp_number(s));
// return tp_True;
// }
// else
return tp_False;
}
/* Socket listen method.
*
* Example:
* s.listen('10.10.1.1', 5000)
*/
tp_obj kolibri_listen(TP)
{
// tp_obj self = TP_TYPE(TP_DICT);
// tp_obj remote_addr_obj = TP_OBJ();
// __u32 remote_addr;
// __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
// __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val;
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
// int s = -1; /* Socket descriptor */
// if (socktype != SOCK_STREAM)
// tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None);
// if (remote_addr_obj.type == TP_NUMBER)
// remote_addr = remote_addr_obj.number.val;
// else if (remote_addr_obj.type == TP_STRING)
// inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);
// if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0)
// {
// tp_set(tp, self, tp_string("socket"), tp_number(s));
// return tp_True;
// }
// else
return tp_False;
}
/* Exported function.
*
* Example:
*
* s = socket(socket.AF_INET, socket.SOCK_DGRAM)
*
* Returns socket object.
*/
tp_obj kolibri_socket(TP)
{
tp_obj s;
tp_obj sockfamily = TP_TYPE(TP_NUMBER);
tp_obj socktype = TP_TYPE(TP_NUMBER);
// if (fabs(sockfamily.number.val - AF_INET) > PRECISION ||
// (fabs(socktype.number.val - SOCK_STREAM) > PRECISION &&
// fabs(socktype.number.val - SOCK_DGRAM) > PRECISION))
// return tp_None;
s = tp_dict(tp);
// tp_set(tp, s, tp_string("family"), sockfamily);
// tp_set(tp, s, tp_string("type"), socktype);
// tp_set(tp, s, tp_string("bind"), tp_method(tp, s, kolibri_bind));
// tp_set(tp, s, tp_string("connect"), tp_method(tp, s, kolibri_connect));
// tp_set(tp, s, tp_string("send"), tp_method(tp, s, kolibri_send));
// tp_set(tp, s, tp_string("recv"), tp_method(tp, s, kolibri_recv));
// tp_set(tp, s, tp_string("close"), tp_method(tp, s, kolibri_close_socket));
// if (fabs(socktype.number.val - SOCK_STREAM) < PRECISION)
// tp_set(tp, s, tp_string("listen"), tp_method(tp, s, kolibri_listen));
return s;
}
tp_obj kolibri_socket_module(TP)
{
tp_obj socket_mod = tp_dict(tp);
// tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET));
// tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(SOCK_STREAM));
// tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(SOCK_DGRAM));
tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(0));
tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(0));
tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(0));
tp_set(tp, socket_mod, tp_string("inet_pton"), tp_fnc(tp, kolibri_inet_pton));
tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
return socket_mod;
}

View File

@@ -0,0 +1,92 @@
/* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3 */
#include "tinypy.h"
#include <kos32sys.h>
#define GET_NUM_ARG() TP_TYPE(TP_NUMBER).number.val
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val
void debug_write_byte(const char ch){
__asm__ __volatile__(
"int $0x40"
::"a"(63), "b"(1), "c"(ch)
);
}
static tp_obj _debug_print(TP){
tp_obj str = TP_TYPE(TP_STRING);
for(int i=0; i < str.string.len; i++)
{
debug_write_byte(str.string.val[i]);
}
return tp_None;
}
static tp_obj _start_draw(TP){
begin_draw();
return tp_None;
}
static tp_obj _end_draw(TP){
end_draw();
return tp_None;
}
static tp_obj _create_window(TP){
int x = GET_NUM_ARG();
int y = GET_NUM_ARG();
int w = GET_NUM_ARG();
int h = GET_NUM_ARG();
const char *title= GET_STR_ARG();
unsigned int color = GET_NUM_ARG();
unsigned int style = GET_NUM_ARG();
sys_create_window(x,y,w,h, title, color,style);
return tp_None;
}
static tp_obj _create_button(TP){
unsigned int x = GET_NUM_ARG();
unsigned int y = GET_NUM_ARG();
unsigned int h = GET_NUM_ARG();
unsigned int w = GET_NUM_ARG();
unsigned int id = GET_NUM_ARG();
unsigned int color = GET_NUM_ARG();
define_button((x << 16) + w, (y << 16) + h, id, color);
return tp_None;
}
static tp_obj _draw_text(TP){
const char *str= GET_STR_ARG();
int x = GET_NUM_ARG();
int y = GET_NUM_ARG();
int len = GET_NUM_ARG();
unsigned color = (unsigned)GET_NUM_ARG();
draw_text_sys(str, x, y, len, color);
return tp_None;
}
static tp_obj _get_event(TP){
return tp_number(get_os_event());
}
static tp_obj _get_button(TP){
return tp_number(get_os_button());
}
static tp_obj _get_sys_colors(TP){
tp_obj color_obj = tp_dict(tp);
struct kolibri_system_colors colors;
get_system_colors(&colors);
tp_set(tp, color_obj, tp_string("frame_area"), tp_number(colors.frame_area));
tp_set(tp, color_obj, tp_string("grab_bar"), tp_number(colors.grab_bar));
tp_set(tp, color_obj, tp_string("grab_bar_button)"), tp_number(colors.grab_bar_button));
tp_set(tp, color_obj, tp_string( "grab_button_text)"), tp_number(colors.grab_button_text));
tp_set(tp, color_obj, tp_string("grab_text"), tp_number(colors.grab_text));
tp_set(tp, color_obj, tp_string("work_area"), tp_number(colors.work_area));
tp_set(tp, color_obj, tp_string("work_button"), tp_number(colors.work_button));
tp_set(tp, color_obj, tp_string("work_button_text"), tp_number(colors.work_button_text));
tp_set(tp, color_obj, tp_string("work_graph"), tp_number(colors.work_graph));
tp_set(tp, color_obj, tp_string("work_text"), tp_number(colors.work_text));
return color_obj;
}