feat(tvix/eval): implement string concatenation

Change-Id: If61066e59232b2bad42b5cb5f0f2d9b9c416be8b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6137
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
Vincent Ambo 2022-08-10 20:31:35 +03:00 committed by tazjin
parent 322ce36cea
commit fb3d024d75
6 changed files with 23 additions and 1 deletions

View file

@ -0,0 +1 @@
"hello world"

View file

@ -0,0 +1 @@
"hello " + "world"

View file

@ -0,0 +1 @@
"hello world"

View file

@ -0,0 +1 @@
"hello ${"world"}"

View file

@ -63,6 +63,12 @@ impl NixString {
Cow::Owned(s) => Cow::Owned(format!("\"{}\"", s)),
}
}
pub fn concat(&self, other: &Self) -> Self {
let mut s = self.as_str().to_owned();
s.push_str(other.as_str());
NixString::Heap(s)
}
}
fn nix_escape_char(ch: char) -> Option<&'static str> {

View file

@ -67,7 +67,19 @@ impl VM {
self.push(c);
}
OpCode::OpAdd => arithmetic_op!(self, +),
OpCode::OpAdd => {
let b = self.pop();
let a = self.pop();
let result = if let (Value::String(s1), Value::String(s2)) = (&a, &b) {
Value::String(s1.concat(s2))
} else {
arithmetic_op!(b, a, +)
};
self.push(result)
}
OpCode::OpSub => arithmetic_op!(self, -),
OpCode::OpMul => arithmetic_op!(self, *),
OpCode::OpDiv => arithmetic_op!(self, /),