l0n0lc 0.8.4__py3-none-any.whl → 0.8.6__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/StdList.py +1 -1
- l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py +7 -2
- l0n0lc/jit.py +20 -7
- l0n0lc-0.8.6.dist-info/METADATA +587 -0
- l0n0lc-0.8.6.dist-info/RECORD +12 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-0.8.6.dist-info}/licenses/LICENSE +0 -0
- l0n0lc-0.8.4.dist-info/METADATA +0 -241
- l0n0lc-0.8.4.dist-info/RECORD +0 -12
- {l0n0lc-0.8.4.dist-info → l0n0lc-0.8.6.dist-info}/WHEEL +0 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-0.8.6.dist-info}/top_level.txt +0 -0
l0n0lc/StdList.py
CHANGED
@@ -18,7 +18,7 @@ class StdList(c变量):
|
|
18
18
|
def __setitem__(self, key, value):
|
19
19
|
return f'{self}[{key}] = {value};'
|
20
20
|
|
21
|
-
def 初始化(self,
|
21
|
+
def 初始化(self, 初始值, 强转类型: str | None):
|
22
22
|
if self.初始化列表.类型 == cpp类型.ANY:
|
23
23
|
return super().初始化(初始值)
|
24
24
|
return f'{self.类型} {self.名字}[] = {初始值};'
|
@@ -60,6 +60,7 @@ def py类型转ctypes类型(类型):
|
|
60
60
|
if 类型 is 指针:
|
61
61
|
return ctypes.c_void_p
|
62
62
|
|
63
|
+
|
63
64
|
额外py转c函数 = []
|
64
65
|
|
65
66
|
|
@@ -105,6 +106,7 @@ def py类型转c类型(类型):
|
|
105
106
|
|
106
107
|
return cpp类型.ANY
|
107
108
|
|
109
|
+
|
108
110
|
def cpp类型检查(类型, 支持提示: str):
|
109
111
|
if 类型 not in [int, float, str, bool]:
|
110
112
|
raise Exception(f'{支持提示} 仅支持 [int, float, str, bool]')
|
@@ -203,5 +205,8 @@ class c变量:
|
|
203
205
|
def c类型(self):
|
204
206
|
return cpp获取变量类型(self)
|
205
207
|
|
206
|
-
def 初始化(self,
|
207
|
-
|
208
|
+
def 初始化(self, 初始值, 强转类型: str | None = None):
|
209
|
+
if 强转类型:
|
210
|
+
return f'{self.类型} {self.c名字} = (({强转类型})({初始值}));'
|
211
|
+
else:
|
212
|
+
return f'{self.类型} {self.c名字} = {初始值};'
|
l0n0lc/jit.py
CHANGED
@@ -420,6 +420,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
420
420
|
else:
|
421
421
|
self.添加c代码(';')
|
422
422
|
if len(node.orelse) > 0:
|
423
|
+
self.添加c代码('else')
|
423
424
|
with self.花括号:
|
424
425
|
for stmt in node.orelse:
|
425
426
|
self.visit(stmt)
|
@@ -444,11 +445,11 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
444
445
|
if fn is range:
|
445
446
|
参数 = [self.获取值(arg) for arg in node.iter.args]
|
446
447
|
if len(参数) == 1:
|
447
|
-
代码 = f'for (int64_t {目标} = 0;
|
448
|
+
代码 = f'for (int64_t {目标} = 0; {目标} < {参数[0]}; ++{目标})'
|
448
449
|
elif len(参数) == 2:
|
449
|
-
代码 = f'for (int64_t {目标} = {参数[0]};
|
450
|
+
代码 = f'for (int64_t {目标} = {参数[0]}; {目标} < {参数[1]}; ++{目标})'
|
450
451
|
elif len(参数) == 3:
|
451
|
-
代码 = f'for (int64_t {目标} = {参数[0]};
|
452
|
+
代码 = f'for (int64_t {目标} = {参数[0]}; {目标} < {参数[1]}; {目标} += {参数[2]})'
|
452
453
|
else:
|
453
454
|
调用代码 = self.调用Call(node.iter)
|
454
455
|
代码 = f'for (auto {目标} : {调用代码})'
|
@@ -491,7 +492,7 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
491
492
|
代码 = self.调用Call(node)
|
492
493
|
self.添加c代码(str(代码))
|
493
494
|
|
494
|
-
def _赋值(self, target, 值, node):
|
495
|
+
def _赋值(self, target, 值, node, c强转类型: str | None = None):
|
495
496
|
目标 = self.获取值(target)
|
496
497
|
if self.正在调用直接函数:
|
497
498
|
if isinstance(target, ast.Name):
|
@@ -512,12 +513,15 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
512
513
|
self.include目录.add('<any>')
|
513
514
|
else:
|
514
515
|
目标 = c变量('auto', target.id, False)
|
515
|
-
self.添加c代码(目标.初始化(
|
516
|
+
self.添加c代码(目标.初始化(值, c强转类型))
|
516
517
|
self.添加c变量(目标)
|
517
518
|
else:
|
518
519
|
self.抛出代码异常('for中target必须是ast.Name', node)
|
519
520
|
else:
|
520
|
-
|
521
|
+
if c强转类型:
|
522
|
+
self.添加c代码(f'{目标} = {c强转类型}({值});')
|
523
|
+
else:
|
524
|
+
self.添加c代码(f'{目标} = {值};')
|
521
525
|
|
522
526
|
def visit_Assign(self, node: ast.Assign) -> Any:
|
523
527
|
值 = self.获取值(node.value)
|
@@ -528,6 +532,16 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
528
532
|
值 = self.计算二元运算(node)
|
529
533
|
self._赋值(node.target, 值, node)
|
530
534
|
|
535
|
+
def visit_AnnAssign(self, node: ast.AnnAssign) -> Any:
|
536
|
+
值 = self.获取值(node.value)
|
537
|
+
目标类型 = self.获取值(node.annotation)
|
538
|
+
if 目标类型 is None:
|
539
|
+
self.抛出代码异常(f'不支持的类型嗯{node.annotation}', node)
|
540
|
+
c类型 = py类型转c类型(目标类型)
|
541
|
+
if c类型 is None:
|
542
|
+
self.抛出代码异常(f'不支持的类型嗯{目标类型}', node)
|
543
|
+
self._赋值(node.target, 值, node, str(c类型))
|
544
|
+
|
531
545
|
def 获取定义(self):
|
532
546
|
参数定义 = []
|
533
547
|
for arg in self.参数变量.values():
|
@@ -592,7 +606,6 @@ class py2cpp编译器(ast.NodeVisitor):
|
|
592
606
|
|
593
607
|
def jit(jit编译器类=None, cpp编译器类=None, 每次运行都重新编译: bool = False):
|
594
608
|
def 编译函数(fn: Callable):
|
595
|
-
# print(ast.dump(语法树, indent=' '))
|
596
609
|
_c编译器类 = cpp编译器类 or cpp编译器
|
597
610
|
_jit编译器类 = jit编译器类 or py2cpp编译器
|
598
611
|
c语言函数编译器 = _jit编译器类(fn, _c编译器类())
|
@@ -0,0 +1,587 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: l0n0lc
|
3
|
+
Version: 0.8.6
|
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 (True):
|
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
|
+
Module(
|
105
|
+
body=[
|
106
|
+
FunctionDef(
|
107
|
+
name='test编译的函数',
|
108
|
+
args=arguments(
|
109
|
+
posonlyargs=[],
|
110
|
+
args=[
|
111
|
+
arg(
|
112
|
+
arg='a',
|
113
|
+
annotation=Name(id='int', ctx=Load())),
|
114
|
+
arg(
|
115
|
+
arg='b',
|
116
|
+
annotation=Name(id='int', ctx=Load()))],
|
117
|
+
kwonlyargs=[],
|
118
|
+
kw_defaults=[],
|
119
|
+
defaults=[]),
|
120
|
+
body=[
|
121
|
+
Return(
|
122
|
+
value=BinOp(
|
123
|
+
left=Name(id='a', ctx=Load()),
|
124
|
+
op=Mult(),
|
125
|
+
right=Name(id='b', ctx=Load())))],
|
126
|
+
decorator_list=[
|
127
|
+
Call(
|
128
|
+
func=Attribute(
|
129
|
+
value=Name(id='lc', ctx=Load()),
|
130
|
+
attr='jit',
|
131
|
+
ctx=Load()),
|
132
|
+
args=[],
|
133
|
+
keywords=[])],
|
134
|
+
returns=Name(id='int', ctx=Load()))],
|
135
|
+
type_ignores=[])
|
136
|
+
Module(
|
137
|
+
body=[
|
138
|
+
FunctionDef(
|
139
|
+
name='test_add',
|
140
|
+
args=arguments(
|
141
|
+
posonlyargs=[],
|
142
|
+
args=[
|
143
|
+
arg(
|
144
|
+
arg='a',
|
145
|
+
annotation=Name(id='int', ctx=Load())),
|
146
|
+
arg(
|
147
|
+
arg='b',
|
148
|
+
annotation=Name(id='int', ctx=Load()))],
|
149
|
+
kwonlyargs=[],
|
150
|
+
kw_defaults=[],
|
151
|
+
defaults=[]),
|
152
|
+
body=[
|
153
|
+
If(
|
154
|
+
test=Compare(
|
155
|
+
left=Name(id='a', ctx=Load()),
|
156
|
+
ops=[
|
157
|
+
Gt()],
|
158
|
+
comparators=[
|
159
|
+
Constant(value=1)]),
|
160
|
+
body=[
|
161
|
+
Return(
|
162
|
+
value=BinOp(
|
163
|
+
left=Name(id='a', ctx=Load()),
|
164
|
+
op=Add(),
|
165
|
+
right=Name(id='b', ctx=Load())))],
|
166
|
+
orelse=[]),
|
167
|
+
For(
|
168
|
+
target=Name(id='i', ctx=Store()),
|
169
|
+
iter=Call(
|
170
|
+
func=Name(id='range', ctx=Load()),
|
171
|
+
args=[
|
172
|
+
Constant(value=1),
|
173
|
+
Constant(value=10),
|
174
|
+
Constant(value=2)],
|
175
|
+
keywords=[]),
|
176
|
+
body=[
|
177
|
+
AugAssign(
|
178
|
+
target=Name(id='a', ctx=Store()),
|
179
|
+
op=Add(),
|
180
|
+
value=Name(id='i', ctx=Load()))],
|
181
|
+
orelse=[]),
|
182
|
+
For(
|
183
|
+
target=Name(id='i', ctx=Store()),
|
184
|
+
iter=List(
|
185
|
+
elts=[
|
186
|
+
Constant(value=1),
|
187
|
+
Constant(value=2),
|
188
|
+
Constant(value=3)],
|
189
|
+
ctx=Load()),
|
190
|
+
body=[
|
191
|
+
AugAssign(
|
192
|
+
target=Name(id='a', ctx=Store()),
|
193
|
+
op=Add(),
|
194
|
+
value=Name(id='i', ctx=Load()))],
|
195
|
+
orelse=[]),
|
196
|
+
Assign(
|
197
|
+
targets=[
|
198
|
+
Name(id='a', ctx=Store())],
|
199
|
+
value=Call(
|
200
|
+
func=Attribute(
|
201
|
+
value=Name(id='math', ctx=Load()),
|
202
|
+
attr='ceil',
|
203
|
+
ctx=Load()),
|
204
|
+
args=[
|
205
|
+
Constant(value=12.5)],
|
206
|
+
keywords=[])),
|
207
|
+
Assign(
|
208
|
+
targets=[
|
209
|
+
Name(id='cc', ctx=Store())],
|
210
|
+
value=Dict(
|
211
|
+
keys=[
|
212
|
+
Constant(value='a'),
|
213
|
+
Constant(value='b')],
|
214
|
+
values=[
|
215
|
+
Constant(value=1),
|
216
|
+
Constant(value=2)])),
|
217
|
+
Assign(
|
218
|
+
targets=[
|
219
|
+
Subscript(
|
220
|
+
value=Name(id='cc', ctx=Load()),
|
221
|
+
slice=Constant(value='c'),
|
222
|
+
ctx=Store())],
|
223
|
+
value=Constant(value=3)),
|
224
|
+
Expr(
|
225
|
+
value=Call(
|
226
|
+
func=Name(id='print', ctx=Load()),
|
227
|
+
args=[
|
228
|
+
Constant(value='输出map:')],
|
229
|
+
keywords=[])),
|
230
|
+
For(
|
231
|
+
target=Name(id='ii', ctx=Store()),
|
232
|
+
iter=Name(id='cc', ctx=Load()),
|
233
|
+
body=[
|
234
|
+
Expr(
|
235
|
+
value=Call(
|
236
|
+
func=Name(id='print', ctx=Load()),
|
237
|
+
args=[
|
238
|
+
Attribute(
|
239
|
+
value=Name(id='ii', ctx=Load()),
|
240
|
+
attr='first',
|
241
|
+
ctx=Load()),
|
242
|
+
Attribute(
|
243
|
+
value=Name(id='ii', ctx=Load()),
|
244
|
+
attr='second',
|
245
|
+
ctx=Load())],
|
246
|
+
keywords=[]))],
|
247
|
+
orelse=[]),
|
248
|
+
Assign(
|
249
|
+
targets=[
|
250
|
+
Name(id='aa', ctx=Store())],
|
251
|
+
value=List(
|
252
|
+
elts=[
|
253
|
+
Constant(value=1),
|
254
|
+
Constant(value=3),
|
255
|
+
Constant(value=2)],
|
256
|
+
ctx=Load())),
|
257
|
+
Assign(
|
258
|
+
targets=[
|
259
|
+
Subscript(
|
260
|
+
value=Name(id='aa', ctx=Load()),
|
261
|
+
slice=Constant(value=0),
|
262
|
+
ctx=Store())],
|
263
|
+
value=Constant(value=134)),
|
264
|
+
Expr(
|
265
|
+
value=Call(
|
266
|
+
func=Name(id='print', ctx=Load()),
|
267
|
+
args=[
|
268
|
+
Constant(value='输出list:')],
|
269
|
+
keywords=[])),
|
270
|
+
For(
|
271
|
+
target=Name(id='i', ctx=Store()),
|
272
|
+
iter=Call(
|
273
|
+
func=Name(id='range', ctx=Load()),
|
274
|
+
args=[
|
275
|
+
Constant(value=3)],
|
276
|
+
keywords=[]),
|
277
|
+
body=[
|
278
|
+
Expr(
|
279
|
+
value=Call(
|
280
|
+
func=Name(id='print', ctx=Load()),
|
281
|
+
args=[
|
282
|
+
Name(id='i', ctx=Load()),
|
283
|
+
Subscript(
|
284
|
+
value=Name(id='aa', ctx=Load()),
|
285
|
+
slice=Name(id='i', ctx=Load()),
|
286
|
+
ctx=Load())],
|
287
|
+
keywords=[]))],
|
288
|
+
orelse=[]),
|
289
|
+
Expr(
|
290
|
+
value=Call(
|
291
|
+
func=Name(id='print', ctx=Load()),
|
292
|
+
args=[
|
293
|
+
Constant(value='Hello World'),
|
294
|
+
Name(id='a', ctx=Load()),
|
295
|
+
Name(id='b', ctx=Load())],
|
296
|
+
keywords=[])),
|
297
|
+
Expr(
|
298
|
+
value=Call(
|
299
|
+
func=Name(id='print', ctx=Load()),
|
300
|
+
args=[
|
301
|
+
Constant(value='test_other_fn'),
|
302
|
+
Call(
|
303
|
+
func=Name(id='test_other_fn', ctx=Load()),
|
304
|
+
args=[
|
305
|
+
Name(id='a', ctx=Load()),
|
306
|
+
Name(id='b', ctx=Load())],
|
307
|
+
keywords=[])],
|
308
|
+
keywords=[])),
|
309
|
+
Expr(
|
310
|
+
value=Call(
|
311
|
+
func=Name(id='print', ctx=Load()),
|
312
|
+
args=[
|
313
|
+
Constant(value='test编译的函数'),
|
314
|
+
Call(
|
315
|
+
func=Name(id='test编译的函数', ctx=Load()),
|
316
|
+
args=[
|
317
|
+
Name(id='a', ctx=Load()),
|
318
|
+
Name(id='b', ctx=Load())],
|
319
|
+
keywords=[])],
|
320
|
+
keywords=[])),
|
321
|
+
Assign(
|
322
|
+
targets=[
|
323
|
+
Name(id='v', ctx=Store())],
|
324
|
+
value=Constant(value=0)),
|
325
|
+
Assign(
|
326
|
+
targets=[
|
327
|
+
Name(id='vv', ctx=Store())],
|
328
|
+
value=BoolOp(
|
329
|
+
op=And(),
|
330
|
+
values=[
|
331
|
+
Constant(value=True),
|
332
|
+
BoolOp(
|
333
|
+
op=Or(),
|
334
|
+
values=[
|
335
|
+
Constant(value=False),
|
336
|
+
Constant(value=1)])])),
|
337
|
+
Expr(
|
338
|
+
value=Call(
|
339
|
+
func=Name(id='print', ctx=Load()),
|
340
|
+
args=[
|
341
|
+
Constant(value='vv:'),
|
342
|
+
Name(id='vv', ctx=Load())],
|
343
|
+
keywords=[])),
|
344
|
+
While(
|
345
|
+
test=Constant(value=True),
|
346
|
+
body=[
|
347
|
+
Expr(
|
348
|
+
value=Call(
|
349
|
+
func=Name(id='py_cin', ctx=Load()),
|
350
|
+
args=[
|
351
|
+
Name(id='v', ctx=Load())],
|
352
|
+
keywords=[])),
|
353
|
+
If(
|
354
|
+
test=Compare(
|
355
|
+
left=Name(id='v', ctx=Load()),
|
356
|
+
ops=[
|
357
|
+
Gt()],
|
358
|
+
comparators=[
|
359
|
+
Constant(value=100)]),
|
360
|
+
body=[
|
361
|
+
Break()],
|
362
|
+
orelse=[
|
363
|
+
Expr(
|
364
|
+
value=Call(
|
365
|
+
func=Name(id='print', ctx=Load()),
|
366
|
+
args=[
|
367
|
+
Constant(value='输入的'),
|
368
|
+
Name(id='v', ctx=Load()),
|
369
|
+
Constant(value='小于等于100')],
|
370
|
+
keywords=[]))])],
|
371
|
+
orelse=[]),
|
372
|
+
Return(
|
373
|
+
value=BinOp(
|
374
|
+
left=BinOp(
|
375
|
+
left=BinOp(
|
376
|
+
left=BinOp(
|
377
|
+
left=Name(id='a', ctx=Load()),
|
378
|
+
op=Add(),
|
379
|
+
right=Name(id='b', ctx=Load())),
|
380
|
+
op=Add(),
|
381
|
+
right=Constant(value=1)),
|
382
|
+
op=Add(),
|
383
|
+
right=Call(
|
384
|
+
func=Name(id='test_直接调用', ctx=Load()),
|
385
|
+
args=[],
|
386
|
+
keywords=[])),
|
387
|
+
op=Add(),
|
388
|
+
right=Name(id='v', ctx=Load())))],
|
389
|
+
decorator_list=[
|
390
|
+
Call(
|
391
|
+
func=Attribute(
|
392
|
+
value=Name(id='lc', ctx=Load()),
|
393
|
+
attr='jit',
|
394
|
+
ctx=Load()),
|
395
|
+
args=[],
|
396
|
+
keywords=[
|
397
|
+
keyword(
|
398
|
+
arg='每次运行都重新编译',
|
399
|
+
value=Constant(value=True))])],
|
400
|
+
returns=Name(id='int', ctx=Load()))],
|
401
|
+
type_ignores=[])
|
402
|
+
输出map:
|
403
|
+
c 3
|
404
|
+
a 1
|
405
|
+
b 2
|
406
|
+
输出list:
|
407
|
+
0 134
|
408
|
+
1 3
|
409
|
+
2 2
|
410
|
+
Hello World 13 3
|
411
|
+
test_other_fn 10
|
412
|
+
test编译的函数 39
|
413
|
+
vv: 1
|
414
|
+
请输入>>>输入的 1 小于等于100
|
415
|
+
请输入>>>输入的 2 小于等于100
|
416
|
+
请输入>>>输入的 100 小于等于100
|
417
|
+
请输入>>>Module(
|
418
|
+
body=[
|
419
|
+
FunctionDef(
|
420
|
+
name='test_other_fn',
|
421
|
+
args=arguments(
|
422
|
+
posonlyargs=[],
|
423
|
+
args=[
|
424
|
+
arg(
|
425
|
+
arg='a',
|
426
|
+
annotation=Name(id='int', ctx=Load())),
|
427
|
+
arg(
|
428
|
+
arg='b',
|
429
|
+
annotation=Name(id='int', ctx=Load()))],
|
430
|
+
kwonlyargs=[],
|
431
|
+
kw_defaults=[],
|
432
|
+
defaults=[]),
|
433
|
+
body=[
|
434
|
+
Return(
|
435
|
+
value=BinOp(
|
436
|
+
left=Name(id='a', ctx=Load()),
|
437
|
+
op=Sub(),
|
438
|
+
right=Name(id='b', ctx=Load())))],
|
439
|
+
decorator_list=[],
|
440
|
+
returns=Name(id='int', ctx=Load()))],
|
441
|
+
type_ignores=[])
|
442
|
+
结果: 241
|
443
|
+
|
444
|
+
```
|
445
|
+
|
446
|
+
## 4. 查看输出文件
|
447
|
+
```bash
|
448
|
+
ls -al ./l0n0lcoutput
|
449
|
+
total 32
|
450
|
+
drwxr-xr-x 2 root root 4096 Sep 16 03:28 .
|
451
|
+
drwxrwxrwx 11 1000 1000 4096 Sep 16 03:18 ..
|
452
|
+
-rw-r--r-- 1 root root 284 Sep 16 03:28 sum_and_filter_@e48c1f185531e3af.cpp
|
453
|
+
-rw-r--r-- 1 root root 83 Sep 16 03:28 sum_and_filter_@e48c1f185531e3af.h
|
454
|
+
-rwxr-xr-x 1 root root 15616 Sep 16 03:28 sum_and_filter_@e48c1f185531e3af.so
|
455
|
+
|
456
|
+
```
|
457
|
+
## 5. sum_and_filter_@e48c1f185531e3af.cpp
|
458
|
+
```c++
|
459
|
+
#include "sum_and_filter_@e48c1f185531e3af.h"
|
460
|
+
extern "C" int64_t sum_and_filter (int64_t n)
|
461
|
+
{
|
462
|
+
auto total = ((int64_t)(0));
|
463
|
+
for (int64_t x = 0; x < n; ++x)
|
464
|
+
{
|
465
|
+
if ((x % 2 == 0))
|
466
|
+
{
|
467
|
+
total = total + x;
|
468
|
+
}
|
469
|
+
|
470
|
+
{
|
471
|
+
total = total - x;
|
472
|
+
}
|
473
|
+
|
474
|
+
}
|
475
|
+
|
476
|
+
return total;
|
477
|
+
}
|
478
|
+
|
479
|
+
```
|
480
|
+
## 6. sum_and_filter_@e48c1f185531e3af.h
|
481
|
+
```c++
|
482
|
+
#include <cstdint>
|
483
|
+
#include <string>
|
484
|
+
extern "C" int64_t sum_and_filter (int64_t n);
|
485
|
+
```
|
486
|
+
## 7. test_add_@6a812a013615c16d.cpp
|
487
|
+
```c++
|
488
|
+
#include "test_add_@6a812a013615c16d.h"
|
489
|
+
extern "C" int64_t test_add (int64_t a, int64_t b)
|
490
|
+
{
|
491
|
+
if ((a > 1))
|
492
|
+
{
|
493
|
+
return a + b;
|
494
|
+
}
|
495
|
+
|
496
|
+
for (int64_t i = 1; i < 10; i += 2)
|
497
|
+
{
|
498
|
+
a = a + i;
|
499
|
+
}
|
500
|
+
|
501
|
+
for (auto i : {1,2,3})
|
502
|
+
{
|
503
|
+
a = a + i;
|
504
|
+
}
|
505
|
+
|
506
|
+
a = std::ceil(12.5);;
|
507
|
+
std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
508
|
+
cc[u8"c"] = 3;
|
509
|
+
std::cout<< u8"输出map:" << " "<< std::endl;
|
510
|
+
for (auto ii : cc)
|
511
|
+
{
|
512
|
+
std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
|
513
|
+
}
|
514
|
+
|
515
|
+
int64_t aa[] = {1,3,2};
|
516
|
+
aa[0] = 134;
|
517
|
+
std::cout<< u8"输出list:" << " "<< std::endl;
|
518
|
+
for (int64_t i = 0; i < 3; ++i)
|
519
|
+
{
|
520
|
+
std::cout<< i << " "<< aa[i] << " "<< std::endl;
|
521
|
+
}
|
522
|
+
|
523
|
+
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
524
|
+
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
|
525
|
+
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
|
526
|
+
auto v = 0;
|
527
|
+
auto vv = true&&false||1;
|
528
|
+
std::cout<< u8"vv:" << " "<< vv << " "<< std::endl;
|
529
|
+
while (true)
|
530
|
+
{
|
531
|
+
std::cout << u8"请输入>>>"; std::cin >> v;
|
532
|
+
if ((v > 100))
|
533
|
+
{
|
534
|
+
break;
|
535
|
+
}
|
536
|
+
|
537
|
+
{
|
538
|
+
std::cout<< u8"输入的" << " "<< v << " "<< u8"小于等于100" << " "<< std::endl;
|
539
|
+
}
|
540
|
+
|
541
|
+
}
|
542
|
+
|
543
|
+
return a + b + 1 + 123 + v;
|
544
|
+
}
|
545
|
+
|
546
|
+
```
|
547
|
+
## 8. test_add_@6a812a013615c16d.h
|
548
|
+
```c++
|
549
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
550
|
+
#include "test编译的函数_@3bf4501e0408a243.h"
|
551
|
+
#include <cmath>
|
552
|
+
#include <cstdint>
|
553
|
+
#include <iostream>
|
554
|
+
#include <string>
|
555
|
+
#include <unordered_map>
|
556
|
+
extern "C" int64_t test_add (int64_t a, int64_t b);
|
557
|
+
```
|
558
|
+
## 9. test_other_fn_@75fdd928ab58a8e3.cpp
|
559
|
+
```c++
|
560
|
+
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
561
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b)
|
562
|
+
{
|
563
|
+
return a - b;
|
564
|
+
}
|
565
|
+
|
566
|
+
```
|
567
|
+
## 10. test_other_fn_@75fdd928ab58a8e3.h
|
568
|
+
```c++
|
569
|
+
#include <cstdint>
|
570
|
+
#include <string>
|
571
|
+
extern "C" int64_t test_other_fn (int64_t a, int64_t b);
|
572
|
+
```
|
573
|
+
## 11. test编译的函数_@3bf4501e0408a243.cpp
|
574
|
+
```c++
|
575
|
+
#include "test编译的函数_@3bf4501e0408a243.h"
|
576
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b)
|
577
|
+
{
|
578
|
+
return a * b;
|
579
|
+
}
|
580
|
+
|
581
|
+
```
|
582
|
+
## 12. test编译的函数_@3bf4501e0408a243.h
|
583
|
+
```c++
|
584
|
+
#include <cstdint>
|
585
|
+
#include <string>
|
586
|
+
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b);
|
587
|
+
```
|
@@ -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=9YT2fC8dsHymyrmtQGGmc0KBfenzP1QYJX5SVUNBu3I,5835
|
5
|
+
l0n0lc/jit.py,sha256=nocYOYiORnaOyYPGs7pLRacKWhzBdP-7Sux9ubm7QOY,24647
|
6
|
+
l0n0lc/编译.py,sha256=Ty4gPDHAB1jn8LgnE4w3R06bOAEeYr8iwqahYf6QeHI,2154
|
7
|
+
l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
|
8
|
+
l0n0lc-0.8.6.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
|
9
|
+
l0n0lc-0.8.6.dist-info/METADATA,sha256=3z1lqqULiw7i5xO6Wz8mZwAthAVK3ulO3nnmcQ1z03c,15058
|
10
|
+
l0n0lc-0.8.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
11
|
+
l0n0lc-0.8.6.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
|
12
|
+
l0n0lc-0.8.6.dist-info/RECORD,,
|
File without changes
|
l0n0lc-0.8.4.dist-info/METADATA
DELETED
@@ -1,241 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: l0n0lc
|
3
|
-
Version: 0.8.4
|
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 (True):
|
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
|
-
total 96
|
127
|
-
drwxr-xr-x 2 root root 4096 Sep 16 01:49 .
|
128
|
-
drwxrwxrwx 11 1000 1000 4096 Sep 16 01:41 ..
|
129
|
-
-rw-r--r-- 1 root root 1318 Sep 16 01:49 test_add_@6a812a013615c16d.cpp
|
130
|
-
-rw-r--r-- 1 root root 246 Sep 16 01:49 test_add_@6a812a013615c16d.h
|
131
|
-
-rwxr-xr-x 1 root root 29968 Sep 16 01:49 test_add_@6a812a013615c16d.so
|
132
|
-
-rw-r--r-- 1 root root 121 Sep 16 01:49 test_other_fn_@75fdd928ab58a8e3.cpp
|
133
|
-
-rw-r--r-- 1 root root 93 Sep 16 01:49 test_other_fn_@75fdd928ab58a8e3.h
|
134
|
-
-rwxr-xr-x 1 root root 15616 Sep 16 01:49 test_other_fn_@75fdd928ab58a8e3.so
|
135
|
-
-rw-r--r-- 1 root root 185 Sep 16 01:41 test编译的函数_@3bf4501e0408a243.cpp
|
136
|
-
-rw-r--r-- 1 root root 151 Sep 16 01:41 test编译的函数_@3bf4501e0408a243.h
|
137
|
-
-rwxr-xr-x 1 root root 15656 Sep 16 01:41 test编译的函数_@3bf4501e0408a243.so
|
138
|
-
|
139
|
-
```
|
140
|
-
## 5. test_add_@6a812a013615c16d.cpp
|
141
|
-
```c++
|
142
|
-
#include "test_add_@6a812a013615c16d.h"
|
143
|
-
extern "C" int64_t test_add (int64_t a, int64_t b)
|
144
|
-
{
|
145
|
-
if ((a > 1))
|
146
|
-
{
|
147
|
-
return a + b;
|
148
|
-
}
|
149
|
-
|
150
|
-
for (int64_t i = 1; i < 10; i += 2)
|
151
|
-
{
|
152
|
-
a = a + i;
|
153
|
-
}
|
154
|
-
|
155
|
-
for (auto i : {1,2,3})
|
156
|
-
{
|
157
|
-
a = a + i;
|
158
|
-
}
|
159
|
-
|
160
|
-
a = std::ceil(12.5);;
|
161
|
-
std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
162
|
-
cc[u8"c"] = 3;
|
163
|
-
std::cout<< u8"输出map:" << " "<< std::endl;
|
164
|
-
for (auto ii : cc)
|
165
|
-
{
|
166
|
-
std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
|
167
|
-
}
|
168
|
-
|
169
|
-
int64_t aa[] = {1,3,2};
|
170
|
-
aa[0] = 134;
|
171
|
-
std::cout<< u8"输出list:" << " "<< std::endl;
|
172
|
-
for (int64_t i = 0; i < 3; ++i)
|
173
|
-
{
|
174
|
-
std::cout<< i << " "<< aa[i] << " "<< std::endl;
|
175
|
-
}
|
176
|
-
|
177
|
-
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
178
|
-
std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
|
179
|
-
std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
|
180
|
-
auto v = 0;
|
181
|
-
auto vv = true&&false||1;
|
182
|
-
std::cout<< u8"vv:" << " "<< vv << " "<< std::endl;
|
183
|
-
while (true)
|
184
|
-
{
|
185
|
-
std::cout << u8"请输入>>>"; std::cin >> v;
|
186
|
-
if ((v > 100))
|
187
|
-
{
|
188
|
-
break;
|
189
|
-
}
|
190
|
-
|
191
|
-
{
|
192
|
-
std::cout<< u8"输入的" << " "<< v << " "<< u8"小于等于100" << " "<< std::endl;
|
193
|
-
}
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
return a + b + 1 + 123 + v;
|
198
|
-
}
|
199
|
-
|
200
|
-
```
|
201
|
-
## 6. test_add_@6a812a013615c16d.h
|
202
|
-
```c++
|
203
|
-
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
204
|
-
#include "test编译的函数_@3bf4501e0408a243.h"
|
205
|
-
#include <cmath>
|
206
|
-
#include <cstdint>
|
207
|
-
#include <iostream>
|
208
|
-
#include <string>
|
209
|
-
#include <unordered_map>
|
210
|
-
extern "C" int64_t test_add (int64_t a, int64_t b);
|
211
|
-
```
|
212
|
-
## 7. test_other_fn_@75fdd928ab58a8e3.cpp
|
213
|
-
```c++
|
214
|
-
#include "test_other_fn_@75fdd928ab58a8e3.h"
|
215
|
-
extern "C" int64_t test_other_fn (int64_t a, int64_t b)
|
216
|
-
{
|
217
|
-
return a - b;
|
218
|
-
}
|
219
|
-
|
220
|
-
```
|
221
|
-
## 8. test_other_fn_@75fdd928ab58a8e3.h
|
222
|
-
```c++
|
223
|
-
#include <cstdint>
|
224
|
-
#include <string>
|
225
|
-
extern "C" int64_t test_other_fn (int64_t a, int64_t b);
|
226
|
-
```
|
227
|
-
## 9. test编译的函数_@3bf4501e0408a243.cpp
|
228
|
-
```c++
|
229
|
-
#include "test编译的函数_@3bf4501e0408a243.h"
|
230
|
-
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b)
|
231
|
-
{
|
232
|
-
return a * b;
|
233
|
-
}
|
234
|
-
|
235
|
-
```
|
236
|
-
## 10. test编译的函数_@3bf4501e0408a243.h
|
237
|
-
```c++
|
238
|
-
#include <cstdint>
|
239
|
-
#include <string>
|
240
|
-
extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b);
|
241
|
-
```
|
l0n0lc-0.8.4.dist-info/RECORD
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
l0n0lc/StdList.py,sha256=fA6sWAfW2JgDUhQDK2Hlx_PPuSdLwTcCTk5aha4zA-Q,863
|
2
|
-
l0n0lc/StdMap.py,sha256=TIVKqhT078cjLIM0Zlq-TwGFSy-KwOyJe00BLFPVxr8,735
|
3
|
-
l0n0lc/__init__.py,sha256=fUC6TMeN_fEL7HDazrBEdoWOQkXi5uSGZb7LZj6juog,137
|
4
|
-
l0n0lc/c基础处理.py,sha256=FMauoVx4-33JXl4OLpUl1D6NCkLjk6yBH7do9WnT0Wg,5671
|
5
|
-
l0n0lc/jit.py,sha256=MNvtnt_SPqwdJWzvkZKOSwrdN7zqjCx6KLhy5Z5bJAo,23946
|
6
|
-
l0n0lc/编译.py,sha256=Ty4gPDHAB1jn8LgnE4w3R06bOAEeYr8iwqahYf6QeHI,2154
|
7
|
-
l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
|
8
|
-
l0n0lc-0.8.4.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
|
9
|
-
l0n0lc-0.8.4.dist-info/METADATA,sha256=FM7tcMjK56Kteal4Er5l8MB2pGGaU77UDaWRH2E2em0,5660
|
10
|
-
l0n0lc-0.8.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
11
|
-
l0n0lc-0.8.4.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
|
12
|
-
l0n0lc-0.8.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|