l0n0lc 0.4.0__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.
@@ -1,296 +0,0 @@
1
- from .utils import 十进制转换为其他进制
2
- from typing import Union, Any, Callable, Type, List
3
- from .c域 import 输出, c函数域, c结构体域
4
- import inspect
5
-
6
-
7
- class c类型基础:
8
- _进制字符 = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"
9
- 变量自增ID = 0
10
- 类型名 = ''
11
- 是结构体类 = False
12
-
13
- @staticmethod
14
- def 生成ID():
15
- ID = '_' + 十进制转换为其他进制(c类型基础.变量自增ID, len(c类型基础._进制字符), c类型基础._进制字符)
16
- c类型基础.变量自增ID += 1
17
- return ID
18
-
19
- def __init__(
20
- self,
21
- 初始值: Union[Any, None] = '',
22
- 变量名: Union[str, None] = None,
23
- 是静态变量: Union[bool, None] = False,
24
- 使用等号赋值: bool = False):
25
- self.类型 = self.__class__.类型名
26
- self.变量名 = 变量名 or c类型基础.生成ID()
27
- self.是静态变量 = 是静态变量 # TODO
28
- self.使用等号赋值 = 使用等号赋值
29
- self.初始值 = 初始值
30
- self.生成初始值字符串()
31
- self.输出定义()
32
-
33
- def 类型名称(self):
34
- return f'{self.类型}'
35
-
36
- def 生成初始值字符串(self):
37
- self.初始化字符串 = f'({self.初始值})'
38
- if self.使用等号赋值:
39
- self.初始化字符串 = f'= {self.初始化字符串}'
40
- if isinstance(self.初始值, str) and len(self.初始值) == 0:
41
- self.初始化字符串 = ''
42
-
43
- def 输出定义(self):
44
- 输出(f'{self.类型名称()} {self.变量名} {self.初始化字符串};\n')
45
-
46
- def 赋值为(self, new_value):
47
- 输出(f'{self.变量名} = ({new_value});\n')
48
-
49
- def 获取成员值(self, 字段名: str):
50
- if self.__class__.类型名.strip().endswith('*'):
51
- return f'{self.变量名}->{字段名}'
52
- else:
53
- return f'{self.变量名}.{字段名}'
54
-
55
- def 成员赋值(self, 字段名: str, 数值):
56
- if self.__class__.类型名.strip().endswith('*'):
57
- 输出(f'{self.变量名}->{字段名} = {数值};\n')
58
- else:
59
- 输出(f'{self.变量名}.{字段名} = {数值};\n')
60
-
61
- def __str__(self) -> str:
62
- return self.变量名
63
-
64
- def 取址(self):
65
- return f'(&{self.变量名})'
66
-
67
- def 析址(self):
68
- return f'(*{self.变量名})'
69
-
70
- def __add__(self, other):
71
- return f'{self} + {other}'
72
-
73
- def __sub__(self, other):
74
- return f'{self} - {other}'
75
-
76
- def __mul__(self, other):
77
- return f'{self} * {other}'
78
-
79
- def __truediv__(self, other):
80
- return f'{self} / {other}'
81
-
82
- def __mod__(self, other):
83
- return f'{self} % {other}'
84
-
85
- def __lshift__(self, other):
86
- return f'{self} << {other}'
87
-
88
- def __rshift__(self, other):
89
- return f'{self} >> {other}'
90
-
91
- def __and__(self, other):
92
- return f'{self} & {other}'
93
-
94
- def __or__(self, other):
95
- return f'{self} | {other}'
96
-
97
- def __xor__(self, other):
98
- return f'{self} ^ {other}'
99
-
100
- def __iadd__(self, other):
101
- 输出(f'{self} += {other};\n')
102
- return self
103
-
104
- def __isub__(self, other):
105
- 输出(f'{self} -= {other}\n')
106
- return self
107
-
108
- def __imul__(self, other):
109
- 输出(f'{self} *= {other}\n')
110
- return self
111
-
112
- def __itruediv__(self, other):
113
- 输出(f'{self} /= {other}\n')
114
- return self
115
-
116
- def __imod__(self, other):
117
- 输出(f'{self} %= {other}\n')
118
- return self
119
-
120
- def __ilshift__(self, other):
121
- 输出(f'{self} <<= {other}\n')
122
- return self
123
-
124
- def __irshift__(self, other):
125
- 输出(f'{self} >>= {other}\n')
126
- return self
127
-
128
- def __iand__(self, other):
129
- 输出(f'{self} &= {other}\n')
130
- return self
131
-
132
- def __ior__(self, other):
133
- 输出(f'{self} |= {other}\n')
134
- return self
135
-
136
- def __ixor__(self, other):
137
- 输出(f'{self} ^= {other}\n')
138
- return self
139
-
140
- def __invert__(self):
141
- return f'~{self}'
142
-
143
- def __getitem__(self, key):
144
- return f'{self}[{key}]'
145
-
146
- def __setitem__(self, key, value):
147
- 输出(f'{self}[{key}] = {value};\n')
148
-
149
- def __call__(self, *args: Any) -> Any:
150
- if self.__class__.是结构体类:
151
- if len(args) == 0:
152
- return self.赋值为
153
- if len(args) == 1:
154
- return self.获取成员值(args[0])
155
- if len(args) == 2:
156
- self.成员赋值(args[0], args[1])
157
- return self
158
-
159
- self.赋值为(args[0])
160
-
161
- def 等于(self, other):
162
- return f'{self} == {other}'
163
-
164
- def 不等于(self, other):
165
- return f'{self} != {other}'
166
-
167
- def 大于(self, other):
168
- return f'{self} > {other}'
169
-
170
- def 小于(self, other):
171
- return f'{self} < {other}'
172
-
173
- def 大于等于(self, other):
174
- return f'{self} >= {other}'
175
-
176
- def 小于等于(self, other):
177
- return f'{self} <= {other}'
178
-
179
- def 强转(self, 类型):
180
- return f'(({类型.类型名}){self})'
181
-
182
-
183
- def 创建普通类(类型: str) -> Type:
184
- class 类(c类型基础):
185
- 类型名 = 类型
186
- return 类
187
-
188
-
189
- class c_void(c类型基础):
190
- 类型名 = 'void'
191
-
192
- def __init__(self):
193
- self.类型 = self.__class__.类型名
194
- self.变量名 = ''
195
-
196
- def 输出定义(self):
197
- pass
198
-
199
- def 赋值为(self, new_value):
200
- pass
201
-
202
-
203
- class c_void_p(c类型基础):
204
- 类型名 = 'void*'
205
-
206
-
207
- class 已存在变量(c类型基础):
208
- def __init__(self, 变量名: str) -> None:
209
- self.变量名 = 变量名
210
-
211
-
212
- def c结构体():
213
- def 装饰函数(fn: Callable):
214
- 定义 = fn()
215
- with c结构体域(fn.__name__):
216
- for 字段名, (类型, 默认值) in 定义.items():
217
- 类型(默认值, 变量名=字段名, 使用等号赋值=True)
218
-
219
- class 结构体类(c类型基础):
220
- 类型名 = fn.__name__
221
- 是结构体类 = True
222
- return 结构体类
223
- return 装饰函数
224
-
225
-
226
- def 根据signature构建并调用函数(
227
- fn: Callable,
228
- self_=None,
229
- 前缀: Union[None, str, List[str]] = None):
230
- sig = inspect.signature(fn)
231
- with c函数域(fn.__name__, sig.parameters,
232
- sig.return_annotation, 前缀) as f:
233
- 参数列表 = []
234
-
235
- for 参数名, 参数 in sig.parameters.items():
236
- class Temp(c类型基础):
237
- 类型名 = 参数.annotation.类型名
238
- 是结构体类 = 参数.annotation.是结构体类
239
-
240
- def __init__(self):
241
- self.类型 = Temp.类型名
242
- self.变量名 = 参数名
243
-
244
- 参数列表.append(Temp())
245
- ret = fn(*参数列表)
246
- if ret is not None:
247
- f.返回(ret)
248
- return f
249
-
250
-
251
- def 调用函数域(fn: c函数域, *args):
252
- 参数字符串 = ', '.join([str(arg) for arg in args])
253
- ID = c类型基础.生成ID()
254
- if fn.返回类型 == 'void':
255
- 输出(f'{fn.函数名} ({参数字符串});\n')
256
- return 已存在变量('')
257
- else:
258
- 输出(f'auto {ID} = {fn.函数名} ({参数字符串});\n')
259
- return 已存在变量(ID)
260
-
261
-
262
- def c函数(前缀: Union[None, str, List[str]] = None):
263
- class 装饰类:
264
- def __init__(self, fn: Callable) -> None:
265
- self.函数 = 根据signature构建并调用函数(
266
- fn, None, 前缀)
267
- self.函数名 = fn.__name__
268
-
269
- def __call__(self, *args: Any) -> Any:
270
- return 调用函数域(self.函数, *args)
271
-
272
- return 装饰类
273
-
274
-
275
- def c数组(类型, 长度: int):
276
- class c_数组类型(c类型基础):
277
- 类型名 = f'{类型.类型名}*'
278
- 数组长度 = 长度
279
-
280
- def 输出定义(self):
281
- if self.使用等号赋值:
282
- 输出(f'{类型.类型名} {self}[{长度}] = {self.初始化字符串};\n')
283
- else:
284
- 输出(f'{类型.类型名} {self}[{长度}];\n')
285
-
286
- def __getitem__(self, key):
287
- if key < 0 or key >= 长度:
288
- raise Exception(f'key = {key}。 但是key应该在[0, {长度})之间。')
289
- return super().__getitem__(key)
290
-
291
- def __setitem__(self, key, value):
292
- if key < 0 or key >= 长度:
293
- raise Exception(f'key = {key}。 但是key应该在[0, {长度})之间。')
294
- return super().__setitem__(key, value)
295
-
296
- return c_数组类型()
@@ -1,73 +0,0 @@
1
- from .c域 import 输出
2
-
3
-
4
- def c_include(path: str):
5
- 输出(f'#include "{path}"\n')
6
-
7
-
8
- def c_pragma(v: str):
9
- 输出(f'#pragma {v}\n')
10
-
11
-
12
- def c_define(名字: str, 值: str):
13
- 输出(f'#define {名字} {值}\n')
14
-
15
-
16
- def c_undef(名字: str):
17
- 输出(f'#undef {名字}\n')
18
-
19
-
20
- def c_error(错误信息: str):
21
- 输出(f'#error "{错误信息}"\n')
22
-
23
-
24
- def c_line(行号: int, 文件名: str):
25
- 输出(f'#line {行号} {文件名}\n')
26
-
27
-
28
- class 如果定义了:
29
- def __init__(self, 宏定义名: str):
30
- self.宏定义名 = 宏定义名
31
-
32
- def __enter__(self, *args, **kwargs):
33
- 输出(f"#ifdef {self.宏定义名}\n")
34
- return self
35
-
36
- def __exit__(self, *args, **kwargs):
37
- 输出("#endif\n")
38
-
39
- def 否则(self):
40
- 输出("#else\n")
41
-
42
-
43
- class 如果没有定义:
44
- def __init__(self, 宏定义名: str):
45
- self.宏定义名 = 宏定义名
46
-
47
- def __enter__(self, *args, **kwargs):
48
- 输出(f"#ifndef {self.宏定义名}\n")
49
- return self
50
-
51
- def __exit__(self, *args, **kwargs):
52
- 输出("#endif\n")
53
-
54
- def 否则(self):
55
- 输出("#else\n")
56
-
57
-
58
- class 宏如果:
59
- def __init__(self, 条件: str):
60
- self.条件 = 条件
61
-
62
- def __enter__(self, *args, **kwargs):
63
- 输出(f"#if {self.条件}\n")
64
- return self
65
-
66
- def __exit__(self, *args, **kwargs):
67
- 输出("#endif\n")
68
-
69
- def 否则(self):
70
- 输出("#else\n")
71
-
72
- def 否则如果(self, 条件: str):
73
- 输出(f"#elif {条件}\n")
l0n0lc/utils.py DELETED
@@ -1,50 +0,0 @@
1
- import inspect
2
-
3
-
4
- def 变量名(var):
5
- frame = inspect.currentframe()
6
- if frame is None:
7
- return
8
- frame = frame.f_back
9
- if frame is None:
10
- return
11
- for name, value in frame.f_locals.items():
12
- if value is var:
13
- return name
14
- for name, value in frame.f_globals.items():
15
- if value is var:
16
- return name
17
- return None
18
-
19
-
20
- def 上层变量名(var):
21
- frame = inspect.currentframe()
22
- if frame is None:
23
- return
24
- frame = frame.f_back
25
- if frame is None:
26
- return
27
- counter = 0
28
- for name, value in frame.f_locals.items():
29
- if value is var:
30
- counter += 1
31
- if counter > 1:
32
- return name
33
- for name, value in frame.f_globals.items():
34
- if value is var:
35
- counter += 1
36
- if counter > 1:
37
- return name
38
- return None
39
-
40
-
41
- def 十进制转换为其他进制(数值: int, 进制: int, digits="0123456789ABCDEF"):
42
- if 数值 == 0:
43
- return "0"
44
- result = ""
45
- is_negative = 数值 < 0
46
- 数值 = abs(数值)
47
- while 数值 > 0:
48
- 数值, remainder = divmod(数值, 进制) # 除基取余
49
- result = digits[remainder] + result # 余数映射字符
50
- return ("-" if is_negative else "") + result
@@ -1,17 +0,0 @@
1
- import sys
2
- from typing import Any
3
-
4
-
5
- class 输出:
6
- def __init__(self) -> None:
7
- self.输出函数 = sys.stdout.write
8
-
9
- def __call__(self, *args: Any, **kwds: Any) -> None:
10
- self.输出函数(*args, **kwds)
11
-
12
-
13
- 输出函数 = 输出()
14
-
15
-
16
- def 配置输出函数(fn):
17
- 输出函数.输出函数 = fn
@@ -1,79 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: l0n0lc
3
- Version: 0.4.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
- Dynamic: license-file
10
-
11
- # 用python写c语言
12
- ```python
13
- import l0n0lc as lc
14
- import os
15
- import ctypes
16
-
17
- if not os.path.exists('./l0n0lcoutput'):
18
- os.mkdir('l0n0lcoutput')
19
-
20
-
21
- fp = open('l0n0lcoutput/helloworld.cpp', 'w')
22
- lc.配置输出函数(fp.write)
23
- lc.c_include('stdio.h')
24
- lc.c_include('stdlib.h')
25
-
26
- with lc.如果没有定义('hahaha'):
27
- lc.c_define('hahaha', '2')
28
-
29
-
30
- @lc.c_函数(前缀=['extern "C"'])
31
- def test_add(a: lc.c_i32, b: lc.c_i32) -> lc.c_i32:
32
- with lc.如果(a.大于(100)) as f:
33
- a(10)
34
- with lc.否则如果(a.等于(10)):
35
- a(100)
36
- with lc.否则():
37
- a(1000)
38
- return a + b # type: ignore
39
-
40
- @lc.c_函数()
41
- def main(argc: lc.c_i32, argv: lc.c_char_p_p) -> lc.c_i32:
42
- c =lc.c_i32(test_add(1, 2))
43
- lc.输出(f'printf("Hello World! c = %d\\n", {c});\n')
44
- lc.输出('printf("Hello World!\\n");\n')
45
- with lc.如果定义了('hahaha'):
46
- lc.输出('printf("哈哈哈!\\n");\n')
47
- with lc.宏如果('hahaha == 1') as c:
48
- lc.输出('printf("111!\\n");\n')
49
- c.否则如果('hahaha == 2')
50
- lc.输出('printf("否则2!\\n");\n')
51
- c.否则()
52
- lc.输出('printf("否则!\\n");\n')
53
- i = lc.c_i32(0)
54
- with lc.循环(i.小于(argc)):
55
- lc.输出(f'printf("i = %d, v = %s\\n", {i}, {argv[i]});\n')
56
- i += 1
57
- return 0 # type: ignore
58
-
59
-
60
- fp.close()
61
-
62
- cm = lc.编译()
63
- cm.编译文件('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/helloworld')
64
- os.system('./l0n0lcoutput/helloworld 1 2')
65
-
66
- cm.编译为动态库('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/libhelloworld.so')
67
- path = f'{os.getcwd()}/l0n0lcoutput/libhelloworld.so'
68
- print(path)
69
- libhelloworld = ctypes.CDLL(path)
70
- libhelloworld.test_add.argtypes=[ctypes.c_int32, ctypes.c_int32]
71
- libhelloworld.test_add.restype = ctypes.c_int32
72
- a = libhelloworld.test_add(300, 6)
73
- print(a)
74
- a = libhelloworld.test_add(10, 6)
75
- print(a)
76
- a = libhelloworld.test_add(1, 6)
77
- print(a)
78
-
79
- ```
@@ -1,13 +0,0 @@
1
- l0n0lc/__init__.py,sha256=FdeyabIdYOFtX2TGIfMfOv43Q7qiEdSvY3oclyzEom8,280
2
- l0n0lc/c域.py,sha256=iJXbsmI7jpM4THHSxRGBmUbHeOhBSEk8Js25Ri0wN98,3602
3
- l0n0lc/c基础类型.py,sha256=eSKeDGg2r1i4g6AcMCXC2bAL_5-ooksRzv6fsYs2zyE,21967
4
- l0n0lc/c类型基础.py,sha256=FmHGKICEkRm4mnUmqqf-PDF2yUg91mzBdJjq07L6e8k,8471
5
- l0n0lc/c语句.py,sha256=1KuFjBpONO_MWTwQUxa14GFOp1yddPKsY-3-41qGe5Q,1506
6
- l0n0lc/utils.py,sha256=6OTdWdfwKIAqicSRTFmw-DYLn1xZv-JHC7J9aQffFZQ,1287
7
- l0n0lc/编译.py,sha256=mN5UoKCuNrUtJYn7cFkU5iSzmMdr8vw8Cz80kiOyZ3w,1971
8
- l0n0lc/输出.py,sha256=vJ-9cQ9U41-m4oyrDmhSWpsJG8FNtChm4sJ75Fy3m00,317
9
- l0n0lc-0.4.0.dist-info/licenses/LICENSE,sha256=1L-MAjulZ3kpbYwsJtlXpDVITRxykna2Ecg_521YfkA,1093
10
- l0n0lc-0.4.0.dist-info/METADATA,sha256=rQR7nEJUID-ez9wScIxdX4uns0Qt56uJ4QWP_GPk59U,2128
11
- l0n0lc-0.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
12
- l0n0lc-0.4.0.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
13
- l0n0lc-0.4.0.dist-info/RECORD,,
File without changes