thyscript 1.0.0

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 (49) hide show
  1. package/bin/thy-node.js +332 -0
  2. package/bin/thy.js +828 -0
  3. package/bin/thys.js +25 -0
  4. package/examples/calculator.thy +274 -0
  5. package/examples/calculator.thy.js +628 -0
  6. package/examples/hello.thy +34 -0
  7. package/examples/login.thy +382 -0
  8. package/examples/login.thy.js +710 -0
  9. package/package.json +54 -0
  10. package/scripts/_class.js +18 -0
  11. package/scripts/_class2.js +11 -0
  12. package/scripts/_class3.js +21 -0
  13. package/scripts/_dstruct.js +20 -0
  14. package/scripts/_dstruct2.js +24 -0
  15. package/scripts/_scaffold.js +16 -0
  16. package/scripts/_scaffold2.js +16 -0
  17. package/scripts/_scaffold3.js +26 -0
  18. package/scripts/_tpl_compile.js +13 -0
  19. package/scripts/_tpl_test.js +9 -0
  20. package/scripts/_typeof.js +26 -0
  21. package/scripts/_typeof2.js +25 -0
  22. package/scripts/patch_genmethod.js +44 -0
  23. package/scripts/smoke_test.js +167 -0
  24. package/scripts/smoke_test2.js +151 -0
  25. package/scripts/test_control_panel.js +58 -0
  26. package/src/compiler/CodeGenerator.js +1449 -0
  27. package/src/compiler/JITCompiler.js +325 -0
  28. package/src/database/DatabaseEngine.js +931 -0
  29. package/src/errors/ErrorDetector.js +305 -0
  30. package/src/errors/ErrorMessages.js +497 -0
  31. package/src/index.js +169 -0
  32. package/src/lexer/Lexer.js +342 -0
  33. package/src/lexer/TokenTypes.js +1234 -0
  34. package/src/memory/MemoryManager.js +283 -0
  35. package/src/parser/AST.js +606 -0
  36. package/src/parser/Parser.js +1942 -0
  37. package/src/runtime/ThyRuntime.js +265 -0
  38. package/src/runtime/stdlib/ArrayLib.js +207 -0
  39. package/src/runtime/stdlib/FileLib.js +168 -0
  40. package/src/runtime/stdlib/MathLib.js +411 -0
  41. package/src/runtime/stdlib/StringLib.js +387 -0
  42. package/src/server/ControlPanel.js +1188 -0
  43. package/src/server/HTMLCSSEngine.js +866 -0
  44. package/src/server/SecurityEngine.js +714 -0
  45. package/src/server/ServerEngine.js +738 -0
  46. package/tests/test1_math.thy +82 -0
  47. package/tests/test2_oop.thy +133 -0
  48. package/tests/test3_server.thy +143 -0
  49. package/tests/test4_functions.thy +159 -0
