micalang 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.
mica/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from .engine import MicaEngine, MicaError, MicaLimits, VMHandle
2
+
3
+ __all__ = ["MicaEngine", "MicaError", "MicaLimits", "VMHandle"]
4
+ __version__ = "0.1.0"
mica/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ from .cli import main
2
+
3
+
4
+ if __name__ == "__main__":
5
+ raise SystemExit(main())
mica/cli.py ADDED
@@ -0,0 +1,54 @@
1
+ import argparse
2
+ import sys
3
+ from collections.abc import Sequence
4
+ from pathlib import Path
5
+
6
+ from . import __version__
7
+ from .engine import MicaEngine, MicaError
8
+
9
+
10
+ def _read_stdin() -> str | None:
11
+ value = sys.stdin.readline()
12
+ return value if value else None
13
+
14
+
15
+ def main(argv: Sequence[str] | None = None) -> int:
16
+ parser = argparse.ArgumentParser(
17
+ prog="micalang",
18
+ description="Mica language runtime (Mica-PY reference implementation)",
19
+ )
20
+ parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
21
+ parser.add_argument("source", help="Mica source file")
22
+ args = parser.parse_args(argv)
23
+ try:
24
+ source = Path(args.source).read_text(encoding="utf-8")
25
+ except OSError as exc:
26
+ print(f"mica: unable to read source: {exc.strerror or 'I/O error'}", file=sys.stderr)
27
+ return 1
28
+
29
+ engine = MicaEngine()
30
+ try:
31
+ root = engine.create_root(source, input_provider=_read_stdin)
32
+ except MicaError as exc:
33
+ print(exc, file=sys.stderr)
34
+ return 1
35
+
36
+ try:
37
+ root.run()
38
+ except MicaError as exc:
39
+ sys.stdout.write(root.stdout())
40
+ print(exc, file=sys.stderr)
41
+ return 1
42
+
43
+ sys.stdout.write(root.stdout())
44
+ snapshot = root.snapshot()
45
+ if snapshot["state"] == "waiting_input":
46
+ error = MicaError(
47
+ "input required",
48
+ kind="input_error",
49
+ filename="<mica>",
50
+ line=snapshot["next_line"],
51
+ )
52
+ print(error, file=sys.stderr)
53
+ return 1
54
+ return 0