externum 4.2.2__tar.gz → 4.3.0__tar.gz

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 (37) hide show
  1. {externum-4.2.2 → externum-4.3.0}/PKG-INFO +2 -2
  2. {externum-4.2.2 → externum-4.3.0}/README.md +1 -1
  3. {externum-4.2.2 → externum-4.3.0}/externum/__init__.py +1 -1
  4. {externum-4.2.2 → externum-4.3.0}/externum/__main__.py +62 -1
  5. {externum-4.2.2 → externum-4.3.0}/externum/bash_backend.py +23 -0
  6. {externum-4.2.2 → externum-4.3.0}/externum/bytecode.py +40 -4
  7. {externum-4.2.2 → externum-4.3.0}/externum/compiler.py +21 -0
  8. {externum-4.2.2 → externum-4.3.0}/externum/parser.py +11 -0
  9. externum-4.3.0/externum/translate.py +922 -0
  10. {externum-4.2.2 → externum-4.3.0}/externum/vm.py +52 -2
  11. {externum-4.2.2 → externum-4.3.0}/externum.egg-info/PKG-INFO +2 -2
  12. {externum-4.2.2 → externum-4.3.0}/externum.egg-info/SOURCES.txt +3 -0
  13. {externum-4.2.2 → externum-4.3.0}/setup.py +3 -1
  14. externum-4.3.0/tests/test_genexpr_and_vmclass.py +95 -0
  15. externum-4.3.0/tests/test_translate.py +261 -0
  16. externum-4.3.0/tests/test_vm_bytecode.py +102 -0
  17. externum-4.2.2/tests/test_vm_bytecode.py +0 -49
  18. {externum-4.2.2 → externum-4.3.0}/LICENSE +0 -0
  19. {externum-4.2.2 → externum-4.3.0}/externum/analysis.py +0 -0
  20. {externum-4.2.2 → externum-4.3.0}/externum/diagnostics.py +0 -0
  21. {externum-4.2.2 → externum-4.3.0}/externum/drm.py +0 -0
  22. {externum-4.2.2 → externum-4.3.0}/externum/lexer.py +0 -0
  23. {externum-4.2.2 → externum-4.3.0}/externum/runtime/__init__.py +0 -0
  24. {externum-4.2.2 → externum-4.3.0}/externum/runtime/rtlib.py +0 -0
  25. {externum-4.2.2 → externum-4.3.0}/externum/typesys.py +0 -0
  26. {externum-4.2.2 → externum-4.3.0}/externum.egg-info/dependency_links.txt +0 -0
  27. {externum-4.2.2 → externum-4.3.0}/externum.egg-info/entry_points.txt +0 -0
  28. {externum-4.2.2 → externum-4.3.0}/externum.egg-info/top_level.txt +0 -0
  29. {externum-4.2.2 → externum-4.3.0}/setup.cfg +0 -0
  30. {externum-4.2.2 → externum-4.3.0}/tests/test_analysis.py +0 -0
  31. {externum-4.2.2 → externum-4.3.0}/tests/test_diagnostics.py +0 -0
  32. {externum-4.2.2 → externum-4.3.0}/tests/test_differential.py +0 -0
  33. {externum-4.2.2 → externum-4.3.0}/tests/test_drm.py +0 -0
  34. {externum-4.2.2 → externum-4.3.0}/tests/test_externum.py +0 -0
  35. {externum-4.2.2 → externum-4.3.0}/tests/test_extra.py +0 -0
  36. {externum-4.2.2 → externum-4.3.0}/tests/test_smoke.py +0 -0
  37. {externum-4.2.2 → externum-4.3.0}/tests/test_v4.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: externum
3
- Version: 4.2.2
3
+ Version: 4.3.0
4
4
  Summary: A self-hosted programming language blending Python readability, binary performance, and Bash control
5
5
  Home-page: https://github.com/BartoszOsiej/externum
6
6
  Author: Bartosz Osiej
@@ -39,7 +39,7 @@ Dynamic: summary
39
39
 
