l0n0lc 0.4.0__py3-none-any.whl → 0.5.1__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 +24 -0
- l0n0lc/StdMap.py +21 -0
- l0n0lc/__init__.py +6 -6
- l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py +307 -0
- l0n0lc/jit.py +570 -0
- l0n0lc//347/274/226/350/257/221.py +2 -1
- l0n0lc//351/200/232/347/224/250.py +134 -0
- l0n0lc-0.5.1.dist-info/METADATA +120 -0
- l0n0lc-0.5.1.dist-info/RECORD +12 -0
- l0n0lc/c/345/237/237.py +0 -152
- l0n0lc/c/345/237/272/347/241/200/347/261/273/345/236/213.py +0 -670
- l0n0lc/c/347/261/273/345/236/213/345/237/272/347/241/200.py +0 -296
- l0n0lc/c/350/257/255/345/217/245.py +0 -73
- l0n0lc/utils.py +0 -50
- l0n0lc//350/276/223/345/207/272.py +0 -17
- l0n0lc-0.4.0.dist-info/METADATA +0 -79
- l0n0lc-0.4.0.dist-info/RECORD +0 -13
- {l0n0lc-0.4.0.dist-info → l0n0lc-0.5.1.dist-info}/WHEEL +0 -0
- {l0n0lc-0.4.0.dist-info → l0n0lc-0.5.1.dist-info}/licenses/LICENSE +0 -0
- {l0n0lc-0.4.0.dist-info → l0n0lc-0.5.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import os
|
|
3
|
+
import inspect
|
|
4
|
+
from typing import List, Optional, Any, Dict
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def 变量名(var):
|
|
8
|
+
frame = inspect.currentframe()
|
|
9
|
+
if frame is None:
|
|
10
|
+
return
|
|
11
|
+
frame = frame.f_back
|
|
12
|
+
if frame is None:
|
|
13
|
+
return
|
|
14
|
+
for name, value in frame.f_locals.items():
|
|
15
|
+
if value is var:
|
|
16
|
+
return name
|
|
17
|
+
for name, value in frame.f_globals.items():
|
|
18
|
+
if value is var:
|
|
19
|
+
return name
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def 上层变量名(var):
|
|
24
|
+
frame = inspect.currentframe()
|
|
25
|
+
if frame is None:
|
|
26
|
+
return
|
|
27
|
+
frame = frame.f_back
|
|
28
|
+
if frame is None:
|
|
29
|
+
return
|
|
30
|
+
counter = 0
|
|
31
|
+
for name, value in frame.f_locals.items():
|
|
32
|
+
if value is var:
|
|
33
|
+
counter += 1
|
|
34
|
+
if counter > 1:
|
|
35
|
+
return name
|
|
36
|
+
for name, value in frame.f_globals.items():
|
|
37
|
+
if value is var:
|
|
38
|
+
counter += 1
|
|
39
|
+
if counter > 1:
|
|
40
|
+
return name
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def 十进制转换为其他进制(数值: int, 进制: int, digits="0123456789ABCDEF"):
|
|
45
|
+
if 数值 == 0:
|
|
46
|
+
return "0"
|
|
47
|
+
result = ""
|
|
48
|
+
is_negative = 数值 < 0
|
|
49
|
+
数值 = abs(数值)
|
|
50
|
+
while 数值 > 0:
|
|
51
|
+
数值, remainder = divmod(数值, 进制) # 除基取余
|
|
52
|
+
result = digits[remainder] + result # 余数映射字符
|
|
53
|
+
return ("-" if is_negative else "") + result
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class c函数映射:
|
|
57
|
+
def __init__(
|
|
58
|
+
self, 目标函数,
|
|
59
|
+
include目录: Optional[List[str]],
|
|
60
|
+
库列表: Optional[List[str]],
|
|
61
|
+
库目录: Optional[List[str]]) -> None:
|
|
62
|
+
self.目标函数 = 目标函数
|
|
63
|
+
self.include目录 = include目录 or []
|
|
64
|
+
self.库列表 = 库列表 or []
|
|
65
|
+
self.库目录 = 库目录 or []
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class 通用信息:
|
|
69
|
+
直接调用函数 = set()
|
|
70
|
+
函数映射表: Dict[Any, c函数映射] = {}
|
|
71
|
+
include表 = set()
|
|
72
|
+
连接库表 = set()
|
|
73
|
+
变量最大ID = 0
|
|
74
|
+
python内置映射 = {}
|
|
75
|
+
使用unicode编程 = True
|
|
76
|
+
工作文件夹地址 = './l0n0lcoutput'
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def 缓存接调用函数():
|
|
80
|
+
通用信息.直接调用函数.add(range)
|
|
81
|
+
|
|
82
|
+
@staticmethod
|
|
83
|
+
def 添加内置映射(v):
|
|
84
|
+
通用信息.python内置映射[v.__name__] = v
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def 内置映射():
|
|
88
|
+
for v in [int, float, str, bool, range, complex, set, tuple, list, dict,
|
|
89
|
+
print, input, abs, round, pow, divmod, sum, min, max,
|
|
90
|
+
isinstance, len, open, ]:
|
|
91
|
+
通用信息.添加内置映射(v)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
通用信息.内置映射()
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def 直接调用函数(fn):
|
|
98
|
+
通用信息.直接调用函数.add(fn)
|
|
99
|
+
return fn
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def 映射函数(
|
|
103
|
+
被映射函数,
|
|
104
|
+
include目录: Optional[List[str]] = None,
|
|
105
|
+
链接库列表: Optional[List[str]] = None,
|
|
106
|
+
库目录列表: Optional[List[str]] = None):
|
|
107
|
+
def 装饰器(映射目标):
|
|
108
|
+
通用信息.函数映射表[被映射函数] = c函数映射(映射目标, include目录, 链接库列表, 库目录列表)
|
|
109
|
+
return 映射目标
|
|
110
|
+
return 装饰器
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def 有非英文变量字符(s):
|
|
114
|
+
return bool(re.search(r'[^A-Za-z0-9_]', s))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def 生成变量Id(原始名字: str | None = None):
|
|
118
|
+
if 原始名字 is not None and (通用信息.使用unicode编程 or not 有非英文变量字符(原始名字)):
|
|
119
|
+
return 原始名字
|
|
120
|
+
ret = f'_{通用信息.变量最大ID}'
|
|
121
|
+
通用信息.变量最大ID += 1
|
|
122
|
+
return ret
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def 尝试创建文件夹(文件夹名: str):
|
|
126
|
+
if os.path.exists(文件夹名):
|
|
127
|
+
return
|
|
128
|
+
os.mkdir(文件夹名)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def toCString(v):
|
|
132
|
+
if isinstance(v, str):
|
|
133
|
+
return f'u8"{v}"'
|
|
134
|
+
return str(v)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: l0n0lc
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: 用python写c
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: numpy>=1.26.0
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# 将python函数翻译为c++函数并运行
|
|
13
|
+
|
|
14
|
+
## hello_world.py
|
|
15
|
+
```python
|
|
16
|
+
import l0n0lc as lc
|
|
17
|
+
import math
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@lc.映射函数(math.ceil, ['<cmath>'])
|
|
21
|
+
def cpp_ceil(v):
|
|
22
|
+
return f'std::ceil({lc.toCString(v)});'
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@lc.映射函数(print, ['<iostream>'])
|
|
26
|
+
def cpp_cout(*args):
|
|
27
|
+
code = f'std::cout'
|
|
28
|
+
for arg in args:
|
|
29
|
+
code += f'<< {lc.toCString(arg)} << " "'
|
|
30
|
+
code += '<< std::endl;'
|
|
31
|
+
return code
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@lc.jit(每次运行都重新编译=True)
|
|
35
|
+
def test_add(a: int, b: int) -> int:
|
|
36
|
+
if a > 1:
|
|
37
|
+
return a + b
|
|
38
|
+
for i in range(1, 10, 2):
|
|
39
|
+
a += i
|
|
40
|
+
for i in [1, 2, 3]:
|
|
41
|
+
a += i
|
|
42
|
+
a = math.ceil(12.5)
|
|
43
|
+
cc = {'a': 1, 'b': 2}
|
|
44
|
+
cc['123'] = 111
|
|
45
|
+
aa = [1, 3, 2]
|
|
46
|
+
aa[0] = 134
|
|
47
|
+
for i in range(3):
|
|
48
|
+
print(i, aa[i])
|
|
49
|
+
print('Hello World', a, b)
|
|
50
|
+
return a + b + 1
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
print(test_add(1, 3))
|
|
54
|
+
```
|
|
55
|
+
## 执行hello_world.py
|
|
56
|
+
```bash
|
|
57
|
+
$ python hello_world.py
|
|
58
|
+
0 134
|
|
59
|
+
1 3
|
|
60
|
+
2 2
|
|
61
|
+
Hello World 13 3
|
|
62
|
+
17
|
|
63
|
+
```
|
|
64
|
+
## 查看生成的c++代码文件
|
|
65
|
+
```bash
|
|
66
|
+
$ ls -al l0n0lcoutput
|
|
67
|
+
total 44
|
|
68
|
+
drwxr-xr-x 2 root root 4096 Sep 11 01:38 .
|
|
69
|
+
drwxrwxrwx 12 1000 1000 4096 Sep 10 02:36 ..
|
|
70
|
+
-rw-r--r-- 1 root root 599 Sep 11 01:38 test_add_@402bbf73254216d8.cpp
|
|
71
|
+
-rw-r--r-- 1 root root 150 Sep 11 01:38 test_add_@402bbf73254216d8.h
|
|
72
|
+
-rwxr-xr-x 1 root root 25264 Sep 11 01:38 test_add_@402bbf73254216d8.so
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## test_add_@402bbf73254216d8.h
|
|
76
|
+
|
|
77
|
+
```c++
|
|
78
|
+
#include <cmath>
|
|
79
|
+
#include <cstdint>
|
|
80
|
+
#include <iostream>
|
|
81
|
+
#include <string>
|
|
82
|
+
#include <unordered_map>
|
|
83
|
+
extern "C" int64_t test_add (int64_t a, int64_t b);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## test_add_@402bbf73254216d8.cpp
|
|
87
|
+
```c++
|
|
88
|
+
#include "test_add_@402bbf73254216d8.h"
|
|
89
|
+
extern "C" int64_t test_add (int64_t a, int64_t b)
|
|
90
|
+
{
|
|
91
|
+
if ((a > 1))
|
|
92
|
+
{
|
|
93
|
+
return a + b;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (int64_t i = 1; i < 10; i += 2)
|
|
97
|
+
{
|
|
98
|
+
a = a + i;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (auto i : {1,2,3})
|
|
102
|
+
{
|
|
103
|
+
a = a + i;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
a = std::ceil(12.5);;
|
|
107
|
+
std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
|
|
108
|
+
cc[u8"123"] = 111;
|
|
109
|
+
int64_t aa[] = {1,3,2};
|
|
110
|
+
aa[0] = 134;
|
|
111
|
+
for (int64_t i = 0; i < 3; ++i)
|
|
112
|
+
{
|
|
113
|
+
std::cout<< i << " "<< aa[i] << " "<< std::endl;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
|
|
117
|
+
return a + b + 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
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=ZJEMxK1MyRAgbUnpfUUZC0ZHj61RCD6UMlhVssCyiCE,8201
|
|
5
|
+
l0n0lc/jit.py,sha256=N4R5KDDhRnn0eAmNI_8wOWgiCSdi3D8CP_RogXbj4as,22730
|
|
6
|
+
l0n0lc/编译.py,sha256=f1ugdTpnKcDOprcGu2Q5zMm_sYdxWMphBeo4ZL-cW3o,2016
|
|
7
|
+
l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
|
|
8
|
+
l0n0lc-0.5.1.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
|
|
9
|
+
l0n0lc-0.5.1.dist-info/METADATA,sha256=soTH6jJhHdlThj4Ngiapf85P-25SD4OYRfBiaCEWxKA,2429
|
|
10
|
+
l0n0lc-0.5.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
11
|
+
l0n0lc-0.5.1.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
|
|
12
|
+
l0n0lc-0.5.1.dist-info/RECORD,,
|
l0n0lc/c/345/237/237.py
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
from typing import List, Union
|
|
2
|
-
from .输出 import 输出函数
|
|
3
|
-
import types
|
|
4
|
-
当前域 = []
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def 进入域(域):
|
|
8
|
-
当前域.append(域)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def 退出域():
|
|
12
|
-
当前域.pop(-1)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def 输出(内容: str, 输出前缀: bool = True):
|
|
16
|
-
if 输出前缀:
|
|
17
|
-
for i in range(len(当前域)):
|
|
18
|
-
输出函数(' ')
|
|
19
|
-
输出函数(内容)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class 花括号:
|
|
23
|
-
def __enter__(self, *args, **kwargs):
|
|
24
|
-
输出("{\n")
|
|
25
|
-
进入域(self)
|
|
26
|
-
|
|
27
|
-
def __exit__(self, *args, **kwargs):
|
|
28
|
-
退出域()
|
|
29
|
-
输出("}\n")
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class c函数域:
|
|
33
|
-
def __init__(
|
|
34
|
-
self,
|
|
35
|
-
函数名: str,
|
|
36
|
-
参数名及类型,
|
|
37
|
-
返回类型,
|
|
38
|
-
前缀: Union[None, str, List[str]] = None) -> None:
|
|
39
|
-
self.函数名 = 函数名
|
|
40
|
-
self.参数名及类型 = 参数名及类型
|
|
41
|
-
try:
|
|
42
|
-
self.返回类型 = getattr(返回类型, '类型名')
|
|
43
|
-
except:
|
|
44
|
-
self.返回类型 = None
|
|
45
|
-
if isinstance(前缀, str):
|
|
46
|
-
self.前缀 = 前缀
|
|
47
|
-
elif isinstance(前缀, list):
|
|
48
|
-
self.前缀 = ' '.join(前缀)
|
|
49
|
-
else:
|
|
50
|
-
self.前缀 = ''
|
|
51
|
-
|
|
52
|
-
def __enter__(self, *args, **kwargs):
|
|
53
|
-
输出(self.前缀)
|
|
54
|
-
输出(' ')
|
|
55
|
-
if self.返回类型 is None:
|
|
56
|
-
输出('void ')
|
|
57
|
-
else:
|
|
58
|
-
输出(self.返回类型)
|
|
59
|
-
输出(' ', False)
|
|
60
|
-
输出(self.函数名, False)
|
|
61
|
-
输出('(', False)
|
|
62
|
-
参数数量 = len(self.参数名及类型.keys())
|
|
63
|
-
i = 0
|
|
64
|
-
for k, v in self.参数名及类型.items():
|
|
65
|
-
if k == 'self':
|
|
66
|
-
i += 1
|
|
67
|
-
continue
|
|
68
|
-
输出(f'{v.annotation.类型名} {k}', False)
|
|
69
|
-
if i < 参数数量 - 1:
|
|
70
|
-
输出(', ', False)
|
|
71
|
-
i += 1
|
|
72
|
-
输出(')', False)
|
|
73
|
-
输出("{\n", False)
|
|
74
|
-
进入域(self)
|
|
75
|
-
return self
|
|
76
|
-
|
|
77
|
-
def __exit__(self, *args, **kwargs):
|
|
78
|
-
退出域()
|
|
79
|
-
输出("}\n")
|
|
80
|
-
|
|
81
|
-
def 返回(self, 返回值):
|
|
82
|
-
输出(f'return {str(返回值)};\n')
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
class c结构体域:
|
|
86
|
-
def __init__(self, 类型名: str) -> None:
|
|
87
|
-
self.类型名 = 类型名
|
|
88
|
-
|
|
89
|
-
def __enter__(self, *args, **kwargs):
|
|
90
|
-
输出(f"struct {self.类型名}")
|
|
91
|
-
输出("{\n")
|
|
92
|
-
进入域(self)
|
|
93
|
-
|
|
94
|
-
def __exit__(self, *args, **kwargs):
|
|
95
|
-
退出域()
|
|
96
|
-
输出("};\n")
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
class 如果:
|
|
100
|
-
def __init__(self, 条件) -> types.NoneType:
|
|
101
|
-
self.条件 = 条件
|
|
102
|
-
|
|
103
|
-
def __enter__(self, *args, **kwargs):
|
|
104
|
-
输出(f"if ({self.条件}){{\n")
|
|
105
|
-
进入域(self)
|
|
106
|
-
|
|
107
|
-
def __exit__(self, *args, **kwargs):
|
|
108
|
-
退出域()
|
|
109
|
-
输出("}\n")
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
class 否则:
|
|
113
|
-
def __enter__(self, *args, **kwargs):
|
|
114
|
-
输出("else {\n")
|
|
115
|
-
进入域(self)
|
|
116
|
-
|
|
117
|
-
def __exit__(self, *args, **kwargs):
|
|
118
|
-
退出域()
|
|
119
|
-
输出("}\n")
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
class 否则如果:
|
|
123
|
-
def __init__(self, 条件) -> types.NoneType:
|
|
124
|
-
self.条件 = 条件
|
|
125
|
-
|
|
126
|
-
def __enter__(self, *args, **kwargs):
|
|
127
|
-
输出(f"else if ({self.条件}){{\n")
|
|
128
|
-
进入域(self)
|
|
129
|
-
|
|
130
|
-
def __exit__(self, *args, **kwargs):
|
|
131
|
-
退出域()
|
|
132
|
-
输出("}\n")
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
class 循环:
|
|
136
|
-
def __init__(self, 条件) -> types.NoneType:
|
|
137
|
-
self.条件 = 条件
|
|
138
|
-
|
|
139
|
-
def __enter__(self, *args, **kwargs):
|
|
140
|
-
输出(f"while ({self.条件}){{\n")
|
|
141
|
-
进入域(self)
|
|
142
|
-
return self
|
|
143
|
-
|
|
144
|
-
def __exit__(self, *args, **kwargs):
|
|
145
|
-
退出域()
|
|
146
|
-
输出("}\n")
|
|
147
|
-
|
|
148
|
-
def 继续(self):
|
|
149
|
-
输出("continue;\n")
|
|
150
|
-
|
|
151
|
-
def 跳出(self):
|
|
152
|
-
输出("break;\n")
|