pyscriptc 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.
Files changed (54) hide show
  1. pyscriptc/__init__.py +3 -0
  2. pyscriptc/__main__.py +3 -0
  3. pyscriptc/_posthog_token.py +1 -0
  4. pyscriptc/builtins.py +156 -0
  5. pyscriptc/canonical.py +167 -0
  6. pyscriptc/check.py +49 -0
  7. pyscriptc/cli.py +271 -0
  8. pyscriptc/coverage.py +388 -0
  9. pyscriptc/debug.py +120 -0
  10. pyscriptc/diagnostics.py +179 -0
  11. pyscriptc/errors.py +56 -0
  12. pyscriptc/ir.py +365 -0
  13. pyscriptc/llvm.py +2129 -0
  14. pyscriptc/log.py +127 -0
  15. pyscriptc/lower/__init__.py +1071 -0
  16. pyscriptc/lower/_calls.py +1499 -0
  17. pyscriptc/lower/_classes.py +969 -0
  18. pyscriptc/lower/_core.py +301 -0
  19. pyscriptc/lower/_expr.py +886 -0
  20. pyscriptc/lower/_stdlib.py +491 -0
  21. pyscriptc/lower/_stdlib_shared.py +93 -0
  22. pyscriptc/lower/_stmt.py +1173 -0
  23. pyscriptc/package.py +249 -0
  24. pyscriptc/restlower.py +1008 -0
  25. pyscriptc/runtime/ps_argv.c +28 -0
  26. pyscriptc/runtime/ps_b64.c +168 -0
  27. pyscriptc/runtime/ps_coll.c +133 -0
  28. pyscriptc/runtime/ps_dt.c +388 -0
  29. pyscriptc/runtime/ps_hash.c +411 -0
  30. pyscriptc/runtime/ps_help.c +219 -0
  31. pyscriptc/runtime/ps_hmac.c +120 -0
  32. pyscriptc/runtime/ps_io.c +198 -0
  33. pyscriptc/runtime/ps_island.c +272 -0
  34. pyscriptc/runtime/ps_json.c +402 -0
  35. pyscriptc/runtime/ps_mix.c +47 -0
  36. pyscriptc/runtime/ps_more.c +1089 -0
  37. pyscriptc/runtime/ps_osmore.c +447 -0
  38. pyscriptc/runtime/ps_path.c +547 -0
  39. pyscriptc/runtime/ps_re.c +1102 -0
  40. pyscriptc/runtime/ps_rt.c +1175 -0
  41. pyscriptc/runtime/ps_rt.h +189 -0
  42. pyscriptc/runtime/ps_struct.c +291 -0
  43. pyscriptc/runtime/ps_time.c +207 -0
  44. pyscriptc/runtime/ps_unicode.c +1521 -0
  45. pyscriptc/runtime/ps_url.c +327 -0
  46. pyscriptc/stdlib_queue.py +191 -0
  47. pyscriptc/stdmore.py +1596 -0
  48. pyscriptc/telemetry.py +219 -0
  49. pyscriptc-0.1.0.dist-info/METADATA +187 -0
  50. pyscriptc-0.1.0.dist-info/RECORD +54 -0
  51. pyscriptc-0.1.0.dist-info/WHEEL +5 -0
  52. pyscriptc-0.1.0.dist-info/entry_points.txt +2 -0
  53. pyscriptc-0.1.0.dist-info/licenses/LICENSE +201 -0
  54. pyscriptc-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,547 @@
