l0n0lc 0.10.2__tar.gz → 0.10.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: l0n0lc
3
- Version: 0.10.2
3
+ Version: 0.10.4
4
4
  Summary: 一个将python函数翻译为c++函数并运行的jit编译器
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Requires-Python: >=3.10
@@ -63,6 +63,7 @@ import l0n0lc as lc
63
63
  import math
64
64
  import 测试可执行
65
65
 
66
+
66
67
  @lc.映射函数(math.ceil, ['<cmath>'])
67
68
  def cpp_ceil(v):
68
69
  return f'std::ceil({lc.toCString(v)});'
@@ -112,6 +113,12 @@ class CppVectorInt:
112
113
  return 0
113
114
 
114
115
 
116
+ @lc.映射类型('short')
117
+ class ShortInt:
118
+ def __init__(self, v) -> None:
119
+ pass
120
+
121
+
115
122
  @lc.jit(每次运行都重新编译=True)
116
123
  def jit_all_ops(a: int, b: int) -> int:
117
124
  # 常量与基础赋值
@@ -120,8 +127,10 @@ def jit_all_ops(a: int, b: int) -> int:
120
127
  z = 3.14
121
128
  flag = True
122
129
  nums = [1, 2, 3]
130
+ numsshorts = [ShortInt(1), ShortInt(2), ShortInt(3)]
123
131
  tup = (4, 5)
124
132
  mp = {1: 10, 2: 20}
133
+ mp2 = {ShortInt(1): 10, ShortInt(2): 20}
125
134
 
126
135
  # 一元运算
127
136
  pos = +(a + 1)
@@ -202,6 +211,7 @@ def jit_all_ops(a: int, b: int) -> int:
202
211
  print('vector->', i, '=', vector[i])
203
212
  return y
204
213
 
214
+
205
215
  @lc.jit(每次运行都重新编译=True)
206
216
  def test_add(a: int, b: int) -> int:
207
217
  if a > 1:
@@ -243,7 +253,6 @@ def test_add(a: int, b: int) -> int:
243
253
 
244
254
  print('结果:', test_add(1, 3))
245
255
 
246
-
247
256
  ```
248
257
 
249
258
  ## 4. 运行hello_world.py
@@ -282,24 +291,24 @@ vv: 1
282
291
  ## 5. 查看输出文件
283
292
  ```bash
284
293
  ls -al ./l0n0lcoutput
285
- total 164
286
- drwxr-xr-x 2 root root 4096 Oct 16 01:39 .
287
- drwxrwxrwx 11 1000 1000 4096 Oct 16 01:39 ..
288
- -rw-r--r-- 1 root root 298 Oct 16 01:39 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
289
- -rw-r--r-- 1 root root 128 Oct 16 01:39 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.h
290
- -rw-r--r-- 1 root root 1811 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.cpp
291
- -rw-r--r-- 1 root root 167 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h
292
- -rwxr-xr-x 1 root root 23584 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.so
293
- -rw-r--r-- 1 root root 1531 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.cpp
294
- -rw-r--r-- 1 root root 398 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
295
- -rwxr-xr-x 1 root root 40408 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.so
296
- -rw-r--r-- 1 root root 155 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.cpp
297
- -rw-r--r-- 1 root root 106 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h
298
- -rwxr-xr-x 1 root root 15648 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.so
299
- -rw-r--r-- 1 root root 219 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.cpp
300
- -rw-r--r-- 1 root root 164 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h
301
- -rwxr-xr-x 1 root root 15688 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.so
302
- -rwxr-xr-x 1 root root 17248 Oct 16 01:39 测试可执行文件
294
+ total 176
295
+ drwxr-xr-x 2 root root 4096 Oct 22 02:51 .
296
+ drwxrwxrwx 11 1000 1000 4096 Oct 22 02:51 ..
297
+ -rw-r--r-- 1 root root 298 Oct 22 02:51 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
298
+ -rw-r--r-- 1 root root 128 Oct 22 02:51 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.h
299
+ -rw-r--r-- 1 root root 1944 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.cpp
300
+ -rw-r--r-- 1 root root 167 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h
301
+ -rwxr-xr-x 1 root root 29000 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.so
302
+ -rw-r--r-- 1 root root 1531 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.cpp
303
+ -rw-r--r-- 1 root root 398 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
304
+ -rwxr-xr-x 1 root root 41728 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.so
305
+ -rw-r--r-- 1 root root 155 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.cpp
306
+ -rw-r--r-- 1 root root 106 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h
307
+ -rwxr-xr-x 1 root root 15648 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.so
308
+ -rw-r--r-- 1 root root 219 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.cpp
309
+ -rw-r--r-- 1 root root 164 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h
310
+ -rwxr-xr-x 1 root root 15688 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.so
311
+ -rwxr-xr-x 1 root root 17248 Oct 22 02:51 测试可执行文件
303
312
 
