65 lines
1.8 KiB
Rust
65 lines
1.8 KiB
Rust
use std::fmt;
|
|
use std::fmt::Display;
|
|
|
|
pub type FuncID = u16;
|
|
pub type HeapID = u16;
|
|
pub type RegID = u8;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Command {
|
|
InitializeVariable(HeapID, VariableType), // Initializes new variable to HeapID at VariableType
|
|
BeginScope, // Begins new Scope
|
|
EndScope, // Ends Scope
|
|
Pop(RegID), // Pop into registery at RegID
|
|
Push(RegID), // Push out of registery at RegID
|
|
AssignVariable(HeapID, RegID), // Assign variable from registery at RegID
|
|
VarToReg(HeapID, RegID), // Bring Variable to registery at RegID
|
|
StringLit(String), // Bring String Literal to Stack
|
|
I32Lit(i32), // Bring i32 Literal to Stack
|
|
FunctionCall(FuncID), // Call Function at FuncID
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct CompiledReid {
|
|
pub list: Vec<Command>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum Value {
|
|
StringVal(String),
|
|
I32Val(i32),
|
|
}
|
|
|
|
impl Value {
|
|
pub fn get_type(&self) -> VariableType {
|
|
match self {
|
|
Value::StringVal(_) => VariableType::TypeString,
|
|
Value::I32Val(_) => VariableType::TypeI32,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
pub enum VariableType {
|
|
TypeString,
|
|
TypeI32,
|
|
}
|
|
|
|
impl ToString for VariableType {
|
|
fn to_string(&self) -> String {
|
|
match self {
|
|
VariableType::TypeString => "String".to_string(),
|
|
VariableType::TypeI32 => "i32".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct Position(pub usize, pub usize);
|
|
|
|
impl Display for Position {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "line {}, column {}", self.0, self.1)
|
|
}
|
|
}
|