l0n0lc 0.8.7__py3-none-any.whl → 0.8.9__py3-none-any.whl
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/jit.py +16 -9
- l0n0lc//351/200/232/347/224/250.py +29 -0
- l0n0lc-0.8.9.dist-info/METADATA +455 -0
- l0n0lc-0.8.9.dist-info/RECORD +12 -0
- l0n0lc-0.8.7.dist-info/METADATA +0 -233
- l0n0lc-0.8.7.dist-info/RECORD +0 -12
- {l0n0lc-0.8.7.dist-info → l0n0lc-0.8.9.dist-info}/WHEEL +0 -0
- {l0n0lc-0.8.7.dist-info → l0n0lc-0.8.9.dist-info}/licenses/LICENSE +0 -0
- {l0n0lc-0.8.7.dist-info → l0n0lc-0.8.9.dist-info}/top_level.txt +0 -0
l0n0lc/jit.py
CHANGED
@@ -6,7 +6,7 @@ from typing import Callable, Any, List, Dict
|
|
6
6
|
from .c基础处理 import *
|
7
7
|
from .StdList import StdList
|
8
8
|
from .StdMap import StdUnorderedMap
|
9
|
-
from .通用 import 通用信息, 有非英文变量字符,
|
9
|
+
from .通用 import 通用信息, 有非英文变量字符, 尝试创建文件夹, c类型映射
|
10
10
|
from .编译 import cpp编译器
|
11
11
|
|
12
12
|
|
@@ -189,7 +189,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
189
189
|
|
190
190
|
if isinstance(value, ast.Attribute):
|
191
191
|
对象 = self.获取值(value.value)
|
192
|
-
if isinstance(对象, c
|
192
|
+
if isinstance(对象, (c变量, c类型映射)):
|
193
193
|
return c获取属性(对象, value.attr)
|
194
194
|
if 对象 is None:
|
195
195
|
self.抛出代码异常(f'没找到{value.value}', value)
|
@@ -307,11 +307,16 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
307
307
|
|
308
308
|
# 如果是创建类型
|
309
309
|
if inspect.isclass(fn):
|
310
|
-
c类型 =
|
311
|
-
if c类型 is None:
|
312
|
-
|
313
|
-
|
314
|
-
|
310
|
+
c类型 = 通用信息.类型映射表.get(fn)
|
311
|
+
if c类型 is not None:
|
312
|
+
for include目录 in c类型.include目录:
|
313
|
+
self.include目录.add(include目录)
|
314
|
+
for 链接库 in c类型.库列表:
|
315
|
+
self.链接库列表.add(链接库)
|
316
|
+
for 库目录 in c类型.库目录:
|
317
|
+
self.库目录.add(库目录)
|
318
|
+
参数文本 = self.构建参数列表文本(node.args)
|
319
|
+
return c函数调用(c类型, 参数文本)
|
315
320
|
|
316
321
|
# 直接调用函数
|
317
322
|
if fn in 通用信息.直接调用函数:
|
@@ -332,7 +337,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
332
337
|
|
333
338
|
return 映射函数.目标函数(*参数列表)
|
334
339
|
|
335
|
-
if not isinstance(fn, Callable):
|
340
|
+
if not isinstance(fn, Callable) and not isinstance(fn, c获取属性):
|
336
341
|
self.抛出代码异常(f'{ast.dump(node.func)} 没找到', node)
|
337
342
|
|
338
343
|
if len(node.keywords) > 0:
|
@@ -343,6 +348,8 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
343
348
|
if isinstance(fn, py2cpp编译器):
|
344
349
|
self.依赖函数.append(fn)
|
345
350
|
return c函数调用(fn.c函数名, 参数文本)
|
351
|
+
elif isinstance(fn, c获取属性):
|
352
|
+
return c函数调用(fn, 参数文本)
|
346
353
|
# 编译该函数
|
347
354
|
else:
|
348
355
|
依赖函数编译器 = self.__class__(fn, self.c编译器)
|
@@ -503,7 +510,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
503
510
|
|
504
511
|
def visit_Call(self, node: ast.Call) -> Any:
|
505
512
|
代码 = self.调用Call(node)
|
506
|
-
self.添加c代码(
|
513
|
+
self.添加c代码(f'{代码};')
|
507
514
|
|
508
515
|
def _赋值(self, target, 值, node, c强转类型: str | None = None):
|
509
516
|
目标 = self.获取值(target)
|
@@ -64,10 +64,29 @@ class c函数映射:
|
|
64
64
|
self.库列表 = 库列表 or []
|
65
65
|
self.库目录 = 库目录 or []
|
66
66
|
|
67
|
+
def __str__(self) -> str:
|
68
|
+
return self.目标函数
|
69
|
+
|
70
|
+
|
71
|
+
class c类型映射:
|
72
|
+
def __init__(
|
73
|
+
self, 目标类型,
|
74
|
+
include目录: Optional[List[str]],
|
75
|
+
库列表: Optional[List[str]],
|
76
|
+
库目录: Optional[List[str]]) -> None:
|
77
|
+
self.目标类型 = 目标类型
|
78
|
+
self.include目录 = include目录 or []
|
79
|
+
self.库列表 = 库列表 or []
|
80
|
+
self.库目录 = 库目录 or []
|
81
|
+
|
82
|
+
def __str__(self) -> str:
|
83
|
+
return self.目标类型
|
84
|
+
|
67
85
|
|
68
86
|
class 通用信息:
|
69
87
|
直接调用函数 = set()
|
70
88
|
函数映射表: Dict[Any, c函数映射] = {}
|
89
|
+
类型映射表: Dict[Any, c类型映射] = {}
|
71
90
|
include表 = set()
|
72
91
|
连接库表 = set()
|
73
92
|
变量最大ID = 0
|
@@ -110,6 +129,16 @@ def 映射函数(
|
|
110
129
|
return 装饰器
|
111
130
|
|
112
131
|
|
132
|
+
def 映射类型(被映射类型,
|
133
|
+
include目录: Optional[List[str]] = None,
|
134
|
+
链接库列表: Optional[List[str]] = None,
|
135
|
+
库目录列表: Optional[List[str]] = None):
|
136
|
+
def 装饰器(映射目标):
|
137
|
+
通用信息.类型映射表[映射目标] = c类型映射(被映射类型, include目录, 链接库列表, 库目录列表)
|
138
|
+
return 映射目标
|
139
|
+
return 装饰器
|
140
|
+
|
141
|
+
|
113
142
|
def 有非英文变量字符(s):
|
114
143
|
return bool(re.search(r'[^A-Za-z0-9_]', s))
|
115
144
|
|
@@ -0,0 +1,455 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: l0n0lc
|
3
|
+
Version: 0.8.9
|
4
|
+
Summary: 一个将python函数翻译为c++函数并运行的jit编译器
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
6
|
+
Requires-Python: >=3.10
|
7
|
+
Description-Content-Type: text/markdown
|
8
|
+
License-File: LICENSE
|
9
|
+
Dynamic: license-file
|
10
|
+
|
11
|
+
|
12
|
+
# 将python函数翻译为c++函数并运行
|
13
|
+
## 1. 安装
|
14
|
+
```
|
15
|
+
pip install l0n0lc
|
16
|
+
```
|
17
|
+
## 2. hello_world.py
|
18
|
+
```python
|
19
|
+
import l0n0lc as lc
|
20
|
+
import math
|
21
|
+
|
22
|
+
|
23
|
+
@lc.映射函数(math.ceil, ['<cmath>'])
|
24
|
+
def cpp_ceil(v):
|
25
|
+
return f'std::ceil({lc.toCString(v)});'
|
26
|
+
|
27
|
+
|
28
|
+
@lc.映射函数(print, ['<iostream>'])
|
29
|
+
def cpp_cout(*args):
|
30
|
+
code = f'std::cout'
|
31
|
+
for arg in args:
|
32
|
+
code += f'<< {lc.toCString(arg)} << " "'
|
33
|
+
code += '<< std::endl;'
|
34
|
+
return code
|
35
|
+
|
36
|
+
|
37
|
+
def py_cin(v):
|
38
|
+
pass
|
39
|
+
|
40
|
+
|
41
|
+
@lc.映射函数(py_cin, ['<iostream>'])
|
42
|
+
def cpp_cin(v):
|
43
|
+
return f'std::cout << u8"请输入>>>"; std::cin >> {v};'
|
44
|
+
|
45
|
+
|
46
|
+
@lc.直接调用函数
|
47
|
+
def test_直接调用():
|
48
|
+
return 123
|
49
|
+
|
50
|
+
|
51
|
+
def test_other_fn(a: int, b: int) -> int:
|
52
|
+
return a - b
|
53
|
+
|
54
|
+
|
55
|
+
@lc.jit()
|
56
|
+
def test编译的函数(a: int, b: int) -> int:
|
57
|
+
return a * b
|
58
|
+
|
59
|
+
|
60
|
+
@lc.映射类型('std::vector<int>', ['<vector>'])
|
61
|
+
class CppVectorInt:
|
62
|
+
def push_back(self, v):
|
63
|
+
pass
|
64
|
+
|
65
|
+
def size(self):
|
66
|
+
return 0
|
67
|
+
|
68
|
+
def __getitem__(self, key):
|
69
|
+
return 0
|
70
|
+
|
71
|
+
|
72
|
+
@lc.jit()
|
73
|
+
def jit_all_ops(a: int, b: int) -> int:
|
74
|
+
# 常量与基础赋值
|
75
|
+
x = 42
|
76
|
+
y: int = a + b
|
77
|
+
z = 3.14
|
78
|
+
flag = True
|
79
|
+
nums = [1, 2, 3]
|
80
|
+
tup = (4, 5)
|
81
|
+
mp = {1: 10, 2: 20}
|
82
|
+
|
83
|
+
# 一元运算
|
84
|
+
pos = +a
|
85
|
+
neg = -b
|
86
|
+
inv = ~a
|
87
|
+
not_flag = not flag
|
88
|
+
|
89
|
+
# 二元运算
|
90
|
+
add = a + b
|
91
|
+
sub = a - b
|
92
|
+
mul = a * b
|
93
|
+
div = a / (b if b != 0 else 1)
|
94
|
+
mod = a % (b if b != 0 else 1)
|
95
|
+
band = a & b
|
96
|
+
bor = a | b
|
97
|
+
bxor = a ^ b
|
98
|
+
lshift = a << 1
|
99
|
+
rshift = a >> 1
|
100
|
+
|
101
|
+
# 比较运算
|
102
|
+
cmp1 = a == b
|
103
|
+
cmp2 = a != b
|
104
|
+
cmp3 = a < b
|
105
|
+
cmp4 = a <= b
|
106
|
+
cmp5 = a > b
|
107
|
+
cmp6 = a >= b
|
108
|
+
|
109
|
+
# 逻辑运算与三元表达式
|
110
|
+
logic_and = cmp1 and cmp2
|
111
|
+
logic_or = cmp3 or cmp4
|
112
|
+
ternary = a if a > b else b
|
113
|
+
|
114
|
+
# if / else
|
115
|
+
if a > b:
|
116
|
+
y += 1
|
117
|
+
else:
|
118
|
+
y -= 1
|
119
|
+
|
120
|
+
# for 循环 range
|
121
|
+
for i in range(3):
|
122
|
+
y += i
|
123
|
+
|
124
|
+
# for 循环 列表
|
125
|
+
for v in nums:
|
126
|
+
y += v
|
127
|
+
if v == 2:
|
128
|
+
continue
|
129
|
+
if v == 3:
|
130
|
+
break
|
131
|
+
|
132
|
+
# while 循环
|
133
|
+
count = 0
|
134
|
+
while count < 2:
|
135
|
+
y += count
|
136
|
+
count += 1
|
137
|
+
|
138
|
+
# 增强赋值
|
139
|
+
y += 5
|
140
|
+
y -= 1
|
141
|
+
y *= 2
|
142
|
+
y //= 2
|
143
|
+
y %= 10
|
144
|
+
y &= 7
|
145
|
+
y |= 3
|
146
|
+
y ^= 1
|
147
|
+
y <<= 1
|
148
|
+
y >>= 1
|
149
|
+
|
150
|
+
# 下标访问
|
151
|
+
first_num = nums[0]
|
152
|
+
mp_val = mp[1]
|
153
|
+
y += first_num + mp_val
|
154
|
+
|
155
|
+
vector = CppVectorInt()
|
156
|
+
vector.push_back(count)
|
157
|
+
vector.push_back(y)
|
158
|
+
for i in range(vector.size()):
|
159
|
+
print('vector->', i, '=', vector[i])
|
160
|
+
return y
|
161
|
+
|
162
|
+
@lc.jit(每次运行都重新编译=True)
|
163
|
+
def test_add(a: int, b: int) -> int:
|
164
|
+
if a > 1:
|
165
|
+
return a + b
|
166
|
+
for i in range(1, 10, 2):
|
167
|
+
a += i
|
168
|
+
for i in [1, 2, 3]:
|
169
|
+
a += i
|
170
|
+
a = math.ceil(12.5)
|
171
|
+
cc = {'a': 1, 'b': 2}
|
172
|
+
cc['c'] = 3
|
173
|
+
print('输出map:')
|
174
|
+
for ii in cc:
|
175
|
+
print(ii.first, ii.second) # type: ignore
|
176
|
+
aa = [1, 3, 2]
|
177
|
+
aa[0] = 134
|
178
|
+
print('输出list:')
|
179
|
+
for i in range(3):
|
180
|
+
print(i, aa[i])
|
181
|
+
print('Hello World', a, b)
|
182
|
+
print('test_other_fn', test_other_fn(a, b))
|
183
|
+
print('test编译的函数', test编译的函数(a, b))
|
184
|
+
|
185
|
+
print('测试所有操作:')
|
186
|
+
jit_all_ops(a, b)
|
187
|
+
|
188
|
+
v = 0
|
189
|
+
vv = True and (False or 1)
|
190
|
+
print('vv:', vv)
|
191
|
+
print('测试while:')
|
192
|
+
while (vv):
|
193
|
+
py_cin(v)
|
194
|
+
if v > 100:
|
195
|
+
break
|
196
|
+
else:
|
197
|
+
print('输入的', v, '小于等于100')
|
198
|
+
return a + b + 1 + test_直接调用() + v
|
199
|
+
|
200
|
+
|
201
|
+
print('结果:', test_add(1, 3))
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
## 3. 运行hello_world.py
|
206
|
+
```
|
207
|
+
uv run tests/hello_world.py
|
208
|
+
# 输入: b'1\n2\n100\n101\n'
|
209
|
+
```
|
210
|
+
```bash
|
211
|
+
输出map:
|
212
|
+
c 3
|
213
|
+
a 1
|
214
|
+
b 2
|
215
|
+
输出list:
|
216
|
+
0 134
|
217
|
+
1 3
|
218
|
+
2 2
|
219
|
+
Hello World 13 3
|
220
|
+
test_other_fn 10
|
221
|
+
test编译的函数 39
|
222
|
+
测试所有操作:
|
223
|
+
vector-> 0 = 2
|
224
|
+
vector-> 1 = 13
|
225
|
+
vv: 1
|
226
|
+
测试while:
|
227
|
+
请输入>>>输入的 1 小于等于100
|
228
|
+
请输入>>>输入的 2 小于等于100
|
229
|
+
请输入>>>输入的 100 小于等于100
|
230
|
+
请输入>>>结果: 241
|
231
|
+
|
232
|
+
```
|
233
|
+
|
234
|
+
## 4. 查看输出文件
|
235
|
+
```bash
|
236
|
+
ls -al ./l0n0lcoutput
|
237
|
+
|
238
|
+
```
|
239
|
+
## 5. jit_all_ops_@0ec883892076cd03.cpp
|
240
|
+
```c++
|
241
|
+
#include "jit_all_ops_@0ec883892076cd03.h"
|
242
|
+
extern "C" int16_t jit_all_ops (int16_t a, int16_t b)
|
243
|
+
{
|
244
|
+
auto x = 42;
|
245
|
+
auto y = ((int16_t)(a + b));
|
246
|
+
auto z = 3.14;
|
247
|
+
auto flag = true;
|
248
|
+
int16_t nums[] = {1,2,3};
|
249
|
+
int16_t tup[] = {4,5};
|
250
|
+
std::unordered_map<int16_t, int16_t> mp = {{ 1, 10 },{ 2, 20 }};
|
251
|
+
auto pos = +a;
|
252
|
+
auto neg = -b;
|
253
|
+
auto inv = ~a;
|
254
|
+
auto not_flag = !flag;
|
255
|
+
auto add = a + b;
|
256
|
+
auto sub = a - b;
|
257
|
+
auto mul = a * b;
|
258
|
+
auto div = a / ((b != 0) ? (b) : (1));
|
259
|
+
auto mod = a % ((b != 0) ? (b) : (1));
|
260
|
+
auto band = a & b;
|
261
|
+
auto bor = a | b;
|
262
|
+
auto bxor = a ^ b;
|
263
|
+
auto lshift = a << 1;
|
264
|
+
auto rshift = a >> 1;
|
265
|
+
auto cmp1 = (a == b);
|
266
|
+
auto cmp2 = (a != b);
|
267
|
+
auto cmp3 = (a < b);
|
268
|
+
auto cmp4 = (a <= b);
|
269
|
+
auto cmp5 = (a > b);
|
270
|
+
auto cmp6 = (a >= b);
|
271
|
+
auto logic_and = cmp1&&cmp2;
|
272
|
+
auto logic_or = cmp3||cmp4;
|
273
|
+
auto ternary = ((a > b) ? (a) : (b));
|
274
|
+
if ((a > b))
|
275
|
+
{
|
276
|
+
y = y + 1;
|
277
|
+
}
|
278
|
+
|
279
|
+
else
|
280
|
+
{
|
281
|
+
y = y - 1;
|
282
|
+
}
|
283
|
+
|
284
|
+
for (int64_t i = 0; i < 3; ++i)
|
285
|
+
{
|
286
|
+
y = y + i;
|
287
|
+
}
|
288
|
+
|
289
|
+
for (auto v : nums)
|
290
|
+
{
|
291
|
+
y = y + v;
|
292
|
+
if ((v == 2))
|
293
|
+
{
|
294
|
+
continue;
|
295
|
+
}
|
296
|
+
|
297
|
+
if ((v == 3))
|
298
|
+
{
|
299
|
+
break;
|
300
|
+
}
|
301
|
+
|
302
|
+
}
|
303
|
+
|
304
|
+
auto count = 0;
|
305
|
+
while ((count < 2))
|
306
|
+
{
|
307
|
+
y = y + count;
|
308
|
+
count = count + 1;
|
309
|
+
}
|
310
|
+
|
311
|
+
y = y + 5;
|
312
|
+
y = y - 1;
|
313
|
+
y = y * 2;
|
314
|
+
y = y / 2;
|
315
|
+
y = y % 10;
|
316
|
+
y = y & 7;
|
317
|
+
y = y | 3;
|
318
|
+
y = y ^ 1;
|
319
|
+
y = y << 1;
|
320
|
+
y = y >> 1;
|
321
|
+
auto first_num = nums[0];
|
322
|
+
auto mp_val = mp[1];
|
323
|
+
y = y + first_num + mp_val;
|
324
|
+
auto vector = std::vector<int>();
|
325
|
+
vector.push_back(count);
|
326
|
+
vector.push_back(y);
|
327
|
+
for (int64_t i = 0; i < vector.size(); ++i)
|
328
|
+
{
|
329
|
+
std::cout<< u8"vector->" << " "<< i << " "<< u8"=" << " "<< vector[i] << " "<< std::endl;;
|
330
|
+
}
|
331
|
+
|
332
|
+
return y;
|
333
|
+
}
|
334
|
+
|
335
|
+
```
|
336
|
+
## 6. jit_all_ops_@0ec883892076cd03.h
|
337
|
+
```c++
|
338
|
+
#pragma once
|
339
|
+
#include <cstdint>
|
340
|
+
#include <iostream>
|
341
|
+
#include <string>
|
342
|
+
#include <unordered_map>
|
343
|
+
#include <vector>
|
344
|
+
extern "C" int16_t jit_all_ops (int16_t a, int16_t b);
|
345
|
+
```
|
346
|
+
## 7. test_add_@122c4be59097d472.cpp
|
347
|
+
```c++
|
348
|
+
#include "test_add_@122c4be59097d472.h"
|
349
|
+
extern "C" int16_t test_add (int16_t a, int16_t b)
|
350
|
+
{
|
351
|
+
if ((a > 1))
|
352
|
+
{
|
353
|
+
return a + b;
|
354
|
+
}
|
355
|
+
|
356
|
+
for (int64_t i = 1; i < 10; i += 2)
|
357
|
+
{
|
358
|
+
a = a + i;
|
359
|
+
}
|
360
|
+
|
361
|
+
for (auto i : {1,2,3})
|
362
|
+
{
|
363
|
+
a = a + i;
|
364
|
+
}
|
365
|
+
|
366
|
+
a = std::ceil(12.5);;
|
367
|
+
std::unordered_map<std::string, int16_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
368
|
+
cc[u8"c"] = 3;
|
369
|
+
std::cout<< u8"输出map:" << " "<< std::endl;;
|
370
|
+
for (auto ii : cc)
|
371
|
+
{
|
372
|
+
std::cout<< ii.first << " "<< ii.second << " "<< std::endl;;
|
373
|
+
}
|
374
|
+
|
375
|
+
int16_t aa[] = {1,3,2};
|
376
|
+
aa[0] = 134;
|
377
|
+
std::cout<< u8"输出list:" << " "<< std::endl;;
|
378
|
+
for (int64_t i = 0; i < 3; ++i)
|
379
|
+
{
|
380
|
+
std::cout<< i << " "<< aa[i] << " "<< std::endl;;
|
381
|
+
}
|
382
|
+
|
383
|
+
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;;
|
384
|
+
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;;
|
385
|
+
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;;
|
386
|
+
std::cout<< u8"测试所有操作:" << " "<< std::endl;;
|
387
|
+
jit_all_ops(a,b);
|
388
|
+
auto v = 0;
|
389
|
+
auto vv = true&&false||1;
|
390
|
+
std::cout<< u8"vv:" << " "<< vv << " "<< std::endl;;
|
391
|
+
std::cout<< u8"测试while:" << " "<< std::endl;;
|
392
|
+
while (vv)
|
393
|
+
{
|
394
|
+
std::cout << u8"请输入>>>"; std::cin >> v;;
|
395
|
+
if ((v > 100))
|
396
|
+
{
|
397
|
+
break;
|
398
|
+
}
|
399
|
+
|
400
|
+
else
|
401
|
+
{
|
402
|
+
std::cout<< u8"输入的" << " "<< v << " "<< u8"小于等于100" << " "<< std::endl;;
|
403
|
+
}
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
return a + b + 1 + 123 + v;
|
408
|
+
}
|
409
|
+
|
410
|
+
```
|
411
|
+
## 8. test_add_@122c4be59097d472.h
|
412
|
+
```c++
|
413
|
+
#pragma once
|
414
|
+
#include "jit_all_ops_@0ec883892076cd03.h"
|
415
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
416
|
+
#include "test编译的函数_@3bf4501e0408a243.h"
|
417
|
+
#include <cmath>
|
418
|
+
#include <cstdint>
|
419
|
+
#include <iostream>
|
420
|
+
#include <string>
|
421
|
+
#include <unordered_map>
|
422
|
+
extern "C" int16_t test_add (int16_t a, int16_t b);
|
423
|
+
```
|
424
|
+
## 9. test_other_fn_@75fdd928ab58a8e3.cpp
|
425
|
+
```c++
|
426
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
427
|
+
extern "C" int16_t test_other_fn (int16_t a, int16_t b)
|
428
|
+
{
|
429
|
+
return a - b;
|
430
|
+
}
|
431
|
+
|
432
|
+
```
|
433
|
+
## 10. test_other_fn_@75fdd928ab58a8e3.h
|
434
|
+
```c++
|
435
|
+
#pragma once
|
436
|
+
#include <cstdint>
|
437
|
+
#include <string>
|
438
|
+
extern "C" int16_t test_other_fn (int16_t a, int16_t b);
|
439
|
+
```
|
440
|
+
## 11. test编译的函数_@3bf4501e0408a243.cpp
|
441
|
+
```c++
|
442
|
+
#include "test编译的函数_@3bf4501e0408a243.h"
|
443
|
+
extern "C" int16_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int16_t a, int16_t b)
|
444
|
+
{
|
445
|
+
return a * b;
|
446
|
+
}
|
447
|
+
|
448
|
+
```
|
449
|
+
## 12. test编译的函数_@3bf4501e0408a243.h
|
450
|
+
```c++
|
451
|
+
#pragma once
|
452
|
+
#include <cstdint>
|
453
|
+
#include <string>
|
454
|
+
extern "C" int16_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int16_t a, int16_t b);
|
455
|
+
```
|
@@ -0,0 +1,12 @@
|
|
1
|
+
l0n0lc/StdList.py,sha256=0NTIpaRrNHaCiLrRbEVob21pZHa8S8rfaBRALPT-tD0,889
|
2
|
+
l0n0lc/StdMap.py,sha256=TIVKqhT078cjLIM0Zlq-TwGFSy-KwOyJe00BLFPVxr8,735
|
3
|
+
l0n0lc/__init__.py,sha256=fUC6TMeN_fEL7HDazrBEdoWOQkXi5uSGZb7LZj6juog,137
|
4
|
+
l0n0lc/c基础处理.py,sha256=ata-iQTp64kM0Ea_ktfQhaGPrejRzls3YZ7GcnjRlqE,5723
|
5
|
+
l0n0lc/jit.py,sha256=UG5CwhmtD9BJWWiWreFxDCTCFgKgjSOqREhaZuW3sBU,25619
|
6
|
+
l0n0lc/编译.py,sha256=Ty4gPDHAB1jn8LgnE4w3R06bOAEeYr8iwqahYf6QeHI,2154
|
7
|
+
l0n0lc/通用.py,sha256=RkqP9cMhKhl2h4YxOpAWFFftIPXLZTYMyOOD1PuB59M,4630
|
8
|
+
l0n0lc-0.8.9.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
|
9
|
+
l0n0lc-0.8.9.dist-info/METADATA,sha256=-R8zwl0NhatjTkk-hIdyeNHOk6MuUEUwIe_0Eq4pdAI,8926
|
10
|
+
l0n0lc-0.8.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
11
|
+
l0n0lc-0.8.9.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
|
12
|
+
l0n0lc-0.8.9.dist-info/RECORD,,
|
l0n0lc-0.8.7.dist-info/METADATA
DELETED
@@ -1,233 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: l0n0lc
|
3
|
-
Version: 0.8.7
|
4
|
-
Summary: 一个将python函数翻译为c++函数并运行的jit编译器
|
5
|
-
Classifier: Programming Language :: Python :: 3
|
6
|
-
Requires-Python: >=3.10
|
7
|
-
Description-Content-Type: text/markdown
|
8
|
-
License-File: LICENSE
|
9
|
-
Dynamic: license-file
|
10
|
-
|
11
|
-
|
12
|
-
# 将python函数翻译为c++函数并运行
|
13
|
-
## 1. 安装
|
14
|
-
```
|
15
|
-
pip install l0n0lc
|
16
|
-
```
|
17
|
-
## 2. hello_world.py
|
18
|
-
```python
|
19
|
-
import l0n0lc as lc
|
20
|
-
import math
|
21
|
-
|
22
|
-
|
23
|
-
@lc.映射函数(math.ceil, ['<cmath>'])
|
24
|
-
def cpp_ceil(v):
|
25
|
-
return f'std::ceil({lc.toCString(v)});'
|
26
|
-
|
27
|
-
|
28
|
-
@lc.映射函数(print, ['<iostream>'])
|
29
|
-
def cpp_cout(*args):
|
30
|
-
code = f'std::cout'
|
31
|
-
for arg in args:
|
32
|
-
code += f'<< {lc.toCString(arg)} << " "'
|
33
|
-
code += '<< std::endl;'
|
34
|
-
return code
|
35
|
-
|
36
|
-
|
37
|
-
def py_cin(v):
|
38
|
-
pass
|
39
|
-
|
40
|
-
|
41
|
-
@lc.映射函数(py_cin, ['<iostream>'])
|
42
|
-
def cpp_cin(v):
|
43
|
-
return f'std::cout << u8"请输入>>>"; std::cin >> {v};'
|
44
|
-
|
45
|
-
|
46
|
-
@lc.直接调用函数
|
47
|
-
def test_直接调用():
|
48
|
-
return 123
|
49
|
-
|
50
|
-
|
51
|
-
def test_other_fn(a: int, b: int) -> int:
|
52
|
-
return a - b
|
53
|
-
|
54
|
-
|
55
|
-
@lc.jit()
|
56
|
-
def test编译的函数(a: int, b: int) -> int:
|
57
|
-
return a * b
|
58
|
-
|
59
|
-
|
60
|
-
@lc.jit(每次运行都重新编译=True)
|
61
|
-
def test_add(a: int, b: int) -> int:
|
62
|
-
if a > 1:
|
63
|
-
return a + b
|
64
|
-
for i in range(1, 10, 2):
|
65
|
-
a += i
|
66
|
-
for i in [1, 2, 3]:
|
67
|
-
a += i
|
68
|
-
a = math.ceil(12.5)
|
69
|
-
cc = {'a': 1, 'b': 2}
|
70
|
-
cc['c'] = 3
|
71
|
-
print('输出map:')
|
72
|
-
for ii in cc:
|
73
|
-
print(ii.first, ii.second) # type: ignore
|
74
|
-
aa = [1, 3, 2]
|
75
|
-
aa[0] = 134
|
76
|
-
print('输出list:')
|
77
|
-
for i in range(3):
|
78
|
-
print(i, aa[i])
|
79
|
-
print('Hello World', a, b)
|
80
|
-
print('test_other_fn', test_other_fn(a, b))
|
81
|
-
print('test编译的函数', test编译的函数(a, b))
|
82
|
-
v = 0
|
83
|
-
vv = True and (False or 1)
|
84
|
-
print('vv:', vv)
|
85
|
-
while (vv):
|
86
|
-
py_cin(v)
|
87
|
-
if v > 100:
|
88
|
-
break
|
89
|
-
else:
|
90
|
-
print('输入的', v, '小于等于100')
|
91
|
-
return a + b + 1 + test_直接调用() + v
|
92
|
-
|
93
|
-
|
94
|
-
print('结果:', test_add(1, 3))
|
95
|
-
|
96
|
-
```
|
97
|
-
|
98
|
-
## 3. 运行hello_world.py
|
99
|
-
```
|
100
|
-
uv run tests/hello_world.py
|
101
|
-
# 输入: b'1\n2\n100\n101\n'
|
102
|
-
```
|
103
|
-
```bash
|
104
|
-
输出map:
|
105
|
-
c 3
|
106
|
-
a 1
|
107
|
-
b 2
|
108
|
-
输出list:
|
109
|
-
0 134
|
110
|
-
1 3
|
111
|
-
2 2
|
112
|
-
Hello World 13 3
|
113
|
-
test_other_fn 10
|
114
|
-
test编译的函数 39
|
115
|
-
vv: 1
|
116
|
-
请输入>>>输入的 1 小于等于100
|
117
|
-
请输入>>>输入的 2 小于等于100
|
118
|
-
请输入>>>输入的 100 小于等于100
|
119
|
-
请输入>>>结果: 241
|
120
|
-
|
121
|
-
```
|
122
|
-
|
123
|
-
## 4. 查看输出文件
|
124
|
-
```bash
|
125
|
-
ls -al ./l0n0lcoutput
|
126
|
-
|
127
|
-
```
|
128
|
-
## 5. test_add_@ac596ca343e844de.cpp
|
129
|
-
```c++
|
130
|
-
#include "test_add_@ac596ca343e844de.h"
|
131
|
-
extern "C" int16_t test_add (int16_t a, int16_t b)
|
132
|
-
{
|
133
|
-
if ((a > 1))
|
134
|
-
{
|
135
|
-
return a + b;
|
136
|
-
}
|
137
|
-
|
138
|
-
for (int64_t i = 1; i < 10; i += 2)
|
139
|
-
{
|
140
|
-
a = a + i;
|
141
|
-
}
|
142
|
-
|
143
|
-
for (auto i : {1,2,3})
|
144
|
-
{
|
145
|
-
a = a + i;
|
146
|
-
}
|
147
|
-
|
148
|
-
a = std::ceil(12.5);;
|
149
|
-
std::unordered_map<std::string, int16_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
150
|
-
cc[u8"c"] = 3;
|
151
|
-
std::cout<< u8"输出map:" << " "<< std::endl;
|
152
|
-
for (auto ii : cc)
|
153
|
-
{
|
154
|
-
std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
|
155
|
-
}
|
156
|
-
|
157
|
-
int16_t aa[] = {1,3,2};
|
158
|
-
aa[0] = 134;
|
159
|
-
std::cout<< u8"输出list:" << " "<< std::endl;
|
160
|
-
for (int64_t i = 0; i < 3; ++i)
|
161
|
-
{
|
162
|
-
std::cout<< i << " "<< aa[i] << " "<< std::endl;
|
163
|
-
}
|
164
|
-
|
165
|
-
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
166
|
-
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
|
167
|
-
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
|
168
|
-
auto v = 0;
|
169
|
-
auto vv = true&&false||1;
|
170
|
-
std::cout<< u8"vv:" << " "<< vv << " "<< std::endl;
|
171
|
-
while (vv)
|
172
|
-
{
|
173
|
-
std::cout << u8"请输入>>>"; std::cin >> v;
|
174
|
-
if ((v > 100))
|
175
|
-
{
|
176
|
-
break;
|
177
|
-
}
|
178
|
-
|
179
|
-
else
|
180
|
-
{
|
181
|
-
std::cout<< u8"输入的" << " "<< v << " "<< u8"小于等于100" << " "<< std::endl;
|
182
|
-
}
|
183
|
-
|
184
|
-
}
|
185
|
-
|
186
|
-
return a + b + 1 + 123 + v;
|
187
|
-
}
|
188
|
-
|
189
|
-
```
|
190
|
-
## 6. test_add_@ac596ca343e844de.h
|
191
|
-
```c++
|
192
|
-
#pragma once
|
193
|
-
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
194
|
-
#include "test编译的函数_@3bf4501e0408a243.h"
|
195
|
-
#include <cmath>
|
196
|
-
#include <cstdint>
|
197
|
-
#include <iostream>
|
198
|
-
#include <string>
|
199
|
-
#include <unordered_map>
|
200
|
-
extern "C" int16_t test_add (int16_t a, int16_t b);
|
201
|
-
```
|
202
|
-
## 7. test_other_fn_@75fdd928ab58a8e3.cpp
|
203
|
-
```c++
|
204
|
-
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
205
|
-
extern "C" int16_t test_other_fn (int16_t a, int16_t b)
|
206
|
-
{
|
207
|
-
return a - b;
|
208
|
-
}
|
209
|
-
|
210
|
-
```
|
211
|
-
## 8. test_other_fn_@75fdd928ab58a8e3.h
|
212
|
-
```c++
|
213
|
-
#pragma once
|
214
|
-
#include <cstdint>
|
215
|
-
#include <string>
|
216
|
-
extern "C" int16_t test_other_fn (int16_t a, int16_t b);
|
217
|
-
```
|
218
|
-
## 9. test编译的函数_@3bf4501e0408a243.cpp
|
219
|
-
```c++
|
220
|
-
#include "test编译的函数_@3bf4501e0408a243.h"
|
221
|
-
extern "C" int16_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int16_t a, int16_t b)
|
222
|
-
{
|
223
|
-
return a * b;
|
224
|
-
}
|
225
|
-
|
226
|
-
```
|
227
|
-
## 10. test编译的函数_@3bf4501e0408a243.h
|
228
|
-
```c++
|
229
|
-
#pragma once
|
230
|
-
#include <cstdint>
|
231
|
-
#include <string>
|
232
|
-
extern "C" int16_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int16_t a, int16_t b);
|
233
|
-
```
|
l0n0lc-0.8.7.dist-info/RECORD
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
l0n0lc/StdList.py,sha256=0NTIpaRrNHaCiLrRbEVob21pZHa8S8rfaBRALPT-tD0,889
|
2
|
-
l0n0lc/StdMap.py,sha256=TIVKqhT078cjLIM0Zlq-TwGFSy-KwOyJe00BLFPVxr8,735
|
3
|
-
l0n0lc/__init__.py,sha256=fUC6TMeN_fEL7HDazrBEdoWOQkXi5uSGZb7LZj6juog,137
|
4
|
-
l0n0lc/c基础处理.py,sha256=ata-iQTp64kM0Ea_ktfQhaGPrejRzls3YZ7GcnjRlqE,5723
|
5
|
-
l0n0lc/jit.py,sha256=VP-CnfyU-bXNHzd6Wxb8UiHVrjMFUH2gQ2iDT0QmZI0,25178
|
6
|
-
l0n0lc/编译.py,sha256=Ty4gPDHAB1jn8LgnE4w3R06bOAEeYr8iwqahYf6QeHI,2154
|
7
|
-
l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
|
8
|
-
l0n0lc-0.8.7.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
|
9
|
-
l0n0lc-0.8.7.dist-info/METADATA,sha256=Uh8SYkhSUZ4cG5FA-NUZPMj_iQlR3FYeWScAFy9Xt2s,4899
|
10
|
-
l0n0lc-0.8.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
11
|
-
l0n0lc-0.8.7.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
|
12
|
-
l0n0lc-0.8.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|