dark_vm/values/
value_kinds.rs

1//! The ValueKind enum maintains the various values in the language.
2//! All of the supported values are in this enum. This makes it easy to expand in the future.
3
4use std::fmt;
5
6#[derive(PartialEq)]
7pub enum ValueKind {
8    Void,
9    Any,
10    Int(i64),
11    Float(f64),
12    Boolean(bool),
13    String(String),
14    Identifier(String),
15    Label(String),
16    End,
17
18    Push,
19    Pop,
20    Peek,
21    Add,
22    Sub,
23    Mul,
24    Div,
25    LessThan,
26    LessThanEqual,
27    GreaterThan,
28    GreaterThanEqual,
29    Equal,
30    NotEqual,
31    Jump,
32    RelativeJump,
33    JumpIfTrue,
34    JumpIfFalse,
35    Print,
36    PrintNewLine,
37    Set,
38    Call,
39}
40
41impl ValueKind {
42    /// This function gets the name of the value.
43    /// For example, an int with the value of 15, will have the value name 'Int'.
44    /// This method is used to provide the right error messages.
45    pub fn get_value_name(&self) -> String {
46        match self {
47            ValueKind::Void => "Void",
48            ValueKind::Any => "Any",
49            ValueKind::Int(_) => "Int",
50            ValueKind::Float(_) => "Float",
51            ValueKind::Boolean(_) => "Boolean",
52            ValueKind::String(_) => "String",
53            ValueKind::Identifier(_) => "Identifier",
54            ValueKind::Label(_) => "Label",
55            ValueKind::End => "End",
56            ValueKind::Push => "Instruction Push",
57            ValueKind::Pop => "Instruction Pop",
58            ValueKind::Peek => "Instruction Peek",
59            ValueKind::Add => "Instruction Add",
60            ValueKind::Sub => "Instruction Sub",
61            ValueKind::Mul => "Instruction Mul",
62            ValueKind::Div => "Instruction Div",
63            ValueKind::LessThan => "Instruction LessThan",
64            ValueKind::LessThanEqual => "Instruction LessThanEqual",
65            ValueKind::GreaterThan => "Instruction GreaterThan",
66            ValueKind::GreaterThanEqual => "Instruction GreaterThanEqual",
67            ValueKind::Equal => "Instruction Equal",
68            ValueKind::NotEqual => "Instruction NotEqual",
69            ValueKind::Jump => "Instruction Jump",
70            ValueKind::RelativeJump => "Instruction JumpRelative",
71            ValueKind::JumpIfTrue => "Instruction JumpIfTrue",
72            ValueKind::JumpIfFalse => "Instruction JumpIfFalse",
73            ValueKind::Print => "Instruction Print",
74            ValueKind::PrintNewLine => "Instruction PrintNewLine",
75            ValueKind::Set => "Instruction Set",
76            ValueKind::Call => "Instruction Call",
77        }
78        .to_owned()
79    }
80}
81
82impl fmt::Debug for ValueKind {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        match self {
85            ValueKind::Void => write!(f, "Void"),
86            ValueKind::Any => write!(f, "Any"),
87            ValueKind::Int(value) => write!(f, "{}", value),
88            ValueKind::Float(value) => write!(f, "{}", value),
89            ValueKind::Boolean(value) => write!(f, "{}", value),
90            ValueKind::String(value) => write!(f, "{}", value),
91            ValueKind::Identifier(name) => write!(f, "Identifier '{}'", name),
92            ValueKind::Label(name) => write!(f, "Label '{}'", name),
93            ValueKind::End => write!(f, "End"),
94            ValueKind::Push => write!(f, "<instruction push>"),
95            ValueKind::Pop => write!(f, "<instruction pop>"),
96            ValueKind::Peek => write!(f, "<instruction peek>"),
97            ValueKind::Add => write!(f, "<instruction add>"),
98            ValueKind::Sub => write!(f, "<instruction sub>"),
99            ValueKind::Mul => write!(f, "<instruction mul>"),
100            ValueKind::Div => write!(f, "<instruction div>"),
101            ValueKind::LessThan => write!(f, "<instruction lt>"),
102            ValueKind::LessThanEqual => write!(f, "<instruction lte>"),
103            ValueKind::GreaterThan => write!(f, "<instruction gt>"),
104            ValueKind::GreaterThanEqual => write!(f, "<instruction gte>"),
105            ValueKind::Equal => write!(f, "<instruction eq>"),
106            ValueKind::NotEqual => write!(f, "<instruction neq>"),
107            ValueKind::Jump => write!(f, "<instruction jmp>"),
108            ValueKind::RelativeJump => write!(f, "<instruction rjmp>"),
109            ValueKind::JumpIfTrue => write!(f, "<instruction jmpt>"),
110            ValueKind::JumpIfFalse => write!(f, "<instruction jmpf>"),
111            ValueKind::Print => write!(f, "<instruction print>"),
112            ValueKind::PrintNewLine => write!(f, "<instruction printn>"),
113            ValueKind::Set => write!(f, "<instruction set>"),
114            ValueKind::Call => write!(f, "<instruction call>"),
115        }
116    }
117}