304
313
  ```
305
314
  ## 6. 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
@@ -325,9 +334,9 @@ extern "C" int /*可执行*/ main (int argc, char** argv)
325
334
  #include <string>
326
335
  extern "C" int /*可执行*/ main (int argc, char** argv);
327
336
  ```
328
- ## 8. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.cpp
337
+ ## 8. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.cpp
329
338
  ```c++
330
- #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h"
339
+ #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h"
331
340
  extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
332
341
  {
333
342
  auto x = 42;
@@ -335,8 +344,10 @@ extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
335
344
  auto z = 3.14;
336
345
  auto flag = true;
337
346
  int64_t nums[] = {1,2,3};
347
+ short numsshorts[] = {short(1),short(2),short(3)};
338
348
  int64_t tup[] = {4,5};
339
349
  std::unordered_map<int64_t, int64_t> mp = {{ 1, 10 },{ 2, 20 }};
350
+ std::unordered_map<short, int64_t> mp2 = {{ short(1), 10 },{ short(2), 20 }};
340
351
  auto pos = (+(a + 1));
341
352
  auto neg = (-b);
342
353
  auto inv = (~a);
@@ -422,7 +433,7 @@ extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
422
433
  }
423
434
 
424
435
  ```
425
- ## 9. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h
436
+ ## 9. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h
426
437
  ```c++
427
438
  #pragma once
428
439
  #include <cstdint>
@@ -500,7 +511,7 @@ extern "C" int64_t test_add (int64_t a, int64_t b)
500
511
  ## 11. c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
501
512
  ```c++
502
513
  #pragma once
503
- #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h"
514
+ #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h"
504
515
  #include "c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h"
505
516
  #include "c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h"
506
517
  #include <cmath>
@@ -53,6 +53,7 @@ import l0n0lc as lc
53
53
  import math
54
54
  import 测试可执行
55
55
 
56
+
56
57
  @lc.映射函数(math.ceil, ['<cmath>'])
57
58
  def cpp_ceil(v):
58
59
  return f'std::ceil({lc.toCString(v)});'
@@ -102,6 +103,12 @@ class CppVectorInt:
102
103
  return 0
103
104
 
104
105
 
106
+ @lc.映射类型('short')
107
+ class ShortInt:
108
+ def __init__(self, v) -> None:
109
+ pass
110
+
111
+
105
112
  @lc.jit(每次运行都重新编译=True)
106
113
  def jit_all_ops(a: int, b: int) -> int:
107
114
  # 常量与基础赋值
@@ -110,8 +117,10 @@ def jit_all_ops(a: int, b: int) -> int:
110
117
  z = 3.14
111
118
  flag = True
112
119
  nums = [1, 2, 3]
120
+ numsshorts = [ShortInt(1), ShortInt(2), ShortInt(3)]
113
121
  tup = (4, 5)
114
122
  mp = {1: 10, 2: 20}
123
+ mp2 = {ShortInt(1): 10, ShortInt(2): 20}
115
124
 
116
125
  # 一元运算
117
126
  pos = +(a + 1)
@@ -192,6 +201,7 @@ def jit_all_ops(a: int, b: int) -> int:
192
201
  print('vector->', i, '=', vector[i])
