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() .as_ref()
.map(|value| value.to_string()) .map(|value| value.to_string())
.unwrap_or("empty".to_string()); .unwrap_or("empty".to_string());
let trucated_length = 8; let trucated_length = value_display.len().min(self.width - 2);
let with_elipsis = trucated_length - 3; let with_elipsis = trucated_length.saturating_sub(3);
let constant_display = if value_display.len() > with_elipsis { let constant_display = if with_elipsis > self.width - 2 {
format!("{index:<5} {value_display:.<trucated_length$.with_elipsis$}") format!("{index:<5} {value_display:.<trucated_length$.with_elipsis$}")
} else { } else {
format!("{index:<5} {value_display:<trucated_length$}") format!("{index:<5} {value_display:<trucated_length$}")

View File

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

View File

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