1
+ /* posixpath, os, stat predicates, fnmatch, and pathlib.Path.
2
+
3
+ Path objects are `{ PsBuf _path }`. OS errors raise RuntimeError. An unset
4
+ getenv raises KeyError, which is the subset stand-in for None.
5
+ */
6
+
7
+ #include "ps_rt.h"
8
+
9
+ #include <dirent.h>
10
+ #include <errno.h>
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+ #include <sys/stat.h>
15
+ #include <unistd.h>
16
+
17
+ typedef struct {
18
+ PsBuf path;
19
+ } PsPath;
20
+
21
+ /* Field names avoid the st_mtime macro from <sys/stat.h>. Layout is still
22
+ mode, size, mtime, which is what the Stat class stores. */
23
+ typedef struct {
24
+ int64_t mode;
25
+ int64_t size;
26
+ double mtime;
27
+ } PsStat;
28
+
29
+ static void ps_os_fail(const char *what) { ps_raise_cstr(PS_RUNTIME, what); }
30
+
31
+ static char *ps_cstr(int64_t n, const char *s) {
32
+ char *text;
33
+ if (n < 0) {
34
+ ps_raise_code(PS_VALUE);
35
+ }
36
+ text = malloc((size_t)n + 1);
37
+ if (text == NULL) {
38
+ abort();
39
+ }
40
+ if (n > 0) {
41
+ memcpy(text, s, (size_t)n);
42
+ }
43
+ text[n] = '\0';
44
+ return text;
45
+ }
46
+
47
+ static int ps_abs(int64_t n, const char *s) { return n > 0 && s[0] == '/'; }
48
+
49
+ int32_t ps_path_isabs(int64_t n, const char *s) { return ps_abs(n, s) ? 1 : 0; }
50
+
51
+ static void ps_copy_slice(const char *s, int64_t at, int64_t n, PsBuf *out) { ps_buf_copy(out, s + at, n); }
52
+
53
+ void ps_path_base(int64_t n, const char *s, PsBuf *out) {
54
+ int64_t i = n;
55
+ while (i > 0 && s[i - 1] != '/') {
56
+ i -= 1;
57
+ }
58
+ ps_copy_slice(s, i, n - i, out);
59
+ }
60
+
61
+ void ps_path_dir(int64_t n, const char *s, PsBuf *out) {
62
+ int64_t i = n;
63
+ int64_t end;
64
+ while (i > 0 && s[i - 1] != '/') {
65
+ i -= 1;
66
+ }
67
+ end = i;
68
+ if (end > 1) {
69
+ while (end > 1 && s[end - 1] == '/') {
70
+ end -= 1;
71
+ }
72
+ }
73
+ ps_buf_copy(out, s, end);
74
+ }
75
+
76
+ void ps_path_join(PsBuf *parts, int64_t n, PsBuf *out) {
77
+ PsGrow g;
78
+ int64_t i;
79
+ ps_grow_init(&g);
80
+ for (i = 0; i < n; i++) {
81
+ PsBuf part = parts[i];
82
+ if (ps_abs(part.len, part.data)) {
83
+ g.len = 0;
84
+ ps_grow_add(&g, part.data, part.len);
85
+ continue;
86
+ }
87
+ if (g.len > 0 && g.data[g.len - 1] != '/') {
88
+ ps_grow_byte(&g, '/');
89
+ }
90
+ ps_grow_add(&g, part.data, part.len);
91
+ }
92
+ ps_grow_finish(&g, out);
93
+ }
94
+
95
+ PsSeq *ps_path_split(int64_t n, const char *s) {
96
+ PsBuf dir;
97
+ PsBuf base;
98
+ PsSeq *seq = ps_seq_new((int64_t)sizeof(PsBuf), 2);
99
+ ps_path_dir(n, s, &dir);
100
+ ps_path_base(n, s, &base);
101
+ ps_seq_set(seq, 0, &dir);
102
+ ps_seq_set(seq, 1, &base);
103
+ return seq;
104
+ }
105
+
106
+ PsSeq *ps_path_splitext(int64_t n, const char *s) {
107
+ int64_t slash = -1;
108
+ int64_t dot = -1;
109
+ int64_t i;
110
+ PsBuf root;
111
+ PsBuf ext;
112
+ PsSeq *seq;
113
+ for (i = 0; i < n; i++) {
114
+ if (s[i] == '/') {
115
+ slash = i;
116
+ dot = -1;
117
+ } else if (s[i] == '.') {
118
+ dot = i;
119
+ }
120
+ }
121
+ if (dot <= slash + 1) {
122
+ ps_buf_copy(&root, s, n);
123
+ ps_buf_copy(&ext, "", 0);
124
+ } else {
125
+ ps_buf_copy(&root, s, dot);
126
+ ps_buf_copy(&ext, s + dot, n - dot);
127
+ }
128
+ seq = ps_seq_new((int64_t)sizeof(PsBuf), 2);
129
+ ps_seq_set(seq, 0, &root);
130
+ ps_seq_set(seq, 1, &ext);
131
+ return seq;
132
+ }
133
+
134
+ void ps_path_norm(int64_t n, const char *s, PsBuf *out) {
135
+ int absolute = ps_abs(n, s);
136
+ int64_t parts_at[64];
137
+ int64_t parts_n[64];
138
+ int count = 0;
139
+ int64_t i = 0;
140
+ PsGrow g;
141
+ int p;
142
+ while (i < n) {
143
+ int64_t start;
144
+ while (i < n && s[i] == '/') {
145
+ i += 1;
146
+ }
147
+ start = i;
148
+ while (i < n && s[i] != '/') {
149
+ i += 1;
150
+ }
151
+ if (start == i) {
152
+ break;
153
+ }
154
+ if (i - start == 1 && s[start] == '.') {
155
+ continue;
156
+ }
157
+ if (i - start == 2 && s[start] == '.' && s[start + 1] == '.') {
158
+ if (count > 0) {
159
+ count -= 1;
160
+ } else if (!absolute) {
161
+ if (count == 64) {
162
+ ps_raise_cstr(PS_RUNTIME, "path is too deep");
163
+ }
164
+ parts_at[count] = start;
165
+ parts_n[count] = 2;
166
+ count += 1;
167
+ }
168
+ continue;
169
+ }
170
+ if (count == 64) {
171
+ ps_raise_cstr(PS_RUNTIME, "path is too deep");
172
+ }
173
+ parts_at[count] = start;
174
+ parts_n[count] = i - start;
175
+ count += 1;
176
+ }
177
+ ps_grow_init(&g);
178
+ if (absolute) {
179
+ ps_grow_byte(&g, '/');
180
+ }
181
+ if (count == 0 && !absolute) {
182
+ ps_grow_byte(&g, '.');
183
+ }
184
+ for (p = 0; p < count; p++) {
185
+ if (!(absolute && p == 0)) {
186
+ if (g.len > 0 && g.data[g.len - 1] != '/') {
187
+ ps_grow_byte(&g, '/');
188
+ }
189
+ }
190
+ ps_grow_add(&g, s + parts_at[p], parts_n[p]);
191
+ }
192
+ ps_grow_finish(&g, out);
193
+ }
194
+
195
+ void ps_os_getcwd(PsBuf *out) {
196
+ char buf[4096];
197
+ if (getcwd(buf, sizeof buf) == NULL) {
198
+ ps_os_fail("getcwd failed");
199
+ }
200
+ ps_buf_copy(out, buf, (int64_t)strlen(buf));
201
+ }
202
+
203
+ void ps_path_abs(int64_t n, const char *s, PsBuf *out) {
204
+ PsBuf cwd;
205
+ PsBuf joined;
206
+ PsBuf parts[2];
207
+ if (ps_abs(n, s)) {
208
+ ps_path_norm(n, s, out);
209
+ return;
210
+ }
211
+ ps_os_getcwd(&cwd);
212
+ parts[0] = cwd;
213
+ parts[1].len = n;
214
+ parts[1].data = (char *)s;
215
+ ps_path_join(parts, 2, &joined);
216
+ ps_path_norm(joined.len, joined.data, out);
217
+ free(cwd.data);
218
+ free(joined.data);
219
+ }
220
+
221
+ static int ps_stat_into(int64_t n, const char *s, struct stat *st) {
222
+ char *text = ps_cstr(n, s);
223
+ int rc = stat(text, st);
224
+ free(text);
225
+ return rc;
226
+ }
227
+
228
+ int32_t ps_path_exists(int64_t n, const char *s) {
229
+ struct stat st;
230
+ return ps_stat_into(n, s, &st) == 0;
231
+ }
232
+
233
+ int32_t ps_path_isfile(int64_t n, const char *s) {
234
+ struct stat st;
235
+ return ps_stat_into(n, s, &st) == 0 && S_ISREG(st.st_mode);
236
+ }
237
+
238
+ int32_t ps_path_isdir(int64_t n, const char *s) {
239
+ struct stat st;
240
+ return ps_stat_into(n, s, &st) == 0 && S_ISDIR(st.st_mode);
241
+ }
242
+
243
+ void ps_os_remove(int64_t n, const char *s) {
244
+ char *text = ps_cstr(n, s);
245
+ int rc = unlink(text);
246
+ free(text);
247
+ if (rc != 0) {
248
+ ps_os_fail("remove failed");
249
+ }
250
+ }
251
+
252
+ void ps_os_rename(int64_t an, const char *a, int64_t bn, const char *b) {
253
+ char *left = ps_cstr(an, a);
254
+ char *right = ps_cstr(bn, b);
255
+ int rc = rename(left, right);
256
+ free(left);
257
+ free(right);
258
+ if (rc != 0) {
259
+ ps_os_fail("rename failed");
260
+ }
261
+ }
262
+
263
+ void ps_os_makedirs(int64_t n, const char *s) {
264
+ char *text = ps_cstr(n, s);
265
+ int64_t i;
266
+ for (i = 1; i <= n; i++) {
267
+ int rc;
268
+ char save;
269
+ if (i < n && text[i] != '/') {
270
+ continue;
271
+ }
272
+ save = text[i];
273
+ text[i] = '\0';
274
+ rc = mkdir(text, 0755);
275
+ if (rc != 0 && errno == EEXIST && i == n) {
276
+ free(text);
277
+ ps_os_fail("makedirs failed");
278
+ }
279
+ if (rc != 0 && errno != EEXIST) {
280
+ free(text);
281
+ ps_os_fail("makedirs failed");
282
+ }
283
+ text[i] = save;
284
+ }
285
+ free(text);
286
+ }
287
+
288
+ void ps_os_getenv(int64_t n, const char *s, PsBuf *out) {
289
+ char *text = ps_cstr(n, s);
290
+ const char *value = getenv(text);
291
+ free(text);
292
+ if (value == NULL) {
293
+ ps_raise_cstr(PS_KEY, "environment variable is unset");
294
+ }
295
+ ps_buf_copy(out, value, (int64_t)strlen(value));
296
+ }
297
+
298
+ PsSeq *ps_os_listdir(int64_t n, const char *s) {
299
+ char *text = ps_cstr(n, s);
300
+ DIR *dir = opendir(text);
301
+ struct dirent *ent;
302
+ PsSeq *seq;
303
+ free(text);
304
+ if (dir == NULL) {
305
+ ps_os_fail("listdir failed");
306
+ }
307
+ seq = ps_seq_new((int64_t)sizeof(PsBuf), 0);
308
+ while ((ent = readdir(dir)) != NULL) {
309
+ PsBuf name;
310
+ if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
311
+ continue;
312
+ }
313
+ ps_buf_copy(&name, ent->d_name, (int64_t)strlen(ent->d_name));
314
+ ps_seq_push(seq, &name);
315
+ }
316
+ closedir(dir);
317
+ return seq;
318
+ }
319
+
320
+ void ps_os_stat(int64_t n, const char *s, PsStat *out) {
321
+ struct stat st;
322
+ if (ps_stat_into(n, s, &st) != 0) {
323
+ ps_os_fail("stat failed");
324
+ }
325
+ out->mode = (int64_t)st.st_mode;
326
+ out->size = (int64_t)st.st_size;
327
+ out->mtime = (double)st.st_mtime;
328
+ }
329
+
330
+ int32_t ps_stat_isreg(int64_t mode) { return S_ISREG(mode) ? 1 : 0; }
331
+
332
+ int32_t ps_stat_isdir(int64_t mode) { return S_ISDIR(mode) ? 1 : 0; }
333
+
334
+ int32_t ps_stat_islnk(int64_t mode) { return S_ISLNK(mode) ? 1 : 0; }
335
+
336
+ static int ps_match(const char *s, int64_t ns, int64_t si, const char *p, int64_t np, int64_t pi, int fuel) {
337
+ if (fuel <= 0) {
338
+ ps_raise_cstr(PS_RUNTIME, "fnmatch spent its fuel");
339
+ }
340
+ while (pi < np) {
341
+ if (p[pi] == '*') {
342
+ while (pi < np && p[pi] == '*') {
343
+ pi += 1;
344
+ }
345
+ if (pi == np) {
346
+ return 1;
347
+ }
348
+ for (; si <= ns; si++) {
349
+ if (ps_match(s, ns, si, p, np, pi, fuel - 1)) {
350
+ return 1;
351
+ }
352
+ }
353
+ return 0;
354
+ }
355
+ if (si >= ns) {
356
+ return 0;
357
+ }
358
+ if (p[pi] == '?') {
359
+ si += 1;
360
+ pi += 1;
361
+ continue;
362
+ }
363
+ if (p[pi] == '[') {
364
+ int neg = 0;
365
+ int hit = 0;
366
+ pi += 1;
367
+ if (pi < np && p[pi] == '!') {
368
+ neg = 1;
369
+ pi += 1;
370
+ }
371
+ if (pi < np && p[pi] == ']') {
372
+ hit = s[si] == ']';
373
+ pi += 1;
374
+ }
375
+ while (pi < np && p[pi] != ']') {
376
+ char lo = p[pi];
377
+ if (pi + 2 < np && p[pi + 1] == '-' && p[pi + 2] != ']') {
378
+ char hi = p[pi + 2];
379
+ if (s[si] >= lo && s[si] <= hi) {
380
+ hit = 1;
381
+ }
382
+ pi += 3;
383
+ continue;
384
+ }
385
+ if (s[si] == lo) {
386
+ hit = 1;
387
+ }
388
+ pi += 1;
389
+ }
390
+ if (pi >= np || p[pi] != ']') {
391
+ return 0;
392
+ }
393
+ pi += 1;
394
+ if (neg) {
395
+ hit = !hit;
396
+ }
397
+ if (!hit) {
398
+ return 0;
399
+ }
400
+ si += 1;
401
+ continue;
402
+ }
403
+ if (s[si] != p[pi]) {
404
+ return 0;
405
+ }
406
+ si += 1;
407
+ pi += 1;
408
+ }
409
+ return si == ns;
410
+ }
411
+
412
+ int32_t ps_fnmatch(int64_t nn, const char *name, int64_t pn, const char *pat) {
413
+ return ps_match(name, nn, 0, pat, pn, 0, 10000) ? 1 : 0;
414
+ }
415
+
416
+ PsSeq *ps_fnfilter(PsSeq *names, int64_t pn, const char *pat) {
417
+ PsSeq *out = ps_seq_new(names->elem, 0);
418
+ int64_t i;
419
+ for (i = 0; i < names->len; i++) {
420
+ PsBuf name;
421
+ memcpy(&name, (char *)names->data + (size_t)i * (size_t)names->elem, sizeof name);
422
+ if (ps_fnmatch(name.len, name.data, pn, pat)) {
423
+ ps_seq_push(out, &name);
424
+ }
425
+ }
426
+ return out;
427
+ }
428
+
429
+ void ps_path_new(int64_t n, const char *s, PsPath *out) { ps_buf_copy(&out->path, s, n); }
430
+
431
+ void ps_path_posix(PsPath *path, PsBuf *out) { ps_buf_copy(out, path->path.data, path->path.len); }
432
+
433
+ void ps_path_name(PsPath *path, PsBuf *out) { ps_path_base(path->path.len, path->path.data, out); }
434
+
435
+ void ps_path_suffix(PsPath *path, PsBuf *out) {
436
+ PsSeq *parts = ps_path_splitext(path->path.len, path->path.data);
437
+ PsBuf ext;
438
+ ps_seq_get(parts, 1, &ext);
439
+ ps_buf_copy(out, ext.data, ext.len);
440
+ }
441
+
442
+ void ps_path_stem(PsPath *path, PsBuf *out) {
443
+ PsBuf name;
444
+ PsSeq *parts;
445
+ PsBuf root;
446
+ PsBuf ext;
447
+ int64_t slash;
448
+ ps_path_base(path->path.len, path->path.data, &name);
449
+ parts = ps_path_splitext(name.len, name.data);
450
+ ps_seq_get(parts, 0, &root);
451
+ ps_seq_get(parts, 1, &ext);
452
+ (void)slash;
453
+ ps_buf_copy(out, root.data, root.len);
454
+ }
455
+
456
+ void ps_path_parent(PsPath *path, PsPath *out) {
457
+ PsBuf dir;
458
+ ps_path_dir(path->path.len, path->path.data, &dir);
459
+ if (dir.len == 0) {
460
+ ps_buf_copy(&out->path, ".", 1);
461
+ return;
462
+ }
463
+ out->path = dir;
464
+ }
465
+
466
+ int32_t ps_path_exists_p(PsPath *path) { return ps_path_exists(path->path.len, path->path.data); }
467
+
468
+ int32_t ps_path_isfile_p(PsPath *path) { return ps_path_isfile(path->path.len, path->path.data); }
469
+
470
+ int32_t ps_path_isdir_p(PsPath *path) { return ps_path_isdir(path->path.len, path->path.data); }
471
+
472
+ static void ps_read_file(int64_t n, const char *path, PsBuf *out) {
473
+ char *text = ps_cstr(n, path);
474
+ FILE *fp = fopen(text, "rb");
475
+ long size;
476
+ char *buf;
477
+ free(text);
478
+ if (fp == NULL) {
479
+ ps_os_fail("read failed");
480
+ }
481
+ if (fseek(fp, 0, SEEK_END) != 0) {
482
+ fclose(fp);
483
+ ps_os_fail("read failed");
484
+ }
485
+ size = ftell(fp);
486
+ if (size < 0) {
487
+ fclose(fp);
488
+ ps_os_fail("read failed");
489
+ }
490
+ if (fseek(fp, 0, SEEK_SET) != 0) {
491
+ fclose(fp);
492
+ ps_os_fail("read failed");
493
+ }
494
+ buf = malloc((size_t)size + 1);
495
+ if (buf == NULL) {
496
+ fclose(fp);
497
+ abort();
498
+ }
499
+ if (size > 0 && fread(buf, 1, (size_t)size, fp) != (size_t)size) {
500
+ free(buf);
501
+ fclose(fp);
502
+ ps_os_fail("read failed");
503
+ }
504
+ fclose(fp);
505
+ buf[size] = '\0';
506
+ out->len = (int64_t)size;
507
+ out->data = buf;
508
+ }
509
+
510
+ static int64_t ps_write_file(int64_t n, const char *path, int64_t bn, const char *bytes) {
511
+ char *text = ps_cstr(n, path);
512
+ FILE *fp = fopen(text, "wb");
513
+ free(text);
514
+ if (fp == NULL) {
515
+ ps_os_fail("write failed");
516
+ }
517
+ if (bn > 0 && fwrite(bytes, 1, (size_t)bn, fp) != (size_t)bn) {
518
+ fclose(fp);
519
+ ps_os_fail("write failed");
520
+ }
521
+ if (fclose(fp) != 0) {
522
+ ps_os_fail("write failed");
523
+ }
524
+ return bn;
525
+ }
526
+
527
+ void ps_path_read_text(PsPath *path, PsBuf *out) { ps_read_file(path->path.len, path->path.data, out); }
528
+
529
+ void ps_path_read_bytes(PsPath *path, PsBuf *out) { ps_read_file(path->path.len, path->path.data, out); }
530
+
531
+ int64_t ps_path_write_text(PsPath *path, int64_t n, const char *s) {
532
+ return ps_write_file(path->path.len, path->path.data, n, s);
533
+ }
534
+
535
+ int64_t ps_path_write_bytes(PsPath *path, int64_t n, const char *s) {
536
+ return ps_write_file(path->path.len, path->path.data, n, s);
537
+ }
538
+
539
+ void ps_path_joinpath(PsPath *path, int64_t n, const char *s, PsPath *out) {
540
+ PsBuf parts[2];
541
+ PsBuf joined;
542
+ parts[0] = path->path;
543
+ parts[1].len = n;
544
+ parts[1].data = (char *)s;
545
+ ps_path_join(parts, 2, &joined);
546
+ out->path = joined;
547
+ }