karaslang 1.0.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.
- __init__.py +7 -0
- cli.py +59 -0
- interpreter.py +504 -0
- karaslang-1.0.0.dist-info/METADATA +5 -0
- karaslang-1.0.0.dist-info/RECORD +9 -0
- karaslang-1.0.0.dist-info/WHEEL +4 -0
- karaslang-1.0.0.dist-info/top_level.txt +1 -0
- pyproject.toml +17 -0
- registry.py +95 -0
__init__.py
ADDED
cli.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .registry import DEFAULT_PKGS_DIR, DEFAULT_REGISTRY_URL, install_package
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
11
|
+
parser = argparse.ArgumentParser(prog="karas", description="أداة سطر الأوامر للغة كاراس")
|
|
12
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
13
|
+
|
|
14
|
+
install_parser = subparsers.add_parser("install", help="تثبيت حزمة من سجل KLUOL إلى مجلد pkgs")
|
|
15
|
+
install_parser.add_argument("package", help="اسم الحزمة (مثال: mathutils أو mathutils.kg)")
|
|
16
|
+
install_parser.add_argument(
|
|
17
|
+
"--force",
|
|
18
|
+
action="store_true",
|
|
19
|
+
help="إعادة تنزيل الحزمة حتى لو كانت موجودة في مجلد pkgs",
|
|
20
|
+
)
|
|
21
|
+
install_parser.add_argument(
|
|
22
|
+
"--pkgs-dir",
|
|
23
|
+
type=Path,
|
|
24
|
+
default=DEFAULT_PKGS_DIR,
|
|
25
|
+
help="مسار مجلد الحزم المحلي (الافتراضي: pkgs)",
|
|
26
|
+
)
|
|
27
|
+
install_parser.add_argument(
|
|
28
|
+
"--registry",
|
|
29
|
+
default=DEFAULT_REGISTRY_URL,
|
|
30
|
+
help="رابط السجل الأساسي (الافتراضي: KLUOL GitHub Pages)",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return parser
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def main(argv: list[str] | None = None) -> int:
|
|
37
|
+
parser = build_parser()
|
|
38
|
+
args = parser.parse_args(argv)
|
|
39
|
+
|
|
40
|
+
if args.command == "install":
|
|
41
|
+
try:
|
|
42
|
+
saved_path = install_package(
|
|
43
|
+
pkg_name=args.package,
|
|
44
|
+
pkgs_dir=args.pkgs_dir,
|
|
45
|
+
registry_url=args.registry,
|
|
46
|
+
force=args.force,
|
|
47
|
+
)
|
|
48
|
+
except Exception as exc:
|
|
49
|
+
print(f"خطأ أثناء تثبيت الحزمة: {exc}", file=sys.stderr)
|
|
50
|
+
return 1
|
|
51
|
+
|
|
52
|
+
print(f"تم حفظ الحزمة في: {saved_path}")
|
|
53
|
+
return 0
|
|
54
|
+
|
|
55
|
+
return 1
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
raise SystemExit(main())
|
interpreter.py
ADDED
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import sys
|
|
3
|
+
import time as py_time
|
|
4
|
+
import tkinter as tk
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class KarasReturn(Exception):
|
|
8
|
+
def __init__(self, value):
|
|
9
|
+
self.value = value
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class KarasFunction:
|
|
13
|
+
def __init__(self, params, body, closure_env):
|
|
14
|
+
self.params = params
|
|
15
|
+
self.body = body
|
|
16
|
+
self.env = closure_env
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# --- مكتبة numvarutils المدمجة ---
|
|
20
|
+
def gauss_sum(a):
|
|
21
|
+
return (a * (a + 1)) // 2
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def counter(a, b):
|
|
25
|
+
b += 1
|
|
26
|
+
for _ in range(0, b):
|
|
27
|
+
yield a
|
|
28
|
+
a += 1
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def countdown(a, b):
|
|
32
|
+
b += 1
|
|
33
|
+
for _ in range(0, b):
|
|
34
|
+
yield a
|
|
35
|
+
a -= 1
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def isprime(a):
|
|
39
|
+
if a <= 1:
|
|
40
|
+
print("not prime")
|
|
41
|
+
return ""
|
|
42
|
+
b = 2
|
|
43
|
+
while True:
|
|
44
|
+
if a == b:
|
|
45
|
+
print("prime")
|
|
46
|
+
break
|
|
47
|
+
elif (a / b) == math.floor(a / b):
|
|
48
|
+
print("not prime")
|
|
49
|
+
break
|
|
50
|
+
b += 1
|
|
51
|
+
return ""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def isprime_bool(a):
|
|
55
|
+
if a <= 1:
|
|
56
|
+
return False
|
|
57
|
+
b = 2
|
|
58
|
+
while True:
|
|
59
|
+
if a == b:
|
|
60
|
+
return True
|
|
61
|
+
elif (a / b) == math.floor(a / b):
|
|
62
|
+
return False
|
|
63
|
+
b += 1
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _primes_gen(n):
|
|
67
|
+
a = 1
|
|
68
|
+
for _ in range(0, n):
|
|
69
|
+
a += 1
|
|
70
|
+
if isprime_bool(a) is True:
|
|
71
|
+
yield a
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def primes_up_to(n):
|
|
75
|
+
return list(_primes_gen(n))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def is_even(a):
|
|
79
|
+
if (a / 2) == math.floor(a / 2):
|
|
80
|
+
return True
|
|
81
|
+
else:
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def is_odd(a):
|
|
86
|
+
if (a / 2) == math.floor(a / 2):
|
|
87
|
+
return False
|
|
88
|
+
else:
|
|
89
|
+
return True
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def fibonacci(n):
|
|
93
|
+
f0 = 0
|
|
94
|
+
f1 = 1
|
|
95
|
+
if n < 2:
|
|
96
|
+
return n
|
|
97
|
+
else:
|
|
98
|
+
for _ in range(2, n + 1):
|
|
99
|
+
f = f0 + f1
|
|
100
|
+
f0 = f1
|
|
101
|
+
f1 = f
|
|
102
|
+
return f
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def integrate_trap(fn, a, b, n=1000):
|
|
106
|
+
h = (b - a) / n
|
|
107
|
+
total = (fn(a) + fn(b)) / 2
|
|
108
|
+
for i in range(1, n):
|
|
109
|
+
total += fn(a + i * h)
|
|
110
|
+
return total * h
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def f2(t, x):
|
|
114
|
+
return t ** x * math.exp(-t)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def factorial(x):
|
|
118
|
+
if x < 0 and x == math.floor(x):
|
|
119
|
+
return None
|
|
120
|
+
elif x == 0:
|
|
121
|
+
return 1
|
|
122
|
+
else:
|
|
123
|
+
return integrate_trap(lambda t: f2(t, x), 0, 100)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def gcd(a, b):
|
|
127
|
+
while b != 0:
|
|
128
|
+
a, b = b, a % b
|
|
129
|
+
return a
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def scd(a, b):
|
|
133
|
+
for i in range(2, min(a, b) + 1):
|
|
134
|
+
if a % i == 0 and b % i == 0:
|
|
135
|
+
return i
|
|
136
|
+
return 1
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def divisors(n):
|
|
140
|
+
result = []
|
|
141
|
+
for i in range(1, n + 1):
|
|
142
|
+
if (n / i) == math.floor(n / i):
|
|
143
|
+
result.append(i)
|
|
144
|
+
return result
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def primes_between(a, b):
|
|
148
|
+
return [p for p in primes_up_to(b) if p >= a]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def nth_prime(n):
|
|
152
|
+
if n < 3:
|
|
153
|
+
return primes_up_to(n)[n - 1]
|
|
154
|
+
else:
|
|
155
|
+
return primes_up_to(n ** 2)[n - 1]
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def is_palindrome_num(n):
|
|
159
|
+
s = str(n)
|
|
160
|
+
return s == s[::-1]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def convert_to_palindrome(n, max_iter=1000):
|
|
164
|
+
if is_palindrome_num(n) is True:
|
|
165
|
+
return n
|
|
166
|
+
for _ in range(0, max_iter):
|
|
167
|
+
rev = int(str(n)[::-1])
|
|
168
|
+
n += rev
|
|
169
|
+
if is_palindrome_num(n) is True:
|
|
170
|
+
return n
|
|
171
|
+
raise ValueError("Function Not Support Lychrel")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def digital_root(n):
|
|
175
|
+
while n >= 10:
|
|
176
|
+
total = 0
|
|
177
|
+
for d in str(n):
|
|
178
|
+
total += int(d)
|
|
179
|
+
n = total
|
|
180
|
+
return n
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def is_armstrong(n):
|
|
184
|
+
s = str(n)
|
|
185
|
+
power = len(s)
|
|
186
|
+
total = 0
|
|
187
|
+
for d in s:
|
|
188
|
+
total += int(d) ** power
|
|
189
|
+
if total == n:
|
|
190
|
+
return True
|
|
191
|
+
else:
|
|
192
|
+
return False
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def prime_factors(n):
|
|
196
|
+
result = []
|
|
197
|
+
d = 2
|
|
198
|
+
while d * d <= n:
|
|
199
|
+
while (n / d) == math.floor(n / d):
|
|
200
|
+
result.append(d)
|
|
201
|
+
n = n // d
|
|
202
|
+
d += 1
|
|
203
|
+
if n > 1:
|
|
204
|
+
result.append(n)
|
|
205
|
+
return result
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def g(x):
|
|
209
|
+
d = [8, 3, 6, 14, 4, 6, 6, 3, 4, 6, 11]
|
|
210
|
+
if x > 10:
|
|
211
|
+
return g(math.floor(x / 10)) + g(10 * (math.floor(x / 10)) - math.floor(x)) - 2
|
|
212
|
+
elif x <= 10 and x >= 0:
|
|
213
|
+
return d[math.floor(x)]
|
|
214
|
+
elif x < 0:
|
|
215
|
+
return 2 + g(abs(x))
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
MATHUTILS_FUNCS = {
|
|
219
|
+
"gauss_sum": gauss_sum,
|
|
220
|
+
"counter": counter,
|
|
221
|
+
"countdown": countdown,
|
|
222
|
+
"isprime": isprime,
|
|
223
|
+
"isprime_bool": isprime_bool,
|
|
224
|
+
"primes_up_to": primes_up_to,
|
|
225
|
+
"is_even": is_even,
|
|
226
|
+
"is_odd": is_odd,
|
|
227
|
+
"fibonacci": fibonacci,
|
|
228
|
+
"integrate_trap": integrate_trap,
|
|
229
|
+
"f2": f2,
|
|
230
|
+
"factorial": factorial,
|
|
231
|
+
"gcd": gcd,
|
|
232
|
+
"scd": scd,
|
|
233
|
+
"divisors": divisors,
|
|
234
|
+
"primes_between": primes_between,
|
|
235
|
+
"nth_prime": nth_prime,
|
|
236
|
+
"convert_to_palindrome": convert_to_palindrome,
|
|
237
|
+
"digital_root": digital_root,
|
|
238
|
+
"is_palindrome_num": is_palindrome_num,
|
|
239
|
+
"is_armstrong": is_armstrong,
|
|
240
|
+
"prime_factors": prime_factors,
|
|
241
|
+
"g": g,
|
|
242
|
+
}
|
|
243
|
+
# --------------------------------
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
class KarasInterpreter:
|
|
247
|
+
def __init__(self):
|
|
248
|
+
self.start_time = py_time.time()
|
|
249
|
+
self.env = {}
|
|
250
|
+
self.render_items = []
|
|
251
|
+
self._init_builtins()
|
|
252
|
+
|
|
253
|
+
def _init_builtins(self):
|
|
254
|
+
for name in dir(math):
|
|
255
|
+
if not name.startswith("_"):
|
|
256
|
+
self.env[name] = getattr(math, name)
|
|
257
|
+
|
|
258
|
+
self.env.update(MATHUTILS_FUNCS)
|
|
259
|
+
self.env.update({
|
|
260
|
+
"math": math,
|
|
261
|
+
"list": list,
|
|
262
|
+
"abs": abs,
|
|
263
|
+
"min": min,
|
|
264
|
+
"max": max,
|
|
265
|
+
"time": 0.0,
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
def update_time(self):
|
|
269
|
+
self.env["time"] = py_time.time() - self.start_time
|
|
270
|
+
|
|
271
|
+
def eval_expr(self, expr_str):
|
|
272
|
+
self.update_time()
|
|
273
|
+
expr_str = expr_str.strip()
|
|
274
|
+
normalized = ""
|
|
275
|
+
for i, ch in enumerate(expr_str):
|
|
276
|
+
if i > 0 and ch == "x" and (expr_str[i - 1].isdigit() or expr_str[i - 1] == ")"):
|
|
277
|
+
normalized += "*x"
|
|
278
|
+
else:
|
|
279
|
+
normalized += ch
|
|
280
|
+
return eval(normalized, {"__builtins__": {}}, self.env)
|
|
281
|
+
|
|
282
|
+
def _split_top_level_commas(self, text):
|
|
283
|
+
parts = []
|
|
284
|
+
buf = []
|
|
285
|
+
depth = 0
|
|
286
|
+
for ch in text:
|
|
287
|
+
if ch == "(":
|
|
288
|
+
depth += 1
|
|
289
|
+
elif ch == ")":
|
|
290
|
+
depth -= 1
|
|
291
|
+
if ch == "," and depth == 0:
|
|
292
|
+
parts.append("".join(buf).strip())
|
|
293
|
+
buf = []
|
|
294
|
+
else:
|
|
295
|
+
buf.append(ch)
|
|
296
|
+
if buf:
|
|
297
|
+
parts.append("".join(buf).strip())
|
|
298
|
+
return parts
|
|
299
|
+
|
|
300
|
+
def execute_lines(self, lines):
|
|
301
|
+
i = 0
|
|
302
|
+
n = len(lines)
|
|
303
|
+
while i < n:
|
|
304
|
+
raw = lines[i]
|
|
305
|
+
stripped = raw.strip()
|
|
306
|
+
if not stripped or stripped.startswith("#"):
|
|
307
|
+
i += 1
|
|
308
|
+
continue
|
|
309
|
+
|
|
310
|
+
indent = len(raw) - len(raw.lstrip(" "))
|
|
311
|
+
block = []
|
|
312
|
+
j = i + 1
|
|
313
|
+
while j < n:
|
|
314
|
+
nxt = lines[j]
|
|
315
|
+
if not nxt.strip() or nxt.strip().startswith("#"):
|
|
316
|
+
block.append(nxt)
|
|
317
|
+
j += 1
|
|
318
|
+
continue
|
|
319
|
+
nxt_indent = len(nxt) - len(nxt.lstrip(" "))
|
|
320
|
+
if nxt_indent > indent:
|
|
321
|
+
block.append(nxt)
|
|
322
|
+
j += 1
|
|
323
|
+
else:
|
|
324
|
+
break
|
|
325
|
+
|
|
326
|
+
if stripped.startswith("def ") or stripped.startswith("function "):
|
|
327
|
+
header = stripped.split(" ", 1)[1].rstrip(":").strip()
|
|
328
|
+
fn_name, rest = header.split("(", 1)
|
|
329
|
+
params_str = rest.rstrip(")").strip()
|
|
330
|
+
params = [p.strip() for p in params_str.split(",") if p.strip()]
|
|
331
|
+
self.env[fn_name.strip()] = KarasFunction(params, block, self.env)
|
|
332
|
+
i = j
|
|
333
|
+
continue
|
|
334
|
+
|
|
335
|
+
if stripped.startswith("if ") and stripped.endswith(":"):
|
|
336
|
+
cond_expr = stripped[3:-1].strip()
|
|
337
|
+
cond_val = bool(self.eval_expr(cond_expr))
|
|
338
|
+
else_block = []
|
|
339
|
+
k = j
|
|
340
|
+
if k < n and lines[k].strip() == "else:":
|
|
341
|
+
else_indent = len(lines[k]) - len(lines[k].lstrip(" "))
|
|
342
|
+
k += 1
|
|
343
|
+
while k < n:
|
|
344
|
+
nxt = lines[k]
|
|
345
|
+
if not nxt.strip() or nxt.strip().startswith("#"):
|
|
346
|
+
else_block.append(nxt)
|
|
347
|
+
k += 1
|
|
348
|
+
continue
|
|
349
|
+
nxt_indent = len(nxt) - len(nxt.lstrip(" "))
|
|
350
|
+
if nxt_indent > else_indent:
|
|
351
|
+
else_block.append(nxt)
|
|
352
|
+
k += 1
|
|
353
|
+
else:
|
|
354
|
+
break
|
|
355
|
+
if cond_val:
|
|
356
|
+
self.execute_lines(block)
|
|
357
|
+
elif else_block:
|
|
358
|
+
self.execute_lines(else_block)
|
|
359
|
+
i = k
|
|
360
|
+
continue
|
|
361
|
+
|
|
362
|
+
if stripped.startswith("while ") and stripped.endswith(":"):
|
|
363
|
+
cond_expr = stripped[6:-1].strip()
|
|
364
|
+
while bool(self.eval_expr(cond_expr)):
|
|
365
|
+
try:
|
|
366
|
+
self.execute_lines(block)
|
|
367
|
+
except KarasReturn:
|
|
368
|
+
raise
|
|
369
|
+
i = j
|
|
370
|
+
continue
|
|
371
|
+
|
|
372
|
+
if stripped.startswith("return"):
|
|
373
|
+
expr_part = stripped[6:].strip()
|
|
374
|
+
val = self.eval_expr(expr_part) if expr_part else None
|
|
375
|
+
raise KarasReturn(val)
|
|
376
|
+
|
|
377
|
+
if stripped.startswith("print(") and stripped.endswith(")"):
|
|
378
|
+
inner = stripped[6:-1].strip()
|
|
379
|
+
print(self.eval_expr(inner))
|
|
380
|
+
i += 1
|
|
381
|
+
continue
|
|
382
|
+
|
|
383
|
+
if stripped.startswith("grf(") and stripped.endswith(")"):
|
|
384
|
+
inner = stripped[4:-1].strip()
|
|
385
|
+
self.render_items.append(("grf", inner))
|
|
386
|
+
i += 1
|
|
387
|
+
continue
|
|
388
|
+
|
|
389
|
+
if stripped.startswith("Scelastapa(") and stripped.endswith(")"):
|
|
390
|
+
inner = stripped[11:-1].strip()
|
|
391
|
+
args = self._split_top_level_commas(inner)
|
|
392
|
+
if len(args) == 2:
|
|
393
|
+
self.render_items.append(("scelastapa", args[0], args[1]))
|
|
394
|
+
i += 1
|
|
395
|
+
continue
|
|
396
|
+
|
|
397
|
+
if "=" in stripped and not any(op in stripped for op in ("==", "!=", "<=", ">=")):
|
|
398
|
+
var_name, expr_part = stripped.split("=", 1)
|
|
399
|
+
self.env[var_name.strip()] = self.eval_expr(expr_part.strip())
|
|
400
|
+
else:
|
|
401
|
+
self.eval_expr(stripped)
|
|
402
|
+
|
|
403
|
+
i += 1
|
|
404
|
+
|
|
405
|
+
def run(self, code):
|
|
406
|
+
self.start_time = py_time.time()
|
|
407
|
+
self.render_items.clear()
|
|
408
|
+
lines = code.splitlines()
|
|
409
|
+
self.execute_lines(lines)
|
|
410
|
+
|
|
411
|
+
if self.render_items:
|
|
412
|
+
self._start_graph_window()
|
|
413
|
+
|
|
414
|
+
def run_file(self, path):
|
|
415
|
+
if not path.endswith(".kg"):
|
|
416
|
+
print("Warning: expected a .kg file")
|
|
417
|
+
with open(path, "r", encoding="utf-8") as f:
|
|
418
|
+
self.run(f.read())
|
|
419
|
+
|
|
420
|
+
def _start_graph_window(self):
|
|
421
|
+
root = tk.Tk()
|
|
422
|
+
root.title("karas (.kg) — Graph Mode")
|
|
423
|
+
width, height = 800, 500
|
|
424
|
+
canvas = tk.Canvas(root, width=width, height=height, bg="#111111")
|
|
425
|
+
canvas.pack(fill=tk.BOTH, expand=True)
|
|
426
|
+
|
|
427
|
+
cx, cy = width / 2, height / 2
|
|
428
|
+
|
|
429
|
+
def draw_frame():
|
|
430
|
+
self.update_time()
|
|
431
|
+
t = self.env["time"]
|
|
432
|
+
canvas.delete("all")
|
|
433
|
+
|
|
434
|
+
canvas.create_line(0, cy, width, cy, fill="#2b2b2b")
|
|
435
|
+
canvas.create_line(cx, 0, cx, height, fill="#2b2b2b")
|
|
436
|
+
|
|
437
|
+
for item in self.render_items:
|
|
438
|
+
kind = item[0]
|
|
439
|
+
|
|
440
|
+
if kind == "grf":
|
|
441
|
+
expr_str = item[1]
|
|
442
|
+
points = []
|
|
443
|
+
for px in range(0, width, 4):
|
|
444
|
+
x_val = (px - cx) / 50.0
|
|
445
|
+
self.env["x"] = x_val
|
|
446
|
+
try:
|
|
447
|
+
y_val = float(self.eval_expr(expr_str))
|
|
448
|
+
except Exception:
|
|
449
|
+
y_val = 0.0
|
|
450
|
+
py = cy - (y_val * 50.0)
|
|
451
|
+
points.extend([px, py])
|
|
452
|
+
if len(points) >= 4:
|
|
453
|
+
canvas.create_line(points, fill="#00e5ff", width=2, smooth=True)
|
|
454
|
+
|
|
455
|
+
elif kind == "scelastapa":
|
|
456
|
+
x_expr, y_expr = item[1], item[2]
|
|
457
|
+
try:
|
|
458
|
+
bx = float(self.eval_expr(x_expr))
|
|
459
|
+
by = float(self.eval_expr(y_expr))
|
|
460
|
+
except Exception:
|
|
461
|
+
bx, by = 0.0, 0.0
|
|
462
|
+
radius = 35.0
|
|
463
|
+
angle = t * 2.5
|
|
464
|
+
rx = bx + radius * math.cos(angle)
|
|
465
|
+
ry = by + radius * math.sin(angle)
|
|
466
|
+
|
|
467
|
+
sx = cx + bx
|
|
468
|
+
sy = cy - by
|
|
469
|
+
px = cx + rx
|
|
470
|
+
py = cy - ry
|
|
471
|
+
|
|
472
|
+
canvas.create_oval(sx - 3, sy - 3, sx + 3, sy + 3, outline="#666666")
|
|
473
|
+
canvas.create_line(sx, sy, px, py, fill="#ffaa00", width=1)
|
|
474
|
+
canvas.create_oval(px - 6, py - 6, px + 6, py + 6, fill="#ff5500", outline="")
|
|
475
|
+
|
|
476
|
+
root.after(16, draw_frame)
|
|
477
|
+
|
|
478
|
+
draw_frame()
|
|
479
|
+
root.mainloop()
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def repl(interpreter):
|
|
483
|
+
print("karas REPL (.kg) — type 'exit' to quit")
|
|
484
|
+
while True:
|
|
485
|
+
try:
|
|
486
|
+
line = input("karas> ")
|
|
487
|
+
except (EOFError, KeyboardInterrupt):
|
|
488
|
+
break
|
|
489
|
+
if line.strip() == "exit":
|
|
490
|
+
break
|
|
491
|
+
if not line.strip():
|
|
492
|
+
continue
|
|
493
|
+
try:
|
|
494
|
+
interpreter.run(line)
|
|
495
|
+
except Exception as err:
|
|
496
|
+
print("Error:", err)
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
if __name__ == "__main__":
|
|
500
|
+
interpreter = KarasInterpreter()
|
|
501
|
+
if len(sys.argv) > 1:
|
|
502
|
+
interpreter.run_file(sys.argv[1])
|
|
503
|
+
else:
|
|
504
|
+
repl(interpreter)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
karaslang/__init__.py,sha256=1363d011881042f00c12c9e1dbaf123e0e4f856cc98e09fd2dda80dbd39392bf
|
|
2
|
+
karaslang/interpreter.py,sha256=3642e44a9c83818cf52731716dc4a0f80a6a35a67d6b50173a5f3883901fc106
|
|
3
|
+
karaslang/cli.py,sha256=99f8952bc7bd0de55a75a4420521d0c9ca7500a77597413eb0555c99926c68b8
|
|
4
|
+
karaslang-1.0.0.dist-info/METADATA,sha256=3b45074cb78ee32478f8afd0fbf86ed78e22777bd45fb8a0572e1e36f82a53f8
|
|
5
|
+
karaslang-1.0.0.dist-info/WHEEL,sha256=5e0106cc80d43074b8d465fa1b52e7a9c408e0799d2637672e27b21502920548
|
|
6
|
+
karaslang-1.0.0.dist-info/RECORD,,
|
|
7
|
+
karaslang-1.0.0.dist-info/top_level.txt,sha256=00ed03d0da7cd1d762a228985fe0c4148f7e23042275703a3e54a409ee078ddc
|
|
8
|
+
karaslang/pyproject.toml,sha256=6bc332e906cfd3e3bd2fe0733e162961670b829bcdf5b80448b43c704a5d3518
|
|
9
|
+
karaslang/registry.py,sha256=b5e5632071302b3b644e43b0f5927505cc8f91b0c734f744baba502246afa380
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
karaslang
|
pyproject.toml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "karas-pkg"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Karas language package installer backed by KLUOL static registry"
|
|
9
|
+
requires-python = ">=3.9"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[project.scripts]
|
|
13
|
+
karas = "karaslang.cli:main"
|
|
14
|
+
|
|
15
|
+
[tool.setuptools.packages.find]
|
|
16
|
+
where = ["."]
|
|
17
|
+
include = ["karaslang*"]
|
registry.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import urllib.error
|
|
6
|
+
import urllib.request
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
DEFAULT_REGISTRY_URL = "https://2-2-gif.github.io/kluol-this-is-Not-all-of-name"
|
|
10
|
+
DEFAULT_PKGS_DIR = Path("pkgs")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def compute_sha256(data: bytes) -> str:
|
|
14
|
+
return hashlib.sha256(data).hexdigest()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def fetch_json(url: str, timeout: int = 15) -> dict:
|
|
18
|
+
req = urllib.request.Request(url, headers={"User-Agent": "karas-pkg/0.1.0"})
|
|
19
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
20
|
+
raw = resp.read().decode("utf-8")
|
|
21
|
+
parsed = json.loads(raw)
|
|
22
|
+
if not isinstance(parsed, dict):
|
|
23
|
+
raise ValueError("صيغة index.json غير صالحة.")
|
|
24
|
+
return parsed
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def fetch_bytes(url: str, timeout: int = 20) -> bytes:
|
|
28
|
+
req = urllib.request.Request(url, headers={"User-Agent": "karas-pkg/0.1.0"})
|
|
29
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
30
|
+
return resp.read()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def resolve_packages_map(index_data: dict) -> dict:
|
|
34
|
+
# يدعم شكل {"packages": {"name": {...}}} أو شكل القاموس المباشر {"name": {...}}
|
|
35
|
+
pkgs = index_data.get("packages")
|
|
36
|
+
if isinstance(pkgs, dict):
|
|
37
|
+
return pkgs
|
|
38
|
+
return {
|
|
39
|
+
k: v
|
|
40
|
+
for k, v in index_data.items()
|
|
41
|
+
if isinstance(v, dict) and k not in {"version", "generated_at", "meta"}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def install_package(
|
|
46
|
+
pkg_name: str,
|
|
47
|
+
pkgs_dir: Path = DEFAULT_PKGS_DIR,
|
|
48
|
+
registry_url: str = DEFAULT_REGISTRY_URL,
|
|
49
|
+
force: bool = False,
|
|
50
|
+
) -> Path:
|
|
51
|
+
clean_name = pkg_name.removesuffix(".kg").strip()
|
|
52
|
+
if not clean_name:
|
|
53
|
+
raise ValueError("اسم الحزمة غير صالح.")
|
|
54
|
+
|
|
55
|
+
pkgs_dir = Path(pkgs_dir)
|
|
56
|
+
pkgs_dir.mkdir(parents=True, exist_ok=True)
|
|
57
|
+
target_path = pkgs_dir / f"{clean_name}.kg"
|
|
58
|
+
|
|
59
|
+
if target_path.exists() and not force:
|
|
60
|
+
return target_path
|
|
61
|
+
|
|
62
|
+
base_url = registry_url.rstrip("/")
|
|
63
|
+
index_url = f"{base_url}/index.json"
|
|
64
|
+
|
|
65
|
+
try:
|
|
66
|
+
index_data = fetch_json(index_url)
|
|
67
|
+
except urllib.error.URLError as exc:
|
|
68
|
+
raise RuntimeError(f"تعذر الوصول إلى السجل ({index_url}): {exc}") from exc
|
|
69
|
+
|
|
70
|
+
packages_map = resolve_packages_map(index_data)
|
|
71
|
+
pkg_meta = packages_map.get(clean_name)
|
|
72
|
+
if not isinstance(pkg_meta, dict):
|
|
73
|
+
raise FileNotFoundError(f"الحزمة '{clean_name}' غير موجودة في index.json.")
|
|
74
|
+
|
|
75
|
+
rel_file = str(pkg_meta.get("file") or pkg_meta.get("path") or f"pkgs/{clean_name}.kg").lstrip("/")
|
|
76
|
+
pkg_url = f"{base_url}/{rel_file}"
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
content = fetch_bytes(pkg_url)
|
|
80
|
+
except urllib.error.URLError as exc:
|
|
81
|
+
raise RuntimeError(f"فشل تنزيل الحزمة من ({pkg_url}): {exc}") from exc
|
|
82
|
+
|
|
83
|
+
expected_sha256 = pkg_meta.get("sha256")
|
|
84
|
+
if isinstance(expected_sha256, str) and expected_sha256.strip():
|
|
85
|
+
actual_sha256 = compute_sha256(content)
|
|
86
|
+
if actual_sha256.lower() != expected_sha256.strip().lower():
|
|
87
|
+
raise ValueError(
|
|
88
|
+
f"فشل التحقق من SHA-256 للحزمة '{clean_name}': "
|
|
89
|
+
f"المتوقع {expected_sha256} والفعلي {actual_sha256}"
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
tmp_path = target_path.with_suffix(".kg.tmp")
|
|
93
|
+
tmp_path.write_bytes(content)
|
|
94
|
+
tmp_path.replace(target_path)
|
|
95
|
+
return target_path
|