@@ -0,0 +1,606 @@
1
+ 'use strict';
2
+ /**
3
+ * ThyScript AST Node Factory
4
+ * Produces plain-object AST nodes for every language construct.
5
+ * Every node carries:
6
+ * type – string discriminator
7
+ * line – source line (1-indexed)
8
+ * col – source column (1-indexed)
9
+ * plus construct-specific fields documented below.
10
+ */
11
+
12
+ // ─────────────────────────────────────────────────────────────
13
+ // Helper – attach source location to every node
14
+ // ─────────────────────────────────────────────────────────────
15
+ function node(type, fields, line, col) {
16
+ return Object.assign({ type, line: line || 0, col: col || 0 }, fields);
17
+ }
18
+
19
+ // ─────────────────────────────────────────────────────────────
20
+ // Top-level
21
+ // ─────────────────────────────────────────────────────────────
22
+
23
+ /** Root of every ThyScript program */
24
+ function Program(body, line, col) {
25
+ return node('Program', { body }, line, col);
26
+ }
27
+
28
+ /** Sequence of statements wrapped by :…end() or a block delimiter */
29
+ function Block(body, line, col) {
30
+ return node('Block', { body }, line, col);
31
+ }
32
+
33
+ // ─────────────────────────────────────────────────────────────
34
+ // Declarations
35
+ // ─────────────────────────────────────────────────────────────
36
+
37
+ /**
38
+ * `const table name = value`
39
+ * @param {string} name
40
+ * @param {*} init – AST expression or null
41
+ * @param {boolean} isConst
42
+ */
43
+ function VarDecl(name, init, isConst, line, col) {
44
+ return node('VarDecl', { name, init, isConst }, line, col);
45
+ }
46
+
47
+ /**
48
+ * `[async] func name(params): body end()`
49
+ * @param {string} name
50
+ * @param {string[]} params – parameter name strings
51
+ * @param {Block} body
52
+ * @param {boolean} isAsync
53
+ * @param {boolean} isExported
54
+ */
55
+ function FunctionDecl(name, params, body, isAsync, isExported, line, col) {
56
+ return node('FunctionDecl', { name, params, body, isAsync, isExported }, line, col);
57
+ }
58
+
59
+ /**
60
+ * `[public|private|protected] class(Name)[extends Super]: body end()`
61
+ * @param {string} name
62
+ * @param {string|null} superClass
63
+ * @param {Block} body
64
+ * @param {string} access – 'public' | 'private' | 'protected'
65
+ */
66
+ function ClassDecl(name, superClass, body, access, line, col) {
67
+ return node('ClassDecl', { name, superClass, body, access }, line, col);
68
+ }
69
+
70
+ /**
71
+ * `classHtml(Name): rawHtml end()`
72
+ * @param {string} name
73
+ * @param {string} html – raw HTML string captured by lexer
74
+ */
75
+ function HtmlClassDecl(name, html, line, col) {
76
+ return node('HtmlClassDecl', { name, html }, line, col);
77
+ }
78
+
79
+ /**
80
+ * `import name from 'module'`
81
+ * @param {string[]} specifiers
82
+ * @param {string} source
83
+ * @param {boolean} isDefault
84
+ */
85
+ function ImportDecl(specifiers, source, isDefault, line, col) {
86
+ return node('ImportDecl', { specifiers, source, isDefault }, line, col);
87
+ }
88
+
89
+ /**
90
+ * `export func …` / `export const table …`
91
+ * @param {*} declaration – the inner declaration node
92
+ */
93
+ function ExportDecl(declaration, line, col) {
94
+ return node('ExportDecl', { declaration }, line, col);
95
+ }
96
+
97
+ // ─────────────────────────────────────────────────────────────
98
+ // Statements
99
+ // ─────────────────────────────────────────────────────────────
100
+
101
+ /**
102
+ * `if(cond): consequent [elif(cond): alt]* [else: alternate] end()`
103
+ * @param {*} test
104
+ * @param {Block} consequent
105
+ * @param {Array} elseifs – array of { test, body }
106
+ * @param {Block|null} alternate
107
+ */
108
+ function IfStmt(test, consequent, elseifs, alternate, line, col) {
109
+ return node('IfStmt', { test, consequent, elseifs, alternate }, line, col);
110
+ }
111
+
112
+ /**
113
+ * `while(cond): body end()`
114
+ */
115
+ function WhileStmt(test, body, line, col) {
116
+ return node('WhileStmt', { test, body }, line, col);
117
+ }
118
+
119
+ /**
120
+ * `for variable in iterable: body end()`
121
+ * @param {string} variable
122
+ * @param {*} iterable
123
+ * @param {Block} body
124
+ */
125
+ function ForStmt(variable, iterable, body, line, col) {
126
+ return node('ForStmt', { variable, iterable, body }, line, col);
127
+ }
128
+
129
+ /** `return [expr]` */
130
+ function ReturnStmt(argument, line, col) {
131
+ return node('ReturnStmt', { argument }, line, col);
132
+ }
133
+
134
+ /** `print(expr, …)` */
135
+ function PrintStmt(args, line, col) {
136
+ return node('PrintStmt', { args }, line, col);
137
+ }
138
+
139
+ /** Expression used as a statement (assignments, calls, ++/--, etc.) */
140
+ function ExprStmt(expression, line, col) {
141
+ return node('ExprStmt', { expression }, line, col);
142
+ }
143
+
144
+ /**
145
+ * `add N in list`
146
+ * @param {*} value – expression to add
147
+ * @param {string} target – identifier of the list
148
+ */
149
+ function AddStmt(value, target, line, col) {
150
+ return node('AddStmt', { value, target }, line, col);
151
+ }
152
+
153
+ /**
154
+ * `change(variable to(newValue))`
155
+ * @param {string} target
156
+ * @param {*} value
157
+ */
158
+ function ChangeStmt(target, value, line, col) {
159
+ return node('ChangeStmt', { target, value }, line, col);
160
+ }
161
+
162
+ /**
163
+ * `calculate(expression)` – evaluates and returns/prints a math expression
164
+ * @param {*} expression
165
+ */
166
+ function CalculateStmt(expression, line, col) {
167
+ return node('CalculateStmt', { expression }, line, col);
168
+ }
169
+
170
+ /**
171
+ * `save(variable)` – persists a variable to storage
172
+ * @param {string} name
173
+ */
174
+ function SaveStmt(name, line, col) {
175
+ return node('SaveStmt', { name }, line, col);
176
+ }
177
+
178
+ /** `free()` – releases managed memory / clears scope */
179
+ function FreeExpr(line, col) {
180
+ return node('FreeExpr', {}, line, col);
181
+ }
182
+
183
+ /**
184
+ * `try: body catch(e): handler [finally: fin] end()`
185
+ * @param {Block} body
186
+ * @param {string|null} errorVar
187
+ * @param {Block} handler
188
+ * @param {Block|null} finalizer
189
+ */
190
+ function TryCatch(body, errorVar, handler, finalizer, line, col) {
191
+ return node('TryCatch', { body, errorVar, handler, finalizer }, line, col);
192
+ }
193
+
194
+ /** `break` */
195
+ function BreakStmt(line, col) {
196
+ return node('BreakStmt', {}, line, col);
197
+ }
198
+
199
+ /** `continue` */
200
+ function ContinueStmt(line, col) {
201
+ return node('ContinueStmt', {}, line, col);
202
+ }
203
+
204
+ /** `throw expression` */
205
+ function ThrowStmt(argument, line, col) {
206
+ return node('ThrowStmt', { argument }, line, col);
207
+ }
208
+
209
+ // ─────────────────────────────────────────────────────────────
210
+ // Expressions
211
+ // ─────────────────────────────────────────────────────────────
212
+
213
+ /**
214
+ * Binary operator expression
215
+ * @param {string} operator – '+', '-', '*', '/', '**', '%', '==', '!=', '<', etc.
216
+ * @param {*} left
217
+ * @param {*} right
218
+ */
219
+ function BinaryExpr(operator, left, right, line, col) {
220
+ return node('BinaryExpr', { operator, left, right }, line, col);
221
+ }
222
+
223
+ /**
224
+ * Unary prefix operator
225
+ * @param {string} operator – '-', '!', '~', 'typeof', 'new', 'await', 'yield'
226
+ * @param {*} argument
227
+ * @param {boolean} prefix
228
+ */
229
+ function UnaryExpr(operator, argument, prefix, line, col) {
230
+ return node('UnaryExpr', { operator, argument, prefix }, line, col);
231
+ }
232
+
233
+ /**
234
+ * Function call
235
+ * @param {*} callee
236
+ * @param {*[]} args
237
+ */
238
+ function CallExpr(callee, args, line, col) {
239
+ return node('CallExpr', { callee, args }, line, col);
240
+ }
241
+
242
+ /**
243
+ * Property access: obj.prop or obj?.prop
244
+ * @param {*} object
245
+ * @param {string} property
246
+ * @param {boolean} optional – true for ?.
247
+ */
248
+ function MemberExpr(object, property, optional, line, col) {
249
+ return node('MemberExpr', { object, property, optional }, line, col);
250
+ }
251
+
252
+ /**
253
+ * Computed / index access: arr[i]
254
+ * @param {*} object
255
+ * @param {*} index
256
+ */
257
+ function IndexExpr(object, index, line, col) {
258
+ return node('IndexExpr', { object, index }, line, col);
259
+ }
260
+
261
+ /**
262
+ * Scalar literal
263
+ * @param {'number'|'string'|'boolean'|'null'|'undefined'} kind
264
+ * @param {*} value
265
+ */
266
+ function Literal(kind, value, line, col) {
267
+ return node('Literal', { kind, value }, line, col);
268
+ }
269
+
270
+ /**
271
+ * Array literal [e1, e2, …]
272
+ * @param {*[]} elements
273
+ */
274
+ function ArrayExpr(elements, line, col) {
275
+ return node('ArrayExpr', { elements }, line, col);
276
+ }
277
+
278
+ /**
279
+ * Object literal { key: value, … }
280
+ * @param {Array<{key:string|*,value:*,computed:boolean,shorthand:boolean}>} properties
281
+ */
282
+ function ObjectExpr(properties, line, col) {
283
+ return node('ObjectExpr', { properties }, line, col);
284
+ }
285
+
286
+ /**
287
+ * `input(prompt)` – reads from stdin
288
+ * @param {*} prompt – expression (usually a string literal)
289
+ */
290
+ function InputExpr(prompt, line, col) {
291
+ return node('InputExpr', { prompt }, line, col);
292
+ }
293
+
294
+ /**
295
+ * Template literal `` `Hello ${name}` ``
296
+ * @param {Array<{type:'str'|'expr', value:string|*}>} parts
297
+ */
298
+ function TemplateLiteral(parts, line, col) {
299
+ return node('TemplateLiteral', { parts }, line, col);
300
+ }
301
+
302
+ /**
303
+ * Arrow function (params) => expr | block
304
+ * @param {string[]} params
305
+ * @param {*} body – expression or Block
306
+ * @param {boolean} async
307
+ */
308
+ function ArrowFunctionExpr(params, body, isAsync, line, col) {
309
+ return node('ArrowFunctionExpr', { params, body, isAsync }, line, col);
310
+ }
311
+
312
+ /**
313
+ * Spread operator ...expr
314
+ * @param {*} argument
315
+ */
316
+ function SpreadExpr(argument, line, col) {
317
+ return node('SpreadExpr', { argument }, line, col);
318
+ }
319
+
320
+ /**
321
+ * Destructuring declaration
322
+ * `const table [a, b] = arr` or `const table {x, y} = obj`
323
+ * @param {'array'|'object'} kind
324
+ * @param {Array} targets – names or {key,alias} objects
325
+ * @param {*} init
326
+ */
327
+ function DestructureDecl(kind, targets, init, isConst, line, col) {
328
+ return node('DestructureDecl', { kind, targets, init, isConst }, line, col);
329
+ }
330
+
331
+ /**
332
+ * `calculate(expr)` used as an expression that returns a value
333
+ */
334
+ function CalculateExpr(expression, line, col) {
335
+ return node('CalculateExpr', { expression }, line, col);
336
+ }
337
+
338
+ /**
339
+ * Ternary / conditional expression test ? consequent : alternate
340
+ */
341
+ function ConditionalExpr(test, consequent, alternate, line, col) {
342
+ return node('ConditionalExpr', { test, consequent, alternate }, line, col);
343
+ }
344
+
345
+ /**
346
+ * `new Constructor(args)`
347
+ */
348
+ function NewExpr(callee, args, line, col) {
349
+ return node('NewExpr', { callee, args }, line, col);
350
+ }
351
+
352
+ /**
353
+ * Identifier reference
354
+ */
355
+ function Identifier(name, line, col) {
356
+ return node('Identifier', { name }, line, col);
357
+ }
358
+
359
+ /**
360
+ * Assignment expression target = value (and +=, -=, *=, /=, %=)
361
+ */
362
+ function AssignExpr(operator, target, value, line, col) {
363
+ return node('AssignExpr', { operator, target, value }, line, col);
364
+ }
365
+
366
+ // ─────────────────────────────────────────────────────────────
367
+ // Pattern matching
368
+ // ─────────────────────────────────────────────────────────────
369
+
370
+ /**
371
+ * `match expr: when p1: b1 … [otherwise: bN] end()`
372
+ * @param {*} subject
373
+ * @param {Array<{pattern:*,body:Block}>} arms
374
+ * @param {Block|null} otherwise
375
+ */
376
+ function MatchExpr(subject, arms, otherwise, line, col) {
377
+ return node('MatchExpr', { subject, arms, otherwise }, line, col);
378
+ }
379
+
380
+ // ─────────────────────────────────────────────────────────────
381
+ // Concurrency / parallelism
382
+ // ─────────────────────────────────────────────────────────────
383
+
384
+ /**
385
+ * `parallel: body end()`
386
+ * @param {Block} body
387
+ */
388
+ function ParallelDecl(body, line, col) {
389
+ return node('ParallelDecl', { body }, line, col);
390
+ }
391
+
392
+ /**
393
+ * `worker name: body end()`
394
+ * @param {string|null} name
395
+ * @param {Block} body
396
+ */
397
+ function WorkerDecl(name, body, line, col) {
398
+ return node('WorkerDecl', { name, body }, line, col);
399
+ }
400
+
401
+ // ─────────────────────────────────────────────────────────────
402
+ // Streaming / pipeline
403
+ // ─────────────────────────────────────────────────────────────
404
+
405
+ /**
406
+ * `stream name: body end()`
407
+ * @param {string|null} name
408
+ * @param {Block} body
409
+ */
410
+ function StreamDecl(name, body, line, col) {
411
+ return node('StreamDecl', { name, body }, line, col);
412
+ }
413
+
414
+ /**
415
+ * `pipeline name: body end()`
416
+ * @param {string|null} name
417
+ * @param {Block} body
418
+ */
419
+ function PipelineDecl(name, body, line, col) {
420
+ return node('PipelineDecl', { name, body }, line, col);
421
+ }
422
+
423
+ // ─────────────────────────────────────────────────────────────
424
+ // Server / networking
425
+ // ─────────────────────────────────────────────────────────────
426
+
427
+ /**
428
+ * `server: body end()`
429
+ * @param {Block} body
430
+ * @param {*|null} port – optional port expression
431
+ */
432
+ function ServerDecl(body, port, line, col) {
433
+ return node('ServerDecl', { body, port }, line, col);
434
+ }
435
+
436
+ /**
437
+ * `route METHOD '/path': body end()`
438
+ * @param {string} method – 'get'|'post'|'put'|'delete'|'patch'
439
+ * @param {*} path – string expression
440
+ * @param {Block} body
441
+ */
442
+ function RouteDecl(method, path, body, line, col) {
443
+ return node('RouteDecl', { method, path, body }, line, col);
444
+ }
445
+
446
+ /**
447
+ * `socket on 'event': body end()`
448
+ * @param {string} event
449
+ * @param {Block} body
450
+ */
451
+ function SocketDecl(event, body, line, col) {
452
+ return node('SocketDecl', { event, body }, line, col);
453
+ }
454
+
455
+ // ─────────────────────────────────────────────────────────────
456
+ // Database
457
+ // ─────────────────────────────────────────────────────────────
458
+
459
+ /**
460
+ * `database connect: body end()`
461
+ * @param {Block} body
462
+ * @param {*|null} connectionExpr
463
+ */
464
+ function DatabaseDecl(body, connectionExpr, line, col) {
465
+ return node('DatabaseDecl', { body, connectionExpr }, line, col);
466
+ }
467
+
468
+ // ─────────────────────────────────────────────────────────────
469
+ // UI / Components
470
+ // ─────────────────────────────────────────────────────────────
471
+
472
+ /**
473
+ * `component Name: body end()`
474
+ * @param {string} name
475
+ * @param {Block} body
476
+ */
477
+ function ComponentDecl(name, body, line, col) {
478
+ return node('ComponentDecl', { name, body }, line, col);
479
+ }
480
+
481
+ // ─────────────────────────────────────────────────────────────
482
+ // ML / AI
483
+ // ─────────────────────────────────────────────────────────────
484
+
485
+ /**
486
+ * `neural: body end()`
487
+ * @param {Block} body
488
+ * @param {*|null} config – optional configuration expression
489
+ */
490
+ function NeuralDecl(body, config, line, col) {
491
+ return node('NeuralDecl', { body, config }, line, col);
492
+ }
493
+
494
+ // ─────────────────────────────────────────────────────────────
495
+ // Physics
496
+ // ─────────────────────────────────────────────────────────────
497
+
498
+ /**
499
+ * `physics: body end()`
500
+ * @param {Block} body
501
+ * @param {*|null} config
502
+ */
503
+ function PhysicsDecl(body, config, line, col) {
504
+ return node('PhysicsDecl', { body, config }, line, col);
505
+ }
506
+
507
+ // ─────────────────────────────────────────────────────────────
508
+ // Animation
509
+ // ─────────────────────────────────────────────────────────────
510
+
511
+ /**
512
+ * `animate: body end()`
513
+ * @param {Block} body
514
+ * @param {*|null} target
515
+ */
516
+ function AnimateDecl(body, target, line, col) {
517
+ return node('AnimateDecl', { body, target }, line, col);
518
+ }
519
+
520
+ // ─────────────────────────────────────────────────────────────
521
+ // Export
522
+ // ─────────────────────────────────────────────────────────────
523
+ module.exports = {
524
+ // helpers
525
+ node,
526
+
527
+ // top-level
528
+ Program,
529
+ Block,
530
+
531
+ // declarations
532
+ VarDecl,
533
+ FunctionDecl,
534
+ ClassDecl,
535
+ HtmlClassDecl,
536
+ ImportDecl,
537
+ ExportDecl,
538
+ DestructureDecl,
539
+
540
+ // statements
541
+ IfStmt,
542
+ WhileStmt,
543
+ ForStmt,
544
+ ReturnStmt,
545
+ PrintStmt,
546
+ ExprStmt,
547
+ AddStmt,
548
+ ChangeStmt,
549
+ CalculateStmt,
550
+ SaveStmt,
551
+ FreeExpr,
552
+ TryCatch,
553
+ BreakStmt,
554
+ ContinueStmt,
555
+ ThrowStmt,
556
+
557
+ // expressions
558
+ BinaryExpr,
559
+ UnaryExpr,
560
+ CallExpr,
561
+ MemberExpr,
562
+ IndexExpr,
563
+ Literal,
564
+ ArrayExpr,
565
+ ObjectExpr,
566
+ InputExpr,
567
+ TemplateLiteral,
568
+ ArrowFunctionExpr,
569
+ SpreadExpr,
570
+ CalculateExpr,
571
+ ConditionalExpr,
572
+ NewExpr,
573
+ Identifier,
574
+ AssignExpr,
575
+
576
+ // pattern matching
577
+ MatchExpr,
578
+
579
+ // concurrency
580
+ ParallelDecl,
581
+ WorkerDecl,
582
+
583
+ // streaming
584
+ StreamDecl,
585
+ PipelineDecl,
586
+
587
+ // server / networking
588
+ ServerDecl,
589
+ RouteDecl,
590
+ SocketDecl,
591
+
592
+ // database
593
+ DatabaseDecl,
594
+
595
+ // UI
596
+ ComponentDecl,
597
+
598
+ // ML
599
+ NeuralDecl,
600
+
601
+ // physics
602
+ PhysicsDecl,
603
+
604
+ // animation
605
+ AnimateDecl,
606
+ };