Skip to content

Commit 58f63c3

Browse files
committed
interpreter: concatenation; proper type display
1 parent fa77cb8 commit 58f63c3

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎interpreter/src/ir.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ impl OpCode {
224224
| Self::Divide
225225
| Self::Raise
226226
| Self::Modulo
227+
| Self::Concat
227228
)
228229
}
229230

‎interpreter/src/types.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
borrow::Cow,
3+
fmt::Display,
34
hash::Hash,
5+
io::Write,
46
mem::discriminant,
57
ops::{Add, Div, Mul, Sub},
68
};
@@ -62,6 +64,26 @@ impl Value<'_> {
6264
_ => 0.,
6365
}
6466
}
67+
68+
pub fn write_string(&self, f: &mut Vec<u8>) {
69+
match self {
70+
Self::String(s) | Self::Regex(s) => f.extend_from_slice(s),
71+
Self::Float(n) => write!(f, "{n}").unwrap(),
72+
&Self::Bool(false) => f.push(b'0'),
73+
&Self::Bool(true) => f.push(b'1'),
74+
Self::Array(_) => panic!("Attempted to use array in scalar context!"),
75+
_ => {}
76+
}
77+
}
78+
79+
pub fn string_size_hint(&self) -> usize {
80+
match self {
81+
Self::String(s) | Self::Regex(s) => s.len(),
82+
Self::Float(_) => 2,
83+
Self::Bool(_) => 1,
84+
_ => 0,
85+
}
86+
}
6587
}
6688

6789
impl<'a> Add for &'_ Value<'a> {
@@ -109,3 +131,17 @@ impl Hash for Value<'_> {
109131
}
110132
}
111133
}
134+
135+
impl Display for Value<'_> {
136+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137+
match self {
138+
Value::Float(n) => <_ as Display>::fmt(n, f),
139+
Value::String(s) => write!(f, "{:?}", String::from_utf8_lossy(s)),
140+
Value::Regex(s) => write!(f, "/{}/", String::from_utf8_lossy(s)),
141+
&Value::Bool(b) => write!(f, "{}", b as usize),
142+
Value::Array(_) => write!(f, "array"),
143+
Value::Untyped => write!(f, "untyped"),
144+
Value::Unassigned => write!(f, "unassigned"),
145+
}
146+
}
147+
}

‎interpreter/src/vm.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
fmt::{self, Display},
33
mem::replace,
4+
vec::Vec as StdVec,
45
};
56

67
use ahash::RandomState;
@@ -117,6 +118,14 @@ impl Interpreter<'_> {
117118
OpCode::Subtract => lhs - rhs,
118119
OpCode::Multiply => lhs * rhs,
119120
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+
}
120129
_ => todo!(),
121130
}
122131
};
@@ -203,23 +212,23 @@ impl Display for Registers<'_> {
203212
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204213
write!(f, "Registers:")?;
205214
let n = self.0.len().checked_ilog10().unwrap_or(0) as usize + 1;
206-
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "r{i:0n$} = {e:?}"))
215+
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "r{i:0n$} = {e}"))
207216
}
208217
}
209218

210219
impl Display for SymbolTable<'_> {
211220
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212221
write!(f, "Symbols:")?;
213222
fmt_list(f, self.user.iter(), |f, i, (k, v)| {
214-
write!(f, "user[{i}] @ {k} = {v:?}")
223+
write!(f, "user[{i}] @ {k} = {v}")
215224
})
216225
}
217226
}
218227

219228
impl Display for Consts<'_> {
220229
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221230
write!(f, "Consts:")?;
222-
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "mem[{i}] = {e:?}"))
231+
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "mem[{i}] = {e}"))
223232
}
224233
}
225234

0 commit comments

Comments
 (0)