added ifs + new operations
This commit is contained in:
parent
8679172c42
commit
867cea5046
1 changed files with 108 additions and 0 deletions
108
src/executor.rs
108
src/executor.rs
|
@ -289,6 +289,84 @@ impl Executor {
|
|||
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;
|
||||
|
@ -306,6 +384,36 @@ impl Executor {
|
|||
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;
|
||||
}
|
||||
_ => {
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue