l0n0lc 0.3.2__py3-none-any.whl → 0.5.0__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.
@@ -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.0
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=zI6erRTE90chukkpovOCwAHXxFUI-SSaDvsmFKHwLYs,22718
6
+ l0n0lc/编译.py,sha256=f1ugdTpnKcDOprcGu2Q5zMm_sYdxWMphBeo4ZL-cW3o,2016
7
+ l0n0lc/通用.py,sha256=O_Y4bbW1WzHCJkibxAy9WGACv-aEZJLG-fEeVBcAbBQ,3649
8
+ l0n0lc-0.5.0.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
9
+ l0n0lc-0.5.0.dist-info/METADATA,sha256=bpEXrXY0TGG96jvvcscid62YkzZWcQcLMNH6KfruUDc,2429
10
+ l0n0lc-0.5.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ l0n0lc-0.5.0.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
12
+ l0n0lc-0.5.0.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")
@@ -1,209 +0,0 @@
1
-
2
- from .c类型基础 import c类型基础
3
-
4
- class i8(c类型基础):
5
- 类型名 = 'int8_t'
6
-
7
- class i8_p(c类型基础):
8
- 类型名 = f'int8_t*'
9
-
10
- class i8_p_p(c类型基础):
11
- 类型名 = f'int8_t**'
12
-
13
- class i16(c类型基础):
14
- 类型名 = 'int16_t'
15
-
16
- class i16_p(c类型基础):
17
- 类型名 = f'int16_t*'
18
-
19
- class i16_p_p(c类型基础):
20
- 类型名 = f'int16_t**'
21
-
22
- class i32(c类型基础):
23
- 类型名 = 'int32_t'
24
-
25
- class i32_p(c类型基础):
26
- 类型名 = f'int32_t*'
27
-
28
- class i32_p_p(c类型基础):
29
- 类型名 = f'int32_t**'
30
-
31
- class i64(c类型基础):
32
- 类型名 = 'int64_t'
33
-
34
- class i64_p(c类型基础):
35
- 类型名 = f'int64_t*'
36
-
37
- class i64_p_p(c类型基础):
38
- 类型名 = f'int64_t**'
39
-
40
- class u8(c类型基础):
41
- 类型名 = 'uint8_t'
42
-
43
- class u8_p(c类型基础):
44
- 类型名 = f'uint8_t*'
45
-
46
- class u8_p_p(c类型基础):
47
- 类型名 = f'uint8_t**'
48
-
49
- class u16(c类型基础):
50
- 类型名 = 'uint16_t'
51
-
52
- class u16_p(c类型基础):
53
- 类型名 = f'uint16_t*'
54
-
55
- class u16_p_p(c类型基础):
56
- 类型名 = f'uint16_t**'
57
-
58
- class u32(c类型基础):
59
- 类型名 = 'uint32_t'
60
-
61
- class u32_p(c类型基础):
62
- 类型名 = f'uint32_t*'
63
-
64
- class u32_p_p(c类型基础):
65
- 类型名 = f'uint32_t**'
66
-
67
- class u64(c类型基础):
68
- 类型名 = 'uint64_t'
69
-
70
- class u64_p(c类型基础):
71
- 类型名 = f'uint64_t*'
72
-
73
- class u64_p_p(c类型基础):
74
- 类型名 = f'uint64_t**'
75
-
76
- class bf16(c类型基础):
77
- 类型名 = 'bfloat16_t'
78
-
79
- class bf16_p(c类型基础):
80
- 类型名 = f'bfloat16_t*'
81
-
82
- class bf16_p_p(c类型基础):
83
- 类型名 = f'bfloat16_t**'
84
-
85
- class f16(c类型基础):
86
- 类型名 = 'half'
87
-
88
- class f16_p(c类型基础):
89
- 类型名 = f'half*'
90
-
91
- class f16_p_p(c类型基础):
92
- 类型名 = f'half**'
93
-
94
- class f32(c类型基础):
95
- 类型名 = 'float'
96
-
97
- class f32_p(c类型基础):
98
- 类型名 = f'float*'
99
-
100
- class f32_p_p(c类型基础):
101
- 类型名 = f'float**'
102
-
103
- class f64(c类型基础):
104
- 类型名 = 'double'
105
-
106
- class f64_p(c类型基础):
107
- 类型名 = f'double*'
108
-
109
- class f64_p_p(c类型基础):
110
- 类型名 = f'double**'
111
-
112
- class boolean(c类型基础):
113
- 类型名 = 'bool'
114
-
115
- class boolean_p(c类型基础):
116
- 类型名 = f'bool*'
117
-
118
- class boolean_p_p(c类型基础):
119
- 类型名 = f'bool**'
120
-
121
- class char(c类型基础):
122
- 类型名 = 'char'
123
-
124
- class char_p(c类型基础):
125
- 类型名 = f'char*'
126
-
127
- class char_p_p(c类型基础):
128
- 类型名 = f'char**'
129
-
130
- class uchar(c类型基础):
131
- 类型名 = 'unsigned char'
132
-
133
- class uchar_p(c类型基础):
134
- 类型名 = f'unsigned char*'
135
-
136
- class uchar_p_p(c类型基础):
137
- 类型名 = f'unsigned char**'
138
-
139
- class int(c类型基础):
140
- 类型名 = 'int'
141
-
142
- class int_p(c类型基础):
143
- 类型名 = f'int*'
144
-
145
- class int_p_p(c类型基础):
146
- 类型名 = f'int**'
147
-
148
- class uint(c类型基础):
149
- 类型名 = 'unsigned int'
150
-
151
- class uint_p(c类型基础):
152
- 类型名 = f'unsigned int*'
153
-
154
- class uint_p_p(c类型基础):
155
- 类型名 = f'unsigned int**'
156
-
157
- class short(c类型基础):
158
- 类型名 = 'short'
159
-
160
- class short_p(c类型基础):
161
- 类型名 = f'short*'
162
-
163
- class short_p_p(c类型基础):
164
- 类型名 = f'short**'
165
-
166
- class ushort(c类型基础):
167
- 类型名 = 'unsigned short'
168
-
169
- class ushort_p(c类型基础):
170
- 类型名 = f'unsigned short*'
171
-
172
- class ushort_p_p(c类型基础):
173
- 类型名 = f'unsigned short**'
174
-
175
- class long(c类型基础):
176
- 类型名 = 'long'
177
-
178
- class long_p(c类型基础):
179
- 类型名 = f'long*'
180
-
181
- class long_p_p(c类型基础):
182
- 类型名 = f'long**'
183
-
184
- class ulong(c类型基础):
185
- 类型名 = 'unsigned long'
186
-
187
- class ulong_p(c类型基础):
188
- 类型名 = f'unsigned long*'
189
-
190
- class ulong_p_p(c类型基础):
191
- 类型名 = f'unsigned long**'
192
-
193
- class longlong(c类型基础):
194
- 类型名 = 'long long'
195
-
196
- class longlong_p(c类型基础):
197
- 类型名 = f'long long*'
198
-
199
- class longlong_p_p(c类型基础):
200
- 类型名 = f'long long**'
201
-
202
- class ulonglong(c类型基础):
203
- 类型名 = 'unsigned long long'
204
-
205
- class ulonglong_p(c类型基础):
206
- 类型名 = f'unsigned long long*'
207
-
208
- class ulonglong_p_p(c类型基础):
209
- 类型名 = f'unsigned long long**'