193
202
  return y
194
203
 
204
+
195
205
  @lc.jit(每次运行都重新编译=True)
196
206
  def test_add(a: int, b: int) -> int:
197
207
  if a > 1:
@@ -233,7 +243,6 @@ def test_add(a: int, b: int) -> int:
233
243
 
234
244
  print('结果:', test_add(1, 3))
235
245
 
236
-
237
246
  ```
238
247
 
239
248
  ## 4. 运行hello_world.py
@@ -272,24 +281,24 @@ vv: 1
272
281
  ## 5. 查看输出文件
273
282
  ```bash
274
283
  ls -al ./l0n0lcoutput
275
- total 164
276
- drwxr-xr-x 2 root root 4096 Oct 16 01:39 .
277
- drwxrwxrwx 11 1000 1000 4096 Oct 16 01:39 ..
278
- -rw-r--r-- 1 root root 298 Oct 16 01:39 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
279
- -rw-r--r-- 1 root root 128 Oct 16 01:39 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.h
280
- -rw-r--r-- 1 root root 1811 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.cpp
281
- -rw-r--r-- 1 root root 167 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h
282
- -rwxr-xr-x 1 root root 23584 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.so
283
- -rw-r--r-- 1 root root 1531 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.cpp
284
- -rw-r--r-- 1 root root 398 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
285
- -rwxr-xr-x 1 root root 40408 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.so
286
- -rw-r--r-- 1 root root 155 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.cpp
287
- -rw-r--r-- 1 root root 106 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h
288
- -rwxr-xr-x 1 root root 15648 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.so
289
- -rw-r--r-- 1 root root 219 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.cpp
290
- -rw-r--r-- 1 root root 164 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h
291
- -rwxr-xr-x 1 root root 15688 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.so
292
- -rwxr-xr-x 1 root root 17248 Oct 16 01:39 测试可执行文件
284
+ total 176
285
+ drwxr-xr-x 2 root root 4096 Oct 22 02:51 .
286
+ drwxrwxrwx 11 1000 1000 4096 Oct 22 02:51 ..
287
+ -rw-r--r-- 1 root root 298 Oct 22 02:51 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
288
+ -rw-r--r-- 1 root root 128 Oct 22 02:51 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.h
289
+ -rw-r--r-- 1 root root 1944 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.cpp
290
+ -rw-r--r-- 1 root root 167 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h
291
+ -rwxr-xr-x 1 root root 29000 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.so
292
+ -rw-r--r-- 1 root root 1531 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.cpp
293
+ -rw-r--r-- 1 root root 398 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
294
+ -rwxr-xr-x 1 root root 41728 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.so
295
+ -rw-r--r-- 1 root root 155 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.cpp
296
+ -rw-r--r-- 1 root root 106 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h
297
+ -rwxr-xr-x 1 root root 15648 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.so
298
+ -rw-r--r-- 1 root root 219 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.cpp
299
+ -rw-r--r-- 1 root root 164 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h
300
+ -rwxr-xr-x 1 root root 15688 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.so
301
+ -rwxr-xr-x 1 root root 17248 Oct 22 02:51 测试可执行文件
293
302
 
294
303
  ```
295
304
  ## 6. 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
@@ -315,9 +324,9 @@ extern "C" int /*可执行*/ main (int argc, char** argv)
315
324
  #include <string>
316
325
  extern "C" int /*可执行*/ main (int argc, char** argv);
317
326
  ```
318
- ## 8. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.cpp
327
+ ## 8. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.cpp
319
328
  ```c++
320
- #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h"
329
+ #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h"
321
330
  extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
322
331
  {
323
332
  auto x = 42;
@@ -325,8 +334,10 @@ extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
325
334
  auto z = 3.14;
326
335
  auto flag = true;
327
336
  int64_t nums[] = {1,2,3};
337
+ short numsshorts[] = {short(1),short(2),short(3)};
328
338
  int64_t tup[] = {4,5};
329
339
  std::unordered_map<int64_t, int64_t> mp = {{ 1, 10 },{ 2, 20 }};
340
+ std::unordered_map<short, int64_t> mp2 = {{ short(1), 10 },{ short(2), 20 }};
330
341
  auto pos = (+(a + 1));
331
342
  auto neg = (-b);
332
343
  auto inv = (~a);
@@ -412,7 +423,7 @@ extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
412
423
  }
413
424
 
414
425
  ```
