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.
- tumorscript/__init__.py +2 -0
- tumorscript/bin/tumorscript.exe +0 -0
- tumorscript/cli.py +64 -0
- tumorscript/main.lua +62 -0
- tumorscript/src/interpreter.lua +526 -0
- tumorscript/src/lexer.lua +351 -0
- tumorscript/src/memory.lua +303 -0
- tumorscript/src/parser.lua +560 -0
- tumorscript-0.1.0.dist-info/METADATA +193 -0
- tumorscript-0.1.0.dist-info/RECORD +13 -0
- tumorscript-0.1.0.dist-info/WHEEL +5 -0
- tumorscript-0.1.0.dist-info/entry_points.txt +2 -0
- tumorscript-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
-- lexer module for tumorscript
|
|
2
|
+
-- tokenizes source code into a flat token stream
|
|
3
|
+
-- handles indentation-based blocks (python style) and quarantine {} blocks
|
|
4
|
+
|
|
5
|
+
local lexer = {}
|
|
6
|
+
|
|
7
|
+
-- token types
|
|
8
|
+
lexer.TOKEN = {
|
|
9
|
+
-- keywords
|
|
10
|
+
DNA = "DNA",
|
|
11
|
+
RNA = "RNA",
|
|
12
|
+
CELL = "CELL",
|
|
13
|
+
GENE = "GENE",
|
|
14
|
+
MITOSIS = "MITOSIS",
|
|
15
|
+
APOPTOSIS = "APOPTOSIS",
|
|
16
|
+
METASTASIS = "METASTASIS",
|
|
17
|
+
CHEMO = "CHEMO",
|
|
18
|
+
QUARANTINE = "QUARANTINE",
|
|
19
|
+
IF = "IF",
|
|
20
|
+
ELIF = "ELIF",
|
|
21
|
+
ELSE = "ELSE",
|
|
22
|
+
WHILE = "WHILE",
|
|
23
|
+
FOR = "FOR",
|
|
24
|
+
IN = "IN",
|
|
25
|
+
AND = "AND",
|
|
26
|
+
OR = "OR",
|
|
27
|
+
NOT = "NOT",
|
|
28
|
+
PRINT = "PRINT",
|
|
29
|
+
TRUE = "TRUE",
|
|
30
|
+
FALSE = "FALSE",
|
|
31
|
+
NIL = "NIL",
|
|
32
|
+
|
|
33
|
+
-- literals
|
|
34
|
+
NUMBER = "NUMBER",
|
|
35
|
+
STRING = "STRING",
|
|
36
|
+
IDENTIFIER = "IDENTIFIER",
|
|
37
|
+
|
|
38
|
+
-- symbols
|
|
39
|
+
PLUS = "PLUS",
|
|
40
|
+
MINUS = "MINUS",
|
|
41
|
+
STAR = "STAR",
|
|
42
|
+
SLASH = "SLASH",
|
|
43
|
+
PERCENT = "PERCENT",
|
|
44
|
+
ASSIGN = "ASSIGN",
|
|
45
|
+
EQ = "EQ",
|
|
46
|
+
NEQ = "NEQ",
|
|
47
|
+
LT = "LT",
|
|
48
|
+
GT = "GT",
|
|
49
|
+
LTE = "LTE",
|
|
50
|
+
GTE = "GTE",
|
|
51
|
+
LPAREN = "LPAREN",
|
|
52
|
+
RPAREN = "RPAREN",
|
|
53
|
+
LBRACE = "LBRACE", -- only valid after quarantine
|
|
54
|
+
RBRACE = "RBRACE", -- only valid to close quarantine
|
|
55
|
+
COMMA = "COMMA",
|
|
56
|
+
COLON = "COLON",
|
|
57
|
+
DOT = "DOT",
|
|
58
|
+
|
|
59
|
+
-- structure
|
|
60
|
+
INDENT = "INDENT",
|
|
61
|
+
DEDENT = "DEDENT",
|
|
62
|
+
NEWLINE = "NEWLINE",
|
|
63
|
+
EOF = "EOF",
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
local KEYWORDS = {
|
|
67
|
+
dna = lexer.TOKEN.DNA,
|
|
68
|
+
rna = lexer.TOKEN.RNA,
|
|
69
|
+
cell = lexer.TOKEN.CELL,
|
|
70
|
+
gene = lexer.TOKEN.GENE,
|
|
71
|
+
mitosis = lexer.TOKEN.MITOSIS,
|
|
72
|
+
apoptosis = lexer.TOKEN.APOPTOSIS,
|
|
73
|
+
metastasis = lexer.TOKEN.METASTASIS,
|
|
74
|
+
chemo = lexer.TOKEN.CHEMO,
|
|
75
|
+
quarantine = lexer.TOKEN.QUARANTINE,
|
|
76
|
+
["if"] = lexer.TOKEN.IF,
|
|
77
|
+
elif = lexer.TOKEN.ELIF,
|
|
78
|
+
["else"] = lexer.TOKEN.ELSE,
|
|
79
|
+
["while"] = lexer.TOKEN.WHILE,
|
|
80
|
+
["for"] = lexer.TOKEN.FOR,
|
|
81
|
+
["in"] = lexer.TOKEN.IN,
|
|
82
|
+
["and"] = lexer.TOKEN.AND,
|
|
83
|
+
["or"] = lexer.TOKEN.OR,
|
|
84
|
+
["not"] = lexer.TOKEN.NOT,
|
|
85
|
+
print = lexer.TOKEN.PRINT,
|
|
86
|
+
["true"] = lexer.TOKEN.TRUE,
|
|
87
|
+
["false"] = lexer.TOKEN.FALSE,
|
|
88
|
+
["nil"] = lexer.TOKEN.NIL,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
local function make_token(type, value, line, col)
|
|
92
|
+
return { type = type, value = value, line = line, col = col }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
-- tokenize the source string into a list of tokens
|
|
96
|
+
function lexer.tokenize(source, filename)
|
|
97
|
+
local tokens = {}
|
|
98
|
+
local pos = 1
|
|
99
|
+
local line = 1
|
|
100
|
+
local col = 1
|
|
101
|
+
local len = #source
|
|
102
|
+
local indent_stack = { 0 } -- track indentation levels
|
|
103
|
+
local at_line_start = true
|
|
104
|
+
local brace_depth = 0 -- track quarantine brace nesting
|
|
105
|
+
local last_keyword = nil -- track if last keyword was quarantine
|
|
106
|
+
|
|
107
|
+
local function peek(offset)
|
|
108
|
+
offset = offset or 0
|
|
109
|
+
local p = pos + offset
|
|
110
|
+
if p <= len then return source:sub(p, p) end
|
|
111
|
+
return nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
local function advance()
|
|
115
|
+
local ch = source:sub(pos, pos)
|
|
116
|
+
pos = pos + 1
|
|
117
|
+
if ch == "\n" then
|
|
118
|
+
line = line + 1
|
|
119
|
+
col = 1
|
|
120
|
+
else
|
|
121
|
+
col = col + 1
|
|
122
|
+
end
|
|
123
|
+
return ch
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
local function skip_comment()
|
|
127
|
+
while pos <= len and source:sub(pos, pos) ~= "\n" do
|
|
128
|
+
advance()
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
local function read_string(quote)
|
|
133
|
+
local start_line = line
|
|
134
|
+
local buf = {}
|
|
135
|
+
advance() -- skip opening quote
|
|
136
|
+
while pos <= len do
|
|
137
|
+
local ch = source:sub(pos, pos)
|
|
138
|
+
if ch == "\\" then
|
|
139
|
+
advance()
|
|
140
|
+
local esc = advance()
|
|
141
|
+
if esc == "n" then buf[#buf + 1] = "\n"
|
|
142
|
+
elseif esc == "t" then buf[#buf + 1] = "\t"
|
|
143
|
+
elseif esc == "\\" then buf[#buf + 1] = "\\"
|
|
144
|
+
elseif esc == quote then buf[#buf + 1] = quote
|
|
145
|
+
else buf[#buf + 1] = "\\" .. (esc or "") end
|
|
146
|
+
elseif ch == quote then
|
|
147
|
+
advance()
|
|
148
|
+
return table.concat(buf)
|
|
149
|
+
elseif ch == "\n" then
|
|
150
|
+
return nil, "unterminated string at line " .. start_line
|
|
151
|
+
else
|
|
152
|
+
buf[#buf + 1] = advance()
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
return nil, "unterminated string at line " .. start_line
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
local function read_number()
|
|
159
|
+
local start = pos
|
|
160
|
+
local has_dot = false
|
|
161
|
+
while pos <= len do
|
|
162
|
+
local ch = source:sub(pos, pos)
|
|
163
|
+
if ch == "." and not has_dot then
|
|
164
|
+
has_dot = true
|
|
165
|
+
advance()
|
|
166
|
+
elseif ch:match("[0-9]") then
|
|
167
|
+
advance()
|
|
168
|
+
else
|
|
169
|
+
break
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
return tonumber(source:sub(start, pos - 1))
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
local function read_identifier()
|
|
176
|
+
local start = pos
|
|
177
|
+
while pos <= len do
|
|
178
|
+
local ch = source:sub(pos, pos)
|
|
179
|
+
if ch:match("[a-zA-Z0-9_]") then
|
|
180
|
+
advance()
|
|
181
|
+
else
|
|
182
|
+
break
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
return source:sub(start, pos - 1)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
local function emit(type, value)
|
|
189
|
+
tokens[#tokens + 1] = make_token(type, value, line, col)
|
|
190
|
+
-- track last keyword for brace validation
|
|
191
|
+
if type == lexer.TOKEN.QUARANTINE then
|
|
192
|
+
last_keyword = "quarantine"
|
|
193
|
+
elseif type ~= lexer.TOKEN.NEWLINE and type ~= lexer.TOKEN.INDENT
|
|
194
|
+
and type ~= lexer.TOKEN.DEDENT then
|
|
195
|
+
last_keyword = nil
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
-- handle indentation at the start of a logical line
|
|
200
|
+
local function handle_indentation()
|
|
201
|
+
local spaces = 0
|
|
202
|
+
while pos <= len and source:sub(pos, pos) == " " do
|
|
203
|
+
advance()
|
|
204
|
+
spaces = spaces + 1
|
|
205
|
+
end
|
|
206
|
+
-- tabs count as 4 spaces
|
|
207
|
+
while pos <= len and source:sub(pos, pos) == "\t" do
|
|
208
|
+
advance()
|
|
209
|
+
spaces = spaces + 4
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
-- skip blank lines and comment-only lines
|
|
213
|
+
if pos > len or source:sub(pos, pos) == "\n" then
|
|
214
|
+
return
|
|
215
|
+
end
|
|
216
|
+
if source:sub(pos, pos) == "#" then
|
|
217
|
+
return
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
local current_indent = indent_stack[#indent_stack]
|
|
221
|
+
|
|
222
|
+
if spaces > current_indent then
|
|
223
|
+
indent_stack[#indent_stack + 1] = spaces
|
|
224
|
+
emit(lexer.TOKEN.INDENT, spaces)
|
|
225
|
+
elseif spaces < current_indent then
|
|
226
|
+
while #indent_stack > 1 and indent_stack[#indent_stack] > spaces do
|
|
227
|
+
indent_stack[#indent_stack] = nil
|
|
228
|
+
emit(lexer.TOKEN.DEDENT, spaces)
|
|
229
|
+
end
|
|
230
|
+
if indent_stack[#indent_stack] ~= spaces then
|
|
231
|
+
return nil, "inconsistent indentation at line " .. line
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
-- main tokenization loop
|
|
237
|
+
while pos <= len do
|
|
238
|
+
-- handle line starts for indentation
|
|
239
|
+
if at_line_start then
|
|
240
|
+
at_line_start = false
|
|
241
|
+
local err = handle_indentation()
|
|
242
|
+
if err then return nil, err end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
if pos > len then break end
|
|
246
|
+
|
|
247
|
+
local ch = source:sub(pos, pos)
|
|
248
|
+
|
|
249
|
+
-- skip whitespace (not newlines, those are significant)
|
|
250
|
+
if ch == " " or ch == "\t" then
|
|
251
|
+
advance()
|
|
252
|
+
|
|
253
|
+
-- newlines
|
|
254
|
+
elseif ch == "\n" then
|
|
255
|
+
emit(lexer.TOKEN.NEWLINE, "\\n")
|
|
256
|
+
advance()
|
|
257
|
+
at_line_start = true
|
|
258
|
+
|
|
259
|
+
-- carriage return (windows line endings)
|
|
260
|
+
elseif ch == "\r" then
|
|
261
|
+
advance()
|
|
262
|
+
|
|
263
|
+
-- comments
|
|
264
|
+
elseif ch == "#" then
|
|
265
|
+
skip_comment()
|
|
266
|
+
|
|
267
|
+
-- strings
|
|
268
|
+
elseif ch == '"' or ch == "'" then
|
|
269
|
+
local str, err = read_string(ch)
|
|
270
|
+
if err then return nil, err end
|
|
271
|
+
emit(lexer.TOKEN.STRING, str)
|
|
272
|
+
|
|
273
|
+
-- numbers
|
|
274
|
+
elseif ch:match("[0-9]") then
|
|
275
|
+
local num = read_number()
|
|
276
|
+
emit(lexer.TOKEN.NUMBER, num)
|
|
277
|
+
|
|
278
|
+
-- identifiers and keywords
|
|
279
|
+
elseif ch:match("[a-zA-Z_]") then
|
|
280
|
+
local ident = read_identifier()
|
|
281
|
+
local kw = KEYWORDS[ident]
|
|
282
|
+
if kw then
|
|
283
|
+
emit(kw, ident)
|
|
284
|
+
else
|
|
285
|
+
emit(lexer.TOKEN.IDENTIFIER, ident)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
-- two-char operators
|
|
289
|
+
elseif ch == "=" and peek(1) == "=" then
|
|
290
|
+
advance(); advance()
|
|
291
|
+
emit(lexer.TOKEN.EQ, "==")
|
|
292
|
+
elseif ch == "!" and peek(1) == "=" then
|
|
293
|
+
advance(); advance()
|
|
294
|
+
emit(lexer.TOKEN.NEQ, "!=")
|
|
295
|
+
elseif ch == "<" and peek(1) == "=" then
|
|
296
|
+
advance(); advance()
|
|
297
|
+
emit(lexer.TOKEN.LTE, "<=")
|
|
298
|
+
elseif ch == ">" and peek(1) == "=" then
|
|
299
|
+
advance(); advance()
|
|
300
|
+
emit(lexer.TOKEN.GTE, ">=")
|
|
301
|
+
|
|
302
|
+
-- standalone comparison operators (must come after two-char checks)
|
|
303
|
+
elseif ch == "<" then advance(); emit(lexer.TOKEN.LT, "<")
|
|
304
|
+
elseif ch == ">" then advance(); emit(lexer.TOKEN.GT, ">")
|
|
305
|
+
|
|
306
|
+
-- single-char operators and symbols
|
|
307
|
+
elseif ch == "=" then advance(); emit(lexer.TOKEN.ASSIGN, "=")
|
|
308
|
+
elseif ch == "+" then advance(); emit(lexer.TOKEN.PLUS, "+")
|
|
309
|
+
elseif ch == "-" then advance(); emit(lexer.TOKEN.MINUS, "-")
|
|
310
|
+
elseif ch == "*" then advance(); emit(lexer.TOKEN.STAR, "*")
|
|
311
|
+
elseif ch == "/" then advance(); emit(lexer.TOKEN.SLASH, "/")
|
|
312
|
+
elseif ch == "%" then advance(); emit(lexer.TOKEN.PERCENT, "%")
|
|
313
|
+
elseif ch == "(" then advance(); emit(lexer.TOKEN.LPAREN, "(")
|
|
314
|
+
elseif ch == ")" then advance(); emit(lexer.TOKEN.RPAREN, ")")
|
|
315
|
+
elseif ch == "," then advance(); emit(lexer.TOKEN.COMMA, ",")
|
|
316
|
+
elseif ch == ":" then advance(); emit(lexer.TOKEN.COLON, ":")
|
|
317
|
+
elseif ch == "." then advance(); emit(lexer.TOKEN.DOT, ".")
|
|
318
|
+
|
|
319
|
+
-- braces - only valid in quarantine context
|
|
320
|
+
elseif ch == "{" then
|
|
321
|
+
if last_keyword ~= "quarantine" and brace_depth == 0 then
|
|
322
|
+
return nil, "SYNTAX_TUMOR: '{' is only allowed after 'quarantine' at line " .. line
|
|
323
|
+
end
|
|
324
|
+
brace_depth = brace_depth + 1
|
|
325
|
+
advance()
|
|
326
|
+
emit(lexer.TOKEN.LBRACE, "{")
|
|
327
|
+
|
|
328
|
+
elseif ch == "}" then
|
|
329
|
+
if brace_depth == 0 then
|
|
330
|
+
return nil, "SYNTAX_TUMOR: unexpected '}' at line " .. line
|
|
331
|
+
end
|
|
332
|
+
brace_depth = brace_depth - 1
|
|
333
|
+
advance()
|
|
334
|
+
emit(lexer.TOKEN.RBRACE, "}")
|
|
335
|
+
|
|
336
|
+
else
|
|
337
|
+
return nil, "unexpected character '" .. ch .. "' at line " .. line
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
-- emit remaining dedents
|
|
342
|
+
while #indent_stack > 1 do
|
|
343
|
+
indent_stack[#indent_stack] = nil
|
|
344
|
+
emit(lexer.TOKEN.DEDENT, 0)
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
emit(lexer.TOKEN.EOF, nil)
|
|
348
|
+
return tokens, nil
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
return lexer
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
-- memory module for tumorscript
|
|
2
|
+
-- handles dna mutation, rna immutability, infection spread, and organ failure
|
|
3
|
+
|
|
4
|
+
local memory = {}
|
|
5
|
+
memory.__index = memory
|
|
6
|
+
|
|
7
|
+
-- config constants
|
|
8
|
+
memory.INITIAL_MUTATION_RATE = 0.005 -- 0.5% starting mutation
|
|
9
|
+
memory.MAX_MUTATION_RATE = 0.05 -- 5% max mutation
|
|
10
|
+
memory.MUTATION_ESCALATION = 0.003 -- how fast mutation rate climbs per read
|
|
11
|
+
memory.INFECTION_CHANCE = 0.30 -- 30% chance of infecting healthy vars
|
|
12
|
+
memory.CHEMO_KILL_CHANCE = 0.15 -- 15% chance chemo destroys the var
|
|
13
|
+
memory.MALIGNANT_THRESHOLD = 0.025 -- mutation rate above this = malignant
|
|
14
|
+
memory.ORGAN_FAILURE_RATIO = 0.50 -- 50% malignant vars = system crash
|
|
15
|
+
memory.QUARANTINE_CODE_LIMIT = 0.20 -- max 20% of code can be quarantined
|
|
16
|
+
|
|
17
|
+
-- dna variable structure
|
|
18
|
+
local function make_dna(value)
|
|
19
|
+
return {
|
|
20
|
+
type = "dna",
|
|
21
|
+
value = value,
|
|
22
|
+
original_value = value,
|
|
23
|
+
mutation_rate = memory.INITIAL_MUTATION_RATE,
|
|
24
|
+
read_count = 0,
|
|
25
|
+
is_malignant = false,
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
-- rna variable structure (immune to mutation)
|
|
30
|
+
local function make_rna(value)
|
|
31
|
+
return {
|
|
32
|
+
type = "rna",
|
|
33
|
+
value = value,
|
|
34
|
+
original_value = value,
|
|
35
|
+
mutation_rate = 0,
|
|
36
|
+
read_count = 0,
|
|
37
|
+
is_malignant = false,
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
function memory.new()
|
|
42
|
+
local self = setmetatable({}, memory)
|
|
43
|
+
self.scopes = { {} } -- stack of scope tables
|
|
44
|
+
self.total_vars = 0
|
|
45
|
+
self.malignant_count = 0
|
|
46
|
+
self.in_quarantine = false
|
|
47
|
+
self.gene_infection = {} -- tracks gene contamination levels
|
|
48
|
+
return self
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
-- push a new scope (entering a block)
|
|
52
|
+
function memory:push_scope()
|
|
53
|
+
self.scopes[#self.scopes + 1] = {}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
-- pop the current scope
|
|
57
|
+
function memory:pop_scope()
|
|
58
|
+
local scope = self.scopes[#self.scopes]
|
|
59
|
+
if scope then
|
|
60
|
+
-- clean up var counts
|
|
61
|
+
for _, var in pairs(scope) do
|
|
62
|
+
self.total_vars = self.total_vars - 1
|
|
63
|
+
if var.is_malignant then
|
|
64
|
+
self.malignant_count = self.malignant_count - 1
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
self.scopes[#self.scopes] = nil
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
-- declare a new variable in the current scope
|
|
72
|
+
function memory:declare(name, value, var_type)
|
|
73
|
+
local scope = self.scopes[#self.scopes]
|
|
74
|
+
if not scope then return nil, "no active scope" end
|
|
75
|
+
|
|
76
|
+
local var
|
|
77
|
+
if var_type == "rna" then
|
|
78
|
+
var = make_rna(value)
|
|
79
|
+
else
|
|
80
|
+
var = make_dna(value)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
scope[name] = var
|
|
84
|
+
self.total_vars = self.total_vars + 1
|
|
85
|
+
return var
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
-- find a variable by name, searching scopes from top to bottom
|
|
89
|
+
function memory:lookup(name)
|
|
90
|
+
for i = #self.scopes, 1, -1 do
|
|
91
|
+
if self.scopes[i][name] then
|
|
92
|
+
return self.scopes[i][name], i
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
return nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
-- read a variable's value (this triggers mutation for dna)
|
|
99
|
+
function memory:read(name)
|
|
100
|
+
local var = self:lookup(name)
|
|
101
|
+
if not var then
|
|
102
|
+
return nil, "UNDEFINED_CELL: '" .. name .. "' does not exist"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
-- rna is immune, always returns clean value
|
|
106
|
+
if var.type == "rna" then
|
|
107
|
+
return var.value
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
-- quarantine freezes all mutation
|
|
111
|
+
if self.in_quarantine then
|
|
112
|
+
return var.value
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
-- table objects like cells do not mutate directly
|
|
116
|
+
if type(var.value) == "table" then
|
|
117
|
+
return var.value
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
-- dna mutates on every read
|
|
121
|
+
var.read_count = var.read_count + 1
|
|
122
|
+
|
|
123
|
+
-- escalate mutation rate with each read
|
|
124
|
+
var.mutation_rate = math.min(
|
|
125
|
+
var.mutation_rate + memory.MUTATION_ESCALATION,
|
|
126
|
+
memory.MAX_MUTATION_RATE
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
-- apply mutation based on value type
|
|
130
|
+
local val = var.value
|
|
131
|
+
if type(val) == "number" then
|
|
132
|
+
-- numeric mutation adds or subtracts deviation
|
|
133
|
+
local delta = var.mutation_rate * (math.random() * 2 - 1)
|
|
134
|
+
val = val * (1 + delta)
|
|
135
|
+
-- small chance of integer drift
|
|
136
|
+
if math.random() < var.mutation_rate then
|
|
137
|
+
val = val + (math.random() > 0.5 and 1 or -1)
|
|
138
|
+
end
|
|
139
|
+
var.value = val
|
|
140
|
+
elseif type(val) == "string" then
|
|
141
|
+
-- string mutation shifts random character
|
|
142
|
+
if #val > 0 and math.random() < var.mutation_rate * 10 then
|
|
143
|
+
local idx = math.random(1, #val)
|
|
144
|
+
local byte = string.byte(val, idx)
|
|
145
|
+
local shift = math.random(-2, 2)
|
|
146
|
+
byte = math.max(32, math.min(126, byte + shift))
|
|
147
|
+
val = val:sub(1, idx - 1) .. string.char(byte) .. val:sub(idx + 1)
|
|
148
|
+
var.value = val
|
|
149
|
+
end
|
|
150
|
+
elseif type(val) == "boolean" then
|
|
151
|
+
-- booleans can flip when mutation is high
|
|
152
|
+
if math.random() < var.mutation_rate then
|
|
153
|
+
val = not val
|
|
154
|
+
var.value = val
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
-- check if cancer passed fifty percent deviation
|
|
159
|
+
local is_now_malignant = false
|
|
160
|
+
if type(val) == "number" and type(var.original_value) == "number" and var.original_value ~= 0 then
|
|
161
|
+
local deviation = math.abs(val - var.original_value) / math.abs(var.original_value)
|
|
162
|
+
if deviation >= 0.50 then
|
|
163
|
+
is_now_malignant = true
|
|
164
|
+
end
|
|
165
|
+
elseif var.mutation_rate >= memory.MAX_MUTATION_RATE and var.read_count >= 20 then
|
|
166
|
+
is_now_malignant = true
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
if not var.is_malignant and is_now_malignant then
|
|
170
|
+
var.is_malignant = true
|
|
171
|
+
self.malignant_count = self.malignant_count + 1
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
-- check for organ failure
|
|
175
|
+
self:check_organ_failure()
|
|
176
|
+
|
|
177
|
+
return var.value
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
-- write a value to an existing variable
|
|
181
|
+
function memory:write(name, value)
|
|
182
|
+
local var = self:lookup(name)
|
|
183
|
+
if not var then
|
|
184
|
+
return nil, "UNDEFINED_CELL: '" .. name .. "' does not exist"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
if var.type == "rna" then
|
|
188
|
+
return nil, "IMMUNE_VIOLATION: cannot reassign rna constant '" .. name .. "'"
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
var.value = value
|
|
192
|
+
return value
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
-- apply chemo to a variable (reset it, 15% chance of destruction)
|
|
196
|
+
function memory:apply_chemo(name)
|
|
197
|
+
local var, scope_idx = self:lookup(name)
|
|
198
|
+
if not var then
|
|
199
|
+
return false, "UNDEFINED_CELL: '" .. name .. "' does not exist"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
-- 15% chance chemo kills the variable entirely
|
|
203
|
+
if math.random() < memory.CHEMO_KILL_CHANCE then
|
|
204
|
+
self.scopes[scope_idx][name] = nil
|
|
205
|
+
self.total_vars = self.total_vars - 1
|
|
206
|
+
if var.is_malignant then
|
|
207
|
+
self.malignant_count = self.malignant_count - 1
|
|
208
|
+
end
|
|
209
|
+
return true, "CHEMO_LETHAL: variable '" .. name .. "' was destroyed by treatment"
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
-- reset the variable to its original state
|
|
213
|
+
var.value = var.original_value
|
|
214
|
+
var.mutation_rate = memory.INITIAL_MUTATION_RATE
|
|
215
|
+
var.read_count = 0
|
|
216
|
+
if var.is_malignant then
|
|
217
|
+
var.is_malignant = false
|
|
218
|
+
self.malignant_count = self.malignant_count - 1
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
return true, nil
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
-- attempt to infect a target variable from a malignant source
|
|
225
|
+
function memory:try_infect(target_name, source_var)
|
|
226
|
+
if not source_var or source_var.type == "rna" then return end
|
|
227
|
+
if not source_var.is_malignant and source_var.mutation_rate < memory.INITIAL_MUTATION_RATE * 2 then
|
|
228
|
+
return
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
local target = self:lookup(target_name)
|
|
232
|
+
if not target or target.type == "rna" then return end
|
|
233
|
+
|
|
234
|
+
-- 30% chance of infection spreading
|
|
235
|
+
if math.random() < memory.INFECTION_CHANCE then
|
|
236
|
+
target.mutation_rate = math.min(
|
|
237
|
+
target.mutation_rate + source_var.mutation_rate * 0.5,
|
|
238
|
+
memory.MAX_MUTATION_RATE
|
|
239
|
+
)
|
|
240
|
+
if not target.is_malignant and target.mutation_rate >= memory.MALIGNANT_THRESHOLD then
|
|
241
|
+
target.is_malignant = true
|
|
242
|
+
self.malignant_count = self.malignant_count + 1
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
-- infect a gene's internal state when called with mutated args
|
|
248
|
+
function memory:infect_gene(gene_name, arg_mutation_rate)
|
|
249
|
+
if not self.gene_infection[gene_name] then
|
|
250
|
+
self.gene_infection[gene_name] = 0
|
|
251
|
+
end
|
|
252
|
+
self.gene_infection[gene_name] = math.min(
|
|
253
|
+
self.gene_infection[gene_name] + arg_mutation_rate * 0.3,
|
|
254
|
+
memory.MAX_MUTATION_RATE
|
|
255
|
+
)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
-- get a gene's current infection level
|
|
259
|
+
function memory:get_gene_infection(gene_name)
|
|
260
|
+
return self.gene_infection[gene_name] or 0
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
-- check if system has too many malignant variables
|
|
264
|
+
function memory:check_organ_failure()
|
|
265
|
+
if self.total_vars > 0 then
|
|
266
|
+
local ratio = self.malignant_count / self.total_vars
|
|
267
|
+
if ratio >= memory.ORGAN_FAILURE_RATIO and self.total_vars >= 3 then
|
|
268
|
+
error("FATAL: ORGAN_FAILURE_EXCEPTION - "
|
|
269
|
+
.. self.malignant_count .. "/" .. self.total_vars
|
|
270
|
+
.. " variables are malignant ("
|
|
271
|
+
.. string.format("%.1f%%", ratio * 100) .. ")")
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
-- enter quarantine mode
|
|
277
|
+
function memory:enter_quarantine()
|
|
278
|
+
self.in_quarantine = true
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
-- exit quarantine mode
|
|
282
|
+
function memory:exit_quarantine()
|
|
283
|
+
self.in_quarantine = false
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
-- debug: dump current memory state
|
|
287
|
+
function memory:dump()
|
|
288
|
+
print("=== MEMORY DUMP ===")
|
|
289
|
+
print("total vars: " .. self.total_vars)
|
|
290
|
+
print("malignant: " .. self.malignant_count)
|
|
291
|
+
print("quarantine: " .. tostring(self.in_quarantine))
|
|
292
|
+
for i = #self.scopes, 1, -1 do
|
|
293
|
+
print("--- scope " .. i .. " ---")
|
|
294
|
+
for name, var in pairs(self.scopes[i]) do
|
|
295
|
+
print(string.format(" %s [%s] = %s (rate=%.4f, reads=%d, malignant=%s)",
|
|
296
|
+
name, var.type, tostring(var.value),
|
|
297
|
+
var.mutation_rate, var.read_count, tostring(var.is_malignant)))
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
print("===================")
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
return memory
|