remove old executor
This commit is contained in:
parent
a5c2420d20
commit
40a1a459b3
4 changed files with 1 additions and 552 deletions
|
@ -1,33 +0,0 @@
|
||||||
use crate::{executor::{Executor, MemoryData, MemoryRustFunction}, parser::ASTPart};
|
|
||||||
|
|
||||||
fn to_printed(from: &ASTPart) -> String {
|
|
||||||
match from {
|
|
||||||
ASTPart::String(s) => s.value.clone(),
|
|
||||||
ASTPart::Number(n) => n.value.to_string(),
|
|
||||||
ASTPart::Boolean(b) => b.value.to_string(),
|
|
||||||
ASTPart::Null(_) => String::from("null"),
|
|
||||||
_ => String::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ugass(_exec: &mut Executor, args: Vec<ASTPart>) -> ASTPart {
|
|
||||||
let mut out = String::new();
|
|
||||||
for arg in &args {
|
|
||||||
out.push_str(&to_printed(arg));
|
|
||||||
out.push_str(" ");
|
|
||||||
}
|
|
||||||
println!("{}", out);
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_memory(exec: &mut Executor, _args: Vec<ASTPart>) -> ASTPart {
|
|
||||||
println!("Memory dump: {:?}", exec.memory);
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate() -> Vec<MemoryData> {
|
|
||||||
let mut out: Vec<MemoryData> = vec![];
|
|
||||||
out.push(MemoryData::RustFunction(MemoryRustFunction { key: String::from("ugass"), func: ugass }));
|
|
||||||
out.push(MemoryData::RustFunction(MemoryRustFunction { key: String::from("printMemory"), func: print_memory }));
|
|
||||||
return out;
|
|
||||||
}
|
|
503
src/executor.rs
503
src/executor.rs
|
@ -1,503 +0,0 @@
|
||||||
use crate::{enviroment, parser::{ASTPart, AstBool, AstFunction, AstNull, AstNumber, AstRustFunction, AstString}};
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
pub enum State {
|
|
||||||
Running,
|
|
||||||
Paused,
|
|
||||||
Finished,
|
|
||||||
Error(String)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum MemoryData {
|
|
||||||
Number(MemoryNumber),
|
|
||||||
String(MemoryString),
|
|
||||||
Boolean(MemoryBoolean),
|
|
||||||
Null(MemoryNull),
|
|
||||||
Function(MemoryFunction),
|
|
||||||
RustFunction(MemoryRustFunction),
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct MemoryNumber {
|
|
||||||
pub key: String,
|
|
||||||
pub value: i64
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct MemoryString {
|
|
||||||
pub key: String,
|
|
||||||
pub value: String
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct MemoryBoolean {
|
|
||||||
pub key: String,
|
|
||||||
pub value: bool
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct MemoryNull {
|
|
||||||
pub key: String,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct MemoryFunction {
|
|
||||||
pub key: String,
|
|
||||||
pub args: Vec<String>,
|
|
||||||
pub body: Vec<ASTPart>
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct MemoryRustFunction {
|
|
||||||
pub key: String,
|
|
||||||
pub func: fn(&mut Executor, Vec<ASTPart>) -> ASTPart
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Executor {
|
|
||||||
pub code: Vec<ASTPart>,
|
|
||||||
pub pos: usize,
|
|
||||||
pub memory: Vec<Vec<MemoryData>>,
|
|
||||||
pub state: State,
|
|
||||||
pub breakpoints: Vec<usize>,
|
|
||||||
pub max_oppr: usize
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Executor {
|
|
||||||
pub fn new(code: Vec<ASTPart>, max_oppr: usize) -> Executor {
|
|
||||||
let env = enviroment::generate();
|
|
||||||
return Executor {
|
|
||||||
code,
|
|
||||||
pos: 0,
|
|
||||||
memory: vec![
|
|
||||||
env,
|
|
||||||
vec![]
|
|
||||||
],
|
|
||||||
breakpoints: vec![],
|
|
||||||
state: State::Paused,
|
|
||||||
max_oppr,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_memory(&mut self, key: String, check_mode: bool) -> ASTPart {
|
|
||||||
for scope in (0..self.memory.len()).rev() {
|
|
||||||
for data in &self.memory[scope] {
|
|
||||||
match data {
|
|
||||||
MemoryData::Number(num) => {
|
|
||||||
if num.key == key {
|
|
||||||
return ASTPart::Number(AstNumber { value: num.value, pos: 0 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::String(str) => {
|
|
||||||
if str.key == key {
|
|
||||||
return ASTPart::String(AstString { value: str.value.clone(), pos: 0 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::Boolean(bool_data) => {
|
|
||||||
if bool_data.key == key {
|
|
||||||
return ASTPart::Boolean(AstBool { value: bool_data.value, pos: 0 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::Null(null_data) => {
|
|
||||||
if null_data.key == key {
|
|
||||||
return ASTPart::Null(AstNull { pos: 0 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::Function(func) => {
|
|
||||||
if func.key == key {
|
|
||||||
return ASTPart::Function(AstFunction { args: func.args.clone(), body: func.body.clone(), pos: 0 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::RustFunction(func) => {
|
|
||||||
if func.key == key {
|
|
||||||
return ASTPart::RustFunction(AstRustFunction { func: func.func, pos: 0 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if check_mode {
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ASTPart::Null(AstNull { pos: 0 });
|
|
||||||
}
|
|
||||||
|
|
||||||
fn delete_memory(&mut self, key: String) -> bool {
|
|
||||||
let current_scope = self.memory.len() - 1;
|
|
||||||
for data in 0..self.memory[current_scope].len() {
|
|
||||||
match &self.memory[current_scope][data] {
|
|
||||||
MemoryData::Number(num) => {
|
|
||||||
if num.key == key {
|
|
||||||
self.memory[current_scope].remove(data);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::String(str) => {
|
|
||||||
if str.key == key {
|
|
||||||
self.memory[current_scope].remove(data);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::Boolean(bool_data) => {
|
|
||||||
if bool_data.key == key {
|
|
||||||
self.memory[current_scope].remove(data);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::Null(null_data) => {
|
|
||||||
if null_data.key == key {
|
|
||||||
self.memory[current_scope].remove(data);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::Function(func) => {
|
|
||||||
if func.key == key {
|
|
||||||
self.memory[current_scope].remove(data);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
MemoryData::RustFunction(func) => {
|
|
||||||
if func.key == key {
|
|
||||||
self.memory[current_scope].remove(data);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_memory(&mut self, key: String, value: ASTPart, update: bool) {
|
|
||||||
if !update {
|
|
||||||
self.delete_memory(key.clone());
|
|
||||||
} else {
|
|
||||||
let red = self.read_memory(key.clone(), true);
|
|
||||||
match red {
|
|
||||||
ASTPart::NOOP => {
|
|
||||||
self.state = State::Error(format!("Unable to update non-existing memory key: {}", key));
|
|
||||||
return;
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
self.delete_memory(key.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let current_scope = self.memory.len() - 1;
|
|
||||||
match value {
|
|
||||||
ASTPart::Number(num) => {
|
|
||||||
self.memory[current_scope].push(MemoryData::Number(MemoryNumber { key: key, value: num.value }));
|
|
||||||
},
|
|
||||||
ASTPart::String(str) => {
|
|
||||||
self.memory[current_scope].push(MemoryData::String(MemoryString { key: key, value: str.value }));
|
|
||||||
},
|
|
||||||
ASTPart::Boolean(bool_data) => {
|
|
||||||
self.memory[current_scope].push(MemoryData::Boolean(MemoryBoolean { key: key, value: bool_data.value }));
|
|
||||||
},
|
|
||||||
ASTPart::Null(_) => {
|
|
||||||
self.memory[current_scope].push(MemoryData::Null(MemoryNull { key: key }));
|
|
||||||
},
|
|
||||||
ASTPart::Function(func) => {
|
|
||||||
self.memory[current_scope].push(MemoryData::Function(MemoryFunction { key: key, args: func.args.clone(), body: func.body.clone() }));
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
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(_) => {
|
|
||||||
return code.clone();
|
|
||||||
},
|
|
||||||
ASTPart::Number(_) => {
|
|
||||||
return code.clone();
|
|
||||||
},
|
|
||||||
ASTPart::Boolean(_) => {
|
|
||||||
return code.clone();
|
|
||||||
},
|
|
||||||
ASTPart::Function(_) => {
|
|
||||||
return code.clone();
|
|
||||||
},
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Number(AstNumber { value: left_num.value + right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::String(left_str), ASTPart::String(right_str)) => {
|
|
||||||
return ASTPart::String(AstString { value: left_str.value.clone() + &right_str.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Number(AstNumber { value: left_num.value - right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Number(AstNumber { value: left_num.value * right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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 {
|
|
||||||
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 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Number(AstNumber { value: left_num.value.pow(right_num.value as u32), pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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 {
|
|
||||||
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 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_num.value == right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::String(left_str), ASTPart::String(right_str)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_str.value == right_str.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::Boolean(left_bool), ASTPart::Boolean(right_bool)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_bool.value == right_bool.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::Null(_), ASTPart::Null(_)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: true, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_num.value != right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::String(left_str), ASTPart::String(right_str)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_str.value != right_str.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::Boolean(left_bool), ASTPart::Boolean(right_bool)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_bool.value != right_bool.value, pos: 0 });
|
|
||||||
},
|
|
||||||
(ASTPart::Null(_), ASTPart::Null(_)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: false, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_num.value < right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_num.value > right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_num.value <= right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
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)) => {
|
|
||||||
return ASTPart::Boolean(AstBool { value: left_num.value >= right_num.value, pos: 0 });
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
self.state = State::Error(format!("Unable to do operation '>=' on {:?} with {:?} at {}", left, right, op.pos));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.state = State::Error(format!("Unknown operator: {} at {}", op.operator, op.pos));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ASTPart::VarRead(var) => {
|
|
||||||
let data = self.read_memory(var.variable.clone(), false);
|
|
||||||
return data;
|
|
||||||
},
|
|
||||||
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;
|
|
||||||
},
|
|
||||||
ASTPart::If(if_data) => {
|
|
||||||
let condition = self.process_code(&if_data.condition, op_count);
|
|
||||||
if self.state != State::Running {
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
let is_true = match condition {
|
|
||||||
ASTPart::Boolean(bool) => {
|
|
||||||
bool.value
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
self.state = State::Error(format!("If condition must be a boolean, got: {:?} at {}", condition, if_data.pos));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if is_true {
|
|
||||||
self.memory.push(vec![]);
|
|
||||||
for opc in &if_data.body {
|
|
||||||
self.process_code(opc, op_count);
|
|
||||||
if op_count >= &mut self.max_oppr {
|
|
||||||
self.state = State::Error(String::from("Too long without yielding!"));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
if self.state != State::Running {
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.memory.pop();
|
|
||||||
}
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
},
|
|
||||||
ASTPart::Call(call) => {
|
|
||||||
let function = self.process_code(&call.function, op_count);
|
|
||||||
if self.state != State::Running {
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
match function {
|
|
||||||
ASTPart::Function(func) => {
|
|
||||||
self.memory.push(vec![]);
|
|
||||||
for i in 0..call.args.len() {
|
|
||||||
self.write_memory(func.args[i].clone(), call.args[i].clone(), false);
|
|
||||||
}
|
|
||||||
for opc in &func.body {
|
|
||||||
self.process_code(opc, op_count);
|
|
||||||
if op_count >= &mut self.max_oppr {
|
|
||||||
self.state = State::Error(String::from("Too long without yielding!"));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
if self.state != State::Running {
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.memory.pop();
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
},
|
|
||||||
ASTPart::RustFunction(func) => {
|
|
||||||
let res = (func.func)(self, call.args.clone());
|
|
||||||
return res;
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
self.state = State::Error(format!("Call must have a function, got: {:?} at {}", function, call.pos));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
self.state = State::Error(format!("Unknown AST part: {:?}", code));
|
|
||||||
return ASTPart::NOOP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resume(&mut self) {
|
|
||||||
if self.state != State::Paused {
|
|
||||||
panic!("Cannot resume executor, it is not paused.");
|
|
||||||
}
|
|
||||||
self.state = State::Running;
|
|
||||||
let mut op_count: usize = 0;
|
|
||||||
while self.pos < self.code.len() {
|
|
||||||
if self.breakpoints.len() > 0 && self.breakpoints[0] == self.pos {
|
|
||||||
self.state = State::Paused;
|
|
||||||
self.breakpoints.remove(0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
self.process_code(&self.code[self.pos].clone(), &mut op_count);
|
|
||||||
if op_count >= self.max_oppr {
|
|
||||||
self.state = State::Error(String::from("Too long without yielding!"));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if self.state != State::Running {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
self.pos += 1;
|
|
||||||
}
|
|
||||||
if self.pos >= self.code.len() {
|
|
||||||
self.state = State::Finished;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,11 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use executor::Executor;
|
|
||||||
use parser::ASTPart;
|
use parser::ASTPart;
|
||||||
use virtualmachine::Machine;
|
use virtualmachine::Machine;
|
||||||
|
|
||||||
|
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod executor;
|
|
||||||
mod enviroment;
|
mod enviroment;
|
||||||
mod compiler;
|
mod compiler;
|
||||||
mod virtualmachine;
|
mod virtualmachine;
|
||||||
|
@ -102,7 +100,6 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ASTPart::Continue(cnt) => println!("{}{}: Continue", prefix, cnt.pos),
|
ASTPart::Continue(cnt) => println!("{}{}: Continue", prefix, cnt.pos),
|
||||||
ASTPart::RustFunction(func) => println!("{}{}: Rust Function", prefix, func.pos),
|
|
||||||
ASTPart::Return(ret) => {
|
ASTPart::Return(ret) => {
|
||||||
println!("{}{}: Return", prefix, ret.pos);
|
println!("{}{}: Return", prefix, ret.pos);
|
||||||
println!("{} Value:", prefix);
|
println!("{} Value:", prefix);
|
||||||
|
@ -135,12 +132,6 @@ fn main() {
|
||||||
println!("Registers: {:?}", vm.registers);
|
println!("Registers: {:?}", vm.registers);
|
||||||
println!("Stack: {:?}", vm.stack);
|
println!("Stack: {:?}", vm.stack);
|
||||||
println!("Memory: {:?}", vm.memory);
|
println!("Memory: {:?}", vm.memory);
|
||||||
/*let mut executor = Executor::new(ast, 100);
|
|
||||||
executor.resume();
|
|
||||||
println!("\nFinished!");
|
|
||||||
println!("State: {:?}", executor.state);
|
|
||||||
println!("Position: {}", executor.pos);
|
|
||||||
println!("Memory: {:?}", executor.memory);*/
|
|
||||||
},
|
},
|
||||||
Result::Err(err) => {
|
Result::Err(err) => {
|
||||||
panic!("Error while reading file: {}", err)
|
panic!("Error while reading file: {}", err)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use core::panic;
|
use core::panic;
|
||||||
|
|
||||||
use crate::{executor::Executor, lexer::{Token, TokenType}};
|
use crate::lexer::{Token, TokenType};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum ASTPart {
|
pub enum ASTPart {
|
||||||
|
@ -22,7 +22,6 @@ pub enum ASTPart {
|
||||||
For(AstFor),
|
For(AstFor),
|
||||||
Continue(AstContinue),
|
Continue(AstContinue),
|
||||||
Return(AstReturn),
|
Return(AstReturn),
|
||||||
RustFunction(AstRustFunction),
|
|
||||||
NOOP
|
NOOP
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -124,11 +123,6 @@ pub struct AstReturn {
|
||||||
pub value: Box<ASTPart>,
|
pub value: Box<ASTPart>,
|
||||||
pub pos: usize
|
pub pos: usize
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct AstRustFunction {
|
|
||||||
pub func: fn(&mut Executor, Vec<ASTPart>) -> ASTPart,
|
|
||||||
pub pos: usize
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
||||||
for token in end {
|
for token in end {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue