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,2 @@
1
+ # tumorscript package version
2
+ __version__ = "0.1.0"
Binary file
tumorscript/cli.py ADDED
@@ -0,0 +1,64 @@
1
+ # cli entry point for tumorscript command
2
+ import os
3
+ import sys
4
+ import shutil
5
+ import subprocess
6
+
7
+ def find_binary():
8
+ # look for compiled binary in package or nearby directories
9
+ base_dir = os.path.dirname(os.path.abspath(__file__))
10
+ is_windows = sys.platform == "win32"
11
+ bin_name = "tumorscript.exe" if is_windows else "tumorscript"
12
+
13
+ # check package bin folder
14
+ candidate = os.path.join(base_dir, "bin", bin_name)
15
+ if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
16
+ return candidate
17
+
18
+ # check workspace bin folder
19
+ candidate = os.path.join(os.path.dirname(base_dir), "bin", bin_name)
20
+ if os.path.isfile(candidate) and (is_windows or os.access(candidate, os.X_OK)):
21
+ return candidate
22
+
23
+ return None
24
+
25
+ def find_lua_entry():
26
+ # look for main lua script
27
+ base_dir = os.path.dirname(os.path.abspath(__file__))
28
+
29
+ # check inside package
30
+ candidate = os.path.join(base_dir, "main.lua")
31
+ if os.path.isfile(candidate):
32
+ return candidate
33
+
34
+ # check parent directory
35
+ candidate = os.path.join(os.path.dirname(base_dir), "main.lua")
36
+ if os.path.isfile(candidate):
37
+ return candidate
38
+
39
+ return None
40
+
41
+ def main():
42
+ args = sys.argv[1:]
43
+
44
+ # check for compiled binary first
45
+ binary = find_binary()
46
+ if binary:
47
+ res = subprocess.run([binary] + args)
48
+ sys.exit(res.returncode)
49
+
50
+ # fallback to lua interpreter
51
+ lua_script = find_lua_entry()
52
+ lua_cmd = shutil.which("lua") or shutil.which("lua54") or shutil.which("lua53")
53
+
54
+ if lua_cmd and lua_script:
55
+ res = subprocess.run([lua_cmd, lua_script] + args)
56
+ sys.exit(res.returncode)
57
+
58
+ # could not find binary or lua
59
+ print("FATAL: tumorscript executable not found")
60
+ print("please run build.bat or install lua to execute scripts")
61
+ sys.exit(1)
62
+
63
+ if __name__ == "__main__":
64
+ main()
tumorscript/main.lua ADDED
@@ -0,0 +1,62 @@
1
+ -- tumorscript interpreter entry point
2
+ -- reads a .tmq file and runs it through the lexer, parser, and interpreter
3
+
4
+ local lexer = require("src.lexer")
5
+ local parser = require("src.parser")
6
+ local interpreter = require("src.interpreter")
7
+
8
+ local function read_file(path)
9
+ local f = io.open(path, "r")
10
+ if not f then
11
+ print("FATAL: could not open file '" .. path .. "'")
12
+ os.exit(1)
13
+ end
14
+ local content = f:read("*a")
15
+ f:close()
16
+ return content
17
+ end
18
+
19
+ local function main()
20
+ local filename = arg and arg[1]
21
+ if not filename then
22
+ local prog = "tumorscript"
23
+ if arg and arg[0] and not arg[0]:match("tumorscript") then
24
+ prog = "lua main.lua"
25
+ end
26
+ print("TumorScript Interpreter v0.1")
27
+ print("usage: " .. prog .. " <file.tmq>")
28
+ os.exit(0)
29
+ end
30
+
31
+ -- only allow .tmq files for now
32
+ if not filename:match("%.tmq$") then
33
+ print("FATAL: only .tmq files are supported")
34
+ os.exit(1)
35
+ end
36
+
37
+ local source = read_file(filename)
38
+
39
+ -- phase 1: tokenize
40
+ local tokens, lex_err = lexer.tokenize(source, filename)
41
+ if lex_err then
42
+ print("LEXER_FAILURE: " .. lex_err)
43
+ os.exit(1)
44
+ end
45
+
46
+ -- phase 2: parse into ast
47
+ local ast, parse_err = parser.parse(tokens, source)
48
+ if parse_err then
49
+ print("PARSER_FAILURE: " .. parse_err)
50
+ os.exit(1)
51
+ end
52
+
53
+ -- phase 3: interpret with biomorphic runtime
54
+ local runtime = interpreter.new(ast, source)
55
+ local ok, runtime_err = runtime:execute()
56
+ if not ok then
57
+ print("ORGAN_FAILURE_EXCEPTION: " .. (runtime_err or "unknown catastrophic failure"))
58
+ os.exit(1)
59
+ end
60
+ end
61
+
62
+ main()
@@ -0,0 +1,526 @@
1
+ -- interpreter module for tumorscript
2
+ -- walks the ast and executes it with biomorphic runtime semantics
3
+
4
+ local memory = require("src.memory")
5
+
6
+ local interpreter = {}
7
+ interpreter.__index = interpreter
8
+
9
+ function interpreter.new(ast, source)
10
+ local self = setmetatable({}, interpreter)
11
+ self.ast = ast
12
+ self.source = source
13
+ self.mem = memory.new()
14
+ self.genes = {} -- registered gene definitions
15
+ self.cells = {} -- registered cell definitions
16
+ self.call_stack = {} -- for tracking gene calls
17
+ return self
18
+ end
19
+
20
+ -- main execution entry
21
+ function interpreter:execute()
22
+ local ok, err = pcall(function()
23
+ -- first pass: register all genes and cells
24
+ self:register_definitions(self.ast.body)
25
+
26
+ -- second pass: execute top-level statements
27
+ self.mem:push_scope()
28
+ self:exec_block(self.ast.body)
29
+
30
+ -- auto-call main() if it exists
31
+ if self.genes["main"] then
32
+ self:call_gene("main", {})
33
+ end
34
+
35
+ self.mem:pop_scope()
36
+ end)
37
+
38
+ if not ok then
39
+ return false, tostring(err)
40
+ end
41
+ return true, nil
42
+ end
43
+
44
+ -- register gene and cell definitions without executing them
45
+ function interpreter:register_definitions(statements)
46
+ for _, stmt in ipairs(statements) do
47
+ if stmt.node_type == "gene_definition" then
48
+ self.genes[stmt.name] = stmt
49
+ elseif stmt.node_type == "cell_definition" then
50
+ self.cells[stmt.name] = stmt
51
+ end
52
+ end
53
+ end
54
+
55
+ -- execute a block of statements, returns apoptosis value if hit
56
+ function interpreter:exec_block(statements)
57
+ for _, stmt in ipairs(statements) do
58
+ local result, is_apoptosis = self:exec_statement(stmt)
59
+ if is_apoptosis then
60
+ return result, true
61
+ end
62
+ end
63
+ return nil, false
64
+ end
65
+
66
+ -- execute a single statement
67
+ function interpreter:exec_statement(stmt)
68
+ local t = stmt.node_type
69
+
70
+ if t == "var_declaration" then
71
+ return self:exec_var_declaration(stmt)
72
+ elseif t == "assignment" then
73
+ return self:exec_assignment(stmt)
74
+ elseif t == "print_statement" then
75
+ return self:exec_print(stmt)
76
+ elseif t == "if_statement" then
77
+ return self:exec_if(stmt)
78
+ elseif t == "while_statement" then
79
+ return self:exec_while(stmt)
80
+ elseif t == "quarantine_block" then
81
+ return self:exec_quarantine(stmt)
82
+ elseif t == "chemo_statement" then
83
+ return self:exec_chemo(stmt)
84
+ elseif t == "apoptosis_statement" then
85
+ return self:exec_apoptosis(stmt)
86
+ elseif t == "expression_statement" then
87
+ self:eval_expression(stmt.expression)
88
+ return nil, false
89
+ elseif t == "metastasis" then
90
+ -- import is a no-op for now, just acknowledge it
91
+ return nil, false
92
+ elseif t == "gene_definition" or t == "cell_definition" then
93
+ -- already registered in first pass
94
+ return nil, false
95
+ else
96
+ error("RUNTIME_TUMOR: unknown statement type '" .. tostring(t) .. "'")
97
+ end
98
+ end
99
+
100
+ -- variable declaration
101
+ function interpreter:exec_var_declaration(stmt)
102
+ local value = self:eval_expression(stmt.value)
103
+ local var, err = self.mem:declare(stmt.name, value, stmt.var_type)
104
+ if err then error(err) end
105
+ return nil, false
106
+ end
107
+
108
+ -- assignment
109
+ function interpreter:exec_assignment(stmt)
110
+ local value = self:eval_expression(stmt.value)
111
+ local target = stmt.target
112
+
113
+ if target.node_type == "identifier" then
114
+ -- try infection: if the value came from a mutated source
115
+ local source_var = self.mem:lookup(target.name)
116
+ if source_var then
117
+ -- check if the rhs involves any mutated variables
118
+ self:try_infect_from_expression(target.name, stmt.value)
119
+ end
120
+
121
+ local _, err = self.mem:write(target.name, value)
122
+ if err then error(err) end
123
+ elseif target.node_type == "dot_access" then
124
+ -- handle object.field = value
125
+ local obj = self:eval_expression(target.object)
126
+ if type(obj) == "table" and obj.__cell_data then
127
+ local field_name = target.field
128
+ local field_var = obj.__cell_data[field_name]
129
+ if field_var then
130
+ if field_var.type == "rna" then
131
+ error("IMMUNE_VIOLATION: cannot reassign rna field '" .. field_name .. "'")
132
+ end
133
+ field_var.value = value
134
+ else
135
+ error("UNDEFINED_CELL: field '" .. field_name .. "' does not exist")
136
+ end
137
+ else
138
+ error("RUNTIME_TUMOR: cannot assign field on non-cell value")
139
+ end
140
+ else
141
+ error("RUNTIME_TUMOR: invalid assignment target")
142
+ end
143
+
144
+ return nil, false
145
+ end
146
+
147
+ -- try to spread infection through expression evaluation
148
+ function interpreter:try_infect_from_expression(target_name, expr)
149
+ if expr.node_type == "identifier" then
150
+ local source = self.mem:lookup(expr.name)
151
+ if source then
152
+ self.mem:try_infect(target_name, source)
153
+ end
154
+ elseif expr.node_type == "binary_op" then
155
+ self:try_infect_from_expression(target_name, expr.left)
156
+ self:try_infect_from_expression(target_name, expr.right)
157
+ end
158
+ end
159
+
160
+ -- print statement
161
+ function interpreter:exec_print(stmt)
162
+ local parts = {}
163
+ for _, arg in ipairs(stmt.arguments) do
164
+ local val = self:eval_expression(arg)
165
+ parts[#parts + 1] = tostring(val)
166
+ end
167
+ print(table.concat(parts, ""))
168
+ return nil, false
169
+ end
170
+
171
+ -- conditional branching
172
+ function interpreter:exec_if(stmt)
173
+ local cond = self:eval_expression(stmt.condition)
174
+ if self:is_truthy(cond) then
175
+ self.mem:push_scope()
176
+ local result, is_apoptosis = self:exec_block(stmt.body)
177
+ self.mem:pop_scope()
178
+ return result, is_apoptosis
179
+ end
180
+
181
+ -- check elif branches
182
+ if stmt.elif_branches then
183
+ for _, branch in ipairs(stmt.elif_branches) do
184
+ local elif_cond = self:eval_expression(branch.condition)
185
+ if self:is_truthy(elif_cond) then
186
+ self.mem:push_scope()
187
+ local result, is_apoptosis = self:exec_block(branch.body)
188
+ self.mem:pop_scope()
189
+ return result, is_apoptosis
190
+ end
191
+ end
192
+ end
193
+
194
+ -- else branch
195
+ if stmt.else_body then
196
+ self.mem:push_scope()
197
+ local result, is_apoptosis = self:exec_block(stmt.else_body)
198
+ self.mem:pop_scope()
199
+ return result, is_apoptosis
200
+ end
201
+
202
+ return nil, false
203
+ end
204
+
205
+ -- while loop
206
+ function interpreter:exec_while(stmt)
207
+ local max_iterations = 10000 -- safety net
208
+ local count = 0
209
+
210
+ while true do
211
+ count = count + 1
212
+ if count > max_iterations then
213
+ error("RUNTIME_TUMOR: infinite loop detected (>" .. max_iterations .. " iterations)")
214
+ end
215
+
216
+ local cond = self:eval_expression(stmt.condition)
217
+ if not self:is_truthy(cond) then break end
218
+
219
+ self.mem:push_scope()
220
+ local result, is_apoptosis = self:exec_block(stmt.body)
221
+ self.mem:pop_scope()
222
+
223
+ if is_apoptosis then
224
+ return result, true
225
+ end
226
+ end
227
+
228
+ return nil, false
229
+ end
230
+
231
+ -- quarantine block - freezes all mutation
232
+ function interpreter:exec_quarantine(stmt)
233
+ self.mem:enter_quarantine()
234
+ self.mem:push_scope()
235
+ local result, is_apoptosis = self:exec_block(stmt.body)
236
+ self.mem:pop_scope()
237
+ self.mem:exit_quarantine()
238
+ return result, is_apoptosis
239
+ end
240
+
241
+ -- chemo - reset a mutated variable
242
+ function interpreter:exec_chemo(stmt)
243
+ local target = stmt.target
244
+
245
+ -- handle dotted targets like "p1.heart_rate"
246
+ if target:find("%.") then
247
+ local parts = {}
248
+ for part in target:gmatch("[^%.]+") do
249
+ parts[#parts + 1] = part
250
+ end
251
+ -- resolve the object chain
252
+ local obj_name = parts[1]
253
+ local obj_var = self.mem:lookup(obj_name)
254
+ if not obj_var then
255
+ error("UNDEFINED_CELL: '" .. obj_name .. "' does not exist")
256
+ end
257
+ if type(obj_var.value) == "table" and obj_var.value.__cell_data then
258
+ local field_name = parts[2]
259
+ local field = obj_var.value.__cell_data[field_name]
260
+ if not field then
261
+ error("UNDEFINED_CELL: field '" .. field_name .. "' does not exist")
262
+ end
263
+ -- apply chemo to the field directly
264
+ if math.random() < memory.CHEMO_KILL_CHANCE then
265
+ obj_var.value.__cell_data[field_name] = nil
266
+ print("CHEMO_LETHAL: field '" .. field_name .. "' was destroyed by treatment")
267
+ else
268
+ field.value = field.original_value
269
+ field.mutation_rate = memory.INITIAL_MUTATION_RATE
270
+ field.read_count = 0
271
+ field.is_malignant = false
272
+ end
273
+ end
274
+ else
275
+ local success, msg = self.mem:apply_chemo(target)
276
+ if msg then print(msg) end
277
+ end
278
+
279
+ return nil, false
280
+ end
281
+
282
+ -- apoptosis returns value
283
+ function interpreter:exec_apoptosis(stmt)
284
+ local value = nil
285
+ if stmt.value then
286
+ value = self:eval_expression(stmt.value)
287
+ end
288
+ return value, true
289
+ end
290
+
291
+ -- expression evaluation
292
+
293
+ function interpreter:eval_expression(expr)
294
+ local t = expr.node_type
295
+
296
+ if t == "number_literal" then
297
+ return expr.value
298
+ elseif t == "string_literal" then
299
+ return expr.value
300
+ elseif t == "bool_literal" then
301
+ return expr.value
302
+ elseif t == "nil_literal" then
303
+ return nil
304
+ elseif t == "identifier" then
305
+ return self:eval_identifier(expr)
306
+ elseif t == "dot_access" then
307
+ return self:eval_dot_access(expr)
308
+ elseif t == "binary_op" then
309
+ return self:eval_binary_op(expr)
310
+ elseif t == "unary_op" then
311
+ return self:eval_unary_op(expr)
312
+ elseif t == "call_expr" then
313
+ return self:eval_call(expr)
314
+ elseif t == "mitosis_expr" then
315
+ return self:eval_mitosis(expr)
316
+ else
317
+ error("RUNTIME_TUMOR: unknown expression type '" .. tostring(t) .. "'")
318
+ end
319
+ end
320
+
321
+ -- reading a variable triggers mutation
322
+ function interpreter:eval_identifier(expr)
323
+ local val, err = self.mem:read(expr.name)
324
+ if err then error(err) end
325
+ return val
326
+ end
327
+
328
+ -- dot access on cell objects
329
+ function interpreter:eval_dot_access(expr)
330
+ local obj = self:eval_expression(expr.object)
331
+ if type(obj) == "table" and obj.__cell_data then
332
+ local field = obj.__cell_data[expr.field]
333
+ if not field then
334
+ error("UNDEFINED_CELL: field '" .. expr.field .. "' does not exist")
335
+ end
336
+ -- reading a field triggers mutation too
337
+ if field.type == "dna" and not self.mem.in_quarantine then
338
+ field.read_count = field.read_count + 1
339
+ field.mutation_rate = math.min(
340
+ field.mutation_rate + memory.MUTATION_ESCALATION,
341
+ memory.MAX_MUTATION_RATE
342
+ )
343
+ if type(field.value) == "number" then
344
+ local delta = field.mutation_rate * (math.random() * 2 - 1)
345
+ field.value = field.value * (1 + delta)
346
+ elseif type(field.value) == "string" and #field.value > 0 then
347
+ if math.random() < field.mutation_rate * 10 then
348
+ local idx = math.random(1, #field.value)
349
+ local byte = string.byte(field.value, idx)
350
+ byte = math.max(32, math.min(126, byte + math.random(-2, 2)))
351
+ field.value = field.value:sub(1, idx - 1) .. string.char(byte) .. field.value:sub(idx + 1)
352
+ end
353
+ end
354
+ end
355
+ return field.value
356
+ elseif type(obj) == "table" then
357
+ return obj[expr.field]
358
+ end
359
+ error("RUNTIME_TUMOR: cannot access field '" .. expr.field .. "' on non-cell value")
360
+ end
361
+
362
+ -- binary operations
363
+ function interpreter:eval_binary_op(expr)
364
+ -- short-circuit for logical ops
365
+ if expr.op == "and" then
366
+ local left = self:eval_expression(expr.left)
367
+ if not self:is_truthy(left) then return left end
368
+ return self:eval_expression(expr.right)
369
+ end
370
+ if expr.op == "or" then
371
+ local left = self:eval_expression(expr.left)
372
+ if self:is_truthy(left) then return left end
373
+ return self:eval_expression(expr.right)
374
+ end
375
+
376
+ local left = self:eval_expression(expr.left)
377
+ local right = self:eval_expression(expr.right)
378
+
379
+ if expr.op == "+" then
380
+ if type(left) == "string" or type(right) == "string" then
381
+ return tostring(left) .. tostring(right)
382
+ end
383
+ return (left or 0) + (right or 0)
384
+ elseif expr.op == "-" then return (left or 0) - (right or 0)
385
+ elseif expr.op == "*" then return (left or 0) * (right or 0)
386
+ elseif expr.op == "/" then
387
+ if (right or 0) == 0 then error("RUNTIME_TUMOR: division by zero") end
388
+ return (left or 0) / (right or 0)
389
+ elseif expr.op == "%" then return (left or 0) % (right or 0)
390
+ elseif expr.op == "==" then return left == right
391
+ elseif expr.op == "!=" then return left ~= right
392
+ elseif expr.op == "<" then return (left or 0) < (right or 0)
393
+ elseif expr.op == ">" then return (left or 0) > (right or 0)
394
+ elseif expr.op == "<=" then return (left or 0) <= (right or 0)
395
+ elseif expr.op == ">=" then return (left or 0) >= (right or 0)
396
+ end
397
+
398
+ error("RUNTIME_TUMOR: unknown operator '" .. tostring(expr.op) .. "'")
399
+ end
400
+
401
+ -- unary operations
402
+ function interpreter:eval_unary_op(expr)
403
+ local operand = self:eval_expression(expr.operand)
404
+ if expr.op == "-" then return -(operand or 0) end
405
+ if expr.op == "not" then return not self:is_truthy(operand) end
406
+ error("RUNTIME_TUMOR: unknown unary operator '" .. tostring(expr.op) .. "'")
407
+ end
408
+
409
+ -- function calls
410
+ function interpreter:eval_call(expr)
411
+ local callee = expr.callee
412
+ local args = {}
413
+ for _, arg_expr in ipairs(expr.arguments) do
414
+ args[#args + 1] = self:eval_expression(arg_expr)
415
+ end
416
+
417
+ -- resolve the gene name
418
+ local gene_name = nil
419
+ if callee.node_type == "identifier" then
420
+ gene_name = callee.name
421
+ elseif callee.node_type == "dot_access" then
422
+ -- for now just use the field name
423
+ gene_name = callee.field
424
+ end
425
+
426
+ if gene_name and self.genes[gene_name] then
427
+ -- check if any args are from mutated variables, infect the gene
428
+ for i, arg_expr in ipairs(expr.arguments) do
429
+ if arg_expr.node_type == "identifier" then
430
+ local src_var = self.mem:lookup(arg_expr.name)
431
+ if src_var and src_var.mutation_rate > memory.INITIAL_MUTATION_RATE then
432
+ self.mem:infect_gene(gene_name, src_var.mutation_rate)
433
+ end
434
+ end
435
+ end
436
+
437
+ return self:call_gene(gene_name, args)
438
+ end
439
+
440
+ error("RUNTIME_TUMOR: undefined gene '" .. tostring(gene_name) .. "'")
441
+ end
442
+
443
+ -- call a gene (function)
444
+ function interpreter:call_gene(name, args)
445
+ local gene = self.genes[name]
446
+ if not gene then
447
+ error("RUNTIME_TUMOR: undefined gene '" .. name .. "'")
448
+ end
449
+
450
+ -- push call stack
451
+ self.call_stack[#self.call_stack + 1] = name
452
+
453
+ -- create new scope for the gene
454
+ self.mem:push_scope()
455
+
456
+ -- bind parameters
457
+ for i, param_name in ipairs(gene.params) do
458
+ self.mem:declare(param_name, args[i], "dna")
459
+ end
460
+
461
+ -- execute gene body
462
+ local result, _ = self:exec_block(gene.body)
463
+
464
+ -- apply gene infection to return value (if numeric)
465
+ local infection = self.mem:get_gene_infection(name)
466
+ if infection > 0 and type(result) == "number" then
467
+ local delta = infection * (math.random() * 2 - 1)
468
+ result = result * (1 + delta)
469
+ end
470
+
471
+ self.mem:pop_scope()
472
+
473
+ -- pop call stack
474
+ self.call_stack[#self.call_stack] = nil
475
+
476
+ return result
477
+ end
478
+
479
+ -- mitosis: create a new cell instance
480
+ function interpreter:eval_mitosis(expr)
481
+ local cell_def = self.cells[expr.cell_name]
482
+ if not cell_def then
483
+ error("RUNTIME_TUMOR: undefined cell '" .. expr.cell_name .. "'")
484
+ end
485
+
486
+ -- create a new cell instance as a table with __cell_data
487
+ local instance = { __cell_data = {} }
488
+
489
+ -- initialize fields from the cell definition body
490
+ for _, stmt in ipairs(cell_def.body) do
491
+ if stmt.node_type == "var_declaration" then
492
+ local value = self:eval_expression(stmt.value)
493
+ if stmt.var_type == "rna" then
494
+ instance.__cell_data[stmt.name] = {
495
+ type = "rna",
496
+ value = value,
497
+ original_value = value,
498
+ mutation_rate = 0,
499
+ read_count = 0,
500
+ is_malignant = false,
501
+ }
502
+ else
503
+ instance.__cell_data[stmt.name] = {
504
+ type = "dna",
505
+ value = value,
506
+ original_value = value,
507
+ mutation_rate = memory.INITIAL_MUTATION_RATE,
508
+ read_count = 0,
509
+ is_malignant = false,
510
+ }
511
+ end
512
+ end
513
+ end
514
+
515
+ return instance
516
+ end
517
+
518
+ -- truthiness check
519
+ function interpreter:is_truthy(value)
520
+ if value == nil then return false end
521
+ if value == false then return false end
522
+ if value == 0 then return false end
523
+ return true
524
+ end
525
+
526
+ return interpreter