tumorscript 0.1.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.
@@ -0,0 +1,560 @@
1
+ -- parser module for tumorscript
2
+ -- transforms token stream into an abstract syntax tree (ast)
3
+ -- handles indentation-based blocks and quarantine {} blocks
4
+
5
+ local parser = {}
6
+
7
+ local TOKEN -- will be set from lexer module
8
+
9
+ -- ast node constructors
10
+ local function node(type, props)
11
+ props = props or {}
12
+ props.node_type = type
13
+ return props
14
+ end
15
+
16
+ -- parser state
17
+ local State = {}
18
+ State.__index = State
19
+
20
+ function State.new(tokens, source)
21
+ local self = setmetatable({}, State)
22
+ self.tokens = tokens
23
+ self.source = source
24
+ self.pos = 1
25
+ self.total_lines = 0
26
+ self.quarantine_lines = 0
27
+ -- count total source lines for quarantine limit enforcement
28
+ for _ in source:gmatch("[^\n]+") do
29
+ self.total_lines = self.total_lines + 1
30
+ end
31
+ return self
32
+ end
33
+
34
+ function State:peek(offset)
35
+ offset = offset or 0
36
+ local idx = self.pos + offset
37
+ if idx <= #self.tokens then
38
+ return self.tokens[idx]
39
+ end
40
+ return { type = "EOF", value = nil, line = -1 }
41
+ end
42
+
43
+ function State:current()
44
+ return self:peek(0)
45
+ end
46
+
47
+ function State:advance()
48
+ local tok = self.tokens[self.pos]
49
+ self.pos = self.pos + 1
50
+ return tok
51
+ end
52
+
53
+ function State:expect(type, msg)
54
+ local tok = self:current()
55
+ if tok.type ~= type then
56
+ error(string.format("PARSE_TUMOR at line %d: expected %s, got %s (%s). %s",
57
+ tok.line or 0, type, tok.type, tostring(tok.value), msg or ""))
58
+ end
59
+ return self:advance()
60
+ end
61
+
62
+ function State:match(type)
63
+ if self:current().type == type then
64
+ return self:advance()
65
+ end
66
+ return nil
67
+ end
68
+
69
+ function State:skip_newlines()
70
+ while self:current().type == "NEWLINE" do
71
+ self:advance()
72
+ end
73
+ end
74
+
75
+ function State:at_end()
76
+ return self:current().type == "EOF"
77
+ end
78
+
79
+ -- check quarantine limit (max 20% of code)
80
+ function State:check_quarantine_limit(line_count)
81
+ self.quarantine_lines = self.quarantine_lines + line_count
82
+ local ratio = self.quarantine_lines / math.max(self.total_lines, 1)
83
+ if ratio > 0.20 then
84
+ error(string.format(
85
+ "QUARANTINE_OVERFLOW at line %d: quarantine blocks exceed 20%% limit (%.1f%% used)",
86
+ self:current().line or 0, ratio * 100))
87
+ end
88
+ end
89
+
90
+ -- expression parsing
91
+
92
+ function State:parse_expression()
93
+ return self:parse_or()
94
+ end
95
+
96
+ function State:parse_or()
97
+ local left = self:parse_and()
98
+ while self:current().type == "OR" do
99
+ self:advance()
100
+ local right = self:parse_and()
101
+ left = node("binary_op", { op = "or", left = left, right = right })
102
+ end
103
+ return left
104
+ end
105
+
106
+ function State:parse_and()
107
+ local left = self:parse_not()
108
+ while self:current().type == "AND" do
109
+ self:advance()
110
+ local right = self:parse_not()
111
+ left = node("binary_op", { op = "and", left = left, right = right })
112
+ end
113
+ return left
114
+ end
115
+
116
+ function State:parse_not()
117
+ if self:current().type == "NOT" then
118
+ self:advance()
119
+ local operand = self:parse_not()
120
+ return node("unary_op", { op = "not", operand = operand })
121
+ end
122
+ return self:parse_comparison()
123
+ end
124
+
125
+ function State:parse_comparison()
126
+ local left = self:parse_addition()
127
+ local comp_ops = { EQ = "==", NEQ = "!=", LT = "<", GT = ">", LTE = "<=", GTE = ">=" }
128
+ while comp_ops[self:current().type] do
129
+ local op = comp_ops[self:current().type]
130
+ self:advance()
131
+ local right = self:parse_addition()
132
+ left = node("binary_op", { op = op, left = left, right = right })
133
+ end
134
+ return left
135
+ end
136
+
137
+ function State:parse_addition()
138
+ local left = self:parse_multiplication()
139
+ while self:current().type == "PLUS" or self:current().type == "MINUS" do
140
+ local op = self:advance().value
141
+ local right = self:parse_multiplication()
142
+ left = node("binary_op", { op = op, left = left, right = right })
143
+ end
144
+ return left
145
+ end
146
+
147
+ function State:parse_multiplication()
148
+ local left = self:parse_unary()
149
+ while self:current().type == "STAR" or self:current().type == "SLASH"
150
+ or self:current().type == "PERCENT" do
151
+ local op = self:advance().value
152
+ local right = self:parse_unary()
153
+ left = node("binary_op", { op = op, left = left, right = right })
154
+ end
155
+ return left
156
+ end
157
+
158
+ function State:parse_unary()
159
+ if self:current().type == "MINUS" then
160
+ self:advance()
161
+ local operand = self:parse_unary()
162
+ return node("unary_op", { op = "-", operand = operand })
163
+ end
164
+ return self:parse_primary()
165
+ end
166
+
167
+ function State:parse_primary()
168
+ local tok = self:current()
169
+
170
+ -- number literal
171
+ if tok.type == "NUMBER" then
172
+ self:advance()
173
+ return node("number_literal", { value = tok.value })
174
+ end
175
+
176
+ -- string literal
177
+ if tok.type == "STRING" then
178
+ self:advance()
179
+ return node("string_literal", { value = tok.value })
180
+ end
181
+
182
+ -- boolean literals
183
+ if tok.type == "TRUE" then
184
+ self:advance()
185
+ return node("bool_literal", { value = true })
186
+ end
187
+ if tok.type == "FALSE" then
188
+ self:advance()
189
+ return node("bool_literal", { value = false })
190
+ end
191
+
192
+ -- nil
193
+ if tok.type == "NIL" then
194
+ self:advance()
195
+ return node("nil_literal", {})
196
+ end
197
+
198
+ -- mitosis expression (object creation)
199
+ if tok.type == "MITOSIS" then
200
+ self:advance()
201
+ local class_name = self:expect("IDENTIFIER", "expected cell name after 'mitosis'")
202
+ self:expect("LPAREN", "expected '(' after cell name in mitosis")
203
+ self:expect("RPAREN", "expected ')' after mitosis arguments")
204
+ return node("mitosis_expr", { cell_name = class_name.value })
205
+ end
206
+
207
+ -- parenthesized expression
208
+ if tok.type == "LPAREN" then
209
+ self:advance()
210
+ local expr = self:parse_expression()
211
+ self:expect("RPAREN", "expected closing ')'")
212
+ return expr
213
+ end
214
+
215
+ -- identifier (variable access, possibly with dot access or function call)
216
+ if tok.type == "IDENTIFIER" then
217
+ local name = self:advance().value
218
+ local expr = node("identifier", { name = name })
219
+
220
+ -- handle chained dot access and function calls
221
+ while true do
222
+ if self:current().type == "DOT" then
223
+ self:advance()
224
+ local field = self:expect("IDENTIFIER", "expected field name after '.'")
225
+ expr = node("dot_access", { object = expr, field = field.value })
226
+ elseif self:current().type == "LPAREN" then
227
+ self:advance()
228
+ local args = {}
229
+ if self:current().type ~= "RPAREN" then
230
+ args[#args + 1] = self:parse_expression()
231
+ while self:current().type == "COMMA" do
232
+ self:advance()
233
+ args[#args + 1] = self:parse_expression()
234
+ end
235
+ end
236
+ self:expect("RPAREN", "expected ')' after function arguments")
237
+ expr = node("call_expr", { callee = expr, arguments = args })
238
+ else
239
+ break
240
+ end
241
+ end
242
+
243
+ return expr
244
+ end
245
+
246
+ error(string.format("PARSE_TUMOR at line %d: unexpected token '%s' (%s)",
247
+ tok.line or 0, tostring(tok.value), tok.type))
248
+ end
249
+
250
+ -- statement parsing
251
+
252
+ function State:parse_statement()
253
+ self:skip_newlines()
254
+ if self:at_end() then return nil end
255
+
256
+ local tok = self:current()
257
+
258
+ -- variable declaration: dna x = 42
259
+ if tok.type == "DNA" or tok.type == "RNA" then
260
+ return self:parse_var_declaration()
261
+ end
262
+
263
+ -- gene definition: gene foo(x, y):
264
+ if tok.type == "GENE" then
265
+ return self:parse_gene_definition()
266
+ end
267
+
268
+ -- cell definition: cell foo:
269
+ if tok.type == "CELL" then
270
+ return self:parse_cell_definition()
271
+ end
272
+
273
+ -- metastasis import: metastasis system.io
274
+ if tok.type == "METASTASIS" then
275
+ return self:parse_metastasis()
276
+ end
277
+
278
+ -- quarantine block: quarantine { ... }
279
+ if tok.type == "QUARANTINE" then
280
+ return self:parse_quarantine()
281
+ end
282
+
283
+ -- chemo statement: chemo var_name
284
+ if tok.type == "CHEMO" then
285
+ return self:parse_chemo()
286
+ end
287
+
288
+ -- apoptosis (return): apoptosis expr
289
+ if tok.type == "APOPTOSIS" then
290
+ return self:parse_apoptosis()
291
+ end
292
+
293
+ -- if statement
294
+ if tok.type == "IF" then
295
+ return self:parse_if()
296
+ end
297
+
298
+ -- while loop
299
+ if tok.type == "WHILE" then
300
+ return self:parse_while()
301
+ end
302
+
303
+ -- print statement
304
+ if tok.type == "PRINT" then
305
+ return self:parse_print()
306
+ end
307
+
308
+ -- assignment or expression statement
309
+ if tok.type == "IDENTIFIER" then
310
+ return self:parse_assignment_or_expr()
311
+ end
312
+
313
+ -- dedent tokens are handled by the block parser
314
+ if tok.type == "DEDENT" then
315
+ return nil
316
+ end
317
+
318
+ error(string.format("PARSE_TUMOR at line %d: unexpected statement starting with '%s' (%s)",
319
+ tok.line or 0, tostring(tok.value), tok.type))
320
+ end
321
+
322
+ function State:parse_var_declaration()
323
+ local var_type = self:advance().type == "DNA" and "dna" or "rna"
324
+ local name_tok = self:expect("IDENTIFIER", "expected variable name")
325
+ self:expect("ASSIGN", "expected '=' in variable declaration")
326
+ local value = self:parse_expression()
327
+ return node("var_declaration", {
328
+ var_type = var_type,
329
+ name = name_tok.value,
330
+ value = value,
331
+ })
332
+ end
333
+
334
+ function State:parse_gene_definition()
335
+ self:advance() -- consume 'gene'
336
+ local name = self:expect("IDENTIFIER", "expected gene name")
337
+ self:expect("LPAREN", "expected '(' after gene name")
338
+
339
+ local params = {}
340
+ if self:current().type ~= "RPAREN" then
341
+ params[#params + 1] = self:expect("IDENTIFIER", "expected parameter name").value
342
+ while self:current().type == "COMMA" do
343
+ self:advance()
344
+ params[#params + 1] = self:expect("IDENTIFIER", "expected parameter name").value
345
+ end
346
+ end
347
+
348
+ self:expect("RPAREN", "expected ')' after parameters")
349
+ self:expect("COLON", "expected ':' after gene signature")
350
+ local body = self:parse_block()
351
+
352
+ return node("gene_definition", {
353
+ name = name.value,
354
+ params = params,
355
+ body = body,
356
+ })
357
+ end
358
+
359
+ function State:parse_cell_definition()
360
+ self:advance() -- consume 'cell'
361
+ local name = self:expect("IDENTIFIER", "expected cell name")
362
+ self:expect("COLON", "expected ':' after cell name")
363
+ local body = self:parse_block()
364
+
365
+ return node("cell_definition", {
366
+ name = name.value,
367
+ body = body,
368
+ })
369
+ end
370
+
371
+ function State:parse_metastasis()
372
+ self:advance() -- consume 'metastasis'
373
+ local parts = {}
374
+ parts[#parts + 1] = self:expect("IDENTIFIER", "expected module name").value
375
+ while self:current().type == "DOT" do
376
+ self:advance()
377
+ parts[#parts + 1] = self:expect("IDENTIFIER", "expected module path component").value
378
+ end
379
+ return node("metastasis", { module_path = table.concat(parts, ".") })
380
+ end
381
+
382
+ function State:parse_quarantine()
383
+ local start_line = self:current().line
384
+ self:advance() -- consume 'quarantine'
385
+ self:expect("LBRACE", "expected '{' after 'quarantine'")
386
+ self:skip_newlines()
387
+
388
+ -- skip any indent tokens generated inside the brace block
389
+ while self:current().type == "INDENT" do self:advance(); self:skip_newlines() end
390
+
391
+ local statements = {}
392
+ local line_start = self:current().line or start_line
393
+
394
+ while self:current().type ~= "RBRACE" and not self:at_end() do
395
+ -- skip stray indent or dedent tokens inside braces
396
+ if self:current().type == "INDENT" or self:current().type == "DEDENT" then
397
+ self:advance()
398
+ self:skip_newlines()
399
+ else
400
+ local stmt = self:parse_statement()
401
+ if stmt then
402
+ statements[#statements + 1] = stmt
403
+ end
404
+ self:skip_newlines()
405
+ end
406
+ end
407
+
408
+ local line_end = self:current().line or line_start
409
+ self:expect("RBRACE", "expected '}' to close quarantine block")
410
+
411
+ -- enforce quarantine 20% limit
412
+ local q_lines = math.max(1, line_end - line_start + 1)
413
+ self:check_quarantine_limit(q_lines)
414
+
415
+ return node("quarantine_block", { body = statements })
416
+ end
417
+
418
+ function State:parse_chemo()
419
+ self:advance() -- consume 'chemo'
420
+ -- chemo target can be a dotted name like p1.heart_rate
421
+ local target = self:expect("IDENTIFIER", "expected variable name after 'chemo'").value
422
+ while self:current().type == "DOT" do
423
+ self:advance()
424
+ target = target .. "." .. self:expect("IDENTIFIER", "expected field name").value
425
+ end
426
+ return node("chemo_statement", { target = target })
427
+ end
428
+
429
+ function State:parse_apoptosis()
430
+ self:advance() -- consume 'apoptosis'
431
+ local value = nil
432
+ -- apoptosis can optionally return a value
433
+ if self:current().type ~= "NEWLINE" and self:current().type ~= "EOF"
434
+ and self:current().type ~= "DEDENT" then
435
+ value = self:parse_expression()
436
+ end
437
+ return node("apoptosis_statement", { value = value })
438
+ end
439
+
440
+ function State:parse_if()
441
+ self:advance() -- consume 'if'
442
+ local condition = self:parse_expression()
443
+ self:expect("COLON", "expected ':' after if condition")
444
+ local body = self:parse_block()
445
+
446
+ local elif_branches = {}
447
+ local else_body = nil
448
+
449
+ self:skip_newlines()
450
+
451
+ while self:current().type == "ELIF" do
452
+ self:advance()
453
+ local elif_cond = self:parse_expression()
454
+ self:expect("COLON", "expected ':' after elif condition")
455
+ local elif_body = self:parse_block()
456
+ elif_branches[#elif_branches + 1] = { condition = elif_cond, body = elif_body }
457
+ self:skip_newlines()
458
+ end
459
+
460
+ if self:current().type == "ELSE" then
461
+ self:advance()
462
+ self:expect("COLON", "expected ':' after else")
463
+ else_body = self:parse_block()
464
+ end
465
+
466
+ return node("if_statement", {
467
+ condition = condition,
468
+ body = body,
469
+ elif_branches = elif_branches,
470
+ else_body = else_body,
471
+ })
472
+ end
473
+
474
+ function State:parse_while()
475
+ self:advance() -- consume 'while'
476
+ local condition = self:parse_expression()
477
+ self:expect("COLON", "expected ':' after while condition")
478
+ local body = self:parse_block()
479
+ return node("while_statement", { condition = condition, body = body })
480
+ end
481
+
482
+ function State:parse_print()
483
+ self:advance() -- consume 'print'
484
+ self:expect("LPAREN", "expected '(' after print")
485
+ local args = {}
486
+ if self:current().type ~= "RPAREN" then
487
+ args[#args + 1] = self:parse_expression()
488
+ while self:current().type == "COMMA" do
489
+ self:advance()
490
+ args[#args + 1] = self:parse_expression()
491
+ end
492
+ end
493
+ self:expect("RPAREN", "expected ')' after print arguments")
494
+ return node("print_statement", { arguments = args })
495
+ end
496
+
497
+ function State:parse_assignment_or_expr()
498
+ -- could be: x = expr, x.y = expr, or just a function call
499
+ local expr = self:parse_expression()
500
+
501
+ if self:current().type == "ASSIGN" then
502
+ self:advance()
503
+ local value = self:parse_expression()
504
+ return node("assignment", { target = expr, value = value })
505
+ end
506
+
507
+ -- bare expression statement (usually a function call)
508
+ return node("expression_statement", { expression = expr })
509
+ end
510
+
511
+ -- parse an indented block of statements
512
+ function State:parse_block()
513
+ local statements = {}
514
+ self:skip_newlines()
515
+
516
+ if self:current().type ~= "INDENT" then
517
+ -- could be an empty block or single-line, just return empty
518
+ return statements
519
+ end
520
+
521
+ self:advance() -- consume indent
522
+ self:skip_newlines()
523
+
524
+ while self:current().type ~= "DEDENT" and not self:at_end() do
525
+ local stmt = self:parse_statement()
526
+ if stmt then
527
+ statements[#statements + 1] = stmt
528
+ end
529
+ self:skip_newlines()
530
+ end
531
+
532
+ if self:current().type == "DEDENT" then
533
+ self:advance()
534
+ end
535
+
536
+ return statements
537
+ end
538
+
539
+ -- main parse entry point
540
+
541
+ function parser.parse(tokens, source)
542
+ TOKEN = require("src.lexer").TOKEN -- grab token type constants
543
+
544
+ local state = State.new(tokens, source)
545
+ local program = { node_type = "program", body = {} }
546
+
547
+ state:skip_newlines()
548
+
549
+ while not state:at_end() do
550
+ local stmt = state:parse_statement()
551
+ if stmt then
552
+ program.body[#program.body + 1] = stmt
553
+ end
554
+ state:skip_newlines()
555
+ end
556
+
557
+ return program, nil
558
+ end
559
+
560
+ return parser