@starpak/lang 0.0.0-stage → 0.1.3

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.
Files changed (58) hide show
  1. package/LICENSE +22 -0
  2. package/lib/ast.d.ts +341 -0
  3. package/lib/ast.d.ts.map +1 -0
  4. package/lib/ast.js +9 -0
  5. package/lib/ast.js.map +1 -0
  6. package/lib/checker.d.ts +17 -0
  7. package/lib/checker.d.ts.map +1 -0
  8. package/lib/checker.js +525 -0
  9. package/lib/checker.js.map +1 -0
  10. package/lib/diagnostics.d.ts +46 -0
  11. package/lib/diagnostics.d.ts.map +1 -0
  12. package/lib/diagnostics.js +77 -0
  13. package/lib/diagnostics.js.map +1 -0
  14. package/lib/emit/lua.d.ts +19 -0
  15. package/lib/emit/lua.d.ts.map +1 -0
  16. package/lib/emit/lua.js +451 -0
  17. package/lib/emit/lua.js.map +1 -0
  18. package/lib/emit/ts.d.ts +42 -0
  19. package/lib/emit/ts.d.ts.map +1 -0
  20. package/lib/emit/ts.js +950 -0
  21. package/lib/emit/ts.js.map +1 -0
  22. package/lib/index.d.ts +18 -0
  23. package/lib/index.d.ts.map +1 -0
  24. package/lib/index.js +54 -0
  25. package/lib/index.js.map +1 -0
  26. package/lib/lexer.d.ts +10 -0
  27. package/lib/lexer.d.ts.map +1 -0
  28. package/lib/lexer.js +282 -0
  29. package/lib/lexer.js.map +1 -0
  30. package/lib/parser.d.ts +11 -0
  31. package/lib/parser.d.ts.map +1 -0
  32. package/lib/parser.js +1138 -0
  33. package/lib/parser.js.map +1 -0
  34. package/lib/token.d.ts +32 -0
  35. package/lib/token.d.ts.map +1 -0
  36. package/lib/token.js +18 -0
  37. package/lib/token.js.map +1 -0
  38. package/lib/types.d.ts +63 -0
  39. package/lib/types.d.ts.map +1 -0
  40. package/lib/types.js +177 -0
  41. package/lib/types.js.map +1 -0
  42. package/lib/version.d.ts +25 -0
  43. package/lib/version.d.ts.map +1 -0
  44. package/lib/version.js +47 -0
  45. package/lib/version.js.map +1 -0
  46. package/package.json +10 -3
  47. package/src/ast.ts +467 -0
  48. package/src/checker.ts +554 -0
  49. package/src/diagnostics.ts +100 -0
  50. package/src/emit/lua.ts +484 -0
  51. package/src/emit/ts.ts +1024 -0
  52. package/src/index.ts +24 -0
  53. package/src/lexer.ts +301 -0
  54. package/src/parser.ts +1257 -0
  55. package/src/token.ts +34 -0
  56. package/src/types.ts +218 -0
  57. package/src/version.ts +44 -0
  58. package/README.md +0 -3
