@starpak/lang 0.0.0-stage → 0.1.2

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 (53) hide show
  1. package/LICENSE +22 -0
  2. package/lib/ast.d.ts +334 -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 +28 -0
  19. package/lib/emit/ts.d.ts.map +1 -0
  20. package/lib/emit/ts.js +803 -0
  21. package/lib/emit/ts.js.map +1 -0
  22. package/lib/index.d.ts +17 -0
  23. package/lib/index.d.ts.map +1 -0
  24. package/lib/index.js +49 -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 +1133 -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/package.json +10 -3
  43. package/src/ast.ts +459 -0
  44. package/src/checker.ts +554 -0
  45. package/src/diagnostics.ts +100 -0
  46. package/src/emit/lua.ts +484 -0
  47. package/src/emit/ts.ts +861 -0
  48. package/src/index.ts +21 -0
  49. package/src/lexer.ts +301 -0
  50. package/src/parser.ts +1251 -0
  51. package/src/token.ts +34 -0
  52. package/src/types.ts +218 -0
  53. package/README.md +0 -3
package/src/ast.ts ADDED
@@ -0,0 +1,459 @@
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
+ export interface ImportDecl extends NodeBase {
297
+ kind: 'ImportDecl'
298
+ clauses: ImportClause[]
299
+ source: string
300
+ /** Doc comment (`---`) directly above the declaration. */
301
+ doc?: string
302
+ }
303
+
304
+ export interface ExportDecl extends NodeBase {
305
+ kind: 'ExportDecl'
306
+ declaration: Statement
307
+ /** Doc comment (`---`) directly above the declaration. */
308
+ doc?: string
309
+ }
310
+
311
+ export interface ExportDefault extends NodeBase {
312
+ kind: 'ExportDefault'
313
+ expr: Expr
314
+ }
315
+
316
+ export interface TypeAliasDecl extends NodeBase {
317
+ kind: 'TypeAliasDecl'
318
+ name: string
319
+ type: TypeAnn
320
+ /** Doc comment (`---`) directly above the declaration. */
321
+ doc?: string
322
+ }
323
+
324
+ export interface ThrowStmt extends NodeBase {
325
+ kind: 'ThrowStmt'
326
+ value: Expr
327
+ }
328
+
329
+ export interface SpawnStmt extends NodeBase {
330
+ kind: 'SpawnStmt'
331
+ value: Expr
332
+ }
333
+
334
+ export interface ExprStmt extends NodeBase {
335
+ kind: 'ExprStmt'
336
+ expr: Expr
337
+ }
338
+
339
+ /* ------------------------------------------------------------------ */
340
+ /* Expressions */
341
+ /* ------------------------------------------------------------------ */
342
+
343
+ export type Expr =
344
+ | NumberLit
345
+ | StringLit
346
+ | BoolLit
347
+ | NilLit
348
+ | Ident
349
+ | SelfExpr
350
+ | Member
351
+ | Index
352
+ | Call
353
+ | Binary
354
+ | Unary
355
+ | Table
356
+ | FunctionExpr
357
+ | Range
358
+ | AwaitExpr
359
+ | Spread
360
+
361
+ export interface NumberLit extends NodeBase {
362
+ kind: 'NumberLit'
363
+ value: number
364
+ }
365
+
366
+ export interface StringLit extends NodeBase {
367
+ kind: 'StringLit'
368
+ value: string
369
+ multiline?: boolean
370
+ fstring?: boolean
371
+ /** Interpolated segments of an f-string; plain strings and parsed expressions. */
372
+ parts?: Array<string | Expr>
373
+ }
374
+
375
+ export interface BoolLit extends NodeBase {
376
+ kind: 'BoolLit'
377
+ value: boolean
378
+ }
379
+
380
+ export interface NilLit extends NodeBase {
381
+ kind: 'NilLit'
382
+ }
383
+
384
+ export interface Ident extends NodeBase {
385
+ kind: 'Ident'
386
+ name: string
387
+ }
388
+
389
+ export interface SelfExpr extends NodeBase {
390
+ kind: 'SelfExpr'
391
+ }
392
+
393
+ export interface Member extends NodeBase {
394
+ kind: 'Member'
395
+ object: Expr
396
+ property: string
397
+ optional?: boolean
398
+ }
399
+
400
+ export interface Index extends NodeBase {
401
+ kind: 'Index'
402
+ object: Expr
403
+ index: Expr
404
+ isRange?: boolean
405
+ }
406
+
407
+ export interface Call extends NodeBase {
408
+ kind: 'Call'
409
+ callee: Expr
410
+ args: Expr[]
411
+ isMethod?: boolean
412
+ }
413
+
414
+ export interface Binary extends NodeBase {
415
+ kind: 'Binary'
416
+ op: string
417
+ left: Expr
418
+ right: Expr
419
+ }
420
+
421
+ export interface Unary extends NodeBase {
422
+ kind: 'Unary'
423
+ op: string
424
+ argument: Expr
425
+ }
426
+
427
+ export interface Table extends NodeBase {
428
+ kind: 'Table'
429
+ fields: TableField[]
430
+ }
431
+
432
+ export interface FunctionExpr extends NodeBase {
433
+ kind: 'FunctionExpr'
434
+ params: Param[]
435
+ returnType?: TypeAnn
436
+ body?: Block
437
+ exprBody?: Expr
438
+ async?: boolean
439
+ }
440
+
441
+ export interface Range extends NodeBase {
442
+ kind: 'Range'
443
+ from: Expr
444
+ to: Expr
445
+ inclusive: boolean
446
+ }
447
+
448
+ export interface AwaitExpr extends NodeBase {
449
+ kind: 'AwaitExpr'
450
+ argument: Expr
451
+ }
452
+
453
+ /** `f(a, ...rest, ...)`; `argument` is absent for a bare spread. */
454
+ export interface Spread extends NodeBase {
455
+ kind: 'Spread'
456
+ argument?: Expr
457
+ }
458
+
459
+ export type Node = Program | Statement | Expr | Decorator | Pattern