415
- ## 9. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h
426
+ ## 9. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h
416
427
  ```c++
417
428
  #pragma once
418
429
  #include <cstdint>
@@ -490,7 +501,7 @@ extern "C" int64_t test_add (int64_t a, int64_t b)
490
501
  ## 11. c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
491
502
  ```c++
492
503
  #pragma once
493
- #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h"
504
+ #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h"
494
505
  #include "c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h"
495
506
  #include "c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h"
496
507
  #include <cmath>
@@ -29,6 +29,69 @@ class 指针:
29
29
  return f'{self.基础类型}*'
30
30
 
31
31
 
32
+ class 花括号:
33
+ def __init__(self, 编译器) -> None:
34
+ self.编译器 = 编译器
35
+
36
+ def __enter__(self, *args, **kwargs):
37
+ self.编译器.添加c代码('{')
38
+ self.编译器.进入新作用域()
39
+
40
+ def __exit__(self, *args, **kwargs):
41
+ self.编译器.退出作用域()
42
+ self.编译器.添加c代码('}\n')
43
+
44
+
45
+ class c代码:
46
+ def __init__(self, 代码: str, 层级: int) -> None:
47
+ self.代码 = 代码
48
+ self.层级 = 层级
49
+
50
+ def __str__(self) -> str:
51
+ return ' ' * self.层级 + self.代码
52
+
53
+
54
+ class c获取索引项目:
55
+ def __init__(self, 变量, 索引) -> None:
56
+ self.变量 = 变量
57
+ self.索引 = 索引
58
+
59
+ def __str__(self) -> str:
60
+ return f'{self.变量}[{toCString(self.索引)}]'
61
+
62
+
63
+ class c获取属性:
64
+ def __init__(self, 变量, 属性) -> None:
65
+ self.变量 = 变量
66
+ self.属性 = 属性
67
+
68
+ def __str__(self) -> str:
69
+ if isinstance(self.变量, c变量):
70
+ if self.变量.类型.endswith('*'):
71
+ return f'{self.变量}->{self.属性}'
72
+ return f'{self.变量}.{self.属性}'
73
+
74
+
75
+ class c函数调用:
76
+ def __init__(self, 函数名, 参数字符串, 返回的c类型=None) -> None:
77
+ self.函数名 = 函数名
78
+ self.参数字符串 = 参数字符串
79
+ self.返回的c类型 = 返回的c类型
80
+
81
+ def __str__(self) -> str:
82
+ return f'{self.函数名}({self.参数字符串})'
83
+
84
+
85
+ class c布尔:
86
+ def __init__(self, v) -> None:
87
+ self.v = v
88
+
89
+ def __str__(self) -> str:
90
+ if self.v:
91
+ return 'true'
92
+ return 'false'
93
+
94
+
32
95
  def 执行额外函数(函数列表: List, *args):
33
96
  for fn in 函数列表:
34
97
  ret = fn(*args)
@@ -68,7 +131,7 @@ py2c类型映射表 = {
68
131
  }
69
132
 
70
133
 
71
- def py类型转c类型(类型):
134
+ def py类型转c类型(类型, 类型实例=None):
72
135
  ret = 执行额外函数(额外py转c函数, 类型)
73
136
  if ret is not None:
74
137
  return ret
@@ -105,19 +168,28 @@ def py类型转c类型(类型):
105
168
  if isinstance(类型, str):
106
169
  return 类型
107
170
 
108
- return cpp类型.ANY
171
+ if 类型 is c布尔:
172
+ return cpp类型.BOOL
173
+
174
+ if isinstance(类型实例, c函数调用) and 类型实例.返回的c类型 is not None:
175
+ return 类型实例.返回的c类型
176
+
177
+ raise Exception("不支持的类型", 类型, 类型实例)
178
+
179
+
180
+ class 列表内类型不一异常(Exception):
181
+ pass
109
182
 