40
40
  ![License](https://img.shields.io/badge/License-MIT-green?style=flat-square)
41
41
  ![Python](https://img.shields.io/badge/Python-3.10+-3776AB?style=flat-square&logo=python)
42
- ![PyPI](https://img.shields.io/badge/PyPI-externum%402.0.0-3776AB?style=flat-square&logo=pypi)
42
+ ![PyPI](https://img.shields.io/pypi/v/externum?style=flat-square&logo=pypi&logoColor=white)
43
43
  ![Tests](https://img.shields.io/badge/Tests-382%20✓-brightgreen?style=flat-square)
44
44
  ![Docker](https://img.shields.io/badge/Docker-GHCR-2496ED?style=flat-square&logo=docker)
45
45
  [![License: MIT](https://img.shields.io/badge/License-MIT-green?style=flat-square)](https://github.com/BartoszOsiej/externum/blob/main/LICENSE)
@@ -10,7 +10,7 @@
10
10
 
11
11
  ![License](https://img.shields.io/badge/License-MIT-green?style=flat-square)
12
12
  ![Python](https://img.shields.io/badge/Python-3.10+-3776AB?style=flat-square&logo=python)
13
- ![PyPI](https://img.shields.io/badge/PyPI-externum%402.0.0-3776AB?style=flat-square&logo=pypi)
13
+ ![PyPI](https://img.shields.io/pypi/v/externum?style=flat-square&logo=pypi&logoColor=white)
14
14
  ![Tests](https://img.shields.io/badge/Tests-382%20✓-brightgreen?style=flat-square)
15
15
  ![Docker](https://img.shields.io/badge/Docker-GHCR-2496ED?style=flat-square&logo=docker)
16
16
  [![License: MIT](https://img.shields.io/badge/License-MIT-green?style=flat-square)](https://github.com/BartoszOsiej/externum/blob/main/LICENSE)
@@ -14,7 +14,7 @@ One source compiles to EXBC bytecode and runs on the Externum VM,
14
14
  or can still be transpiled to Python for compatibility.
15
15
  """
16
16
 
17
- __version__ = "4.2.2"
17
+ __version__ = "4.3.0"
18
18
  __codename__ = "Abyss"
19
19
 
20
20
  from .analysis import check, check_or_raise, preprocess
@@ -80,6 +80,25 @@ Examples:
80
80
  ide_p = sub.add_parser("ide", help="Launch the Externum TUI IDE")
81
81
  ide_p.add_argument("file", nargs="?", default=None, help="File to open (optional)")
82
82
 
83
+ tr_p = sub.add_parser(
84
+ "translate",
85
+ help="Translate Python or Rust source into Externum (.ext)",
86
+ description="Translate an existing Python or Rust file into Externum source (best-effort, warnings reported).",
87
+ )
88
+ tr_p.add_argument("file", help="Source file (.py or .rs)")
89
+ tr_p.add_argument("-o", "--output", default=None, help="Output .ext file (default: stdout)")
90
+ tr_p.add_argument(
91
+ "--lang",
92
+ choices=["python", "rust", "auto"],
93
+ default="auto",
94
+ help="Source language (default: detected from the extension)",
95
+ )
96
+ tr_p.add_argument(
97
+ "--report",
98
+ action="store_true",
99
+ help="Print the warning/coverage report to stderr",
100
+ )
101
+
83
102
  return p
84
103
 
85
104
 
@@ -100,6 +119,46 @@ def _read_source(path: str) -> str:
100
119
  _last_source: list[str] = [""]
101
120
 
102
121
 
122
+ def cmd_translate(args) -> None:
123
+ """Translate Python/Rust source into Externum (.ext)."""
124
+ import os
125
+
126
+ from .translate import translate_python, translate_rust
127
+
128
+ lang = args.lang
129
+ if lang == "auto":
130
+ ext = os.path.splitext(args.file)[1].lower()
131
+ lang = {".py": "python", ".pyw": "python", ".rs": "rust"}.get(ext)
132
+ if lang is None:
133
+ print(f"Error: cannot detect language from extension '{ext}' — pass --lang python|rust", file=sys.stderr)
134
+ sys.exit(1)
135
+
136
+ source = _read_source(args.file)
137
+ try:
138
+ if lang == "python":
139
+ result, report = translate_python(source, filename=args.file)
140
+ else:
141
+ result, report = translate_rust(source, filename=args.file)
142
+ except SyntaxError as exc:
143
+ from .diagnostics import format_syntax_error
144
+
145
+ if lang == "python":
146
+ print(format_syntax_error(exc, source, filename=args.file), file=sys.stderr)
147
+ else:
148
+ print(f"SyntaxError: {exc}", file=sys.stderr)
149
+ sys.exit(1)
150
+
151
+ if args.output:
152
+ with open(args.output, "w", encoding="utf-8") as fh:
153
+ fh.write(result)
154
+ print(f"Output written to {args.output}")
155
+ else:
156
+ sys.stdout.write(result)
157
+ if args.report:
158
+ print(f"translation report for {args.file}:", file=sys.stderr)
159
+ print(report.summary(), file=sys.stderr)
160
+
161
+
103
162
  def cmd_run(args) -> None:
104
163
  backend = getattr(args, "backend", "vm")
105
164
  if args.file.endswith(".exbc"):
@@ -365,7 +424,7 @@ def main(argv=None) -> None:
365
424
  # Backwards compatible form: `externum file.ext [--target ...]`
366
425
  if (
367
426
  argv
368
- and argv[0] not in ("run", "repl", "compile", "keygen", "check", "vm", "ide")
427
+ and argv[0] not in ("run", "repl", "compile", "keygen", "check", "vm", "ide", "translate")
369
428
  and not argv[0].startswith("-")
370
429
  ):
371
430
  argv = ["compile"] + argv
@@ -383,6 +442,8 @@ def main(argv=None) -> None:
383
442
  cmd_ide(args)
384
443
  elif args.command == "check":
385
444
  cmd_check(args)
445
+ elif args.command == "translate":
446
+ cmd_translate(args)
386
447
  else:
387
448
  cmd_compile(args)
388
449
 
@@ -43,10 +43,33 @@ class BashCodegen:
43
43
  def generate(self) -> tuple[str, list[str]]:
44
44
  self.lines.append("#!/usr/bin/env bash")
45
45
  self.lines.append("set -euo pipefail")
46
+ has_main = any(
47
+ n.type == "FUNCTION" and n.value == "main" for n in self.ast
48
+ )
49
+ main_referenced = False
50
+ if has_main:
51
+ # If any non-FUNCTION top-level statement references main, the
52
+ # script is responsible for calling it — don't add an auto-call.
53
+ for n in self.ast:
54
+ if n.type == "FUNCTION":
55
+ continue
56
+ if self._text(n).find("main") != -1:
57
+ main_referenced = True
58
+ break
46
59
  for node in self.ast:
47
60
  self._stmt(node)
61
+ if has_main and not main_referenced:
62
+ self.lines.append("")
63
+ self.lines.append("main")
48
64
  return "\n".join(self.lines) + "\n", self.warnings
49
65
 
66
+ @staticmethod
67
+ def _text(node: "ASTNode") -> str:
68
+ parts = [str(node.value or "")]
69
+ for c in node.children:
70
+ parts.append(BashCodegen._text(c))
71
+ return " ".join(parts)
72
+
50
73
  # ── statements ──────────────────────────────────────────────────
51
74
  def _stmt(self, node: ASTNode | None):
52
75
  if node is None:
@@ -41,6 +41,7 @@ Opcodes:
41
41
  INTRINSIC=0xF0,
42
42
  """
43
43
 
44
+ import ast as _pyast
44
45
  import struct
45
46
  from dataclasses import dataclass, field
46
47
  from typing import Any
@@ -532,7 +533,14 @@ class BytecodeCompiler:
532
533
 
533
534
  # Store class name
534
535
  idx = self._add_const(name)
535
- self._emit(STORE_VAR, idx)
536
+ # At module level the class lives in VM globals: STORE_VAR at top
537
+ # level lands in the frame's `locals_` dict, which a function body
538
+ # cannot see ("undefined global `X`"). STORE_GLOBAL matches the
539
+ # function path above and the Python backend's semantics.
540
+ if self._current_fn is None:
541
+ self._emit(STORE_GLOBAL, idx)
542
+ else:
543
+ self._emit(STORE_VAR, idx)
536
544
  self._declare_var(name)
537
545
 
538
546
  # Now set each method on the class
@@ -1149,6 +1157,16 @@ class BytecodeCompiler:
1149
1157
  if val == "[]":
1150
1158
  self._emit(MAKE_LIST, 0)
1151
1159
  return
1160
+ # Comprehensions arrive as verbatim text (`[x for x in xs]`). The VM
1161
+ # has no comprehension opcodes; compiling the clause string as a
1162
+ # simple expression used to fail with a confusing "undefined global
1163
+ # `(p`". Raise a clear, actionable error instead.
1164
+ if " for " in val or " if " in val:
1165
+ raise SyntaxError(
1166
+ "the bytecode VM does not support comprehensions yet — "
1167
+ "run with the default backend (`externum run file.ext`), "
1168
+ "or rewrite as an explicit loop"
1169
+ )
1152
1170
  # Parse items from string
1153
1171
  items = self._parse_list_items(val[1:-1]) if val.startswith("[") else []
1154
1172
  for item in items:
@@ -1206,10 +1224,14 @@ class BytecodeCompiler:
1206
1224
  depth += 1
1207
1225
  elif ch == "]":
1208
1226
  depth -= 1
1209
- if bracket > 0:
1227
+ if bracket >= 0:
1210
1228
  obj_name = val[:bracket].strip()
1211
- # Compile the object - could be dotted name or identifier
1212
- if "." in obj_name:
1229
+ # Compile the object - literal, dotted name or identifier
1230
+ if not obj_name:
1231
+ pass # shouldn't happen for INDEX nodes
1232
+ elif obj_name.startswith(("'", '"')):
1233
+ self._emit(LOAD_CONST, self._add_const(_pyast.literal_eval(obj_name)))
1234
+ elif "." in obj_name:
1213
1235
  parts = obj_name.rsplit(".", 1)
1214
1236
  self._compile_name(parts[0])
1215
1237
  self._emit(GET_ATTR, self._add_const(parts[1]))
@@ -1217,6 +1239,20 @@ class BytecodeCompiler:
1217
1239
  self._compile_name(obj_name)
1218
1240
  # Compile the index expression
1219
1241
  idx_expr = val[bracket + 1 : -1].strip()
1242
+ # Slice support: a:b / a:b:c -> build via SLICE opcode with
1243
+ # components on the stack (missing parts -> None)
1244
+ if ":" in idx_expr and not idx_expr.startswith('"') and not idx_expr.startswith("'"):
1245
+ segs = idx_expr.split(":")
1246
+ if 2 <= len(segs) <= 3:
1247
+ for seg in segs:
1248
+ seg = seg.strip()
1249
+ if seg:
1250
+ self._compile_simple_expr(seg)
1251
+ else:
1252
+ self._emit(LOAD_CONST, self._add_const(None))
1253
+ # SLICE carries operand = number of pushed values (3 or 4)
1254
+ self._emit(SLICE, len(segs) + 1)
1255
+ return
1220
1256
  self._compile_simple_expr(idx_expr)
1221
1257
  self._emit(GET_INDEX)
1222
1258
 
@@ -6,6 +6,14 @@ import re
6
6
  from .parser import ASTNode
7
7
 
8
8
 
9
+ def _node_text(node) -> str:
10
+ """Cheap whole-node text dump for 'does top level reference main?' checks."""
11
+ parts = [str(node.value or "")]
12
+ for c in node.children:
13
+ parts.append(_node_text(c))
14
+ return " ".join(parts)
15
+
16
+
9
17
  class Compiler:
10
18
  def __init__(self, ast: list[ASTNode]):
11
19
  self.ast = ast
@@ -29,6 +37,19 @@ class Compiler:
29
37
  if self._has_bash():
30
38
  self.output["python"].insert(0, "import subprocess")
31
39
 
40
+ # v4.3: entrypoint — a module that defines main() but never calls it
41
+ # would exec silently (rc=0, zero output). If main is defined and the
42
+ # top level does not reference it, append an explicit `main()` call
43
+ # so `externum run` actually runs the program.
44
+ has_main = any(
45
+ n.type == "FUNCTION" and n.value == "main" for n in self.ast
46
+ )
47
+ if has_main and not any(
48
+ n.type != "FUNCTION" and "main" in _node_text(n) for n in self.ast
49
+ ):
50
+ self.output["python"].append("if __name__ == '__main__':")
51
+ self.output["python"].append(" main()")
52
+
32
53
  parts = {
33
54
  "python": "\n".join(self.output["python"]),
34
55
  "bash": bash_src,
@@ -1271,6 +1271,17 @@ class Parser:
1271
1271
  args.append(ASTNode("DSTAR", children=[self._parse_expression()]))
1272
1272
  continue
1273
1273
  arg = self._parse_expression()
1274
+ # bare generator expression: `sum(x for x in xs)` (v4.2.3).
1275
+ # Without this, the FOR token after the first expression leaked
1276
+ # into the next argument and codegen produced invalid Python
1277
+ # like `sum(x, for, x in xs)`.
1278
+ if self.pos < len(self.tokens) and self.tokens[self.pos].type == "FOR" and arg.type != "CALL":
1279
+ self.report_genexpr = True
1280
+ comp = self._parse_comprehension_clauses(arg)
1281
+ self._skip_newlines()
1282
+ self._expect("RPAREN")
1283
+ args.append(ASTNode("LIST", value=f"[{comp}]"))
1284
+ break
1274
1285
  if self.pos < len(self.tokens) and self.tokens[self.pos].type == "ASSIGN" and arg.type == "IDENTIFIER":
1275
1286
  self.pos += 1
1276
1287
  val = self._parse_expression()