Skip to content

Commit 3b9ce0e

Browse files
committed
interpreter: initial unary ops; tiny binops refactor
1 parent 58f63c3 commit 3b9ce0e

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

interpreter/src/ir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl OpCode {
201201
fn is_unary(self) -> bool {
202202
matches!(
203203
self,
204-
Self::Record | Self::Negation | Self::ToInt | Self::Negative | Self::Concat
204+
Self::Record | Self::Negation | Self::ToInt | Self::Negative
205205
)
206206
}
207207

interpreter/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl Value<'_> {
5353
}
5454
}
5555

56-
fn to_num(&self) -> f64 {
56+
pub fn to_num(&self) -> f64 {
5757
match self {
5858
&Self::Float(f) => f,
5959
&Self::Bool(b) => b as usize as f64,
@@ -79,7 +79,7 @@ impl Value<'_> {
7979
pub fn string_size_hint(&self) -> usize {
8080
match self {
8181
Self::String(s) | Self::Regex(s) => s.len(),
82-
Self::Float(_) => 2,
82+
Self::Float(_) => 8,
8383
Self::Bool(_) => 1,
8484
_ => 0,
8585
}

interpreter/src/vm.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,34 @@ impl Interpreter<'_> {
108108
pub fn run(&mut self) {
109109
while let Some(instr) = self.bc.code.get(self.program_counter) {
110110
match instr {
111-
// ix if let Some(&(dest, src)) = ix.get_unary() => {}
111+
ix if let Some(&(dest, src)) = ix.get_unary() => {
112+
let src = self.registers.get(src);
113+
let val = match ix.opcode {
114+
OpCode::Record => todo!(),
115+
OpCode::Negation => Value::Float(!src.to_bool() as usize as f64),
116+
OpCode::ToInt => Value::Float(src.to_num()),
117+
OpCode::Negative => Value::Float(-src.to_num()),
118+
_ => unreachable!(),
119+
};
120+
self.registers.write(dest, val);
121+
}
112122
ix if let Some(&(dest, lhs, rhs)) = ix.get_binary() => {
113-
let val = {
114-
let lhs = self.registers.get(lhs);
115-
let rhs = self.registers.get(rhs);
116-
match ix.opcode {
117-
OpCode::Add => lhs + rhs,
118-
OpCode::Subtract => lhs - rhs,
119-
OpCode::Multiply => lhs * rhs,
120-
OpCode::Divide => lhs / rhs,
121-
OpCode::Concat => {
122-
let mut buf = StdVec::with_capacity(
123-
lhs.string_size_hint() + rhs.string_size_hint(),
124-
);
125-
lhs.write_string(&mut buf);
126-
rhs.write_string(&mut buf);
127-
Value::String(buf.into())
128-
}
129-
_ => todo!(),
123+
let lhs = self.registers.get(lhs);
124+
let rhs = self.registers.get(rhs);
125+
let val = match ix.opcode {
126+
OpCode::Add => lhs + rhs,
127+
OpCode::Subtract => lhs - rhs,
128+
OpCode::Multiply => lhs * rhs,
129+
OpCode::Divide => lhs / rhs,
130+
OpCode::Concat => {
131+
let mut buf = StdVec::with_capacity(
132+
lhs.string_size_hint() + rhs.string_size_hint(),
133+
);
134+
lhs.write_string(&mut buf);
135+
rhs.write_string(&mut buf);
136+
Value::String(buf.into())
130137
}
138+
_ => todo!(),
131139
};
132140
self.registers.write(dest, val);
133141
}

0 commit comments

Comments
 (0)