110
183
 
111
184
  class list初始化列表:
112
185
  def __init__(
113
186
  self,
114
187
  代码: str,
115
- 类型列表: Union[List[Union[int, float, bool, str]],
116
- int, float, bool, str],
188
+ 类型列表,
117
189
  长度: int) -> None:
118
190
  self.代码 = 代码
119
- self.类型列表 = 类型列表
120
- self.类型 = py类型转c类型(类型列表)
191
+ self.类型列表 = 类型列表[0]
192
+ self.类型 = py类型转c类型(*类型列表)
121
193
  self.长度 = 长度
122
194
 
123
195
  def __str__(self) -> str:
@@ -129,14 +201,16 @@ def 从list构建初始化列表(value: List):
129
201
  初始化列表 = []
130
202
  for v in value:
131
203
  dtype = type(v)
132
- 数据类型列表.append(dtype)
204
+ 数据类型列表.append((dtype, v))
133
205
  初始化列表.append(toCString(v))
134
206
  # 构建初始化列表
135
207
  初始化列表 = '{' + ','.join(初始化列表) + '}'
136
208
 
137
209
  # 构建类型列表
138
- if all(类型 == 数据类型列表[0] for 类型 in 数据类型列表):
210
+ if all(类型[0] == 数据类型列表[0][0] for 类型 in 数据类型列表):
139
211
  数据类型列表 = 数据类型列表[0]
212
+ else:
213
+ raise key类型不一异常
140
214
 
141
215
  return list初始化列表(初始化列表, 数据类型列表, len(value))
142
216
 
@@ -145,20 +219,26 @@ class dict初始化列表:
145
219
  def __init__(
146
220
  self,
147
221
  代码: str,
148
- key类型列表: Union[List[Union[int, float, bool, str]],
149
- int, float, bool, str],
150
- value类型列表: Union[List[Union[int, float, bool, str]],
151
- int, float, bool, str]) -> None:
222
+ key类型列表,
223
+ value类型列表) -> None:
152
224
  self.代码 = 代码
153
- self.key类型列表 = key类型列表
154
- self.value类型列表 = value类型列表
155
- self.key类型 = py类型转c类型(key类型列表)
156
- self.value类型 = py类型转c类型(value类型列表)
225
+ self.keyPy类型 = key类型列表[0]
226
+ self.valuePy类型 = value类型列表[0]
227
+ self.key类型 = py类型转c类型(*key类型列表)
228
+ self.value类型 = py类型转c类型(*value类型列表)
157
229
 
158
230
  def __str__(self) -> str:
159
231
  return self.代码
160
232
 
161
233
 
234
+ class key类型不一异常(Exception):
235
+ pass
236
+
237
+
238
+ class value类型不一异常(Exception):
239
+ pass
240
+
241
+
162
242
  def 从dict构建初始化列表(value: dict):
163
243
  code = []
164
244
  key类型列表 = []
@@ -172,10 +252,14 @@ def 从dict构建初始化列表(value: dict):
172
252
 
173
253
  # 构建类型列表
174
254
  if all(类型 == key类型列表[0] for 类型 in key类型列表):
175
- key类型列表 = key类型列表[0]
255
+ key类型列表 = (key类型列表[0], k)
256
+ else:
257
+ raise key类型不一异常
176
258
 
177
259
  if all(类型 == value类型列表[0] for 类型 in value类型列表):
178
- value类型列表 = value类型列表[0]
260
+ value类型列表 = (value类型列表[0], v)
261
+ else:
262
+ raise value类型不一异常
179
263
 
180
264
  # 构建初始化列表
181
265
  初始化列表 = '{' + ','.join(code) + '}'
@@ -15,70 +15,14 @@ class 代码异常抛出器:
15
15
  self.代码行 = 代码.split('\n')
16
16
 
17
17
  def __call__(self, 错误信息: str, node: ast.stmt | ast.expr | ast.arg) -> Any:
18
- raise Exception(
19
- f'{错误信息} {self.代码行[node.lineno][node.col_offset:node.end_col_offset]}')
20
-
21
-
22
- class 花括号:
23
- def __init__(self, 编译器) -> None:
24
- self.编译器: py2cpp编译器 = 编译器
25
-
26
- def __enter__(self, *args, **kwargs):
27
- self.编译器.添加c代码('{')
28
- self.编译器.进入新作用域()
29
-
30
- def __exit__(self, *args, **kwargs):
31
- self.编译器.退出作用域()
32
- self.编译器.添加c代码('}\n')
33
-
34
-
35
- class c代码:
36
- def __init__(self, 代码: str, 层级: int) -> None:
37
- self.代码 = 代码
38
- self.层级 = 层级
39
-
40
- def __str__(self) -> str:
41
- return ' ' * self.层级 + self.代码
42
-
43
-
44
- class c获取索引项目:
45
- def __init__(self, 变量, 索引) -> None:
46
- self.变量 = 变量
47
- self.索引 = 索引
48
-
49
- def __str__(self) -> str:
50
- return f'{self.变量}[{toCString(self.索引)}]'
51
-
52
-
53
- class c获取属性:
54
- def __init__(self, 变量, 属性) -> None:
55
- self.变量 = 变量
56
- self.属性 = 属性
57
-
58
- def __str__(self) -> str:
59
- if isinstance(self.变量, c变量):
60
- if self.变量.类型.endswith('*'):
61
- return f'{self.变量}->{self.属性}'
62
- return f'{self.变量}.{self.属性}'
63
-
64
-
65
- class c函数调用:
66
- def __init__(self, 函数名, 参数字符串) -> None:
67
- self.函数名 = 函数名
68
- self.参数字符串 = 参数字符串
69
-
70
- def __str__(self) -> str:
71
- return f'{self.函数名}({self.参数字符串})'
72
-
73
-
74
- class c布尔:
75
- def __init__(self, v) -> None:
76
- self.v = v
77
-
78
- def __str__(self) -> str:
79
- if self.v:
80
- return 'true'
81
- return 'false'
18
+ print(f'\033[31m[错误]:{错误信息}\033[0m')
19
+ print(f'错误代码:')
20
+ print('```python')
21
+ print(self.代码行[node.lineno - 1][:node.col_offset], end='')
22
+ print(
23
+ f'\033[31m{self.代码行[node.lineno - 1][node.col_offset: node.end_col_offset]}\033[0m', end='')
24
+ print(self.代码行[node.lineno - 1][node.end_col_offset:])
25
+ print('```')
82
26
 
83
27
 
84
28
  class py2cpp编译器(ast.NodeVisitor):
@@ -236,16 +180,28 @@ class py2cpp编译器(ast.NodeVisitor):
236
180
 
237
181
  if isinstance(value, ast.List):
238
182
  l = [self.获取值(e) for e in value.elts]
239
- return 从list构建初始化列表(l)
183
+ try:
184
+ return 从list构建初始化列表(l)
185
+ except:
186
+ self.抛出代码异常(str(l) + ' 类型不相同!', value)
240
187
  if isinstance(value, ast.Tuple):
241
188
  l = [self.获取值(e) for e in value.elts]
242
189
  if not self.正在构建参数:
243
- return 从list构建初始化列表(l)
190
+ try:
191
+ return 从list构建初始化列表(l)
192
+ except:
193
+ self.抛出代码异常(str(l) + ' 类型不相同!', value)
244
194
  return tuple(l)
245
195
  if isinstance(value, ast.Dict):
246
196
  d = {self.获取值(k): self.获取值(v)
247
197
  for k, v in zip(value.keys, value.values)}
248
- return 从dict构建初始化列表(d)
198
+ try:
199
+ return 从dict构建初始化列表(d)
200
+ except key类型不一异常:
201
+ self.抛出代码异常(str(d.keys()) + ' 类型不相同!', value)
202
+ except value类型不一异常:
203
+ self.抛出代码异常(str(d.values()) + ' 类型不相同!', value)
204
+ return
249
205
 