package/src/ast.ts ADDED
@@ -0,0 +1,467 @@
1
+ /**
2
+ * Spak Lang - AST definitions
3
+ *
4
+ * Every node is a discriminated union member with a `kind` field and a
5
+ * 1-based `loc` position pointing at the first token of the node.
6
+ */
7
+
8
+ export interface Position {
9
+ line: number
10
+ column: number
11
+ }
12
+
13
+ export interface NodeBase {
14
+ loc: Position
15
+ }
16
+
17
+ /* ------------------------------------------------------------------ */
18
+ /* Types */
19
+ /* ------------------------------------------------------------------ */
20
+
21
+ export type TypeAnn =
22
+ | NamedType
23
+ | ArrayType
24
+ | DictType
25
+ | RecordType
26
+ | UnionType
27
+ | FnType
28
+ | OptionalType
29
+
30
+ export interface NamedType extends NodeBase {
31
+ kind: 'named'
32
+ name: string
33
+ }
34
+
35
+ export interface ArrayType extends NodeBase {
36
+ kind: 'array'
37
+ element: TypeAnn
38
+ }
39
+
40
+ /** `{ string: number }` */
41
+ export interface DictType extends NodeBase {
42
+ kind: 'dict'
43
+ key: TypeAnn
44
+ value: TypeAnn
45
+ }
46
+
47
+ /** `{ x: number, y: number }` */
48
+ export interface RecordType extends NodeBase {
49
+ kind: 'record'
50
+ fields: TypeField[]
51
+ }
52
+
53
+ export interface TypeField {
54
+ name: string
55
+ type: TypeAnn
56
+ }
57
+
58
+ export interface UnionType extends NodeBase {
59
+ kind: 'union'
60
+ types: TypeAnn[]
61
+ }
62
+
63
+ export interface FnType extends NodeBase {
64
+ kind: 'fn'
65
+ params: TypeAnn[]
66
+ returns: TypeAnn
67
+ }
68
+
69
+ export interface OptionalType extends NodeBase {
70
+ kind: 'optional'
71
+ type: TypeAnn
72
+ /** Alias of `type`, kept for consumers that expect the `inner` spelling. */
73
+ inner?: TypeAnn
74
+ }
75
+
76
+ /* ------------------------------------------------------------------ */
77
+ /* Common pieces */
78
+ /* ------------------------------------------------------------------ */
79
+
80
+ export interface Decorator extends NodeBase {
81
+ kind: 'Decorator'
82
+ name: string
83
+ args: Expr[]
84
+ }
85
+
86
+ export interface Param {
87
+ name: string
88
+ type?: TypeAnn
89
+ default?: Expr
90
+ rest?: boolean
91
+ }
92
+
93
+ export interface VarName {
94
+ name: string
95
+ type?: TypeAnn
96
+ }
97
+
98
+ export interface ElifBranch {
99
+ cond: Expr
100
+ body: Block
101
+ }
102
+
103
+ export interface MatchArm extends NodeBase {
104
+ patterns: Expr[]
105
+ guard?: Expr
106
+ body: Block
107
+ }
108
+
109
+ export interface ImportClause {
110
+ name: string
111
+ alias?: string
112
+ }
113
+
114
+ export interface TableField {
115
+ key?: Expr
116
+ value: Expr
117
+ }
118
+
119
+ /** Entry of a table destructuring pattern: `{ x }`, `{ a = alias }`, `{ a = 1 }`. */
120
+ export interface TablePatternEntry {
121
+ /** Key read from the source table. */
122
+ key: string
123
+ /** Variable the value is bound to when renamed (`{ a = first }`), else `key`. */
124
+ target?: string
125
+ /** Default value used when the key is absent. */
126
+ default?: Expr
127
+ }
128
+
129
+ /** Destructuring pattern, only valid as an assignment target. */
130
+ export type Pattern = TablePattern | ArrayPattern
131
+
132
+ export interface TablePattern extends NodeBase {
133
+ kind: 'TablePattern'
134
+ entries: TablePatternEntry[]
135
+ }
136
+
137
+ export interface ArrayPattern extends NodeBase {
138
+ kind: 'ArrayPattern'
139
+ elements: string[]
140
+ }
141
+
142
+ /* ------------------------------------------------------------------ */
143
+ /* Statements */
144
+ /* ------------------------------------------------------------------ */
145
+
146
+ export type Statement =
147
+ | VarDecl
148
+ | Assign
149
+ | FnDecl
150
+ | ClassDecl
151
+ | IfStmt
152
+ | UnlessStmt
153
+ | WhileStmt
154
+ | RepeatStmt
155
+ | ForNumeric
156
+ | ForIn
157
+ | ReturnStmt
158
+ | BreakStmt
159
+ | ContinueStmt
160
+ | MatchStmt
161
+ | TryStmt
162
+ | ImportDecl
163
+ | ExportDecl
164
+ | ExportDefault
165
+ | TypeAliasDecl
166
+ | ThrowStmt
167
+ | SpawnStmt
168
+ | ExprStmt
169
+
170
+ export interface Program extends NodeBase {
171
+ kind: 'Program'
172
+ body: Statement[]
173
+ }
174
+
175
+ export interface Block extends NodeBase {
176
+ kind: 'Block'
177
+ body: Statement[]
178
+ }
179
+
180
+ export interface VarDecl extends NodeBase {
181
+ kind: 'VarDecl'
182
+ names: VarName[]
183
+ values: Expr[]
184
+ scope: 'local' | 'global' | 'const'
185
+ export?: boolean
186
+ /** Doc comment (`---`) directly above the declaration. */
187
+ doc?: string
188
+ /** Member decorators, only used inside a class body. */
189
+ decorators?: Decorator[]
190
+ }
191
+
192
+ export interface Assign extends NodeBase {
193
+ kind: 'Assign'
194
+ targets: Array<Expr | Pattern>
195
+ values: Expr[]
196
+ }
197
+
198
+ export interface FnDecl extends NodeBase {
199
+ kind: 'FnDecl'
200
+ name: string
201
+ /** Receiver of `fn Point:len()`, i.e. `Point`. */
202
+ receiver?: string
203
+ params: Param[]
204
+ returnType?: TypeAnn
205
+ body?: Block
206
+ exprBody?: Expr
207
+ isMethod: boolean
208
+ decorators: Decorator[]
209
+ async?: boolean
210
+ /** Doc comment (`---`) directly above the declaration. */
211
+ doc?: string
212
+ }
213
+
214
+ export interface ClassDecl extends NodeBase {
215
+ kind: 'ClassDecl'
216
+ name: string
217
+ superClass?: string
218
+ members: Array<FnDecl | VarDecl>
219
+ decorators: Decorator[]
220
+ /** Doc comment (`---`) directly above the declaration. */
221
+ doc?: string
222
+ }
223
+
224
+ export interface IfStmt extends NodeBase {
225
+ kind: 'IfStmt'
226
+ cond: Expr
227
+ then: Block
228
+ elifs: ElifBranch[]
229
+ else?: Block
230
+ }
231
+
232
+ export interface UnlessStmt extends NodeBase {
233
+ kind: 'UnlessStmt'
234
+ cond: Expr
235
+ body: Block
236
+ }
237
+
238
+ export interface WhileStmt extends NodeBase {
239
+ kind: 'WhileStmt'
240
+ cond: Expr
241
+ body: Block
242
+ }
243
+
244
+ export interface RepeatStmt extends NodeBase {
245
+ kind: 'RepeatStmt'
246
+ body: Block
247
+ cond: Expr
248
+ }
249
+
250
+ export interface ForNumeric extends NodeBase {
251
+ kind: 'ForNumeric'
252
+ varName: string
253
+ from: Expr
254
+ to: Expr
255
+ step?: Expr
256
+ inclusive: boolean
257
+ body: Block
258
+ }
259
+
260
+ export interface ForIn extends NodeBase {
261
+ kind: 'ForIn'
262
+ vars: string[]
263
+ iter: Expr
264
+ body: Block
265
+ }
266
+
267
+ export interface ReturnStmt extends NodeBase {
268
+ kind: 'ReturnStmt'
269
+ values: Expr[]
270
+ }
271
+
272
+ export interface BreakStmt extends NodeBase {
273
+ kind: 'BreakStmt'
274
+ }
275
+
276
+ export interface ContinueStmt extends NodeBase {
277
+ kind: 'ContinueStmt'
278
+ }
279
+
280
+ export interface MatchStmt extends NodeBase {
281
+ kind: 'MatchStmt'
282
+ subject: Expr
283
+ arms: MatchArm[]
284
+ elseBody?: Block
285
+ }
286
+
287
+ export interface TryStmt extends NodeBase {
288
+ kind: 'TryStmt'
289
+ body: Block
290
+ catchVar?: string
291
+ catchType?: TypeAnn
292
+ catchBody?: Block
293
+ finallyBody?: Block
294
+ }
295
+
296
+ /** Surface form of an import; keeps `import { a }` distinct from `import a`. */
297
+ export type ImportForm = 'named' | 'default' | 'namespace' | 'side-effect'
298
+
299
+ export interface ImportDecl extends NodeBase {
300
+ kind: 'ImportDecl'
301
+ clauses: ImportClause[]
302
+ source: string
303
+ /**
304
+ * Parsed surface form. Older hand-built ASTs may omit it; the emitter then
305
+ * falls back to inferring the form from the clauses.
306
+ */
307
+ form?: ImportForm
308
+ /** Doc comment (`---`) directly above the declaration. */
309
+ doc?: string
310
+ }
311
+
312
+ export interface ExportDecl extends NodeBase {
313
+ kind: 'ExportDecl'
314
+ declaration: Statement
315
+ /** Doc comment (`---`) directly above the declaration. */
316
+ doc?: string
317
+ }
318
+
319
+ export interface ExportDefault extends NodeBase {
320
+ kind: 'ExportDefault'
321
+ expr: Expr
322
+ }
323
+
324
+ export interface TypeAliasDecl extends NodeBase {
325
+ kind: 'TypeAliasDecl'
326
+ name: string
327
+ type: TypeAnn
328
+ /** Doc comment (`---`) directly above the declaration. */
329
+ doc?: string
330
+ }
331
+
332
+ export interface ThrowStmt extends NodeBase {
333
+ kind: 'ThrowStmt'
334
+ value: Expr
335
+ }
336
+
337
+ export interface SpawnStmt extends NodeBase {
338
+ kind: 'SpawnStmt'
339
+ value: Expr
340
+ }
341
+
342
+ export interface ExprStmt extends NodeBase {
343
+ kind: 'ExprStmt'
344
+ expr: Expr
345
+ }
346
+
347
+ /* ------------------------------------------------------------------ */
348
+ /* Expressions */
349
+ /* ------------------------------------------------------------------ */
350
+
351
+ export type Expr =
352
+ | NumberLit
353
+ | StringLit
354
+ | BoolLit
355
+ | NilLit
356
+ | Ident
357
+ | SelfExpr
358
+ | Member
359
+ | Index
360
+ | Call
361
+ | Binary
362
+ | Unary
363
+ | Table
364
+ | FunctionExpr
365
+ | Range
366
+ | AwaitExpr
367
+ | Spread
368
+
369
+ export interface NumberLit extends NodeBase {
370
+ kind: 'NumberLit'
371
+ value: number
372
+ }
373
+
374
+ export interface StringLit extends NodeBase {
375
+ kind: 'StringLit'
376
+ value: string
377
+ multiline?: boolean
378
+ fstring?: boolean
379
+ /** Interpolated segments of an f-string; plain strings and parsed expressions. */
380
+ parts?: Array<string | Expr>
381
+ }
382
+
383
+ export interface BoolLit extends NodeBase {
384
+ kind: 'BoolLit'
385
+ value: boolean
386
+ }
387
+
388
+ export interface NilLit extends NodeBase {
389
+ kind: 'NilLit'
390
+ }
391
+
392
+ export interface Ident extends NodeBase {
393
+ kind: 'Ident'
394
+ name: string
395
+ }
396
+
397
+ export interface SelfExpr extends NodeBase {
398
+ kind: 'SelfExpr'
399
+ }
400
+
401
+ export interface Member extends NodeBase {
402
+ kind: 'Member'
403
+ object: Expr
404
+ property: string
405
+ optional?: boolean
406
+ }
407
+
408
+ export interface Index extends NodeBase {
409
+ kind: 'Index'
410
+ object: Expr
411
+ index: Expr
412
+ isRange?: boolean
413
+ }
414
+
415
+ export interface Call extends NodeBase {
416
+ kind: 'Call'
417
+ callee: Expr
418
+ args: Expr[]
419
+ isMethod?: boolean
420
+ }
421
+
422
+ export interface Binary extends NodeBase {
423
+ kind: 'Binary'
424
+ op: string
425
+ left: Expr
426
+ right: Expr
427
+ }
428
+
429
+ export interface Unary extends NodeBase {
430
+ kind: 'Unary'
431
+ op: string
432
+ argument: Expr
433
+ }
434
+
435
+ export interface Table extends NodeBase {
436
+ kind: 'Table'
437
+ fields: TableField[]
438
+ }
439
+
440
+ export interface FunctionExpr extends NodeBase {
441
+ kind: 'FunctionExpr'
442
+ params: Param[]
443
+ returnType?: TypeAnn
444
+ body?: Block
445
+ exprBody?: Expr
446
+ async?: boolean
447
+ }
448
+
449
+ export interface Range extends NodeBase {
450
+ kind: 'Range'
451
+ from: Expr
452
+ to: Expr
453
+ inclusive: boolean
454
+ }
455
+
456
+ export interface AwaitExpr extends NodeBase {
457
+ kind: 'AwaitExpr'
458
+ argument: Expr
459
+ }
460
+
461
+ /** `f(a, ...rest, ...)`; `argument` is absent for a bare spread. */
462
+ export interface Spread extends NodeBase {
463
+ kind: 'Spread'
464
+ argument?: Expr
465
+ }
466
+
467
+ export type Node = Program | Statement | Expr | Decorator | Pattern