l0n0lc 0.8.3__py3-none-any.whl → 0.8.5__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 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
- return f'{self.类型} {self.c名字} = {初始值};'
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
@@ -208,9 +208,9 @@ class py2cpp编译器(ast.NodeVisitor):
208
208
 
209
209
  if isinstance(value, ast.BoolOp):
210
210
  if isinstance(value.op, ast.And):
211
- return '&&'.join([self.获取值(v) for v in value.values])
211
+ return '&&'.join([str(self.获取值(v)) for v in value.values])
212
212
  if isinstance(value.op, ast.Or):
213
- return '||'.join([self.获取值(v) for v in value.values])
213
+ return '||'.join([str(self.获取值(v)) for v in value.values])
214
214
 
215
215
  if isinstance(value, ast.IfExp):
216
216
  test = self.获取值(value.test)
@@ -444,11 +444,11 @@ class py2cpp编译器(ast.NodeVisitor):
444
444
  if fn is range:
445
445
  参数 = [self.获取值(arg) for arg in node.iter.args]
446
446
  if len(参数) == 1:
447
- 代码 = f'for (int64_t {目标} = 0; i < {参数[0]}; ++i)'
447
+ 代码 = f'for (int64_t {目标} = 0; {目标} < {参数[0]}; ++{目标})'
448
448
  elif len(参数) == 2:
449
- 代码 = f'for (int64_t {目标} = {参数[0]}; i < {参数[1]}; ++i)'
449
+ 代码 = f'for (int64_t {目标} = {参数[0]}; {目标} < {参数[1]}; ++{目标})'
450
450
  elif len(参数) == 3:
451
- 代码 = f'for (int64_t {目标} = {参数[0]}; i < {参数[1]}; i += {参数[2]})'
451
+ 代码 = f'for (int64_t {目标} = {参数[0]}; {目标} < {参数[1]}; {目标} += {参数[2]})'
452
452
  else:
453
453
  调用代码 = self.调用Call(node.iter)
454
454
  代码 = f'for (auto {目标} : {调用代码})'
@@ -491,7 +491,7 @@ class py2cpp编译器(ast.NodeVisitor):
491
491
  代码 = self.调用Call(node)
492
492
  self.添加c代码(str(代码))
493
493
 
494
- def _赋值(self, target, 值, node):
494
+ def _赋值(self, target, 值, node, c强转类型: str | None = None):
495
495
  目标 = self.获取值(target)
496
496
  if self.正在调用直接函数:
497
497
  if isinstance(target, ast.Name):
@@ -512,12 +512,15 @@ class py2cpp编译器(ast.NodeVisitor):
512
512
  self.include目录.add('<any>')
513
513
  else:
514
514
  目标 = c变量('auto', target.id, False)
515
- self.添加c代码(目标.初始化())
515
+ self.添加c代码(目标.初始化(值, c强转类型))
516
516
  self.添加c变量(目标)
517
517
  else:
518
518
  self.抛出代码异常('for中target必须是ast.Name', node)
519
519
  else:
520
- self.添加c代码(f'{目标} = {值};')
520
+ if c强转类型:
521
+ self.添加c代码(f'{目标} = {c强转类型}({值});')
522
+ else:
523
+ self.添加c代码(f'{目标} = {值};')
521
524
 
522
525
  def visit_Assign(self, node: ast.Assign) -> Any:
523
526
  值 = self.获取值(node.value)
@@ -528,6 +531,16 @@ class py2cpp编译器(ast.NodeVisitor):
528
531
  值 = self.计算二元运算(node)
529
532
  self._赋值(node.target, 值, node)
530
533
 
534
+ def visit_AnnAssign(self, node: ast.AnnAssign) -> Any:
535
+ 值 = self.获取值(node.value)
536
+ 目标类型 = self.获取值(node.annotation)
537
+ if 目标类型 is None:
538
+ self.抛出代码异常(f'不支持的类型嗯{node.annotation}', node)
539
+ c类型 = py类型转c类型(目标类型)
540
+ if c类型 is None:
541
+ self.抛出代码异常(f'不支持的类型嗯{目标类型}', node)
542
+ self._赋值(node.target, 值, node, str(c类型))
543
+
531
544
  def 获取定义(self):