250
206
  if isinstance(value, ast.Call):
251
207
  return self.调用Call(value)
@@ -329,7 +285,7 @@ class py2cpp编译器(ast.NodeVisitor):
329
285
  for 库目录 in c类型.库目录:
330
286
  self.库目录.add(库目录)
331
287
  参数文本 = self.构建参数列表文本(node.args)
332
- return c函数调用(c类型, 参数文本)
288
+ return c函数调用(c类型, 参数文本, c类型.目标类型)
333
289
 
334
290
  # 直接调用函数
335
291
  if fn in 通用信息.直接调用函数:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: l0n0lc
3
- Version: 0.10.2
3
+ Version: 0.10.4
4
4
  Summary: 一个将python函数翻译为c++函数并运行的jit编译器
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Requires-Python: >=3.10
@@ -63,6 +63,7 @@ import l0n0lc as lc
63
63
  import math
64
64
  import 测试可执行
65
65
 
66
+
66
67
  @lc.映射函数(math.ceil, ['<cmath>'])
67
68
  def cpp_ceil(v):
68
69
  return f'std::ceil({lc.toCString(v)});'
@@ -112,6 +113,12 @@ class CppVectorInt:
112
113
  return 0
113
114
 
114
115
 
116
+ @lc.映射类型('short')
117
+ class ShortInt:
118
+ def __init__(self, v) -> None:
119
+ pass
120
+
121
+
115
122
  @lc.jit(每次运行都重新编译=True)
116
123
  def jit_all_ops(a: int, b: int) -> int:
117
124
  # 常量与基础赋值
@@ -120,8 +127,10 @@ def jit_all_ops(a: int, b: int) -> int:
120
127
  z = 3.14
121
128
  flag = True
122
129
  nums = [1, 2, 3]
130
+ numsshorts = [ShortInt(1), ShortInt(2), ShortInt(3)]
123
131
  tup = (4, 5)
124
132
  mp = {1: 10, 2: 20}
133
+ mp2 = {ShortInt(1): 10, ShortInt(2): 20}
125
134
 
126
135
  # 一元运算
127
136
  pos = +(a + 1)
@@ -202,6 +211,7 @@ def jit_all_ops(a: int, b: int) -> int:
202
211
  print('vector->', i, '=', vector[i])
203
212
  return y
204
213
 
214
+
205
215
  @lc.jit(每次运行都重新编译=True)
206
216
  def test_add(a: int, b: int) -> int:
207
217
  if a > 1:
@@ -243,7 +253,6 @@ def test_add(a: int, b: int) -> int:
243
253
 
244
254
  print('结果:', test_add(1, 3))
245
255
 
246
-
247
256
  ```
248
257
 
249
258
  ## 4. 运行hello_world.py
@@ -282,24 +291,24 @@ vv: 1
282
291
  ## 5. 查看输出文件
283
292
  ```bash
284
293
  ls -al ./l0n0lcoutput
