kiddo-lang 1.0.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.
kiddo/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """KIDDO Programming Language Engine (Phase 2 — Lexer, Parser, AST & Interpreter)."""
2
+
3
+ __version__ = "1.0.1"
kiddo/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ """Allows running `python -m kiddo <file.kid>` from anywhere."""
2
+
3
+ import sys
4
+ from kiddo.cli import main
5
+
6
+ if __name__ == "__main__":
7
+ sys.exit(main())
kiddo/ast_nodes.py ADDED
@@ -0,0 +1,296 @@
1
+ """Abstract Syntax Tree (AST) node definitions for KIDDO v1.0."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from typing import Any, Optional
7
+
8
+ from kiddo.tokens import SourceSpan
9
+
10
+
11
+ @dataclass
12
+ class ASTNode:
13
+ span: SourceSpan
14
+
15
+
16
+ # ============================================================================
17
+ # Expressions
18
+ # ============================================================================
19
+
20
+ @dataclass
21
+ class Expr(ASTNode):
22
+ pass
23
+
24
+
25
+ @dataclass
26
+ class Literal(Expr):
27
+ value: Any # int | float | str | bool | None
28
+
29
+
30
+ @dataclass
31
+ class InterpolatedString(Expr):
32
+ parts: list[Expr] # Sequence of Literal(str) and arbitrary Expr nodes
33
+
34
+
35
+ @dataclass
36
+ class Identifier(Expr):
37
+ name: str
38
+
39
+
40
+ @dataclass
41
+ class CurrentExpr(Expr):
42
+ pass
43
+
44
+
45
+ @dataclass
46
+ class MeExpr(Expr):
47
+ pass
48
+
49
+
50
+ @dataclass
51
+ class ListLiteral(Expr):
52
+ elements: list[Expr]
53
+
54
+
55
+ @dataclass
56
+ class MapLiteral(Expr):
57
+ entries: list[tuple[Expr, Expr]]
58
+
59
+
60
+ @dataclass
61
+ class UnaryOp(Expr):
62
+ op: str # "-" | "not"
63
+ operand: Expr
64
+
65
+
66
+ @dataclass
67
+ class BinaryOp(Expr):
68
+ left: Expr
69
+ op: str # "+", "-", "*", "/", "//", "%", "^", "==", "!=", "<", "<=", ">", ">=", "and", "or", "in", "not in", "is", "is not"
70
+ right: Expr
71
+
72
+
73
+ @dataclass
74
+ class InlineIf(Expr):
75
+ true_expr: Expr
76
+ condition: Expr
77
+ false_expr: Expr
78
+
79
+
80
+ @dataclass
81
+ class CallArg:
82
+ name: Optional[str]
83
+ value: Expr
84
+ span: SourceSpan
85
+
86
+
87
+ @dataclass
88
+ class CallExpr(Expr):
89
+ callee: Expr
90
+ args: list[CallArg]
91
+
92
+
93
+ @dataclass
94
+ class IndexExpr(Expr):
95
+ target: Expr
96
+ index: Expr
97
+
98
+
99
+ @dataclass
100
+ class SliceExpr(Expr):
101
+ target: Expr
102
+ start: Expr
103
+ end: Expr
104
+
105
+
106
+ @dataclass
107
+ class MemberExpr(Expr):
108
+ target: Expr
109
+ member: str
110
+
111
+
112
+ @dataclass
113
+ class Param:
114
+ name: str
115
+ default: Optional[Expr]
116
+ span: SourceSpan
117
+ variadic: bool = False
118
+
119
+
120
+ @dataclass
121
+ class LambdaExpr(Expr):
122
+ params: list[Param]
123
+ body: Expr
124
+
125
+
126
+ # ============================================================================
127
+ # Statements
128
+ # ============================================================================
129
+
130
+ @dataclass
131
+ class Stmt(ASTNode):
132
+ pass
133
+
134
+
135
+ @dataclass
136
+ class Program(ASTNode):
137
+ statements: list[Stmt] = field(default_factory=list)
138
+
139
+
140
+ @dataclass
141
+ class ExprStmt(Stmt):
142
+ expr: Expr
143
+
144
+
145
+ @dataclass
146
+ class AssignStmt(Stmt):
147
+ targets: list[Expr] # Identifier | IndexExpr | MemberExpr (len > 1 only with "=")
148
+ op: str # "=", "+=", "-=", "*=", "/=", "//=", "%="
149
+ value: Expr
150
+
151
+
152
+ @dataclass
153
+ class IfBranch:
154
+ condition: Expr
155
+ body: list[Stmt]
156
+ span: SourceSpan
157
+
158
+
159
+ @dataclass
160
+ class IfStmt(Stmt):
161
+ branches: list[IfBranch]
162
+ else_body: Optional[list[Stmt]]
163
+
164
+
165
+ @dataclass
166
+ class WhenBranch:
167
+ values: list[Expr]
168
+ body: list[Stmt]
169
+ span: SourceSpan
170
+
171
+
172
+ @dataclass
173
+ class ChooseStmt(Stmt):
174
+ subject: Expr
175
+ branches: list[WhenBranch]
176
+ otherwise_body: Optional[list[Stmt]]
177
+
178
+
179
+ @dataclass
180
+ class RepeatTimesStmt(Stmt):
181
+ count: Expr
182
+ alias: Optional[str]
183
+ body: list[Stmt]
184
+
185
+
186
+ @dataclass
187
+ class RepeatRangeStmt(Stmt):
188
+ start: Expr
189
+ end: Expr
190
+ step: Optional[Expr]
191
+ alias: Optional[str]
192
+ body: list[Stmt]
193
+
194
+
195
+ @dataclass
196
+ class RepeatWhileStmt(Stmt):
197
+ condition: Expr
198
+ body: list[Stmt]
199
+
200
+
201
+ @dataclass
202
+ class RepeatForeverStmt(Stmt):
203
+ body: list[Stmt]
204
+
205
+
206
+ @dataclass
207
+ class RepeatEachStmt(Stmt):
208
+ var1: str
209
+ var2: Optional[str]
210
+ iterable: Expr
211
+ body: list[Stmt]
212
+
213
+
214
+ @dataclass
215
+ class StopStmt(Stmt):
216
+ pass
217
+
218
+
219
+ @dataclass
220
+ class SkipStmt(Stmt):
221
+ pass
222
+
223
+
224
+ @dataclass
225
+ class FuncDefStmt(Stmt):
226
+ name: str
227
+ params: list[Param]
228
+ body: list[Stmt]
229
+
230
+
231
+ @dataclass
232
+ class BlueprintDefStmt(Stmt):
233
+ name: str
234
+ parent: Optional[str]
235
+ methods: list[FuncDefStmt]
236
+
237
+
238
+ @dataclass
239
+ class ReturnStmt(Stmt):
240
+ value: Optional[Expr]
241
+
242
+
243
+ @dataclass
244
+ class FailStmt(Stmt):
245
+ message: Expr
246
+
247
+
248
+ @dataclass
249
+ class TryStmt(Stmt):
250
+ try_body: list[Stmt]
251
+ error_name: Optional[str]
252
+ catch_body: Optional[list[Stmt]]
253
+ always_body: Optional[list[Stmt]]
254
+
255
+
256
+ @dataclass
257
+ class UseStmt(Stmt):
258
+ module: str
259
+ is_path: bool
260
+ alias: Optional[str]
261
+
262
+
263
+ @dataclass
264
+ class FromUseStmt(Stmt):
265
+ module: str
266
+ is_path: bool
267
+ items: list[tuple[str, Optional[str]]] # [(symbol_name, alias_or_none), ...]
268
+ import_all: bool = False
269
+
270
+
271
+ def format_ast(node: Any, indent: int = 0) -> str:
272
+ """Pretty-print an AST tree for debugging (`--ast` and `:ast`)."""
273
+ pad = " " * indent
274
+ if isinstance(node, list):
275
+ if not node:
276
+ return "[]"
277
+ inner = "\n".join(f"{' ' * (indent + 1)}{format_ast(item, indent + 1).lstrip()}" for item in node)
278
+ return f"[\n{inner}\n{pad}]"
279
+ if not isinstance(node, (ASTNode, Param, CallArg, IfBranch, WhenBranch)):
280
+ return repr(node)
281
+
282
+ cls_name = type(node).__name__
283
+ fields_out: list[str] = []
284
+ for k, v in vars(node).items():
285
+ if k == "span":
286
+ continue
287
+ formatted = format_ast(v, indent + 1).lstrip()
288
+ fields_out.append(f"{k}={formatted}")
289
+
290
+ if not fields_out:
291
+ return f"{pad}{cls_name}()"
292
+ joined = ", ".join(fields_out)
293
+ if "\n" not in joined and len(joined) < 72:
294
+ return f"{pad}{cls_name}({joined})"
295
+ multi = "\n".join(f"{' ' * (indent + 1)}{f}" for f in fields_out)
296
+ return f"{pad}{cls_name}(\n{multi}\n{pad})"