Add base branch logic

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2023-04-07 00:00:04 -07:00
parent 4d005494a3
commit 7bac4306c3
17 changed files with 20302 additions and 5491 deletions

View File

@@ -1,6 +1,13 @@
use crate::obj::ObjPtr;
use crate::vm::name::Name;
#[derive(Debug, Clone)]
pub enum JumpCondition {
False,
True,
Always,
}
#[derive(Debug, Clone)]
pub enum Inst {
/// Pushes an object to the stack.
@@ -29,4 +36,37 @@ pub enum Inst {
/// resize the stack to its last size (pre-call), and push the return value
/// back to the top of the stack.
Return,
/// Adds the given value to the current program counter (positive or
/// negative) when the given condition holds..
///
/// `Inst::JumpRelative(1)` functions as a no-op.
/// `Inst::JumpRelative(0)` functions as a "hang"
/// `Inst::JumpRelative(-1)` jumps to the previous instruction.
JumpRelative(isize, JumpCondition),
/// Pops the top value off of the stack and sets the condition flag to
/// "true" or "false".
Compare,
Comment(String),
}
impl Inst {
pub fn disassemble_string(&self) -> String {
match self {
Inst::Push(ptr) => format!("push <{:#x}>", (ptr as *const _ as usize)),
Inst::Load(name) => format!("load {name:?}"),
Inst::Store(name) => format!("store {name:?}"),
Inst::GetAttr(attr) => format!("getattr {attr}"),
Inst::Pop => format!("pop"),
Inst::Call(argc) => format!("call {argc}"),
Inst::Return => format!("return"),
Inst::JumpRelative(distance, condition) => {
format!("jump {:+} (when condition is {:?})", distance, condition)
}
Inst::Compare => format!("compare"),
Inst::Comment(comment) => format!("# {comment}"),
}
}
}