532
545
  参数定义 = []
533
546
  for arg in self.参数变量.values():
@@ -592,7 +605,6 @@ class py2cpp编译器(ast.NodeVisitor):
592
605
 
593
606
  def jit(jit编译器类=None, cpp编译器类=None, 每次运行都重新编译: bool = False):
594
607
  def 编译函数(fn: Callable):
595
- # print(ast.dump(语法树, indent=' '))
596
608
  _c编译器类 = cpp编译器类 or cpp编译器
597
609
  _jit编译器类 = jit编译器类 or py2cpp编译器
598
610
  c语言函数编译器 = _jit编译器类(fn, _c编译器类())
@@ -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
+ ```
@@ -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=bKNShdA5_VPyTUS_FrACdmsfXtkVL6-yfDoWb6761PE,24608
6
+ l0n0lc/编译.py,sha256=Ty4gPDHAB1jn8LgnE4w3R06bOAEeYr8iwqahYf6QeHI,2154
7
+ l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
8
+ l0n0lc-0.8.5.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
9
+ l0n0lc-0.8.5.dist-info/METADATA,sha256=UAZi2_TijJNUbR-jr61rFYT77MBfahKNarylpTuPNmg,15058
10
+ l0n0lc-0.8.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ l0n0lc-0.8.5.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
12
+ l0n0lc-0.8.5.dist-info/RECORD,,
@@ -1,238 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: l0n0lc
3
- Version: 0.8.3
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
84
- while (vv):
85
- py_cin(v)
86
- if v > 100:
87
- break
88
- else:
89
- print('输入的', v, '小于等于100')
90
- return a + b + 1 + test_直接调用() + v
91
-
92
-
93
- print('结果:', test_add(1, 3))
94
-
95
- ```
96
-
97
- ## 3. 运行hello_world.py
98
- ```
99
- uv run tests/hello_world.py
100
- # 输入: b'1\n2\n100\n101\n'
101
- ```
102
- ```bash
103
- 输出map:
104
- c 3
105
- a 1
106
- b 2
107
- 输出list:
108
- 0 134
109
- 1 3
110
- 2 2
111
- Hello World 13 3
112
- test_other_fn 10
113
- test编译的函数 39
114
- 请输入>>>输入的 1 小于等于100
115
- 请输入>>>输入的 2 小于等于100
116
- 请输入>>>输入的 100 小于等于100
117
- 请输入>>>结果: 241
118
-
119
- ```
120
-
121
- ## 4. 查看输出文件
122
- ```bash
123
- ls -al ./l0n0lcoutput
124
- total 96
125
- drwxr-xr-x 2 root root 4096 Sep 16 01:38 .
126
- drwxrwxrwx 11 1000 1000 4096 Sep 16 01:36 ..
127
- -rw-r--r-- 1 root root 1252 Sep 16 01:38 test_add_@05ade4b088e9b383.cpp
128
- -rw-r--r-- 1 root root 246 Sep 16 01:38 test_add_@05ade4b088e9b383.h
129
- -rwxr-xr-x 1 root root 29904 Sep 16 01:38 test_add_@05ade4b088e9b383.so
130
- -rw-r--r-- 1 root root 121 Sep 16 01:38 test_other_fn_@75fdd928ab58a8e3.cpp
131
- -rw-r--r-- 1 root root 93 Sep 16 01:38 test_other_fn_@75fdd928ab58a8e3.h
132
- -rwxr-xr-x 1 root root 15616 Sep 16 01:38 test_other_fn_@75fdd928ab58a8e3.so
133
- -rw-r--r-- 1 root root 185 Sep 16 01:36 test编译的函数_@3bf4501e0408a243.cpp
134
- -rw-r--r-- 1 root root 151 Sep 16 01:36 test编译的函数_@3bf4501e0408a243.h
135
- -rwxr-xr-x 1 root root 15656 Sep 16 01:36 test编译的函数_@3bf4501e0408a243.so
136
-
137
- ```
138
- ## 5. test_add_@05ade4b088e9b383.cpp
139
- ```c++
140
- #include "test_add_@05ade4b088e9b383.h"
141
- extern "C" int64_t test_add (int64_t a, int64_t b)
142
- {
143
- if ((a > 1))
144
- {
145
- return a + b;
146
- }
147
-
148
- for (int64_t i = 1; i < 10; i += 2)
149
- {
150
- a = a + i;
151
- }
152
-
153
- for (auto i : {1,2,3})
154
- {
155
- a = a + i;
156
- }
157
-
158
- a = std::ceil(12.5);;
159
- std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
160
- cc[u8"c"] = 3;
161
- std::cout<< u8"输出map:" << " "<< std::endl;
162
- for (auto ii : cc)
163
- {
164
- std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
165
- }
166
-
167
- int64_t aa[] = {1,3,2};
168
- aa[0] = 134;
169
- std::cout<< u8"输出list:" << " "<< std::endl;
170
- for (int64_t i = 0; i < 3; ++i)
171
- {
172
- std::cout<< i << " "<< aa[i] << " "<< std::endl;
173
- }
174
-
175
- std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
176
- std::cout<< u8"test_other_fn" << " "<< test_other_fn(a,b) << " "<< std::endl;
177
- std::cout<< u8"test编译的函数" << " "<< function_74657374e7bc96e8af91e79a84e587bde695b0(a,b) << " "<< std::endl;
178
- auto v = 0;
179
- auto vv = true;
180
- while (vv)
181
- {
182
- std::cout << u8"请输入>>>"; std::cin >> v;
183
- if ((v > 100))
184
- {
185
- break;
186
- }
187
-
188
- {
189
- std::cout<< u8"输入的" << " "<< v << " "<< u8"小于等于100" << " "<< std::endl;
190
- }
191
-
192
- }
193
-
194
- return a + b + 1 + 123 + v;
195
- }
196
-
197
- ```
198
- ## 6. test_add_@05ade4b088e9b383.h
199
- ```c++
200
- #include "test_other_fn_@75fdd928ab58a8e3.h"
201
- #include "test编译的函数_@3bf4501e0408a243.h"
202
- #include <cmath>
203
- #include <cstdint>
204
- #include <iostream>
205
- #include <string>
206
- #include <unordered_map>
207
- extern "C" int64_t test_add (int64_t a, int64_t b);
208
- ```
209
- ## 7. test_other_fn_@75fdd928ab58a8e3.cpp
210
- ```c++
211
- #include "test_other_fn_@75fdd928ab58a8e3.h"
212
- extern "C" int64_t test_other_fn (int64_t a, int64_t b)
213
- {
214
- return a - b;
215
- }
216
-
217
- ```
218
- ## 8. test_other_fn_@75fdd928ab58a8e3.h
219
- ```c++
220
- #include <cstdint>
221
- #include <string>
222
- extern "C" int64_t test_other_fn (int64_t a, int64_t b);
223
- ```
224
- ## 9. test编译的函数_@3bf4501e0408a243.cpp
225
- ```c++
226
- #include "test编译的函数_@3bf4501e0408a243.h"
227
- extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b)
228
- {
229
- return a * b;
230
- }
231
-
232
- ```
233
- ## 10. test编译的函数_@3bf4501e0408a243.h
234
- ```c++
235
- #include <cstdint>
236
- #include <string>
237
- extern "C" int64_t /*test编译的函数*/ function_74657374e7bc96e8af91e79a84e587bde695b0 (int64_t a, int64_t b);
238
- ```
@@ -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=FBGvpIeRINR2QaI4sB6fnsHjAAurHTqk9cJBOzQ_4nk,23936
6
- l0n0lc/编译.py,sha256=Ty4gPDHAB1jn8LgnE4w3R06bOAEeYr8iwqahYf6QeHI,2154
7
- l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
8
- l0n0lc-0.8.3.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
9
- l0n0lc-0.8.3.dist-info/METADATA,sha256=e0DJP_v-J92RjhpLItzAeV_qAzcvEtxHEHBQqEwFbjo,5547
10
- l0n0lc-0.8.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
- l0n0lc-0.8.3.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
12
- l0n0lc-0.8.3.dist-info/RECORD,,
File without changes