1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
//! This module implements the virtual (or abstract) machine that runs
//! Tvix bytecode.
use crate::{
chunk::Chunk,
errors::{Error, EvalResult},
opcode::OpCode,
value::Value,
};
pub struct VM {
ip: usize,
chunk: Chunk,
stack: Vec<Value>,
}
impl VM {
fn push(&mut self, value: Value) {
self.stack.push(value)
}
fn pop(&mut self) -> Value {
self.stack.pop().expect("TODO")
}
fn pop_number_pair(&mut self) -> EvalResult<NumberPair> {
let v2 = self.pop();
let v1 = self.pop();
match (v1, v2) {
(Value::Integer(i1), Value::Integer(i2)) => Ok(NumberPair::Integer(i1, i2)),
(Value::Float(f1), Value::Float(f2)) => Ok(NumberPair::Floats(f1, f2)),
(Value::Integer(i1), Value::Float(f2)) => Ok(NumberPair::Floats(i1 as f64, f2)),
(Value::Float(f1), Value::Integer(i2)) => Ok(NumberPair::Floats(f1, i2 as f64)),
_ => Err(Error::TypeError {
expected: "number (either int or float)",
actual: if v1.is_number() {
v2.type_of()
} else {
v1.type_of()
},
}),
}
}
fn inc_ip(&mut self) -> OpCode {
let op = self.chunk.code[self.ip];
self.ip += 1;
op
}
fn run(&mut self) -> EvalResult<Value> {
loop {
match self.inc_ip() {
OpCode::OpConstant(idx) => {
let c = self.chunk.constant(idx).clone();
self.push(c);
}
OpCode::OpAdd => match self.pop_number_pair()? {
NumberPair::Floats(f1, f2) => self.push(Value::Float(f1 + f2)),
NumberPair::Integer(i1, i2) => self.push(Value::Integer(i1 + i2)),
},
OpCode::OpSub => match self.pop_number_pair()? {
NumberPair::Floats(f1, f2) => self.push(Value::Float(f1 - f2)),
NumberPair::Integer(i1, i2) => self.push(Value::Integer(i1 - i2)),
},
OpCode::OpMul => match self.pop_number_pair()? {
NumberPair::Floats(f1, f2) => self.push(Value::Float(f1 * f2)),
NumberPair::Integer(i1, i2) => self.push(Value::Integer(i1 * i2)),
},
OpCode::OpDiv => match self.pop_number_pair()? {
NumberPair::Floats(f1, f2) => self.push(Value::Float(f1 / f2)),
NumberPair::Integer(i1, i2) => self.push(Value::Integer(i1 / i2)),
},
OpCode::OpInvert => {
let v = self.pop().as_bool()?;
self.push(Value::Bool(!v));
}
OpCode::OpNegate => match self.pop() {
Value::Integer(i) => self.push(Value::Integer(-i)),
Value::Float(f) => self.push(Value::Float(-f)),
v => {
return Err(Error::TypeError {
expected: "number (either int or float)",
actual: v.type_of(),
})
}
},
OpCode::OpEqual => {
let v2 = self.pop();
let v1 = self.pop();
let eq = match (v1, v2) {
(Value::Float(f), Value::Integer(i))
| (Value::Integer(i), Value::Float(f)) => f == (i as f64),
_ => v2 == v2,
};
self.push(Value::Bool(eq))
}
OpCode::OpNull => self.push(Value::Null),
OpCode::OpTrue => self.push(Value::Bool(true)),
OpCode::OpFalse => self.push(Value::Bool(false)),
}
if self.ip == self.chunk.code.len() {
return Ok(self.pop());
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NumberPair {
Floats(f64, f64),
Integer(i64, i64),
}
pub fn run_chunk(chunk: Chunk) -> EvalResult<Value> {
let mut vm = VM {
chunk,
ip: 0,
stack: vec![],
};
vm.run()
}
|