285
- total 164
286
- drwxr-xr-x 2 root root 4096 Oct 16 01:39 .
287
- drwxrwxrwx 11 1000 1000 4096 Oct 16 01:39 ..
288
- -rw-r--r-- 1 root root 298 Oct 16 01:39 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
289
- -rw-r--r-- 1 root root 128 Oct 16 01:39 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.h
290
- -rw-r--r-- 1 root root 1811 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.cpp
291
- -rw-r--r-- 1 root root 167 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h
292
- -rwxr-xr-x 1 root root 23584 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.so
293
- -rw-r--r-- 1 root root 1531 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.cpp
294
- -rw-r--r-- 1 root root 398 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
295
- -rwxr-xr-x 1 root root 40408 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.so
296
- -rw-r--r-- 1 root root 155 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.cpp
297
- -rw-r--r-- 1 root root 106 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h
298
- -rwxr-xr-x 1 root root 15648 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.so
299
- -rw-r--r-- 1 root root 219 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.cpp
300
- -rw-r--r-- 1 root root 164 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h
301
- -rwxr-xr-x 1 root root 15688 Oct 16 01:39 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.so
302
- -rwxr-xr-x 1 root root 17248 Oct 16 01:39 测试可执行文件
294
+ total 176
295
+ drwxr-xr-x 2 root root 4096 Oct 22 02:51 .
296
+ drwxrwxrwx 11 1000 1000 4096 Oct 22 02:51 ..
297
+ -rw-r--r-- 1 root root 298 Oct 22 02:51 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
298
+ -rw-r--r-- 1 root root 128 Oct 22 02:51 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.h
299
+ -rw-r--r-- 1 root root 1944 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.cpp
300
+ -rw-r--r-- 1 root root 167 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h
301
+ -rwxr-xr-x 1 root root 29000 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.so
302
+ -rw-r--r-- 1 root root 1531 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.cpp
303
+ -rw-r--r-- 1 root root 398 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
304
+ -rwxr-xr-x 1 root root 41728 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.so
305
+ -rw-r--r-- 1 root root 155 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.cpp
306
+ -rw-r--r-- 1 root root 106 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h
307
+ -rwxr-xr-x 1 root root 15648 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.so
308
+ -rw-r--r-- 1 root root 219 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.cpp
309
+ -rw-r--r-- 1 root root 164 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h
310
+ -rwxr-xr-x 1 root root 15688 Oct 22 02:51 c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.so
311
+ -rwxr-xr-x 1 root root 17248 Oct 22 02:51 测试可执行文件
303
312
 
304
313
  ```
305
314
  ## 6. 6cbfb68e317eace4_测试可执行.py_可执行_@d82ed7910ac3268c.cpp
@@ -325,9 +334,9 @@ extern "C" int /*可执行*/ main (int argc, char** argv)
325
334
  #include <string>
326
335
  extern "C" int /*可执行*/ main (int argc, char** argv);
327
336
  ```
328
- ## 8. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.cpp
337
+ ## 8. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.cpp
329
338
  ```c++
330
- #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h"
339
+ #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h"
331
340
  extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
332
341
  {
333
342
  auto x = 42;
@@ -335,8 +344,10 @@ extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
335
344
  auto z = 3.14;
336
345
  auto flag = true;
337
346
  int64_t nums[] = {1,2,3};
347
+ short numsshorts[] = {short(1),short(2),short(3)};
338
348
  int64_t tup[] = {4,5};
339
349
  std::unordered_map<int64_t, int64_t> mp = {{ 1, 10 },{ 2, 20 }};
350
+ std::unordered_map<short, int64_t> mp2 = {{ short(1), 10 },{ short(2), 20 }};
340
351
  auto pos = (+(a + 1));
341
352
  auto neg = (-b);
342
353
  auto inv = (~a);
@@ -422,7 +433,7 @@ extern "C" int64_t jit_all_ops (int64_t a, int64_t b)
422
433
  }
423
434
 
424
435
  ```
425
- ## 9. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h
436
+ ## 9. c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h
426
437
  ```c++
427
438
  #pragma once
428
439
  #include <cstdint>
@@ -500,7 +511,7 @@ extern "C" int64_t test_add (int64_t a, int64_t b)
500
511
  ## 11. c31f68e99ed4ce3c_hello_world.py_test_add_@469de6acaaabc570.h
501
512
  ```c++
502
513
  #pragma once
503
- #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@7801528c3d61baf7.h"
514
+ #include "c31f68e99ed4ce3c_hello_world.py_jit_all_ops_@c636865fefd2fd71.h"
504
515
  #include "c31f68e99ed4ce3c_hello_world.py_test_other_fn_@75fdd928ab58a8e3.h"
505
516
  #include "c31f68e99ed4ce3c_hello_world.py_test编译的函数_@3bf4501e0408a243.h"
506
517
  #include <cmath>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "l0n0lc"
3
- version = "0.10.2"
3
+ version = "0.10.4"
4
4
  description = "一个将python函数翻译为c++函数并运行的jit编译器"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
File without changes
File without changes
File without changes
File without changes
File without changes