Fix concat

This commit is contained in:
Sofia 2026-03-20 19:28:49 +02:00
parent fb12568e33
commit 8fe3ffc8e0

View File

@ -1,4 +1,9 @@
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
use std::{
cell::RefCell,
collections::HashMap,
fmt::{Debug, Display},
rc::Rc,
};
use crate::{
ast::{BinaryOperator, UnaryOperator},
@ -31,7 +36,7 @@ impl LuaFloat {
impl Debug for LuaFloat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Debug::fmt(&self.0, f)
}
}
@ -40,7 +45,7 @@ pub struct LuaInteger(pub i64);
impl Debug for LuaInteger {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Debug::fmt(&self.0, f)
}
}
@ -61,7 +66,7 @@ pub struct LuaBool(pub bool);
impl Debug for LuaBool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Debug::fmt(&self.0, f)
}
}
@ -184,13 +189,30 @@ impl From<&str> for Value {
}
}
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::String(value) => Display::fmt(value, f),
Value::Float(vmfloat) => Display::fmt(&vmfloat.lua_number().0, f),
Value::Integer(lua_integer) => Display::fmt(&lua_integer.0, f),
Value::Boolean(lua_bool) => Display::fmt(&lua_bool.0, f),
Value::RustFunction(ref_cell) => {
write!(f, "<RustFunction({})>", ref_cell.borrow().as_indexable())
}
Value::Function(closure) => write!(f, "<function({})>", closure.prototype),
Value::Nil => write!(f, "nil"),
Value::Table(_) => write!(f, "<table>"),
}
}
}
impl Value {
pub fn concat(&self, other: &Value) -> Result<Value, RuntimeError> {
match (self, other) {
(
Value::String(_) | Value::Integer(_) | Value::Boolean(_),
Value::String(_) | Value::Integer(_) | Value::Boolean(_),
) => Ok(Value::String(format!("{:?}{:?}", self, other))),
) => Ok(Value::String(format!("{}{}", self, other))),
(
Value::Float(_) | Value::Integer(_) | Value::Boolean(_),
Value::Float(_) | Value::Integer(_) | Value::Boolean(_),