l0n0lc 0.8.4__tar.gz → 0.8.5__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.
File without changes
l0n0lc-0.8.5/PKG-INFO ADDED
@@ -0,0 +1,587 @@
1
+ Metadata-Version: 2.4
2
+ Name: l0n0lc
3
+ Version: 0.8.5
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
+ ```