utilitize crashed state

This commit is contained in:
afonya2 2025-05-24 18:29:08 +02:00
parent 8403628d7f
commit 724bec6eb2
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC

View file

@ -131,7 +131,8 @@ impl Executor {
let red = self.read_memory(key.clone(), true);
match red {
ASTPart::NOOP => {
panic!("Uanble to update non-existing memory key: {}", key);
self.state = State::Error(format!("Unable to update non-existing memory key: {}", key));
return;
},
_ => {
self.delete_memory(key.clone());
@ -157,13 +158,21 @@ impl Executor {
self.memory[current_scope].push(MemoryData::Null(MemoryNull { key: key }));
},
_ => {
panic!("Unable to write to memory: {:?}", value);
self.state = State::Error(format!("Unable to write to memory: {:?}", value));
return;
}
}
}
fn process_code(&mut self, code: &ASTPart, op_count: &mut usize) -> ASTPart {
println!("Processing code: {:?}", code);
if self.state != State::Running {
return ASTPart::NOOP;
}
if *op_count >= self.max_oppr {
self.state = State::Error(String::from("Too long without yielding!"));
return ASTPart::NOOP;
}
*op_count += 1;
match code {
ASTPart::String(_) => {
@ -177,12 +186,18 @@ impl Executor {
},
ASTPart::Assigment(assign) => {
let value = self.process_code(&assign.value, op_count);
if self.state != State::Running {
return ASTPart::NOOP;
}
self.write_memory(assign.variable.clone(), value, false);
return ASTPart::NOOP;
},
ASTPart::Operation(op) => {
let left = self.process_code(&op.left, op_count);
let right = self.process_code(&op.right, op_count);
if self.state != State::Running {
return ASTPart::NOOP;
}
if op.operator == "+" {
match (&left, &right) {
(ASTPart::Number(left_num), ASTPart::Number(right_num)) => {
@ -192,7 +207,8 @@ impl Executor {
return ASTPart::String(AstString { value: left_str.value.clone() + &right_str.value, pos: 0 });
},
_ => {
panic!("Unable to do operation '+' on {:?} with {:?} at {}", left, right, op.pos);
self.state = State::Error(format!("Unable to do operation '+' on {:?} with {:?} at {}", left, right, op.pos));
return ASTPart::NOOP;
}
}
} else if op.operator == "-" {
@ -201,7 +217,8 @@ impl Executor {
return ASTPart::Number(AstNumber { value: left_num.value - right_num.value, pos: 0 });
},
_ => {
panic!("Unable to do operation '-' on {:?} with {:?} at {}", left, right, op.pos);
self.state = State::Error(format!("Unable to do operation '-' on {:?} with {:?} at {}", left, right, op.pos));
return ASTPart::NOOP;
}
}
} else if op.operator == "*" {
@ -210,19 +227,22 @@ impl Executor {
return ASTPart::Number(AstNumber { value: left_num.value * right_num.value, pos: 0 });
},
_ => {
panic!("Unable to do operation '*' on {:?} with {:?} at {}", left, right, op.pos);
self.state = State::Error(format!("Unable to do operation '*' on {:?} with {:?} at {}", left, right, op.pos));
return ASTPart::NOOP;
}
}
} else if op.operator == "/" {
match (&left, &right) {
(ASTPart::Number(left_num), ASTPart::Number(right_num)) => {
if right_num.value == 0 {
panic!("Unable to divide by zero at {}!", op.pos);
self.state = State::Error(format!("Unable to divide by zero at {}", op.pos));
return ASTPart::NOOP;
}
return ASTPart::Number(AstNumber { value: left_num.value / right_num.value, pos: 0 });
},
_ => {
panic!("Unable to do operation '/' on {:?} with {:?} at {}", left, right, op.pos);
self.state = State::Error(format!("Unable to do operation '/' on {:?} with {:?} at {}", left, right, op.pos));
return ASTPart::NOOP;
}
}
} else if op.operator == "^" {
@ -231,23 +251,27 @@ impl Executor {
return ASTPart::Number(AstNumber { value: left_num.value.pow(right_num.value as u32), pos: 0 });
},
_ => {
panic!("Unable to do operation '^' on {:?} with {:?} at {}", left, right, op.pos);
self.state = State::Error(format!("Unable to do operation '^' on {:?} with {:?} at {}", left, right, op.pos));
return ASTPart::NOOP;
}
}
} else if op.operator == "%" {
match (&left, &right) {
(ASTPart::Number(left_num), ASTPart::Number(right_num)) => {
if right_num.value == 0 {
panic!("Unable to divide by zero at {}!", op.pos);
self.state = State::Error(format!("Unable to divide by zero at {}", op.pos));
return ASTPart::NOOP;
}
return ASTPart::Number(AstNumber { value: left_num.value % right_num.value, pos: 0 });
},
_ => {
panic!("Unable to do operation '%' on {:?} with {:?} at {}", left, right, op.pos);
self.state = State::Error(format!("Unable to do operation '%' on {:?} with {:?} at {}", left, right, op.pos));
return ASTPart::NOOP;
}
}
} else {
panic!("Unknown operator: {} at {}", op.operator, op.pos);
self.state = State::Error(format!("Unknown operator: {} at {}", op.operator, op.pos));
return ASTPart::NOOP;
}
},
ASTPart::VarRead(var) => {
@ -256,6 +280,9 @@ impl Executor {
},
ASTPart::VarUpdate(updt) => {
let value = self.process_code(&updt.value, op_count);
if self.state != State::Running {
return ASTPart::NOOP;
}
self.write_memory(updt.variable.clone(), value, true);
return ASTPart::NOOP;
},