Skip to content

Commit

Permalink
mp2c转换pystone
Browse files Browse the repository at this point in the history
  • Loading branch information
xupingmao committed Feb 5, 2022
1 parent cf38d7e commit c9934cf
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 129 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ tm2c/output/
*.bz2
id_rsa_github*
/01_参考项目/
/build/
/build/
minipy.dSYM
46 changes: 29 additions & 17 deletions doc/01_编码规范.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

命名风格:全大写+下划线

示例:
- `APP_VERSION` 应用版本的常量
> 示例如下
```c
const char* APP_VERSION = "0.1.0" // 应用版本的常量
```

## 全局变量

Expand All @@ -20,37 +23,46 @@

命名风格:全大写+下划线

示例:
- `GLOBAL_VAR` 全局变量
> 示例如下
```c
int GLOBAL_COUNT = 0;
```

## 临时变量

命名风格:小写字符+下划线

示例:
- `int i`
- `local_var` 临时变量
> 示例如下
```c
int i = 0; // 临时的索引
char buf[20]; // 临时缓冲区
```

## 函数

命名风格:函数命名分为3个部分:`[前缀_] 功能描述 [_返回类型]`

在模拟面向对象的场景中,`[前缀_]`可以是第一个参数的类型(或者缩写)

示例:
> 示例如下
1) 简单的函数
- `int is_true_object(MpObj object)`
```c
// 1) 简单的函数
int is_true_object(MpObj object);

2) 模块下面的函数
- `mp_malloc` minipy的申请内存函数
// 2) 模块下面的函数
void* mp_malloc(size_t size); // minipy的申请内存函数

3) 面向对象函数
- `MpObj obj_getattr(MpObj obj, MpObj key)` 获取对象的属性
- `char* obj_getattr_cstr(MpObj obj, MpObj key)` 获取对象属性并且转为C字符串
// 3) 面向对象函数
MpObj obj_getattr(MpObj obj, MpObj key); // 获取对象的属性
char* obj_getattr_cstr(MpObj obj, MpObj key); // 获取对象属性并且转为C字符串

4) 内部函数
- `foo0/_foo` 内部函数,后缀0或者前缀`_` 必须声明为`static`
// 4) 内部函数,后缀0或者前缀`_` 必须声明为`static`
static void foo0(int arg1);
static void _foo();
```
# 缩写前缀
Expand Down
12 changes: 12 additions & 0 deletions doc/03_TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 基础功能

## Unicode支持

由于`UTF-8`是兼容ASCII码的变长编码,所以目前minipy的字符串可以存储`UTF-8`编码的字符串,但是`len(str)`,`str[index]`这类操作的结果是不正确的

# 高级功能

## 性能优化

参考【性能优化技巧】

6 changes: 6 additions & 0 deletions doc/05_JIT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# JIT的主要性能优化策略

## 1、把字节码编译成原生代码

## 2、类型预测

19 changes: 15 additions & 4 deletions mp2c/mp2c.c → mp2c/mp2c.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../src/vm.c"

#ifndef TM2C_C
#define TM2C_C
#ifndef TM2C_H
#define TM2C_H

#define LEVEL_ERROR 0
#define LOG(level, msg, lineno, func_name, value) /* x */
Expand Down Expand Up @@ -38,6 +38,18 @@ MpObj obj_LTEQ(MpObj left, MpObj right) {
return number_obj(mp_cmp(left, right) <= 0);
}

MpObj obj_GTEQ(MpObj left, MpObj right) {
return number_obj(mp_cmp(left, right) >= 0);
}

MpObj obj_not_eq(MpObj left, MpObj right) {
return number_obj(!is_obj_equals(left, right));
}

MpObj obj_and(MpObj left, MpObj right) {
return number_obj(is_true_obj(left) && is_true_obj(right));
}

void gc_local_add(MpObj object) {
gc_track(object);
}
Expand Down Expand Up @@ -211,7 +223,6 @@ MpObj argv_to_dict(int n, ...) {
*/
int mp2c_run_func(int argc, char* argv[], char* mod_name, MpNativeFunc func) {
int ret = vm_init(argc, argv);
int i;
if (ret != 0) {
return ret;
}
Expand All @@ -237,7 +248,7 @@ int mp2c_run_func(int argc, char* argv[], char* mod_name, MpNativeFunc func) {
} else if (code == 1){
mp_traceback();
} else if (code == 2){

mp_traceback();
}
vm_destroy();
return 0;
Expand Down
Loading

0 comments on commit c9934cf

Please sign in to comment.