Datasets:
Download runtime/types.py from Snapkitty/mantra: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/datasets/Snapkitty/mantra/resolve/main/runtime/types.py
- Command line
-
hf download hf://datasets/Snapkitty/mantra/runtime/types.py
-
curl -L -o types.py https://huggingface.co/datasets/Snapkitty/mantra/resolve/main/runtime/types.py
2.92 kB
| # runtime/types.py | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import Any, List, Optional | |
| # ── error hierarchy ──────────────────────────────────────────────────────── | |
| class MantraError(Exception): pass | |
| class DivisionByZero(MantraError): pass | |
| class InvalidShift(MantraError): pass | |
| class InvalidWidth(MantraError): pass | |
| class InvalidHex(MantraError): pass | |
| class InvalidUtf8(MantraError): pass | |
| class IntegerOverflow(MantraError): pass | |
| class InvalidArgument(MantraError): pass | |
| class InvalidType(MantraError): pass | |
| class HashInputError(MantraError): pass | |
| class AssertionFailure(MantraError): pass | |
| class UndefinedName(MantraError): pass | |
| # ── value types ──────────────────────────────────────────────────────────── | |
| class Integer: | |
| value: int | |
| class Boolean: | |
| value: bool | |
| class Byte: | |
| value: int # 0..255 | |
| class Bytes: | |
| data: bytes | |
| class String: | |
| text: str | |
| class FunctionValue: | |
| name: str | |
| params: list | |
| body: list | |
| closure: dict # captured env at definition time | |
| class BuiltinValue: | |
| name: str | |
| fn: Any # callable | |
| class ListValue: | |
| items: list | |
| class DictValue: | |
| fields: dict | |
| class Option: | |
| tag: str # 'some' | 'none' | |
| value: Optional[Any] | |
| class Result: | |
| tag: str # 'ok' | 'err' | |
| value: Any | |
| # ── explicit conversions ─────────────────────────────────────────────────── | |
| def integer_to_u32(i: Integer) -> int: | |
| return i.value & 0xFFFFFFFF | |
| def integer_to_u64(i: Integer) -> int: | |
| return i.value & 0xFFFFFFFFFFFFFFFF | |
| def u32_to_integer(u: int) -> Integer: | |
| if u < 0 or u > 0xFFFFFFFF: | |
| raise InvalidArgument("u32 out of range") | |
| return Integer(u) | |
| def integer_to_bytes(i: Integer, length: int, byteorder: str = 'big', signed: bool = False) -> Bytes: | |
| try: | |
| return Bytes(i.value.to_bytes(length, byteorder, signed=signed)) | |
| except OverflowError: | |
| raise IntegerOverflow("integer does not fit in requested byte length") | |
| def bytes_to_integer(b: Bytes, byteorder: str = 'big', signed: bool = False) -> Integer: | |
| return Integer(int.from_bytes(b.data, byteorder, signed=signed)) | |
| def hex_to_bytes(h: str) -> Bytes: | |
| h = h.removeprefix('0x') | |
| if len(h) % 2 != 0: | |
| raise InvalidHex("hex length must be even") | |
| try: | |
| return Bytes(bytes.fromhex(h)) | |
| except Exception: | |
| raise InvalidHex("invalid hex string") | |
| def bytes_to_hex(b: Bytes) -> String: | |
| return String(b.data.hex()) | |