1
0

Fix function bug

This commit is contained in:
Jeff 2024-10-12 20:34:13 -04:00
parent 2864bee057
commit 743679371d
3 changed files with 8 additions and 23 deletions

View File

@ -543,9 +543,9 @@ impl<'a> ChunkDisassembler<'a> {
.as_ref()
.map(|value| value.to_string())
.unwrap_or("empty".to_string());
let trucated_length = 8;
let with_elipsis = trucated_length - 3;
let constant_display = if value_display.len() > with_elipsis {
let trucated_length = value_display.len().min(self.width - 2);
let with_elipsis = trucated_length.saturating_sub(3);
let constant_display = if with_elipsis > self.width - 2 {
format!("{index:<5} {value_display:.<trucated_length$.with_elipsis$}")
} else {
format!("{index:<5} {value_display:<trucated_length$}")

View File

@ -61,10 +61,10 @@ impl Value {
}
pub fn function(body: Chunk, r#type: FunctionType) -> Self {
Value::Primitive(Primitive::Function(Function {
Value::Function(Function {
chunk: body,
r#type: Type::Function(r#type),
}))
})
}
pub fn integer<T: Into<i64>>(into_i64: T) -> Self {
@ -372,7 +372,6 @@ pub enum Primitive {
Byte(u8),
Character(char),
Float(f64),
Function(Function),
Integer(i64),
Range(RangeValue),
String(String),
@ -384,7 +383,6 @@ impl Primitive {
Primitive::Boolean(_) => Type::Boolean,
Primitive::Byte(_) => Type::Byte,
Primitive::Character(_) => Type::Character,
Primitive::Function(Function { r#type, .. }) => r#type.clone(),
Primitive::Float(_) => Type::Float,
Primitive::Integer(_) => Type::Integer,
Primitive::Range(range) => range.r#type(),
@ -394,14 +392,6 @@ impl Primitive {
}
}
pub fn as_function(&self) -> Option<&Function> {
if let Primitive::Function(function) = self {
Some(function)
} else {
None
}
}
pub fn is_rangeable(&self) -> bool {
matches!(
self,
@ -601,9 +591,6 @@ impl Display for Primitive {
Ok(())
}
Primitive::Function(Function { .. }) => {
write!(f, "function")
}
Primitive::Integer(integer) => write!(f, "{integer}"),
Primitive::Range(range_value) => {
write!(f, "{range_value}")
@ -642,8 +629,6 @@ impl Ord for Primitive {
}
}
(Primitive::Float(_), _) => Ordering::Greater,
(Primitive::Function(left), Primitive::Function(right)) => left.cmp(right),
(Primitive::Function(_), _) => Ordering::Greater,
(Primitive::Integer(left), Primitive::Integer(right)) => left.cmp(right),
(Primitive::Integer(_), _) => Ordering::Greater,
(Primitive::Range(left), Primitive::Range(right)) => left.cmp(right),

View File

@ -1,8 +1,8 @@
use std::mem::replace;
use crate::{
parse, value::Primitive, AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction,
Operation, Span, Value, ValueError,
parse, value::Primitive, AnnotatedError, Chunk, ChunkError, DustError, Function, Identifier,
Instruction, Operation, Span, Value, ValueError,
};
pub fn run(source: &str) -> Result<Option<Value>, DustError> {
@ -371,7 +371,7 @@ impl Vm {
Operation::Call => {
let function_index = instruction.a();
let argument_count = instruction.b();
let function = if let Value::Primitive(Primitive::Function(function)) =
let function = if let Value::Function(function) =
self.get(function_index, position)?.clone()
{
function