l0n0lc 0.8.7__tar.gz → 0.8.8__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.8.8/PKG-INFO ADDED
@@ -0,0 +1,489 @@
1
+ Metadata-Version: 2.4
2
+ Name: l0n0lc
3
+ Version: 0.8.8
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
+ total 160
238
+ drwxr-xr-x 2 root root 4096 Sep 18 01:09 .
239
+ drwxrwxrwx 11 1000 1000 4096 Sep 17 03:11 ..
240
+ -rw-r--r-- 1 root root 96 Sep 18 01:05 helper_@a9eebb1639a5f08c.cpp
241
+ -rw-r--r-- 1 root root 88 Sep 18 01:05 helper_@a9eebb1639a5f08c.h
242
+ -rwxr-xr-x 1 root root 15600 Sep 18 01:05 helper_@a9eebb1639a5f08c.so
243
+ -rw-r--r-- 1 root root 1693 Sep 18 01:09 jit_all_ops_@0ec883892076cd03.cpp
244
+ -rw-r--r-- 1 root root 167 Sep 18 01:09 jit_all_ops_@0ec883892076cd03.h
245
+ -rwxr-xr-x 1 root root 23520 Sep 18 01:09 jit_all_ops_@0ec883892076cd03.so
246
+ -rw-r--r-- 1 root root 1466 Sep 18 01:09 test_add_@122c4be59097d472.cpp
247
+ -rw-r--r-- 1 root root 302 Sep 18 01:09 test_add_@122c4be59097d472.h
248
+ -rwxr-xr-x 1 root root 40264 Sep 18 01:09 test_add_@122c4be59097d472.so
249
+ -rw-r--r-- 1 root root 121 Sep 18 01:09 test_other_fn_@75fdd928ab58a8e3.cpp
250
+ -rw-r--r-- 1 root root 106 Sep 18 01:09 test_other_fn_@75fdd928ab58a8e3.h
251
+ -rwxr-xr-x 1 root root 15616 Sep 18 01:09 test_other_fn_@75fdd928ab58a8e3.so
252
+ -rw-r--r-- 1 root root 185 Sep 18 01:07 test编译的函数_@3bf4501e0408a243.cpp
253
+ -rw-r--r-- 1 root root 164 Sep 18 01:07 test编译的函数_@3bf4501e0408a243.h
254
+ -rwxr-xr-x 1 root root 15656 Sep 18 01:07 test编译的函数_@3bf4501e0408a243.so
255
+
256
+ ```
257
+ ## 5. helper_@a9eebb1639a5f08c.cpp
258
+ ```c++
259
+ #include "helper_@a9eebb1639a5f08c.h"
260
+ extern "C" int16_t helper (int16_t p)
261
+ {
262
+ return p * 2;
263
+ }
264
+
265
+ ```
266
+ ## 6. helper_@a9eebb1639a5f08c.h
267
+ ```c++
268
+ #pragma once
269
+ #include <cstdint>
270
+ #include <string>
271
+ extern "C" int16_t helper (int16_t p);
272
+ ```
273
+ ## 7. jit_all_ops_@0ec883892076cd03.cpp
274
+ ```c++
275
+ #include "jit_all_ops_@0ec883892076cd03.h"
276
+ extern "C" int16_t jit_all_ops (int16_t a, int16_t b)
277
+ {
278
+ auto x = 42;
279
+ auto y = ((int16_t)(a + b));
280
+ auto z = 3.14;
281
+ auto flag = true;
282
+ int16_t nums[] = {1,2,3};
283
+ int16_t tup[] = {4,5};
284
+ std::unordered_map<int16_t, int16_t> mp = {{ 1, 10 },{ 2, 20 }};
285
+ auto pos = +a;
286
+ auto neg = -b;
287
+ auto inv = ~a;
288
+ auto not_flag = !flag;
289
+ auto add = a + b;
290
+ auto sub = a - b;
291
+ auto mul = a * b;
292
+ auto div = a / ((b != 0) ? (b) : (1));
293
+ auto mod = a % ((b != 0) ? (b) : (1));
294
+ auto band = a & b;
295
+ auto bor = a | b;
296
+ auto bxor = a ^ b;
297
+ auto lshift = a << 1;
298
+ auto rshift = a >> 1;
299
+ auto cmp1 = (a == b);
300
+ auto cmp2 = (a != b);
301
+ auto cmp3 = (a < b);
302
+ auto cmp4 = (a <= b);
303
+ auto cmp5 = (a > b);
304
+ auto cmp6 = (a >= b);
305
+ auto logic_and = cmp1&&cmp2;
306
+ auto logic_or = cmp3||cmp4;
307
+ auto ternary = ((a > b) ? (a) : (b));
308
+ if ((a > b))
309
+ {
310
+ y = y + 1;
311
+ }
312
+
313
+ else
314
+ {
315
+ y = y - 1;
316
+ }
317
+
318
+ for (int64_t i = 0; i < 3; ++i)
319
+ {
320
+ y = y + i;
321
+ }
322
+
323
+ for (auto v : nums)
324
+ {
325
+ y = y + v;
326
+ if ((v == 2))
327
+ {
328
+ continue;
329
+ }
330
+
331
+ if ((v == 3))
332
+ {
333
+ break;
334
+ }
335
+
336
+ }
337
+
338
+ auto count = 0;
339
+ while ((count < 2))
340
+ {
341
+ y = y + count;
342
+ count = count + 1;
343
+ }
344
+
345
+ y = y + 5;
346
+ y = y - 1;
347
+ y = y * 2;
348
+ y = y / 2;
349
+ y = y % 10;
350
+ y = y & 7;
351
+ y = y | 3;
352
+ y = y ^ 1;
353
+ y = y << 1;
354
+ y = y >> 1;
355
+ auto first_num = nums[0];
356
+ auto mp_val = mp[1];
357
+ y = y + first_num + mp_val;
358
+ auto vector = std::vector<int>();
359
+ vector.push_back(count);
360
+ vector.push_back(y);
361
+ for (int64_t i = 0; i < vector.size(); ++i)
362
+ {
363
+ std::cout<< u8"vector->" << " "<< i << " "<< u8"=" << " "<< vector[i] << " "<< std::endl;;
364
+ }
365
+
366
+ return y;
367
+ }
368
+
369
+ ```
370
+ ## 8. jit_all_ops_@0ec883892076cd03.h
371
+ ```c++
372
+ #pragma once
373
+ #include <cstdint>
374
+ #include <iostream>
375
+ #include <string>
376
+ #include <unordered_map>
377
+ #include <vector>
378
+ extern "C" int16_t jit_all_ops (int16_t a, int16_t b);
379
+ ```
380
+ ## 9. test_add_@122c4be59097d472.cpp
381
+ ```c++
382
+ #include "test_add_@122c4be59097d472.h"
383
+ extern "C" int16_t test_add (int16_t a, int16_t b)
384
+ {
385
+ if ((a > 1))
386
+ {
387
+ return a + b;
388
+ }
389
+
390
+ for (int64_t i = 1; i < 10; i += 2)
391
+ {
392
+ a = a + i;
393
+ }
394
+
395
+ for (auto i : {1,2,3})
396
+ {
397
+ a = a + i;
398
+ }
399
+
400
+ a = std::ceil(12.5);;
401
+ std::unordered_map<std::string, int16_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
402
+ cc[u8"c"] = 3;
403
+ std::cout<< u8"输出map:" << " "<< std::endl;;
404
+ for (auto ii : cc)
405
+ {
406
+ std::cout<< ii.first << " "<< ii.second << " "<< std::endl;;
407
+ }
408
+
409
+ int16_t aa[] = {1,3,2};
410
+ aa[0] = 134;
411
+ std::cout<< u8"输出list:" << " "<< std::endl;;
412
+ for (int64_t i = 0; i < 3; ++i)
413
+ {
414
+ std::cout<< i << " "<< aa[i] << " "<< std::endl;;
415
+ }
416
+
417
+ std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;;
418
+ std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;;
419
+ std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;;
420
+ std::cout<< u8"测试所有操作:" << " "<< std::endl;;
421
+ jit_all_ops(a,b);
422
+ auto v = 0;
423
+ auto vv = true&&false||1;
424
+ std::cout<< u8"vv:" << " "<< vv << " "<< std::endl;;
425
+ std::cout<< u8"测试while:" << " "<< std::endl;;
426
+ while (vv)
427
+ {
428
+ std::cout << u8"请输入>>>"; std::cin >> v;;
429
+ if ((v > 100))
430
+ {
431
+ break;
432
+ }
433
+
434
+ else
435
+ {
436
+ std::cout<< u8"输入的" << " "<< v << " "<< u8"小于等于100" << " "<< std::endl;;
437
+ }
438
+
439
+ }
440
+
441
+ return a + b + 1 + 123 + v;
442
+ }
443
+
444
+ ```
445
+ ## 10. test_add_@122c4be59097d472.h
446
+ ```c++
447
+ #pragma once
448
+ #include "jit_all_ops_@0ec883892076cd03.h"
449
+ #include "test_other_fn_@75fdd928ab58a8e3.h"
450
+ #include "test编译的函数_@3bf4501e0408a243.h"
451
+ #include <cmath>
452
+ #include <cstdint>
453
+ #include <iostream>
454
+ #include <string>
455
+ #include <unordered_map>
456
+ extern "C" int16_t test_add (int16_t a, int16_t b);
457
+ ```
458
+ ## 11. test_other_fn_@75fdd928ab58a8e3.cpp
459
+ ```c++
460
+ #include "test_other_fn_@75fdd928ab58a8e3.h"
461
+ extern "C" int16_t test_other_fn (int16_t a, int16_t b)
462
+ {
463
+ return a - b;
464
+ }
465
+
466
+ ```
467
+ ## 12. test_other_fn_@75fdd928ab58a8e3.h
468
+ ```c++
469
+ #pragma once
470
+ #include <cstdint>
471
+ #include <string>
472
+ extern "C" int16_t test_other_fn (int16_t a, int16_t b);
473
+ ```
474
+ ## 13. test编译的函数_@3bf4501e0408a243.cpp
475
+ ```c++
476
+ #include "test编译的函数_@3bf4501e0408a243.h"
477
+ extern "C" int16_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int16_t a, int16_t b)
478
+ {
479
+ return a * b;
480
+ }
481
+
482
+ ```
483
+ ## 14. test编译的函数_@3bf4501e0408a243.h
484
+ ```c++
485
+ #pragma once
486
+ #include <cstdint>
487
+ #include <string>
488
+ extern "C" int16_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int16_t a, int16_t b);
489
+ ```