l0n0lc 0.1.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.
l0n0lc/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ from .输出 import 配置输出函数
2
+ from .c类型基础 import c_类型基础, c_void, c_void_p, c_函数, c_结构体, c_数组
3
+ from .c域 import 花括号, c_函数域, 如果, 否则, 否则如果, 循环, 输出
4
+ from .c基础类型 import *
5
+ from .c语句 import *
6
+ from .编译 import 编译
@@ -0,0 +1,152 @@
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")
@@ -0,0 +1,191 @@
1
+
2
+ from .c类型基础 import c_类型基础
3
+
4
+ class c_i8(c_类型基础):
5
+ 类型名 = 'int8_t'
6
+
7
+ class c_i8_p(c_类型基础):
8
+ 类型名 = f'int8_t*'
9
+
10
+ class c_i8_p_p(c_类型基础):
11
+ 类型名 = f'int8_t**'
12
+
13
+ class c_i16(c_类型基础):
14
+ 类型名 = 'int16_t'
15
+
16
+ class c_i16_p(c_类型基础):
17
+ 类型名 = f'int16_t*'
18
+
19
+ class c_i16_p_p(c_类型基础):
20
+ 类型名 = f'int16_t**'
21
+
22
+ class c_i32(c_类型基础):
23
+ 类型名 = 'int32_t'
24
+
25
+ class c_i32_p(c_类型基础):
26
+ 类型名 = f'int32_t*'
27
+
28
+ class c_i32_p_p(c_类型基础):
29
+ 类型名 = f'int32_t**'
30
+
31
+ class c_i64(c_类型基础):
32
+ 类型名 = 'int64_t'
33
+
34
+ class c_i64_p(c_类型基础):
35
+ 类型名 = f'int64_t*'
36
+
37
+ class c_i64_p_p(c_类型基础):
38
+ 类型名 = f'int64_t**'
39
+
40
+ class c_u8(c_类型基础):
41
+ 类型名 = 'uint8_t'
42
+
43
+ class c_u8_p(c_类型基础):
44
+ 类型名 = f'uint8_t*'
45
+
46
+ class c_u8_p_p(c_类型基础):
47
+ 类型名 = f'uint8_t**'
48
+
49
+ class c_u16(c_类型基础):
50
+ 类型名 = 'uint16_t'
51
+
52
+ class c_u16_p(c_类型基础):
53
+ 类型名 = f'uint16_t*'
54
+
55
+ class c_u16_p_p(c_类型基础):
56
+ 类型名 = f'uint16_t**'
57
+
58
+ class c_u32(c_类型基础):
59
+ 类型名 = 'uint32_t'
60
+
61
+ class c_u32_p(c_类型基础):
62
+ 类型名 = f'uint32_t*'
63
+
64
+ class c_u32_p_p(c_类型基础):
65
+ 类型名 = f'uint32_t**'
66
+
67
+ class c_u64(c_类型基础):
68
+ 类型名 = 'uint64_t'
69
+
70
+ class c_u64_p(c_类型基础):
71
+ 类型名 = f'uint64_t*'
72
+
73
+ class c_u64_p_p(c_类型基础):
74
+ 类型名 = f'uint64_t**'
75
+
76
+ class c_f32(c_类型基础):
77
+ 类型名 = 'float'
78
+
79
+ class c_f32_p(c_类型基础):
80
+ 类型名 = f'float*'
81
+
82
+ class c_f32_p_p(c_类型基础):
83
+ 类型名 = f'float**'
84
+
85
+ class c_f64(c_类型基础):
86
+ 类型名 = 'double'
87
+
88
+ class c_f64_p(c_类型基础):
89
+ 类型名 = f'double*'
90
+
91
+ class c_f64_p_p(c_类型基础):
92
+ 类型名 = f'double**'
93
+
94
+ class c_bool(c_类型基础):
95
+ 类型名 = 'bool'
96
+
97
+ class c_bool_p(c_类型基础):
98
+ 类型名 = f'bool*'
99
+
100
+ class c_bool_p_p(c_类型基础):
101
+ 类型名 = f'bool**'
102
+
103
+ class c_char(c_类型基础):
104
+ 类型名 = 'char'
105
+
106
+ class c_char_p(c_类型基础):
107
+ 类型名 = f'char*'
108
+
109
+ class c_char_p_p(c_类型基础):
110
+ 类型名 = f'char**'
111
+
112
+ class c_uchar(c_类型基础):
113
+ 类型名 = 'unsigned char'
114
+
115
+ class c_uchar_p(c_类型基础):
116
+ 类型名 = f'unsigned char*'
117
+
118
+ class c_uchar_p_p(c_类型基础):
119
+ 类型名 = f'unsigned char**'
120
+
121
+ class c_int(c_类型基础):
122
+ 类型名 = 'int'
123
+
124
+ class c_int_p(c_类型基础):
125
+ 类型名 = f'int*'
126
+
127
+ class c_int_p_p(c_类型基础):
128
+ 类型名 = f'int**'
129
+
130
+ class c_uint(c_类型基础):
131
+ 类型名 = 'unsigned int'
132
+
133
+ class c_uint_p(c_类型基础):
134
+ 类型名 = f'unsigned int*'
135
+
136
+ class c_uint_p_p(c_类型基础):
137
+ 类型名 = f'unsigned int**'
138
+
139
+ class c_short(c_类型基础):
140
+ 类型名 = 'short'
141
+
142
+ class c_short_p(c_类型基础):
143
+ 类型名 = f'short*'
144
+
145
+ class c_short_p_p(c_类型基础):
146
+ 类型名 = f'short**'
147
+
148
+ class c_ushort(c_类型基础):
149
+ 类型名 = 'unsigned short'
150
+
151
+ class c_ushort_p(c_类型基础):
152
+ 类型名 = f'unsigned short*'
153
+
154
+ class c_ushort_p_p(c_类型基础):
155
+ 类型名 = f'unsigned short**'
156
+
157
+ class c_long(c_类型基础):
158
+ 类型名 = 'long'
159
+
160
+ class c_long_p(c_类型基础):
161
+ 类型名 = f'long*'
162
+
163
+ class c_long_p_p(c_类型基础):
164
+ 类型名 = f'long**'
165
+
166
+ class c_ulong(c_类型基础):
167
+ 类型名 = 'unsigned long'
168
+
169
+ class c_ulong_p(c_类型基础):
170
+ 类型名 = f'unsigned long*'
171
+
172
+ class c_ulong_p_p(c_类型基础):
173
+ 类型名 = f'unsigned long**'
174
+
175
+ class c_longlong(c_类型基础):
176
+ 类型名 = 'long long'
177
+
178
+ class c_longlong_p(c_类型基础):
179
+ 类型名 = f'long long*'
180
+
181
+ class c_longlong_p_p(c_类型基础):
182
+ 类型名 = f'long long**'
183
+
184
+ class c_ulonglong(c_类型基础):
185
+ 类型名 = 'unsigned long long'
186
+
187
+ class c_ulonglong_p(c_类型基础):
188
+ 类型名 = f'unsigned long long*'
189
+
190
+ class c_ulonglong_p_p(c_类型基础):
191
+ 类型名 = f'unsigned long long**'
@@ -0,0 +1,296 @@
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_数组类型()
@@ -0,0 +1,73 @@
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 ADDED
@@ -0,0 +1,50 @@
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
@@ -0,0 +1,54 @@
1
+ from typing import Union, List
2
+ import subprocess
3
+
4
+
5
+ class 编译:
6
+ def __init__(self) -> None:
7
+ self.编译器 = '/bin/c++'
8
+ self.include目录列表 = []
9
+ self.库目录列表 = []
10
+ self.链接库 = []
11
+ self.编译选项 = []
12
+
13
+ def 设置编译器(self, 编译器地址: str):
14
+ self.编译器 = 编译器地址
15
+
16
+ def 添加include目录(self, 目录: Union[str, List[str]]):
17
+ if isinstance(目录, str):
18
+ self.include目录列表.append(目录)
19
+ return
20
+ self.include目录列表 += 目录
21
+
22
+ def 添加库目录(self, 目录: Union[str, List[str]]):
23
+ if isinstance(目录, str):
24
+ self.库目录列表.append(目录)
25
+ return
26
+ self.库目录列表 += 目录
27
+
28
+ def 添加链接库(self, 库名: Union[str, List[str]]):
29
+ if isinstance(库名, str):
30
+ self.链接库.append(库名)
31
+ return
32
+ self.链接库 += 库名
33
+
34
+ def 添加编译选项(self, 选项: Union[str, List[str]]):
35
+ if isinstance(选项, str):
36
+ self.编译选项.append(选项)
37
+ return
38
+ self.编译选项 += 选项
39
+
40
+ def 编译文件(self, 文件路径: str, 输出路径: str):
41
+ 编译指令 = [self.编译器]
42
+ include指令 = [f'-I{目录}' for 目录 in self.include目录列表]
43
+ 库目录指令 = [f'-L{目录}' for 目录 in self.库目录列表]
44
+ 库链接指令 = [f'-l{库名}' for 库名 in self.链接库]
45
+ 编译指令 += include指令 + 库目录指令 + 库链接指令 + self.编译选项
46
+ 编译指令.append(文件路径)
47
+ 编译指令.append('-o')
48
+ 编译指令.append(输出路径)
49
+ subprocess.run(编译指令)
50
+
51
+ def 编译为动态库(self, 文件路径: str, 输出路径: str):
52
+ self.添加编译选项('-fPIC')
53
+ self.添加编译选项('--shared')
54
+ self.编译文件(文件路径, 输出路径)
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,77 @@
1
+ Metadata-Version: 2.4
2
+ Name: l0n0lc
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Classifier: Programming Language :: Python :: 3
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+
9
+ # 用python写c语言
10
+ ```python
11
+ import l0n0lc as lc
12
+ import os
13
+ import ctypes
14
+
15
+ if not os.path.exists('./l0n0lcoutput'):
16
+ os.mkdir('l0n0lcoutput')
17
+
18
+
19
+ fp = open('l0n0lcoutput/helloworld.cpp', 'w')
20
+ lc.配置输出函数(fp.write)
21
+ lc.c_include('stdio.h')
22
+ lc.c_include('stdlib.h')
23
+
24
+ with lc.如果没有定义('hahaha'):
25
+ lc.c_define('hahaha', '2')
26
+
27
+
28
+ @lc.c_函数(前缀=['extern "C"'])
29
+ def test_add(a: lc.c_i32, b: lc.c_i32) -> lc.c_i32:
30
+ with lc.如果(a.大于(100)) as f:
31
+ a(10)
32
+ with lc.否则如果(a.等于(10)):
33
+ a(100)
34
+ with lc.否则():
35
+ a(1000)
36
+ return a + b # type: ignore
37
+
38
+ @lc.c_函数()
39
+ def main(argc: lc.c_i32, argv: lc.c_char_p_p) -> lc.c_i32:
40
+ c =lc.c_i32(test_add(1, 2))
41
+ lc.输出(f'printf("Hello World! c = %d\\n", {c});\n')
42
+ lc.输出('printf("Hello World!\\n");\n')
43
+ with lc.如果定义了('hahaha'):
44
+ lc.输出('printf("哈哈哈!\\n");\n')
45
+ with lc.宏如果('hahaha == 1') as c:
46
+ lc.输出('printf("111!\\n");\n')
47
+ c.否则如果('hahaha == 2')
48
+ lc.输出('printf("否则2!\\n");\n')
49
+ c.否则()
50
+ lc.输出('printf("否则!\\n");\n')
51
+ i = lc.c_i32(0)
52
+ with lc.循环(i.小于(argc)):
53
+ lc.输出(f'printf("i = %d, v = %s\\n", {i}, {argv[i]});\n')
54
+ i += 1
55
+ return 0 # type: ignore
56
+
57
+
58
+ fp.close()
59
+
60
+ cm = lc.编译()
61
+ cm.编译文件('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/helloworld')
62
+ os.system('./l0n0lcoutput/helloworld 1 2')
63
+
64
+ cm.编译为动态库('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/libhelloworld.so')
65
+ path = f'{os.getcwd()}/l0n0lcoutput/libhelloworld.so'
66
+ print(path)
67
+ libhelloworld = ctypes.CDLL(path)
68
+ libhelloworld.test_add.argtypes=[ctypes.c_int32, ctypes.c_int32]
69
+ libhelloworld.test_add.restype = ctypes.c_int32
70
+ a = libhelloworld.test_add(300, 6)
71
+ print(a)
72
+ a = libhelloworld.test_add(10, 6)
73
+ print(a)
74
+ a = libhelloworld.test_add(1, 6)
75
+ print(a)
76
+
77
+ ```
@@ -0,0 +1,12 @@
1
+ l0n0lc/__init__.py,sha256=L68TK_0liDW_z02MTr5NWF5v3huyM-dwZsUUfsFePWc,297
2
+ l0n0lc/c域.py,sha256=uxxD5A-wekc2bfdVQGUlyzvaQ30MPVhB6qeVkHrkU14,3604
3
+ l0n0lc/c基础类型.py,sha256=PGkixIWWSVox7j-NQbFH0cqUcgDQjrEKcV5XoWlsWN0,4015
4
+ l0n0lc/c类型基础.py,sha256=xItXb9WFF5wzHE5YNlr94_FpPV0TsY8pchx-qeXy3Z0,8493
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.1.0.dist-info/METADATA,sha256=oG0nATnb1aRviJR29tv4cu1P78KIsc1La1dxdf6wUgk,2096
10
+ l0n0lc-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ l0n0lc-0.1.0.dist-info/top_level.txt,sha256=Q21D_eEY_-xgRUPwATGp9YDKSBO4w_7MI2MYxQI1aT4,7
12
+ l0n0lc-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ l0n0lc