phyx 1.6.1__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.
- phyx/__init__.py +37 -0
- phyx/__main__.py +5 -0
- phyx/ast.py +139 -0
- phyx/builtins.py +485 -0
- phyx/cli.py +619 -0
- phyx/compiler.py +879 -0
- phyx/conjugation.py +142 -0
- phyx/errors.py +84 -0
- phyx/fmt.py +93 -0
- phyx/fonts.py +141 -0
- phyx/hints.py +358 -0
- phyx/interpreter.py +379 -0
- phyx/lexer.py +260 -0
- phyx/lib/as/311/231r/316/270.phyx +26 -0
- phyx/lib/compleat.phyx +4 -0
- phyx/lib//305/245ewn.phyx +37 -0
- phyx/lint.py +193 -0
- phyx/numerals.py +55 -0
- phyx/parser.py +574 -0
- phyx/paths.py +116 -0
- phyx/render.py +214 -0
- phyx/repl.py +232 -0
- phyx/testing.py +123 -0
- phyx/values.py +176 -0
- phyx/vertical.py +66 -0
- phyx/vocabulary.py +140 -0
- phyx-1.6.1.dist-info/METADATA +154 -0
- phyx-1.6.1.dist-info/RECORD +32 -0
- phyx-1.6.1.dist-info/WHEEL +5 -0
- phyx-1.6.1.dist-info/entry_points.txt +5 -0
- phyx-1.6.1.dist-info/licenses/LICENSE +21 -0
- phyx-1.6.1.dist-info/top_level.txt +1 -0
phyx/__init__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""φyrx (PhyrxScript / PhyrexianScript) — a programming language written in Phyrexian.
|
|
2
|
+
|
|
3
|
+
Vocabulary, orthography and grammar follow the community Phyrexian
|
|
4
|
+
Dictionary (https://fxbuson.github.io/PhyrexianDictionary/):
|
|
5
|
+
|
|
6
|
+
* sentences start with ``^`` / ``!`` / ``?`` and end with ``.`` or ``:``
|
|
7
|
+
* words are separated by ``-`` (or a line break), phrases by ``,``
|
|
8
|
+
* the verb always comes last (SOV), so expressions are postfix
|
|
9
|
+
* every phrase opens with a mood/tense marker which selects the statement
|
|
10
|
+
* numbers are hexadecimal (Arabic or Basilica Mathematical Monospace)
|
|
11
|
+
* verbs may take chapter-3 person inflection; tense stays on the mood marker
|
|
12
|
+
* text is read vertically: symbols top-to-bottom, lines left-to-right
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from .errors import PhyrexianError, format_error
|
|
16
|
+
from .lexer import tokenize, transliterate
|
|
17
|
+
from .parser import parse
|
|
18
|
+
from .interpreter import Interpreter, run_source
|
|
19
|
+
from .repl import Repl, is_complete
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"PhyrexianError",
|
|
23
|
+
"format_error",
|
|
24
|
+
"tokenize",
|
|
25
|
+
"transliterate",
|
|
26
|
+
"parse",
|
|
27
|
+
"Interpreter",
|
|
28
|
+
"run_source",
|
|
29
|
+
"Repl",
|
|
30
|
+
"is_complete",
|
|
31
|
+
"LANGUAGE",
|
|
32
|
+
"LANGUAGE_ALIASES",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
__version__ = "1.6.1"
|
|
36
|
+
LANGUAGE = "φyrx"
|
|
37
|
+
LANGUAGE_ALIASES = ("PhyrxScript", "PhyrexianScript")
|
phyx/__main__.py
ADDED
phyx/ast.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Syntax tree for φyrx programs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
from .vocabulary import Verb
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# ---------------------------------------------------------------- expressions
|
|
11
|
+
@dataclass
|
|
12
|
+
class Expr:
|
|
13
|
+
line: int = field(default=0, kw_only=True)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class Literal(Expr):
|
|
18
|
+
value: object
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class Name(Expr):
|
|
23
|
+
name: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class Call(Expr):
|
|
28
|
+
"""Built-in verb applied to the objects that precede it."""
|
|
29
|
+
|
|
30
|
+
verb: Verb
|
|
31
|
+
args: list[Expr]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class UserCall(Expr):
|
|
36
|
+
"""A user-defined ability (``ťe`` sentence) applied to its objects."""
|
|
37
|
+
|
|
38
|
+
name: str
|
|
39
|
+
args: list[Expr]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class Assign(Expr):
|
|
44
|
+
"""``value target ţə''`` — put value into target."""
|
|
45
|
+
|
|
46
|
+
target: Expr # Name or Call(tapq)
|
|
47
|
+
value: Expr
|
|
48
|
+
const: bool = False
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass
|
|
52
|
+
class Return(Expr):
|
|
53
|
+
value: Expr | None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass
|
|
57
|
+
class Break(Expr):
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass
|
|
62
|
+
class Continue(Expr):
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# ----------------------------------------------------------------- statements
|
|
67
|
+
@dataclass
|
|
68
|
+
class Stmt:
|
|
69
|
+
line: int = field(default=0, kw_only=True)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class ExprStmt(Stmt):
|
|
74
|
+
exprs: list[Expr]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class Print(Stmt):
|
|
79
|
+
"""``!`` exclamatory sentence."""
|
|
80
|
+
|
|
81
|
+
exprs: list[Expr]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class Input(Stmt):
|
|
86
|
+
"""``?`` interrogative sentence: ask, store the answer in ``target``."""
|
|
87
|
+
|
|
88
|
+
prompt: list[Expr]
|
|
89
|
+
target: Expr
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass
|
|
93
|
+
class If(Stmt):
|
|
94
|
+
cond: Expr
|
|
95
|
+
body: list[Stmt]
|
|
96
|
+
negate: bool = False # ``bu`` / ``əx`` — unless
|
|
97
|
+
orelse: list[Stmt] | None = None
|
|
98
|
+
chained: bool = False # produced by ``qit-ba`` (else-if); qit attaches to it
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@dataclass
|
|
102
|
+
class While(Stmt):
|
|
103
|
+
cond: Expr
|
|
104
|
+
body: list[Stmt]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@dataclass
|
|
108
|
+
class For(Stmt):
|
|
109
|
+
var: str
|
|
110
|
+
iterable: Expr
|
|
111
|
+
body: list[Stmt]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass
|
|
115
|
+
class FuncDef(Stmt):
|
|
116
|
+
name: str
|
|
117
|
+
params: list[str]
|
|
118
|
+
body: list[Stmt]
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@dataclass
|
|
122
|
+
class Assert(Stmt):
|
|
123
|
+
cond: Expr
|
|
124
|
+
message: Expr | None
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@dataclass
|
|
128
|
+
class Try(Stmt):
|
|
129
|
+
body: list[Stmt]
|
|
130
|
+
handler: list[Stmt] | None = None
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@dataclass
|
|
134
|
+
class Else(Stmt):
|
|
135
|
+
"""Transient: a ``qit`` sentence waiting to be attached to the sentence
|
|
136
|
+
before it. Never survives parsing."""
|
|
137
|
+
|
|
138
|
+
body: list[Stmt]
|
|
139
|
+
is_try_handler: bool = False
|
phyx/builtins.py
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
"""Implementations of the built-in verbs (see :mod:`phyx.vocabulary`).
|
|
2
|
+
|
|
3
|
+
Each implementation receives the interpreter and the already-evaluated
|
|
4
|
+
objects, in the order they were written (objects first, verb last)."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import TYPE_CHECKING, Callable
|
|
9
|
+
|
|
10
|
+
from .errors import RuntimeFault, UserMistake
|
|
11
|
+
from .paths import note_hint, resolve_note, resolve_spread
|
|
12
|
+
from .values import (
|
|
13
|
+
Ability,
|
|
14
|
+
is_number,
|
|
15
|
+
kind_of,
|
|
16
|
+
parse_number_text,
|
|
17
|
+
to_source,
|
|
18
|
+
to_text,
|
|
19
|
+
truthy,
|
|
20
|
+
type_word,
|
|
21
|
+
values_equal,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING: # pragma: no cover
|
|
25
|
+
from .interpreter import Interpreter
|
|
26
|
+
|
|
27
|
+
Impl = Callable[["Interpreter", list], object]
|
|
28
|
+
IMPLS: dict[str, Impl] = {}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def verb(word: str):
|
|
32
|
+
def register(fn: Impl) -> Impl:
|
|
33
|
+
IMPLS[word] = fn
|
|
34
|
+
return fn
|
|
35
|
+
return register
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _kinds(*vals: object) -> str:
|
|
39
|
+
return " and ".join(type_word(v) + f" ({kind_of(v)})" for v in vals)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _need_number(v: object, verb_word: str) -> int:
|
|
43
|
+
if not is_number(v):
|
|
44
|
+
raise RuntimeFault(f"'{verb_word}' needs a number, got {_kinds(v)}")
|
|
45
|
+
return v # type: ignore[return-value]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _need_list(v: object, verb_word: str) -> list:
|
|
49
|
+
if not isinstance(v, list):
|
|
50
|
+
raise RuntimeFault(f"'{verb_word}' needs a list, got {_kinds(v)}")
|
|
51
|
+
return v
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _index(coll: list | str, key: object, verb_word: str) -> int:
|
|
55
|
+
if not is_number(key):
|
|
56
|
+
raise RuntimeFault(f"'{verb_word}' needs a number to pick from a {kind_of(coll)}, got {_kinds(key)}")
|
|
57
|
+
n = len(coll)
|
|
58
|
+
i = key if key >= 0 else n + key # type: ignore[operator]
|
|
59
|
+
if not 0 <= i < n: # type: ignore[operator]
|
|
60
|
+
raise RuntimeFault(
|
|
61
|
+
f"the {kind_of(coll)} has {to_source(n)} items; there is no item {to_source(key)}")
|
|
62
|
+
return i # type: ignore[return-value]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _hashable(key: object, verb_word: str) -> object:
|
|
66
|
+
if isinstance(key, (list, dict)):
|
|
67
|
+
raise RuntimeFault(f"a book cannot be keyed by a {kind_of(key)} ('{verb_word}')")
|
|
68
|
+
return key
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
# ---------------------------------------------------------------- arithmetic
|
|
72
|
+
@verb("babg")
|
|
73
|
+
def _add(interp, args):
|
|
74
|
+
a, b = args
|
|
75
|
+
if is_number(a) and is_number(b):
|
|
76
|
+
return a + b
|
|
77
|
+
if isinstance(a, str) and isinstance(b, str):
|
|
78
|
+
return a + b
|
|
79
|
+
if isinstance(a, list) and isinstance(b, list):
|
|
80
|
+
return a + b
|
|
81
|
+
raise RuntimeFault(
|
|
82
|
+
f"cannot add {_kinds(a, b)}",
|
|
83
|
+
hint="babg adds two numbers, two texts, or two lists",
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@verb("mɒɣg")
|
|
88
|
+
def _sub(interp, args):
|
|
89
|
+
a, b = args
|
|
90
|
+
return _need_number(a, "mɒɣg") - _need_number(b, "mɒɣg")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@verb("tøwp")
|
|
94
|
+
def _mul(interp, args):
|
|
95
|
+
a, b = args
|
|
96
|
+
if is_number(a) and is_number(b):
|
|
97
|
+
return a * b
|
|
98
|
+
if isinstance(a, (str, list)) and is_number(b):
|
|
99
|
+
return a * b
|
|
100
|
+
raise RuntimeFault(f"cannot multiply {_kinds(a, b)}")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@verb("χɬkevk")
|
|
104
|
+
def _div(interp, args):
|
|
105
|
+
a, b = args
|
|
106
|
+
a = _need_number(a, "χɬkevk")
|
|
107
|
+
b = _need_number(b, "χɬkevk")
|
|
108
|
+
if b == 0:
|
|
109
|
+
raise RuntimeFault(
|
|
110
|
+
"nothing can be distributed into zero parts",
|
|
111
|
+
hint="χɬkevk is floor division; the second object is the number of parts and cannot be 0",
|
|
112
|
+
)
|
|
113
|
+
return a // b
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@verb("geft")
|
|
117
|
+
def _mod(interp, args):
|
|
118
|
+
a, b = args
|
|
119
|
+
a = _need_number(a, "geft")
|
|
120
|
+
b = _need_number(b, "geft")
|
|
121
|
+
if b == 0:
|
|
122
|
+
raise RuntimeFault("nothing remains when dividing by zero")
|
|
123
|
+
return a % b
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@verb("ķus'ər")
|
|
127
|
+
def _twice(interp, args):
|
|
128
|
+
return _need_number(args[0], "ķus'ər") * 2
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@verb("əxķus'ər")
|
|
132
|
+
def _half(interp, args):
|
|
133
|
+
return _need_number(args[0], "əxķus'ər") // 2
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@verb("lu'f")
|
|
137
|
+
def _pow(interp, args):
|
|
138
|
+
a = _need_number(args[0], "lu'f")
|
|
139
|
+
b = _need_number(args[1], "lu'f")
|
|
140
|
+
if b < 0:
|
|
141
|
+
raise RuntimeFault("lu'f (energize) cannot raise to a negative power")
|
|
142
|
+
if b > 10_000:
|
|
143
|
+
raise RuntimeFault("lu'f (energize): the exponent is too great")
|
|
144
|
+
return a ** b
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@verb("'uɬč")
|
|
148
|
+
def _random(interp, args):
|
|
149
|
+
n = _need_number(args[0], "'uɬč")
|
|
150
|
+
if n <= 0:
|
|
151
|
+
raise RuntimeFault("'uɬč (astray) needs a bound greater than zero")
|
|
152
|
+
return interp.rng.randrange(n)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
# ---------------------------------------------------------- comparison/logic
|
|
156
|
+
@verb("dž'ynpp")
|
|
157
|
+
def _eq(interp, args):
|
|
158
|
+
return values_equal(args[0], args[1])
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _ordered(a: object, b: object, word: str) -> tuple:
|
|
162
|
+
if is_number(a) and is_number(b):
|
|
163
|
+
return a, b
|
|
164
|
+
if isinstance(a, str) and isinstance(b, str):
|
|
165
|
+
return a, b
|
|
166
|
+
raise RuntimeFault(f"'{word}' cannot order {_kinds(a, b)}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@verb("grmɒɣg")
|
|
170
|
+
def _lt(interp, args):
|
|
171
|
+
a, b = _ordered(args[0], args[1], "grmɒɣg")
|
|
172
|
+
return a < b
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@verb("korxgr")
|
|
176
|
+
def _gt(interp, args):
|
|
177
|
+
a, b = _ordered(args[0], args[1], "korxgr")
|
|
178
|
+
return a > b
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@verb("əx")
|
|
182
|
+
def _not(interp, args):
|
|
183
|
+
return not truthy(args[0])
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@verb("xə")
|
|
187
|
+
def _and(interp, args):
|
|
188
|
+
return truthy(args[0]) and truthy(args[1])
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
@verb("ðə")
|
|
192
|
+
def _or(interp, args):
|
|
193
|
+
return truthy(args[0]) or truthy(args[1])
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@verb("ʁšθakɴ")
|
|
197
|
+
def _choose(interp, args):
|
|
198
|
+
cond, a, b = args
|
|
199
|
+
return a if truthy(cond) else b
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
# -------------------------------------------------------------- input/output
|
|
203
|
+
@verb("pihš")
|
|
204
|
+
def _speak(interp, args):
|
|
205
|
+
interp.write(to_text(args[0]) + "\n")
|
|
206
|
+
return args[0]
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
@verb("krepg")
|
|
210
|
+
def _reveal(interp, args):
|
|
211
|
+
interp.write(to_source(args[0]) + "\n")
|
|
212
|
+
return args[0]
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@verb("'ətsxn")
|
|
216
|
+
def _ask(interp, args):
|
|
217
|
+
return interp.read_line(to_text(args[0]))
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# ---------------------------------------------------------------- conversion
|
|
221
|
+
@verb("asərθ")
|
|
222
|
+
def _number(interp, args):
|
|
223
|
+
v = args[0]
|
|
224
|
+
if is_number(v):
|
|
225
|
+
return v
|
|
226
|
+
if isinstance(v, bool):
|
|
227
|
+
return 1 if v else 0
|
|
228
|
+
if isinstance(v, str):
|
|
229
|
+
return parse_number_text(v)
|
|
230
|
+
raise RuntimeFault(f"cannot turn {_kinds(v)} into a number")
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
@verb("xušv")
|
|
234
|
+
def _voice(interp, args):
|
|
235
|
+
return to_text(args[0])
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@verb("πøkk")
|
|
239
|
+
def _type(interp, args):
|
|
240
|
+
return type_word(args[0])
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
# --------------------------------------------------------------- collections
|
|
244
|
+
@verb("ťewn")
|
|
245
|
+
def _list(interp, args):
|
|
246
|
+
return list(args)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
@verb("tekt")
|
|
250
|
+
def _book(interp, args):
|
|
251
|
+
book: dict = {}
|
|
252
|
+
for i in range(0, len(args), 2):
|
|
253
|
+
book[_hashable(args[i], "tekt")] = args[i + 1]
|
|
254
|
+
return book
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
@verb("ronɣ")
|
|
258
|
+
def _turns(interp, args):
|
|
259
|
+
n = _need_number(args[0], "ronɣ")
|
|
260
|
+
return list(range(n))
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
@verb("tapq")
|
|
264
|
+
def _get(interp, args):
|
|
265
|
+
coll, key = args
|
|
266
|
+
if isinstance(coll, (list, str)):
|
|
267
|
+
return coll[_index(coll, key, "tapq")]
|
|
268
|
+
if isinstance(coll, dict):
|
|
269
|
+
k = _hashable(key, "tapq")
|
|
270
|
+
if k not in coll:
|
|
271
|
+
raise RuntimeFault(f"the book has no entry {to_source(key)}")
|
|
272
|
+
return coll[k]
|
|
273
|
+
raise RuntimeFault(f"cannot get from {_kinds(coll)}")
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def put_into(coll: object, key: object, value: object) -> None:
|
|
277
|
+
"""``value coll key tapq ţə''`` — assignment into a place."""
|
|
278
|
+
if isinstance(coll, list):
|
|
279
|
+
coll[_index(coll, key, "ţə''")] = value
|
|
280
|
+
return
|
|
281
|
+
if isinstance(coll, dict):
|
|
282
|
+
coll[_hashable(key, "ţə''")] = value
|
|
283
|
+
return
|
|
284
|
+
if isinstance(coll, str):
|
|
285
|
+
raise RuntimeFault("text cannot be changed in place; build a new text instead")
|
|
286
|
+
raise RuntimeFault(f"cannot put into {_kinds(coll)}")
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
@verb("bɬølπ")
|
|
290
|
+
def _fill(interp, args):
|
|
291
|
+
coll, item = args
|
|
292
|
+
_need_list(coll, "bɬølπ").append(item)
|
|
293
|
+
return coll
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
@verb("čxət'")
|
|
297
|
+
def _exile(interp, args):
|
|
298
|
+
coll, key = args
|
|
299
|
+
if isinstance(coll, list):
|
|
300
|
+
return coll.pop(_index(coll, key, "čxət'"))
|
|
301
|
+
if isinstance(coll, dict):
|
|
302
|
+
k = _hashable(key, "čxət'")
|
|
303
|
+
if k not in coll:
|
|
304
|
+
raise RuntimeFault(f"the book has no entry {to_source(key)}")
|
|
305
|
+
return coll.pop(k)
|
|
306
|
+
raise RuntimeFault(f"cannot exile from {_kinds(coll)}")
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
@verb("'iixtfu'q")
|
|
310
|
+
def _first(interp, args):
|
|
311
|
+
coll = args[0]
|
|
312
|
+
if isinstance(coll, (list, str)):
|
|
313
|
+
if not coll:
|
|
314
|
+
raise RuntimeFault(f"an empty {kind_of(coll)} has no beginning")
|
|
315
|
+
return coll[0]
|
|
316
|
+
raise RuntimeFault(f"{_kinds(coll)} has no beginning")
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
@verb("ǩedžylqitsodrm")
|
|
320
|
+
def _last(interp, args):
|
|
321
|
+
coll = args[0]
|
|
322
|
+
if isinstance(coll, (list, str)):
|
|
323
|
+
if not coll:
|
|
324
|
+
raise RuntimeFault(f"an empty {kind_of(coll)} has no bottom")
|
|
325
|
+
return coll[-1]
|
|
326
|
+
raise RuntimeFault(f"{_kinds(coll)} has no bottom")
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
@verb("samk")
|
|
330
|
+
def _length(interp, args):
|
|
331
|
+
coll = args[0]
|
|
332
|
+
if isinstance(coll, (list, str, dict)):
|
|
333
|
+
return len(coll)
|
|
334
|
+
raise RuntimeFault(f"{_kinds(coll)} has no length")
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
@verb("dl")
|
|
338
|
+
def _in(interp, args):
|
|
339
|
+
item, coll = args
|
|
340
|
+
if isinstance(coll, list):
|
|
341
|
+
return any(values_equal(item, x) for x in coll)
|
|
342
|
+
if isinstance(coll, dict):
|
|
343
|
+
return _hashable(item, "dl") in coll
|
|
344
|
+
if isinstance(coll, str):
|
|
345
|
+
if not isinstance(item, str):
|
|
346
|
+
raise RuntimeFault(f"only text can be found inside text, not {_kinds(item)}")
|
|
347
|
+
return item in coll
|
|
348
|
+
raise RuntimeFault(f"nothing can be inside {_kinds(coll)}")
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
@verb("rukxtekt")
|
|
352
|
+
def _keys(interp, args):
|
|
353
|
+
book = args[0]
|
|
354
|
+
if not isinstance(book, dict):
|
|
355
|
+
raise RuntimeFault(f"'rukxtekt' needs a book, got {_kinds(book)}")
|
|
356
|
+
return list(book.keys())
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@verb("tluxt")
|
|
360
|
+
def _copy(interp, args):
|
|
361
|
+
v = args[0]
|
|
362
|
+
if isinstance(v, list):
|
|
363
|
+
return list(v)
|
|
364
|
+
if isinstance(v, dict):
|
|
365
|
+
return dict(v)
|
|
366
|
+
return v
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
@verb("ɣɒzɢ")
|
|
370
|
+
def _slice(interp, args):
|
|
371
|
+
coll, start, end = args
|
|
372
|
+
if not isinstance(coll, (list, str)):
|
|
373
|
+
raise RuntimeFault(f"cannot flay {_kinds(coll)}")
|
|
374
|
+
if start is not None:
|
|
375
|
+
_need_number(start, "ɣɒzɢ")
|
|
376
|
+
if end is not None:
|
|
377
|
+
_need_number(end, "ɣɒzɢ")
|
|
378
|
+
return coll[start:end]
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
@verb("pən'g")
|
|
382
|
+
def _split(interp, args):
|
|
383
|
+
text, sep = args
|
|
384
|
+
if not isinstance(text, str) or not isinstance(sep, str):
|
|
385
|
+
raise RuntimeFault(f"'pən'g' breaks text with text, got {_kinds(text, sep)}")
|
|
386
|
+
if sep == "":
|
|
387
|
+
return list(text)
|
|
388
|
+
return text.split(sep)
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
@verb("raφoj")
|
|
392
|
+
def _join(interp, args):
|
|
393
|
+
items, sep = args
|
|
394
|
+
if not isinstance(sep, str):
|
|
395
|
+
raise RuntimeFault(f"'raφoj' links with text, got {_kinds(sep)}")
|
|
396
|
+
return sep.join(to_text(x) for x in _need_list(items, "raφoj"))
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
# -------------------------------------------------------------------- errors
|
|
400
|
+
@verb("ɬšeɴazv")
|
|
401
|
+
def _mistake(interp, args):
|
|
402
|
+
raise UserMistake(args[0])
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
@verb("qab'")
|
|
406
|
+
def _kill(interp, args):
|
|
407
|
+
raise SystemExit(_need_number(args[0], "qab'"))
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
@verb("bukð")
|
|
411
|
+
def _cast(interp, args):
|
|
412
|
+
path = args[0]
|
|
413
|
+
if not isinstance(path, str):
|
|
414
|
+
raise RuntimeFault(f"'bukð' (play/cast) wants a path as text, got {_kinds(path)}")
|
|
415
|
+
interp.include(path)
|
|
416
|
+
return None
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
@verb("naķm")
|
|
420
|
+
def _work(interp, args):
|
|
421
|
+
ability = args[0]
|
|
422
|
+
if not isinstance(ability, Ability):
|
|
423
|
+
raise RuntimeFault(f"'naķm' (work) wants an ability, got {_kinds(ability)}")
|
|
424
|
+
return ability
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
@verb("'uxq")
|
|
428
|
+
def _command(interp, args):
|
|
429
|
+
return list(interp.argv)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
@verb("čtəčpliɣn")
|
|
433
|
+
def _activate(interp, args):
|
|
434
|
+
argv, ability = args
|
|
435
|
+
if not isinstance(ability, Ability):
|
|
436
|
+
raise RuntimeFault(f"'čtəčpliɣn' (activate) wants an ability, got {_kinds(ability)}")
|
|
437
|
+
if not isinstance(argv, list):
|
|
438
|
+
raise RuntimeFault(
|
|
439
|
+
f"'čtəčpliɣn' wants a list of objects before the ability, got {_kinds(argv)}"
|
|
440
|
+
)
|
|
441
|
+
if len(argv) != len(ability.params):
|
|
442
|
+
raise RuntimeFault(
|
|
443
|
+
f"'{ability.name}' wants {len(ability.params)} object"
|
|
444
|
+
f"{'' if len(ability.params) == 1 else 's'}, "
|
|
445
|
+
f"this list has {len(argv)}"
|
|
446
|
+
)
|
|
447
|
+
return interp.call(ability, argv)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
@verb("brøtjt")
|
|
451
|
+
def _research(interp, args):
|
|
452
|
+
"""Read a UTF-8 note from a path (dictionary: research)."""
|
|
453
|
+
path = args[0]
|
|
454
|
+
if not isinstance(path, str):
|
|
455
|
+
raise RuntimeFault(f"'brøtjt' (research) wants a path as text, got {_kinds(path)}")
|
|
456
|
+
p = resolve_note(path, interp.base_dir)
|
|
457
|
+
if p is None:
|
|
458
|
+
raise RuntimeFault(
|
|
459
|
+
f"brøtjt (research) cannot read '{path}'",
|
|
460
|
+
hint=note_hint(path, interp.base_dir),
|
|
461
|
+
)
|
|
462
|
+
try:
|
|
463
|
+
return p.read_text(encoding="utf-8")
|
|
464
|
+
except OSError as err:
|
|
465
|
+
raise RuntimeFault(
|
|
466
|
+
f"brøtjt (research) cannot read '{path}': {err}",
|
|
467
|
+
hint=note_hint(path, interp.base_dir),
|
|
468
|
+
) from err
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
@verb("mɴyrx")
|
|
472
|
+
def _spread(interp, args):
|
|
473
|
+
"""Write text onto a path (dictionary: fire/spread). Yields the text."""
|
|
474
|
+
text, path = args
|
|
475
|
+
if not isinstance(text, str) or not isinstance(path, str):
|
|
476
|
+
raise RuntimeFault(
|
|
477
|
+
f"'mɴyrx' (spread) writes text onto a path, got {_kinds(text, path)}"
|
|
478
|
+
)
|
|
479
|
+
p = resolve_spread(path, interp.base_dir)
|
|
480
|
+
try:
|
|
481
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
482
|
+
p.write_text(text, encoding="utf-8")
|
|
483
|
+
except OSError as err:
|
|
484
|
+
raise RuntimeFault(f"mɴyrx (spread) cannot write '{path}': {err}") from err
|
|
485
|
+
return text
|