l0n0lc 0.7.6__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/PKG-INFO +72 -13
- l0n0lc-0.8.0/README.md +190 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc/jit.py +41 -26
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc//347/274/226/350/257/221.py +6 -3
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc.egg-info/PKG-INFO +72 -13
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/pyproject.toml +1 -1
- l0n0lc-0.7.6/README.md +0 -131
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/LICENSE +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc/StdList.py +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc/StdMap.py +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc/__init__.py +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py" +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc//351/200/232/347/224/250.py" +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc.egg-info/SOURCES.txt +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc.egg-info/dependency_links.txt +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/l0n0lc.egg-info/top_level.txt +0 -0
- {l0n0lc-0.7.6 → l0n0lc-0.8.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: l0n0lc
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.8.0
|
4
4
|
Summary: 一个将python函数翻译为c++函数并运行的jit编译器
|
5
5
|
Classifier: Programming Language :: Python :: 3
|
6
6
|
Requires-Python: >=3.10
|
@@ -33,10 +33,21 @@ def cpp_cout(*args):
|
|
33
33
|
code += '<< std::endl;'
|
34
34
|
return code
|
35
35
|
|
36
|
+
|
36
37
|
@lc.直接调用函数
|
37
38
|
def test_直接调用():
|
38
39
|
return 123
|
39
40
|
|
41
|
+
|
42
|
+
def test_other_fn(a: int, b: int) -> int:
|
43
|
+
return a - b
|
44
|
+
|
45
|
+
|
46
|
+
@lc.jit(每次运行都重新编译=True)
|
47
|
+
def test编译的函数(a: int, b: int) -> int:
|
48
|
+
return a * b
|
49
|
+
|
50
|
+
|
40
51
|
@lc.jit(每次运行都重新编译=True)
|
41
52
|
def test_add(a: int, b: int) -> int:
|
42
53
|
if a > 1:
|
@@ -50,17 +61,19 @@ def test_add(a: int, b: int) -> int:
|
|
50
61
|
cc['c'] = 3
|
51
62
|
print('输出map:')
|
52
63
|
for ii in cc:
|
53
|
-
print(ii.first, ii.second)
|
64
|
+
print(ii.first, ii.second) # type: ignore
|
54
65
|
aa = [1, 3, 2]
|
55
66
|
aa[0] = 134
|
56
67
|
print('输出list:')
|
57
68
|
for i in range(3):
|
58
69
|
print(i, aa[i])
|
59
70
|
print('Hello World', a, b)
|
71
|
+
print('test_other_fn', test_other_fn(a, b))
|
72
|
+
print('test编译的函数', test编译的函数(a, b))
|
60
73
|
return a + b + 1 + test_直接调用()
|
61
74
|
|
62
|
-
print('结果:', test_add(1, 3))
|
63
75
|
|
76
|
+
print('结果:', test_add(1, 3))
|
64
77
|
```
|
65
78
|
## 执行hello_world.py
|
66
79
|
```bash
|
@@ -74,22 +87,32 @@ b 2
|
|
74
87
|
1 3
|
75
88
|
2 2
|
76
89
|
Hello World 13 3
|
90
|
+
test_other_fn 10
|
91
|
+
test编译的函数 39
|
77
92
|
结果: 140
|
78
93
|
```
|
79
94
|
## 查看生成的c++代码文件
|
80
95
|
```bash
|
81
96
|
$ ls -al l0n0lcoutput/
|
82
|
-
total
|
83
|
-
drwxr-xr-x 2 root root 4096 Sep
|
84
|
-
drwxrwxrwx 11 1000 1000 4096 Sep 11
|
85
|
-
-rw-r--r-- 1 root root
|
86
|
-
-rw-r--r-- 1 root root
|
87
|
-
-rwxr-xr-x 1 root root
|
97
|
+
total 96
|
98
|
+
drwxr-xr-x 2 root root 4096 Sep 12 00:25 .
|
99
|
+
drwxrwxrwx 11 1000 1000 4096 Sep 11 23:45 ..
|
100
|
+
-rw-r--r-- 1 root root 994 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.cpp
|
101
|
+
-rw-r--r-- 1 root root 246 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.h
|
102
|
+
-rwxr-xr-x 1 root root 29744 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.so
|
103
|
+
-rw-r--r-- 1 root root 121 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.cpp
|
104
|
+
-rw-r--r-- 1 root root 93 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.h
|
105
|
+
-rwxr-xr-x 1 root root 15616 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.so
|
106
|
+
-rw-r--r-- 1 root root 185 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.cpp
|
107
|
+
-rw-r--r-- 1 root root 151 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.h
|
108
|
+
-rwxr-xr-x 1 root root 15656 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.so
|
88
109
|
```
|
89
110
|
|
90
|
-
## test_add_@
|
111
|
+
## test_add_@6ddf6c9d797f5d1b.h
|
91
112
|
|
92
113
|
```c++
|
114
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
115
|
+
#include "test编译的函数_@5f8bb0506cab8a49.h"
|
93
116
|
#include <cmath>
|
94
117
|
#include <cstdint>
|
95
118
|
#include <iostream>
|
@@ -98,9 +121,9 @@ drwxrwxrwx 11 1000 1000 4096 Sep 11 02:32 ..
|
|
98
121
|
extern "C" int64_t test_add (int64_t a, int64_t b);
|
99
122
|
```
|
100
123
|
|
101
|
-
## test_add_@
|
124
|
+
## test_add_@6ddf6c9d797f5d1b.cpp
|
102
125
|
```c++
|
103
|
-
#include "test_add_@
|
126
|
+
#include "test_add_@6ddf6c9d797f5d1b.h"
|
104
127
|
extern "C" int64_t test_add (int64_t a, int64_t b)
|
105
128
|
{
|
106
129
|
if ((a > 1))
|
@@ -136,6 +159,42 @@ extern "C" int64_t test_add (int64_t a, int64_t b)
|
|
136
159
|
}
|
137
160
|
|
138
161
|
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
139
|
-
|
162
|
+
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
|
163
|
+
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
|
164
|
+
return a + b + 1 + 123;
|
165
|
+
}
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
## test_other_fn_@75fdd928ab58a8e3.h
|
170
|
+
```c++
|
171
|
+
#include <cstdint>
|
172
|
+
#include <string>
|
173
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b);
|
174
|
+
```
|
175
|
+
|
176
|
+
## test_other_fn_@75fdd928ab58a8e3.cpp
|
177
|
+
```c++
|
178
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
179
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b)
|
180
|
+
{
|
181
|
+
return a - b;
|
182
|
+
}
|
183
|
+
```
|
184
|
+
|
185
|
+
|
186
|
+
## test编译的函数_@5f8bb0506cab8a49.h
|
187
|
+
```c++
|
188
|
+
#include <cstdint>
|
189
|
+
#include <string>
|
190
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b);
|
191
|
+
```
|
192
|
+
|
193
|
+
## test编译的函数_@5f8bb0506cab8a49.cpp
|
194
|
+
```c++
|
195
|
+
#include "test编译的函数_@5f8bb0506cab8a49.h"
|
196
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b)
|
197
|
+
{
|
198
|
+
return a * b;
|
140
199
|
}
|
141
200
|
```
|
l0n0lc-0.8.0/README.md
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
# 将python函数翻译为c++函数并运行
|
2
|
+
## 安装
|
3
|
+
```
|
4
|
+
pip install l0n0lc
|
5
|
+
```
|
6
|
+
|
7
|
+
## hello_world.py
|
8
|
+
```python
|
9
|
+
import l0n0lc as lc
|
10
|
+
import math
|
11
|
+
|
12
|
+
|
13
|
+
@lc.映射函数(math.ceil, ['<cmath>'])
|
14
|
+
def cpp_ceil(v):
|
15
|
+
return f'std::ceil({lc.toCString(v)});'
|
16
|
+
|
17
|
+
|
18
|
+
@lc.映射函数(print, ['<iostream>'])
|
19
|
+
def cpp_cout(*args):
|
20
|
+
code = f'std::cout'
|
21
|
+
for arg in args:
|
22
|
+
code += f'<< {lc.toCString(arg)} << " "'
|
23
|
+
code += '<< std::endl;'
|
24
|
+
return code
|
25
|
+
|
26
|
+
|
27
|
+
@lc.直接调用函数
|
28
|
+
def test_直接调用():
|
29
|
+
return 123
|
30
|
+
|
31
|
+
|
32
|
+
def test_other_fn(a: int, b: int) -> int:
|
33
|
+
return a - b
|
34
|
+
|
35
|
+
|
36
|
+
@lc.jit(每次运行都重新编译=True)
|
37
|
+
def test编译的函数(a: int, b: int) -> int:
|
38
|
+
return a * b
|
39
|
+
|
40
|
+
|
41
|
+
@lc.jit(每次运行都重新编译=True)
|
42
|
+
def test_add(a: int, b: int) -> int:
|
43
|
+
if a > 1:
|
44
|
+
return a + b
|
45
|
+
for i in range(1, 10, 2):
|
46
|
+
a += i
|
47
|
+
for i in [1, 2, 3]:
|
48
|
+
a += i
|
49
|
+
a = math.ceil(12.5)
|
50
|
+
cc = {'a': 1, 'b': 2}
|
51
|
+
cc['c'] = 3
|
52
|
+
print('输出map:')
|
53
|
+
for ii in cc:
|
54
|
+
print(ii.first, ii.second) # type: ignore
|
55
|
+
aa = [1, 3, 2]
|
56
|
+
aa[0] = 134
|
57
|
+
print('输出list:')
|
58
|
+
for i in range(3):
|
59
|
+
print(i, aa[i])
|
60
|
+
print('Hello World', a, b)
|
61
|
+
print('test_other_fn', test_other_fn(a, b))
|
62
|
+
print('test编译的函数', test编译的函数(a, b))
|
63
|
+
return a + b + 1 + test_直接调用()
|
64
|
+
|
65
|
+
|
66
|
+
print('结果:', test_add(1, 3))
|
67
|
+
```
|
68
|
+
## 执行hello_world.py
|
69
|
+
```bash
|
70
|
+
$ python hello_world.py
|
71
|
+
输出map:
|
72
|
+
c 3
|
73
|
+
a 1
|
74
|
+
b 2
|
75
|
+
输出list:
|
76
|
+
0 134
|
77
|
+
1 3
|
78
|
+
2 2
|
79
|
+
Hello World 13 3
|
80
|
+
test_other_fn 10
|
81
|
+
test编译的函数 39
|
82
|
+
结果: 140
|
83
|
+
```
|
84
|
+
## 查看生成的c++代码文件
|
85
|
+
```bash
|
86
|
+
$ ls -al l0n0lcoutput/
|
87
|
+
total 96
|
88
|
+
drwxr-xr-x 2 root root 4096 Sep 12 00:25 .
|
89
|
+
drwxrwxrwx 11 1000 1000 4096 Sep 11 23:45 ..
|
90
|
+
-rw-r--r-- 1 root root 994 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.cpp
|
91
|
+
-rw-r--r-- 1 root root 246 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.h
|
92
|
+
-rwxr-xr-x 1 root root 29744 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.so
|
93
|
+
-rw-r--r-- 1 root root 121 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.cpp
|
94
|
+
-rw-r--r-- 1 root root 93 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.h
|
95
|
+
-rwxr-xr-x 1 root root 15616 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.so
|
96
|
+
-rw-r--r-- 1 root root 185 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.cpp
|
97
|
+
-rw-r--r-- 1 root root 151 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.h
|
98
|
+
-rwxr-xr-x 1 root root 15656 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.so
|
99
|
+
```
|
100
|
+
|
101
|
+
## test_add_@6ddf6c9d797f5d1b.h
|
102
|
+
|
103
|
+
```c++
|
104
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
105
|
+
#include "test编译的函数_@5f8bb0506cab8a49.h"
|
106
|
+
#include <cmath>
|
107
|
+
#include <cstdint>
|
108
|
+
#include <iostream>
|
109
|
+
#include <string>
|
110
|
+
#include <unordered_map>
|
111
|
+
extern "C" int64_t test_add (int64_t a, int64_t b);
|
112
|
+
```
|
113
|
+
|
114
|
+
## test_add_@6ddf6c9d797f5d1b.cpp
|
115
|
+
```c++
|
116
|
+
#include "test_add_@6ddf6c9d797f5d1b.h"
|
117
|
+
extern "C" int64_t test_add (int64_t a, int64_t b)
|
118
|
+
{
|
119
|
+
if ((a > 1))
|
120
|
+
{
|
121
|
+
return a + b;
|
122
|
+
}
|
123
|
+
|
124
|
+
for (int64_t i = 1; i < 10; i += 2)
|
125
|
+
{
|
126
|
+
a = a + i;
|
127
|
+
}
|
128
|
+
|
129
|
+
for (auto i : {1,2,3})
|
130
|
+
{
|
131
|
+
a = a + i;
|
132
|
+
}
|
133
|
+
|
134
|
+
a = std::ceil(12.5);;
|
135
|
+
std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
136
|
+
cc[u8"c"] = 3;
|
137
|
+
std::cout<< u8"输出map:" << " "<< std::endl;
|
138
|
+
for (auto ii : cc)
|
139
|
+
{
|
140
|
+
std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
|
141
|
+
}
|
142
|
+
|
143
|
+
int64_t aa[] = {1,3,2};
|
144
|
+
aa[0] = 134;
|
145
|
+
std::cout<< u8"输出list:" << " "<< std::endl;
|
146
|
+
for (int64_t i = 0; i < 3; ++i)
|
147
|
+
{
|
148
|
+
std::cout<< i << " "<< aa[i] << " "<< std::endl;
|
149
|
+
}
|
150
|
+
|
151
|
+
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
152
|
+
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
|
153
|
+
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
|
154
|
+
return a + b + 1 + 123;
|
155
|
+
}
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
## test_other_fn_@75fdd928ab58a8e3.h
|
160
|
+
```c++
|
161
|
+
#include <cstdint>
|
162
|
+
#include <string>
|
163
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b);
|
164
|
+
```
|
165
|
+
|
166
|
+
## test_other_fn_@75fdd928ab58a8e3.cpp
|
167
|
+
```c++
|
168
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
169
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b)
|
170
|
+
{
|
171
|
+
return a - b;
|
172
|
+
}
|
173
|
+
```
|
174
|
+
|
175
|
+
|
176
|
+
## test编译的函数_@5f8bb0506cab8a49.h
|
177
|
+
```c++
|
178
|
+
#include <cstdint>
|
179
|
+
#include <string>
|
180
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b);
|
181
|
+
```
|
182
|
+
|
183
|
+
## test编译的函数_@5f8bb0506cab8a49.cpp
|
184
|
+
```c++
|
185
|
+
#include "test编译的函数_@5f8bb0506cab8a49.h"
|
186
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b)
|
187
|
+
{
|
188
|
+
return a * b;
|
189
|
+
}
|
190
|
+
```
|
@@ -62,9 +62,19 @@ class c获取属性:
|
|
62
62
|
return f'{self.变量}.{self.属性}'
|
63
63
|
|
64
64
|
|
65
|
+
class c函数调用:
|
66
|
+
def __init__(self, 函数名, 参数字符串) -> None:
|
67
|
+
self.函数名 = 函数名
|
68
|
+
self.参数字符串 = 参数字符串
|
69
|
+
|
70
|
+
def __str__(self) -> str:
|
71
|
+
return f'{self.函数名}({self.参数字符串})'
|
72
|
+
|
73
|
+
|
65
74
|
class py2cpp编译器(ast.NodeVisitor):
|
66
|
-
def __init__(self, 被编译的函数: Callable) -> None:
|
75
|
+
def __init__(self, 被编译的函数: Callable, c编译器) -> None:
|
67
76
|
self.被编译的函数 = 被编译的函数
|
77
|
+
self.c编译器: cpp编译器 = c编译器
|
68
78
|
self.源代码 = inspect.getsource(被编译的函数)
|
69
79
|
self.代码哈希值 = hashlib.blake2s(
|
70
80
|
self.源代码.encode(), digest_size=8).hexdigest()
|
@@ -77,7 +87,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
77
87
|
self.参数不同c类型前置处理函数 = {}
|
78
88
|
self.ascendc变量对应表: List[Dict[str, Any]] = [{}]
|
79
89
|
self.当前上下文层级 = 0
|
80
|
-
self
|
90
|
+
self.依赖函数: List[py2cpp编译器] = []
|
81
91
|
self.函数映射表 = {}
|
82
92
|
self.代码序列 = []
|
83
93
|
self.花括号 = 花括号(self)
|
@@ -98,6 +108,18 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
98
108
|
def 编译(self):
|
99
109
|
语法树 = ast.parse(self.源代码)
|
100
110
|
self.visit(语法树)
|
111
|
+
# 读取依赖函数
|
112
|
+
cpps = [f'{通用信息.工作文件夹地址}/{self.获取cpp文件名()}']
|
113
|
+
for b in self.依赖函数:
|
114
|
+
self.include目录.add(f'"{b.获取h文件名()}"')
|
115
|
+
cpps.append(f'{通用信息.工作文件夹地址}/{b.获取cpp文件名()}')
|
116
|
+
|
117
|
+
self.将代码保存到文件()
|
118
|
+
self.c编译器.include目录列表 = list(self.include目录)
|
119
|
+
self.c编译器.库目录列表 = list(self.库目录)
|
120
|
+
self.c编译器.链接库 = list(self.链接库列表)
|
121
|
+
输出路径 = f'{通用信息.工作文件夹地址}/{self.获取库文件名()}'
|
122
|
+
self.c编译器.编译为动态库(cpps, 输出路径=输出路径)
|
101
123
|
|
102
124
|
def 添加c代码(self, 代码: str):
|
103
125
|
self.代码序列.append(c代码(代码, self.当前上下文层级))
|
@@ -288,19 +310,20 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
288
310
|
if not isinstance(fn, Callable):
|
289
311
|
self.抛出代码异常(f'{ast.dump(node.func)} 没找到', node)
|
290
312
|
|
291
|
-
依赖函数编译器 = self.__class__(fn)
|
292
|
-
依赖函数编译器.编译()
|
293
|
-
|
294
|
-
# 添加依赖
|
295
|
-
self.依赖函数.append(依赖函数编译器)
|
296
|
-
|
297
|
-
# 构建调用代码
|
298
313
|
if len(node.keywords) > 0:
|
299
314
|
self.代码异常抛出器('暂不支持 keywords 函数调用', node)
|
300
315
|
|
301
316
|
参数列表 = [str(self.获取值(arg)) for arg in node.args]
|
302
317
|
参数字符串 = ','.join(参数列表)
|
303
|
-
|
318
|
+
|
319
|
+
if isinstance(fn, py2cpp编译器):
|
320
|
+
self.依赖函数.append(fn)
|
321
|
+
return c函数调用(fn.c函数名, 参数字符串)
|
322
|
+
else:
|
323
|
+
依赖函数编译器 = self.__class__(fn, self.c编译器)
|
324
|
+
依赖函数编译器.编译()
|
325
|
+
self.依赖函数.append(依赖函数编译器)
|
326
|
+
return c函数调用(fn.__name__, 参数字符串)
|
304
327
|
|
305
328
|
def 获取Subscript(self, node: ast.Subscript):
|
306
329
|
对象 = self.获取值(node.value)
|
@@ -454,7 +477,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
454
477
|
|
455
478
|
def visit_Call(self, node: ast.Call) -> Any:
|
456
479
|
代码 = self.调用Call(node)
|
457
|
-
self.添加c代码(代码)
|
480
|
+
self.添加c代码(str(代码))
|
458
481
|
|
459
482
|
def _赋值(self, target, 值, node):
|
460
483
|
目标 = self.获取值(target)
|
@@ -545,7 +568,8 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
545
568
|
fp.write(cpp代码)
|
546
569
|
|
547
570
|
def 加载库(self):
|
548
|
-
|
571
|
+
库地址 = f'{通用信息.工作文件夹地址}/{self.获取库文件名()}'
|
572
|
+
self.目标库 = ctypes.CDLL(库地址)
|
549
573
|
self.cpp函数 = self.目标库[self.c函数名]
|
550
574
|
self.cpp函数.argtypes = self.ctypes类型
|
551
575
|
self.cpp函数.restype = self.ctypes返回类型
|
@@ -554,24 +578,15 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
554
578
|
return self.cpp函数(*args)
|
555
579
|
|
556
580
|
|
557
|
-
def jit(jit
|
581
|
+
def jit(jit编译器类=None, cpp编译器类=None, 每次运行都重新编译: bool = False):
|
558
582
|
def 编译函数(fn: Callable):
|
559
583
|
# print(ast.dump(语法树, indent=' '))
|
560
|
-
|
584
|
+
_c编译器类 = cpp编译器类 or cpp编译器
|
585
|
+
_jit编译器类 = jit编译器类 or py2cpp编译器
|
586
|
+
c语言函数编译器 = _jit编译器类(fn, _c编译器类())
|
561
587
|
库文件名 = c语言函数编译器.获取库文件名()
|
562
|
-
# 检测是否已存在
|
563
588
|
if 每次运行都重新编译 or not os.path.exists(f'{通用信息.工作文件夹地址}/{库文件名}'):
|
564
589
|
c语言函数编译器.编译()
|
565
|
-
c语言函数编译器.将代码保存到文件()
|
566
|
-
编译器 = c编译器 or cpp编译器()
|
567
|
-
编译器.include目录列表 = list(c语言函数编译器.include目录)
|
568
|
-
编译器.库目录列表 = list(c语言函数编译器.库目录)
|
569
|
-
编译器.链接库 = list(c语言函数编译器.链接库列表)
|
570
|
-
编译器.编译为动态库(f'{通用信息.工作文件夹地址}/{c语言函数编译器.获取cpp文件名()}',
|
571
|
-
f'{通用信息.工作文件夹地址}/{c语言函数编译器.获取库文件名()}')
|
572
590
|
c语言函数编译器.加载库()
|
573
|
-
|
574
|
-
def call(*args):
|
575
|
-
return c语言函数编译器(*args)
|
576
|
-
return call
|
591
|
+
return c语言函数编译器
|
577
592
|
return 编译函数
|
@@ -37,18 +37,21 @@ class cpp编译器:
|
|
37
37
|
return
|
38
38
|
self.编译选项 += 选项
|
39
39
|
|
40
|
-
def 编译文件(self, 文件路径: str, 输出路径: str):
|
40
|
+
def 编译文件(self, 文件路径: Union[List[str], str], 输出路径: str):
|
41
41
|
编译指令 = [self.编译器]
|
42
42
|
include指令 = [f'-I{目录}' for 目录 in self.include目录列表]
|
43
43
|
库目录指令 = [f'-L{目录}' for 目录 in self.库目录列表]
|
44
44
|
库链接指令 = [f'-l{库名}' for 库名 in self.链接库]
|
45
45
|
编译指令 += include指令 + 库目录指令 + 库链接指令 + self.编译选项
|
46
|
-
|
46
|
+
if isinstance(文件路径, list):
|
47
|
+
编译指令 += 文件路径
|
48
|
+
else:
|
49
|
+
编译指令.append(文件路径)
|
47
50
|
编译指令.append('-o')
|
48
51
|
编译指令.append(输出路径)
|
49
52
|
subprocess.run(编译指令)
|
50
53
|
|
51
|
-
def 编译为动态库(self, 文件路径: str, 输出路径: str):
|
54
|
+
def 编译为动态库(self, 文件路径: Union[List[str], str], 输出路径: str):
|
52
55
|
self.添加编译选项('-fPIC')
|
53
56
|
self.添加编译选项('--shared')
|
54
57
|
self.添加编译选项('-O2')
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: l0n0lc
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.8.0
|
4
4
|
Summary: 一个将python函数翻译为c++函数并运行的jit编译器
|
5
5
|
Classifier: Programming Language :: Python :: 3
|
6
6
|
Requires-Python: >=3.10
|
@@ -33,10 +33,21 @@ def cpp_cout(*args):
|
|
33
33
|
code += '<< std::endl;'
|
34
34
|
return code
|
35
35
|
|
36
|
+
|
36
37
|
@lc.直接调用函数
|
37
38
|
def test_直接调用():
|
38
39
|
return 123
|
39
40
|
|
41
|
+
|
42
|
+
def test_other_fn(a: int, b: int) -> int:
|
43
|
+
return a - b
|
44
|
+
|
45
|
+
|
46
|
+
@lc.jit(每次运行都重新编译=True)
|
47
|
+
def test编译的函数(a: int, b: int) -> int:
|
48
|
+
return a * b
|
49
|
+
|
50
|
+
|
40
51
|
@lc.jit(每次运行都重新编译=True)
|
41
52
|
def test_add(a: int, b: int) -> int:
|
42
53
|
if a > 1:
|
@@ -50,17 +61,19 @@ def test_add(a: int, b: int) -> int:
|
|
50
61
|
cc['c'] = 3
|
51
62
|
print('输出map:')
|
52
63
|
for ii in cc:
|
53
|
-
print(ii.first, ii.second)
|
64
|
+
print(ii.first, ii.second) # type: ignore
|
54
65
|
aa = [1, 3, 2]
|
55
66
|
aa[0] = 134
|
56
67
|
print('输出list:')
|
57
68
|
for i in range(3):
|
58
69
|
print(i, aa[i])
|
59
70
|
print('Hello World', a, b)
|
71
|
+
print('test_other_fn', test_other_fn(a, b))
|
72
|
+
print('test编译的函数', test编译的函数(a, b))
|
60
73
|
return a + b + 1 + test_直接调用()
|
61
74
|
|
62
|
-
print('结果:', test_add(1, 3))
|
63
75
|
|
76
|
+
print('结果:', test_add(1, 3))
|
64
77
|
```
|
65
78
|
## 执行hello_world.py
|
66
79
|
```bash
|
@@ -74,22 +87,32 @@ b 2
|
|
74
87
|
1 3
|
75
88
|
2 2
|
76
89
|
Hello World 13 3
|
90
|
+
test_other_fn 10
|
91
|
+
test编译的函数 39
|
77
92
|
结果: 140
|
78
93
|
```
|
79
94
|
## 查看生成的c++代码文件
|
80
95
|
```bash
|
81
96
|
$ ls -al l0n0lcoutput/
|
82
|
-
total
|
83
|
-
drwxr-xr-x 2 root root 4096 Sep
|
84
|
-
drwxrwxrwx 11 1000 1000 4096 Sep 11
|
85
|
-
-rw-r--r-- 1 root root
|
86
|
-
-rw-r--r-- 1 root root
|
87
|
-
-rwxr-xr-x 1 root root
|
97
|
+
total 96
|
98
|
+
drwxr-xr-x 2 root root 4096 Sep 12 00:25 .
|
99
|
+
drwxrwxrwx 11 1000 1000 4096 Sep 11 23:45 ..
|
100
|
+
-rw-r--r-- 1 root root 994 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.cpp
|
101
|
+
-rw-r--r-- 1 root root 246 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.h
|
102
|
+
-rwxr-xr-x 1 root root 29744 Sep 12 00:25 test_add_@6ddf6c9d797f5d1b.so
|
103
|
+
-rw-r--r-- 1 root root 121 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.cpp
|
104
|
+
-rw-r--r-- 1 root root 93 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.h
|
105
|
+
-rwxr-xr-x 1 root root 15616 Sep 12 00:25 test_other_fn_@75fdd928ab58a8e3.so
|
106
|
+
-rw-r--r-- 1 root root 185 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.cpp
|
107
|
+
-rw-r--r-- 1 root root 151 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.h
|
108
|
+
-rwxr-xr-x 1 root root 15656 Sep 12 00:25 test编译的函数_@5f8bb0506cab8a49.so
|
88
109
|
```
|
89
110
|
|
90
|
-
## test_add_@
|
111
|
+
## test_add_@6ddf6c9d797f5d1b.h
|
91
112
|
|
92
113
|
```c++
|
114
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
115
|
+
#include "test编译的函数_@5f8bb0506cab8a49.h"
|
93
116
|
#include <cmath>
|
94
117
|
#include <cstdint>
|
95
118
|
#include <iostream>
|
@@ -98,9 +121,9 @@ drwxrwxrwx 11 1000 1000 4096 Sep 11 02:32 ..
|
|
98
121
|
extern "C" int64_t test_add (int64_t a, int64_t b);
|
99
122
|
```
|
100
123
|
|
101
|
-
## test_add_@
|
124
|
+
## test_add_@6ddf6c9d797f5d1b.cpp
|
102
125
|
```c++
|
103
|
-
#include "test_add_@
|
126
|
+
#include "test_add_@6ddf6c9d797f5d1b.h"
|
104
127
|
extern "C" int64_t test_add (int64_t a, int64_t b)
|
105
128
|
{
|
106
129
|
if ((a > 1))
|
@@ -136,6 +159,42 @@ extern "C" int64_t test_add (int64_t a, int64_t b)
|
|
136
159
|
}
|
137
160
|
|
138
161
|
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
139
|
-
|
162
|
+
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
|
163
|
+
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
|
164
|
+
return a + b + 1 + 123;
|
165
|
+
}
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
## test_other_fn_@75fdd928ab58a8e3.h
|
170
|
+
```c++
|
171
|
+
#include <cstdint>
|
172
|
+
#include <string>
|
173
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b);
|
174
|
+
```
|
175
|
+
|
176
|
+
## test_other_fn_@75fdd928ab58a8e3.cpp
|
177
|
+
```c++
|
178
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
179
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b)
|
180
|
+
{
|
181
|
+
return a - b;
|
182
|
+
}
|
183
|
+
```
|
184
|
+
|
185
|
+
|
186
|
+
## test编译的函数_@5f8bb0506cab8a49.h
|
187
|
+
```c++
|
188
|
+
#include <cstdint>
|
189
|
+
#include <string>
|
190
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b);
|
191
|
+
```
|
192
|
+
|
193
|
+
## test编译的函数_@5f8bb0506cab8a49.cpp
|
194
|
+
```c++
|
195
|
+
#include "test编译的函数_@5f8bb0506cab8a49.h"
|
196
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b)
|
197
|
+
{
|
198
|
+
return a * b;
|
140
199
|
}
|
141
200
|
```
|
l0n0lc-0.7.6/README.md
DELETED
@@ -1,131 +0,0 @@
|
|
1
|
-
# 将python函数翻译为c++函数并运行
|
2
|
-
## 安装
|
3
|
-
```
|
4
|
-
pip install l0n0lc
|
5
|
-
```
|
6
|
-
|
7
|
-
## hello_world.py
|
8
|
-
```python
|
9
|
-
import l0n0lc as lc
|
10
|
-
import math
|
11
|
-
|
12
|
-
|
13
|
-
@lc.映射函数(math.ceil, ['<cmath>'])
|
14
|
-
def cpp_ceil(v):
|
15
|
-
return f'std::ceil({lc.toCString(v)});'
|
16
|
-
|
17
|
-
|
18
|
-
@lc.映射函数(print, ['<iostream>'])
|
19
|
-
def cpp_cout(*args):
|
20
|
-
code = f'std::cout'
|
21
|
-
for arg in args:
|
22
|
-
code += f'<< {lc.toCString(arg)} << " "'
|
23
|
-
code += '<< std::endl;'
|
24
|
-
return code
|
25
|
-
|
26
|
-
@lc.直接调用函数
|
27
|
-
def test_直接调用():
|
28
|
-
return 123
|
29
|
-
|
30
|
-
@lc.jit(每次运行都重新编译=True)
|
31
|
-
def test_add(a: int, b: int) -> int:
|
32
|
-
if a > 1:
|
33
|
-
return a + b
|
34
|
-
for i in range(1, 10, 2):
|
35
|
-
a += i
|
36
|
-
for i in [1, 2, 3]:
|
37
|
-
a += i
|
38
|
-
a = math.ceil(12.5)
|
39
|
-
cc = {'a': 1, 'b': 2}
|
40
|
-
cc['c'] = 3
|
41
|
-
print('输出map:')
|
42
|
-
for ii in cc:
|
43
|
-
print(ii.first, ii.second) # type: ignore
|
44
|
-
aa = [1, 3, 2]
|
45
|
-
aa[0] = 134
|
46
|
-
print('输出list:')
|
47
|
-
for i in range(3):
|
48
|
-
print(i, aa[i])
|
49
|
-
print('Hello World', a, b)
|
50
|
-
return a + b + 1 + test_直接调用()
|
51
|
-
|
52
|
-
print('结果:', test_add(1, 3))
|
53
|
-
|
54
|
-
```
|
55
|
-
## 执行hello_world.py
|
56
|
-
```bash
|
57
|
-
$ python hello_world.py
|
58
|
-
输出map:
|
59
|
-
c 3
|
60
|
-
a 1
|
61
|
-
b 2
|
62
|
-
输出list:
|
63
|
-
0 134
|
64
|
-
1 3
|
65
|
-
2 2
|
66
|
-
Hello World 13 3
|
67
|
-
结果: 140
|
68
|
-
```
|
69
|
-
## 查看生成的c++代码文件
|
70
|
-
```bash
|
71
|
-
$ ls -al l0n0lcoutput/
|
72
|
-
total 48
|
73
|
-
drwxr-xr-x 2 root root 4096 Sep 11 02:32 .
|
74
|
-
drwxrwxrwx 11 1000 1000 4096 Sep 11 02:32 ..
|
75
|
-
-rw-r--r-- 1 root root 794 Sep 11 02:32 test_add_@0bfbc28de3631ddd.cpp
|
76
|
-
-rw-r--r-- 1 root root 150 Sep 11 02:32 test_add_@0bfbc28de3631ddd.h
|
77
|
-
-rwxr-xr-x 1 root root 29496 Sep 11 02:32 test_add_@0bfbc28de3631ddd.so
|
78
|
-
```
|
79
|
-
|
80
|
-
## test_add_@0bfbc28de3631ddd.h
|
81
|
-
|
82
|
-
```c++
|
83
|
-
#include <cmath>
|
84
|
-
#include <cstdint>
|
85
|
-
#include <iostream>
|
86
|
-
#include <string>
|
87
|
-
#include <unordered_map>
|
88
|
-
extern "C" int64_t test_add (int64_t a, int64_t b);
|
89
|
-
```
|
90
|
-
|
91
|
-
## test_add_@0bfbc28de3631ddd.cpp
|
92
|
-
```c++
|
93
|
-
#include "test_add_@141921ba1aaa0352.h"
|
94
|
-
extern "C" int64_t test_add (int64_t a, int64_t b)
|
95
|
-
{
|
96
|
-
if ((a > 1))
|
97
|
-
{
|
98
|
-
return a + b;
|
99
|
-
}
|
100
|
-
|
101
|
-
for (int64_t i = 1; i < 10; i += 2)
|
102
|
-
{
|
103
|
-
a = a + i;
|
104
|
-
}
|
105
|
-
|
106
|
-
for (auto i : {1,2,3})
|
107
|
-
{
|
108
|
-
a = a + i;
|
109
|
-
}
|
110
|
-
|
111
|
-
a = std::ceil(12.5);;
|
112
|
-
std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
113
|
-
cc[u8"c"] = 3;
|
114
|
-
std::cout<< u8"输出map:" << " "<< std::endl;
|
115
|
-
for (auto ii : cc)
|
116
|
-
{
|
117
|
-
std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
|
118
|
-
}
|
119
|
-
|
120
|
-
int64_t aa[] = {1,3,2};
|
121
|
-
aa[0] = 134;
|
122
|
-
std::cout<< u8"输出list:" << " "<< std::endl;
|
123
|
-
for (int64_t i = 0; i < 3; ++i)
|
124
|
-
{
|
125
|
-
std::cout<< i << " "<< aa[i] << " "<< std::endl;
|
126
|
-
}
|
127
|
-
|
128
|
-
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
129
|
-
return a + b + 1;
|
130
|
-
}
|
131
|
-
```
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|