Add vs_function

This commit is contained in:
Andrew Morris
2022-05-02 12:39:05 +10:00
parent 25b7c67698
commit 1912c8282f
4 changed files with 63 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ use super::vs_value::Val;
use super::vs_number::VsNumber;
use super::vs_string::VsString;
use super::vs_pointer::VsPointer;
use super::vs_function::VsFunction;
pub struct BytecodeDecoder {
// TODO: Enable borrow usage to avoid the rc overhead
@@ -77,23 +78,9 @@ impl BytecodeDecoder {
),
Array => std::panic!("Not implemented"),
Object => std::panic!("Not implemented"),
Function => std::panic!("Not implemented"),
Function => self.decode_function_header(),
Instance => std::panic!("Not implemented"),
Pointer => {
let from_pos = self.pos;
let pos = self.decode_pos();
if pos < from_pos {
if self.clone_at(pos).decode_type() != BytecodeType::Function {
std::panic!("Invalid: non-function pointer that points backwards");
}
}
return VsPointer::new(
&self.data,
pos,
);
},
Pointer => self.decode_pointer(),
Register => std::panic!("Not implemented"),
}
}
@@ -144,4 +131,33 @@ impl BytecodeDecoder {
pub fn clone_at(&self, pos: usize) -> BytecodeDecoder {
return BytecodeDecoder { data: self.data.clone(), pos: pos };
}
pub fn decode_pointer(&mut self) -> Val {
let from_pos = self.pos;
let pos = self.decode_pos();
if pos < from_pos {
if self.clone_at(pos).decode_type() != BytecodeType::Function {
std::panic!("Invalid: non-function pointer that points backwards");
}
}
return VsPointer::new(
&self.data,
pos,
);
}
pub fn decode_function_header(&mut self) -> Val {
// TODO: Support >256
let register_count = self.decode_byte() as usize;
let parameter_count = self.decode_byte() as usize;
return Rc::new(VsFunction {
bytecode: self.data.clone(),
register_count: register_count,
parameter_count: parameter_count,
start: self.pos,
});
}
}

View File

@@ -1,6 +1,7 @@
mod vs_value;
mod vs_number;
mod vs_string;
mod vs_function;
mod vs_pointer;
mod operations;
mod bytecode_decoder;

View File

@@ -0,0 +1,29 @@
use std::rc::Rc;
use super::vs_value::VsType;
use super::vs_value::VsValue;
pub struct VsFunction {
pub bytecode: Rc<Vec<u8>>,
pub register_count: usize,
pub parameter_count: usize,
pub start: usize,
}
impl VsValue for VsFunction {
fn typeof_(&self) -> VsType {
return VsType::Function;
}
fn to_string(&self) -> String {
return "[function]".to_string();
}
fn to_number(&self) -> f64 {
return f64::NAN;
}
fn is_primitive(&self) -> bool {
return false;
}
}

View File

@@ -84,7 +84,7 @@ impl VsValue for VsPointer {
Number => true,
String => true,
Array => false,
Object => true,
Object => false,
Function => false,
}
}