Files
not-python/src/vm/inst.rs

94 lines
2.8 KiB
Rust
Raw Normal View History

use crate::{obj::prelude::*, vm::consts::*};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Inst {
/// Push a literal symbol object to the stack.
PushSym(Sym),
/// Push a const value reference to the stack.
PushConst(ConstHandle),
/// Looks up and pushes a local value.
LoadLocal(Name),
/// Looks up and pushes a global value.
LoadGlobal(Name),
/// Pop a value from the stack, possibly into a local symbol.
PopLocal(Option<Name>),
/// Pop a value from the stack, possibly into a global symbol.
PopGlobal(Option<Name>),
/// Pops a symbol value and an object reference.
///
/// This will get an attr from the object reference pointed to by the symbol.
GetAttr(Sym),
/// Pops a target reference and a source reference from the stack.
///
/// The target reference will have the given symbol attribute assigned to the source.
///
/// In code, it would look like this:
///
/// `target.symbol = source`
///
SetAttr(Sym),
/// Pops the top value off of the stack and checks if it is a `Bool` of a true value.
CheckTruth,
/// Jump to a given address in the current function unconditionally.
Jump(usize),
/// Jump to a given address in the current function if the condition flag is true.
///
/// The condition flag may be set by an internal function.
JumpTrue(usize),
/// Calls a function with the supplied number of arguments.
///
/// The stack, from bottom to top, should contain the function followed by the arguments.
///
/// After the function has returned, the VM will have popped the arguments and function
/// pointer, and the return value will be on top of the stack.
Call(usize),
/// Indexes a value, e.g. a list or a dict.
///
/// The stack, from bottom to top, should have the expression being indexed, and then the
/// indexed value.
Index,
/// Pops the top value from the stack, and returns from the function, using the popped value as
/// a return value.
Return,
}
//
// impl Inst
//
impl Inst {
/// Gets the printable name of this instruction.
pub const fn name(&self) -> &'static str {
match self {
Inst::PushSym(_) => "PUSH_SYM",
Inst::PushConst(_) => "PUSH_CONST",
Inst::LoadLocal(_) => "LOAD_LOCAL",
Inst::LoadGlobal(_) => "LOAD_GLOBAL",
Inst::PopLocal(_) => "POP_LOCAL",
Inst::PopGlobal(_) => "POP_GLOBAL",
Inst::GetAttr(_) => "GET_ATTR",
Inst::SetAttr(_) => "SET_ATTR",
Inst::CheckTruth => "CHECK_TRUTH",
Inst::Jump(_) => "JUMP",
Inst::JumpTrue(_) => "JUMP_TRUE",
Inst::Call(_) => "CALL",
Inst::Index => "INDEX",
Inst::Return => "RETURN",
}
}
}
unsafe impl shredder::marker::GcDrop for Inst {}