l0n0lc 0.8.7__py3-none-any.whl → 0.8.8__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 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类型 = py类型转c类型(fn)
311
- if c类型 is None:
312
- self.抛出代码异常(f'不支持的python类型{fn}', node)
313
- 参数文本 = self.构建参数列表文本(node.args)
314
- return c函数调用(c类型, 参数文本)
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代码(str(代码))
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,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
+ ```
@@ -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.8.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
9
+ l0n0lc-0.8.8.dist-info/METADATA,sha256=Dtn6HX9s4q8gr22r3070KzipY3saxyxC9xNy9P8rTwA,10451
10
+ l0n0lc-0.8.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ l0n0lc-0.8.8.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
12
+ l0n0lc-0.8.8.dist-info/RECORD,,
@@ -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
- ```
@@ -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