sipl-interpreter 0.2.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.
- sipl/__init__.py +0 -0
- sipl/__main__.py +5 -0
- sipl/ast.py +116 -0
- sipl/cli.py +231 -0
- sipl/dap/__init__.py +1 -0
- sipl/dap/__main__.py +5 -0
- sipl/dap/protocol.py +57 -0
- sipl/dap/server.py +319 -0
- sipl/grammar/sipl.lark +45 -0
- sipl/interpreter/__init__.py +28 -0
- sipl/interpreter/errors.py +21 -0
- sipl/interpreter/events.py +115 -0
- sipl/interpreter/interpreter.py +222 -0
- sipl/parser.py +173 -0
- sipl_interpreter-0.2.0.dist-info/METADATA +41 -0
- sipl_interpreter-0.2.0.dist-info/RECORD +20 -0
- sipl_interpreter-0.2.0.dist-info/WHEEL +5 -0
- sipl_interpreter-0.2.0.dist-info/entry_points.txt +2 -0
- sipl_interpreter-0.2.0.dist-info/licenses/LICENSE +21 -0
- sipl_interpreter-0.2.0.dist-info/top_level.txt +1 -0
sipl/__init__.py
ADDED
|
File without changes
|
sipl/__main__.py
ADDED
sipl/ast.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict, dataclass, field, fields
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class SourceSpan:
|
|
8
|
+
"""Position of a node in the source text. Lines and columns start from 1."""
|
|
9
|
+
|
|
10
|
+
line: int
|
|
11
|
+
column: int
|
|
12
|
+
end_line: int
|
|
13
|
+
end_column: int
|
|
14
|
+
start: int
|
|
15
|
+
end: int
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class Node:
|
|
20
|
+
# span is not compared, so two trees are equal when their structure is equal.
|
|
21
|
+
# The parser always sets it, None is only for nodes built by hand.
|
|
22
|
+
span: SourceSpan | None = field(default=None, kw_only=True, compare=False, repr=False)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class Num(Node):
|
|
27
|
+
value: int
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class Var(Node):
|
|
32
|
+
name: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class BinOp(Node):
|
|
37
|
+
op: str # "+", "-", "*"
|
|
38
|
+
left: Expr
|
|
39
|
+
right: Expr
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class Compare(Node):
|
|
44
|
+
op: str # "=", ">"
|
|
45
|
+
left: Expr
|
|
46
|
+
right: Expr
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class Or(Node):
|
|
51
|
+
left: Cond
|
|
52
|
+
right: Cond
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass(frozen=True)
|
|
56
|
+
class Not(Node):
|
|
57
|
+
cond: Cond
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass(frozen=True)
|
|
61
|
+
class Assign(Node):
|
|
62
|
+
target: Var
|
|
63
|
+
value: Expr
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True)
|
|
67
|
+
class Seq(Node):
|
|
68
|
+
first: Stmt
|
|
69
|
+
second: Stmt
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True)
|
|
73
|
+
class If(Node):
|
|
74
|
+
cond: Cond
|
|
75
|
+
then: Stmt
|
|
76
|
+
orelse: Stmt
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@dataclass(frozen=True)
|
|
80
|
+
class While(Node):
|
|
81
|
+
cond: Cond
|
|
82
|
+
body: Stmt
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(frozen=True)
|
|
86
|
+
class Block(Node):
|
|
87
|
+
body: Stmt
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class Skip(Node):
|
|
92
|
+
pass
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@dataclass(frozen=True)
|
|
96
|
+
class Program(Node):
|
|
97
|
+
body: Stmt
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def fields_of(node: Node):
|
|
101
|
+
"""Pairs (name, value) for all fields of a node except span."""
|
|
102
|
+
return [(f.name, getattr(node, f.name)) for f in fields(node) if f.name != "span"]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def to_dict(node: Node) -> dict:
|
|
106
|
+
"""The tree as plain dicts, ready for JSON."""
|
|
107
|
+
out: dict = {"type": type(node).__name__}
|
|
108
|
+
for name, value in fields_of(node):
|
|
109
|
+
out[name] = to_dict(value) if isinstance(value, Node) else value
|
|
110
|
+
out["span"] = asdict(node.span) if node.span else None
|
|
111
|
+
return out
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
Expr = Num | Var | BinOp
|
|
115
|
+
Cond = Compare | Or | Not
|
|
116
|
+
Stmt = Assign | Seq | If | While | Block | Skip
|
sipl/cli.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
import sys
|
|
7
|
+
from dataclasses import asdict
|
|
8
|
+
|
|
9
|
+
from . import ast
|
|
10
|
+
from .interpreter import (
|
|
11
|
+
DEFAULT_STEP_LIMIT,
|
|
12
|
+
BeforeStatement,
|
|
13
|
+
ConditionChecked,
|
|
14
|
+
Event,
|
|
15
|
+
Finished,
|
|
16
|
+
Interpreter,
|
|
17
|
+
SiplRuntimeError,
|
|
18
|
+
VariableChanged,
|
|
19
|
+
)
|
|
20
|
+
from .parser import SiplSyntaxError, parse
|
|
21
|
+
|
|
22
|
+
# Exit codes. They are part of the CLI contract, see docs/cli.md.
|
|
23
|
+
EXIT_OK = 0
|
|
24
|
+
EXIT_SYNTAX_ERROR = 1
|
|
25
|
+
EXIT_USAGE = 2 # same code argparse uses for a bad command line
|
|
26
|
+
EXIT_RUNTIME_ERROR = 3
|
|
27
|
+
|
|
28
|
+
_ASSIGNMENT = re.compile(r"([A-Za-z][A-Za-z0-9_]*)=(-?[0-9]+)")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def main(argv: list[str] | None = None) -> int:
|
|
32
|
+
# Python 3.11+ refuses to print integers longer than 4300 digits by default,
|
|
33
|
+
# and a SIPL program gets there with a few multiplications in a loop.
|
|
34
|
+
if hasattr(sys, "set_int_max_str_digits"):
|
|
35
|
+
sys.set_int_max_str_digits(0)
|
|
36
|
+
# The Windows console encoding cannot show ¬ or ∨. Print them escaped instead of crashing.
|
|
37
|
+
for stream in (sys.stdout, sys.stderr):
|
|
38
|
+
if hasattr(stream, "reconfigure"):
|
|
39
|
+
stream.reconfigure(errors="backslashreplace")
|
|
40
|
+
|
|
41
|
+
args = _arguments().parse_args(argv)
|
|
42
|
+
try:
|
|
43
|
+
text = _read(args.file)
|
|
44
|
+
except (OSError, UnicodeDecodeError) as e:
|
|
45
|
+
reason = e.strerror if isinstance(e, OSError) and e.strerror else "not a UTF-8 text"
|
|
46
|
+
return _error(args, None, EXIT_USAGE, "file_error", f"cannot read file: {reason}", None)
|
|
47
|
+
try:
|
|
48
|
+
program = parse(text)
|
|
49
|
+
except SiplSyntaxError as e:
|
|
50
|
+
return _error(args, text, EXIT_SYNTAX_ERROR, "syntax_error", e.message, e.span)
|
|
51
|
+
return args.command(args, text, program)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _arguments() -> argparse.ArgumentParser:
|
|
55
|
+
parser = argparse.ArgumentParser(prog="sipl", description="Tools for the SIPL language.")
|
|
56
|
+
commands = parser.add_subparsers(dest="name", required=True, metavar="command")
|
|
57
|
+
|
|
58
|
+
def command(name, function, help):
|
|
59
|
+
sub = commands.add_parser(name, help=help, description=help)
|
|
60
|
+
sub.set_defaults(command=function)
|
|
61
|
+
sub.add_argument("file", help="program file, or - to read from stdin")
|
|
62
|
+
sub.add_argument("--json", action="store_true", help="print the result as JSON")
|
|
63
|
+
return sub
|
|
64
|
+
|
|
65
|
+
command("check", _check, "check the syntax of a program")
|
|
66
|
+
command("parse", _parse, "print the syntax tree of a program")
|
|
67
|
+
for name, function, help in (
|
|
68
|
+
("run", _run, "run a program and print the final state"),
|
|
69
|
+
("trace", _trace, "run a program and print every step"),
|
|
70
|
+
):
|
|
71
|
+
sub = command(name, function, help)
|
|
72
|
+
sub.add_argument(
|
|
73
|
+
"state",
|
|
74
|
+
nargs="*",
|
|
75
|
+
type=_assignment,
|
|
76
|
+
metavar="NAME=NUMBER",
|
|
77
|
+
help="initial value of a variable, for example X=2",
|
|
78
|
+
)
|
|
79
|
+
sub.add_argument(
|
|
80
|
+
"--step-limit",
|
|
81
|
+
type=_positive,
|
|
82
|
+
default=DEFAULT_STEP_LIMIT,
|
|
83
|
+
metavar="N",
|
|
84
|
+
help=f"stop after N steps (default {DEFAULT_STEP_LIMIT})",
|
|
85
|
+
)
|
|
86
|
+
return parser
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _assignment(text: str) -> tuple[str, int]:
|
|
90
|
+
match = _ASSIGNMENT.fullmatch(text)
|
|
91
|
+
if not match:
|
|
92
|
+
raise argparse.ArgumentTypeError(f"expected NAME=NUMBER, got '{text}'")
|
|
93
|
+
return match.group(1), int(match.group(2))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _positive(text: str) -> int:
|
|
97
|
+
if not text.isdigit() or int(text) == 0:
|
|
98
|
+
raise argparse.ArgumentTypeError(f"expected a positive number, got '{text}'")
|
|
99
|
+
return int(text)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _read(path: str) -> str:
|
|
103
|
+
# utf-8-sig drops the BOM that Windows editors like to add.
|
|
104
|
+
# Line ends are kept as they are, so the offsets in spans match the file.
|
|
105
|
+
if path == "-":
|
|
106
|
+
return sys.stdin.buffer.read().decode("utf-8-sig")
|
|
107
|
+
with open(path, encoding="utf-8-sig", newline="") as file:
|
|
108
|
+
return file.read()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _check(args, text, program) -> int:
|
|
112
|
+
if args.json:
|
|
113
|
+
_print_json({"ok": True})
|
|
114
|
+
else:
|
|
115
|
+
print("ok")
|
|
116
|
+
return EXIT_OK
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _parse(args, text, program) -> int:
|
|
120
|
+
try:
|
|
121
|
+
if args.json:
|
|
122
|
+
_print_json({"ok": True, "ast": ast.to_dict(program)})
|
|
123
|
+
else:
|
|
124
|
+
print("\n".join(_tree_lines(program)))
|
|
125
|
+
except RecursionError:
|
|
126
|
+
message = "program is nested too deeply to print"
|
|
127
|
+
return _error(args, None, EXIT_USAGE, "too_deep", message, None)
|
|
128
|
+
return EXIT_OK
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _run(args, text, program) -> int:
|
|
132
|
+
interpreter = Interpreter(program, dict(args.state), args.step_limit)
|
|
133
|
+
try:
|
|
134
|
+
interpreter.run()
|
|
135
|
+
except SiplRuntimeError as e:
|
|
136
|
+
extra = {"state": _sorted(interpreter.state), "steps": interpreter.steps}
|
|
137
|
+
return _error(args, text, EXIT_RUNTIME_ERROR, e.code, e.message, e.span, extra)
|
|
138
|
+
if args.json:
|
|
139
|
+
_print_json({"ok": True, "state": _sorted(interpreter.state), "steps": interpreter.steps})
|
|
140
|
+
else:
|
|
141
|
+
for name, value in _sorted(interpreter.state).items():
|
|
142
|
+
print(f"{name} = {value}")
|
|
143
|
+
return EXIT_OK
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _trace(args, text, program) -> int:
|
|
147
|
+
events: list[Event] = []
|
|
148
|
+
interpreter = Interpreter(program, dict(args.state), args.step_limit)
|
|
149
|
+
interpreter.subscribe(events.append)
|
|
150
|
+
error = None
|
|
151
|
+
try:
|
|
152
|
+
interpreter.run()
|
|
153
|
+
except SiplRuntimeError as e:
|
|
154
|
+
error = e
|
|
155
|
+
|
|
156
|
+
if args.json:
|
|
157
|
+
payload = {"ok": error is None, "events": [event.to_dict() for event in events]}
|
|
158
|
+
if error:
|
|
159
|
+
payload["error"] = _error_dict(error.code, error.message, error.span)
|
|
160
|
+
_print_json(payload)
|
|
161
|
+
else:
|
|
162
|
+
for line in _trace_lines(events, text):
|
|
163
|
+
print(line)
|
|
164
|
+
if error:
|
|
165
|
+
_print_error(args, text, error.message, error.span)
|
|
166
|
+
return EXIT_RUNTIME_ERROR if error else EXIT_OK
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _error(args, text, exit_code, kind, message, span, extra=None) -> int:
|
|
170
|
+
if args.json:
|
|
171
|
+
_print_json({"ok": False, "error": _error_dict(kind, message, span), **(extra or {})})
|
|
172
|
+
else:
|
|
173
|
+
_print_error(args, text, message, span)
|
|
174
|
+
return exit_code
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _error_dict(kind, message, span) -> dict:
|
|
178
|
+
return {"kind": kind, "message": message, "span": asdict(span) if span else None}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _print_error(args, text, message, span) -> None:
|
|
182
|
+
where = f"{args.file}:{span.line}:{span.column}" if span else args.file
|
|
183
|
+
print(f"{where}: error: {message}", file=sys.stderr)
|
|
184
|
+
lines = text.splitlines() if text and span else []
|
|
185
|
+
if span and span.line <= len(lines):
|
|
186
|
+
line = lines[span.line - 1]
|
|
187
|
+
# tabs stay tabs so the mark ends up under the right character
|
|
188
|
+
padding = "".join("\t" if ch == "\t" else " " for ch in line[: span.column - 1])
|
|
189
|
+
print(f" {line}", file=sys.stderr)
|
|
190
|
+
print(f" {padding}^", file=sys.stderr)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _print_json(payload: dict) -> None:
|
|
194
|
+
print(json.dumps(payload))
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _sorted(state: dict[str, int]) -> dict[str, int]:
|
|
198
|
+
return dict(sorted(state.items()))
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _tree_lines(node: ast.Node, depth: int = 0):
|
|
202
|
+
values: list[object] = []
|
|
203
|
+
children: list[ast.Node] = []
|
|
204
|
+
for _, value in ast.fields_of(node):
|
|
205
|
+
(children if isinstance(value, ast.Node) else values).append(value)
|
|
206
|
+
yield " " * depth + " ".join([type(node).__name__, *map(str, values)])
|
|
207
|
+
for child in children:
|
|
208
|
+
yield from _tree_lines(child, depth + 1)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _trace_lines(events, text):
|
|
212
|
+
for event in events:
|
|
213
|
+
if isinstance(event, VariableChanged):
|
|
214
|
+
old = "-" if event.old is None else event.old
|
|
215
|
+
note = f"{event.name}: {old} -> {event.new}"
|
|
216
|
+
yield _trace_line(event.step, event.statement.span, text, note)
|
|
217
|
+
elif isinstance(event, ConditionChecked):
|
|
218
|
+
note = "true" if event.result else "false"
|
|
219
|
+
yield _trace_line(event.step, event.statement.cond.span, text, note)
|
|
220
|
+
elif isinstance(event, BeforeStatement) and isinstance(event.statement, ast.Skip):
|
|
221
|
+
yield _trace_line(event.step, event.statement.span, text, "")
|
|
222
|
+
elif isinstance(event, Finished):
|
|
223
|
+
yield f"finished in {event.step} steps"
|
|
224
|
+
for name, value in _sorted(event.state).items():
|
|
225
|
+
yield f"{name} = {value}"
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _trace_line(step, span, text, note) -> str:
|
|
229
|
+
source = " ".join(text[span.start : span.end].split())
|
|
230
|
+
where = f"{span.line}:{span.column}"
|
|
231
|
+
return f"{step:>5} {where:<7} {source:<24} {note}".rstrip()
|
sipl/dap/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Debug Adapter Protocol для SIPL."""
|
sipl/dap/__main__.py
ADDED
sipl/dap/protocol.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Читання і запис повідомлень DAP.
|
|
2
|
+
|
|
3
|
+
Кожне повідомлення це заголовки, порожній рядок і тіло JSON:
|
|
4
|
+
|
|
5
|
+
Content-Length: 52\r\n
|
|
6
|
+
\r\n
|
|
7
|
+
{"seq": 1, "type": "request", "command": "initialize"}
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
from typing import Any, BinaryIO
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ProtocolError(Exception):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def read_message(stream: BinaryIO) -> dict[str, Any] | None:
|
|
21
|
+
"""Прочитати одне повідомлення. None, якщо потік закінчився."""
|
|
22
|
+
length = None
|
|
23
|
+
while True:
|
|
24
|
+
line = stream.readline()
|
|
25
|
+
if not line:
|
|
26
|
+
if length is None:
|
|
27
|
+
return None
|
|
28
|
+
raise ProtocolError("stream ended inside headers")
|
|
29
|
+
line = line.rstrip(b"\r\n")
|
|
30
|
+
if not line:
|
|
31
|
+
break
|
|
32
|
+
name, _, value = line.partition(b":")
|
|
33
|
+
if name.strip().lower() == b"content-length":
|
|
34
|
+
try:
|
|
35
|
+
length = int(value.strip())
|
|
36
|
+
except ValueError:
|
|
37
|
+
raise ProtocolError(f"bad Content-Length: {value!r}") from None
|
|
38
|
+
if length is None:
|
|
39
|
+
raise ProtocolError("no Content-Length header")
|
|
40
|
+
|
|
41
|
+
body = stream.read(length)
|
|
42
|
+
if len(body) < length:
|
|
43
|
+
raise ProtocolError("stream ended inside body")
|
|
44
|
+
try:
|
|
45
|
+
message = json.loads(body.decode("utf-8"))
|
|
46
|
+
except (UnicodeDecodeError, json.JSONDecodeError) as error:
|
|
47
|
+
raise ProtocolError(f"bad message body: {error}") from None
|
|
48
|
+
if not isinstance(message, dict):
|
|
49
|
+
raise ProtocolError("message is not an object")
|
|
50
|
+
return message
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def write_message(stream: BinaryIO, message: dict[str, Any]) -> None:
|
|
54
|
+
body = json.dumps(message, ensure_ascii=False).encode("utf-8")
|
|
55
|
+
stream.write(b"Content-Length: %d\r\n\r\n" % len(body))
|
|
56
|
+
stream.write(body)
|
|
57
|
+
stream.flush()
|