use crate::maki::{Kind, Value};
#[derive(Debug, Clone)]
pub struct Program {
pub id: String,
pub version: i32,
pub classes: Vec<String>,
pub methods: Vec<MethodRef>,
pub variables: Vec<VarDecl>,
pub bindings: Vec<Binding>,
pub code: Vec<Op>,
}
impl Program {
pub fn class_name(&self, index: usize) -> String {
self.classes
.get(index)
.map(|g| {
crate::maki::stdapi::class(g)
.map(|c| c.name.to_string())
.unwrap_or_else(|| g.clone())
})
.unwrap_or_default()
}
}
#[derive(Debug, Clone)]
pub struct MethodRef {
pub class_index: usize,
pub name: String,
}
#[derive(Debug, Clone)]
pub struct VarDecl {
pub kind: Kind,
pub guid: Option<String>,
pub class_var: Option<usize>,
pub global: bool,
pub initial: Value,
pub system: bool,
pub constant: Option<Value>,
}
#[derive(Debug, Clone)]
pub struct Binding {
pub variable: usize,
pub method: usize,
pub code_index: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct Op {
pub opcode: u8,
pub arg: i32,
pub argc: i32,
pub offset: i32,
}
pub mod op {
pub const PUSH: u8 = 0x01;
pub const POP: u8 = 0x02;
pub const POP_TO: u8 = 0x03;
pub const EQ: u8 = 0x08;
pub const NE: u8 = 0x09;
pub const GT: u8 = 0x0a;
pub const GE: u8 = 0x0b;
pub const LT: u8 = 0x0c;
pub const LE: u8 = 0x0d;
pub const JUMP_IF_NOT: u8 = 0x10;
pub const JUMP_IF: u8 = 0x11;
pub const JUMP: u8 = 0x12;
pub const CALL: u8 = 0x18;
pub const CALL_GLOBAL: u8 = 0x19;
pub const RETURN: u8 = 0x21;
pub const COMPLETE: u8 = 0x28;
pub const MOV: u8 = 0x30;
pub const POST_INC: u8 = 0x38;
pub const POST_DEC: u8 = 0x39;
pub const PRE_INC: u8 = 0x3a;
pub const PRE_DEC: u8 = 0x3b;
pub const ADD: u8 = 0x40;
pub const SUB: u8 = 0x41;
pub const MUL: u8 = 0x42;
pub const DIV: u8 = 0x43;
pub const MOD: u8 = 0x44;
pub const AND: u8 = 0x48;
pub const OR: u8 = 0x49;
pub const NOT: u8 = 0x4a;
pub const BIT_NOT: u8 = 0x4b;
pub const NEG: u8 = 0x4c;
pub const XOR: u8 = 0x4d;
pub const LOG_AND: u8 = 0x50;
pub const LOG_OR: u8 = 0x51;
pub const SHL: u8 = 0x58;
pub const SHR: u8 = 0x59;
pub const SHL2: u8 = 0x5a;
pub const SHR2: u8 = 0x5b;
pub const NEW: u8 = 0x60;
pub const DELETE: u8 = 0x61;
pub const MEMBER: u8 = 0x68;
pub const CALL2: u8 = 0x70;
pub fn is_jump(o: u8) -> bool {
matches!(o, JUMP_IF_NOT | JUMP_IF | JUMP | CALL_GLOBAL)
}
pub fn is_index(o: u8) -> bool {
matches!(o, PUSH | POP_TO | CALL | CALL2 | NEW | MEMBER)
}
}