move fuzzing code to lib

This commit is contained in:
nora 2021-12-31 17:31:49 +01:00
parent f222e0cb10
commit 6215924049
8 changed files with 102 additions and 23 deletions

View file

@ -64,3 +64,38 @@ fn process_ast(program: &str, ast: Program, mut runtime: RtAlloc) {
Err(err) => errors::display_error(program, err),
}
}
// have the code here and not in the fuzzer, it's easier to find when it breaks like this
#[doc(hidden)]
pub fn _fuzz_compile(program: &str) {
// SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() };
let ast_alloc = Bump::new();
let lexer = lex::Lexer::new(program, &mut runtime);
let ast = parse::parse(lexer, &ast_alloc);
if let Ok(ast) = ast {
let bytecode_alloc = Bump::new();
let _bytecode = compile::compile(&ast, &bytecode_alloc, &mut runtime);
}
}
#[doc(hidden)]
pub fn _fuzz_parse(program: &str) {
// SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() };
let ast_alloc = Bump::new();
let lexer = lex::Lexer::new(program, &mut runtime);
let _ast = parse::parse(lexer, &ast_alloc);
}
#[doc(hidden)]
pub fn _fuzz_lex(program: &str) {
// SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() };
let lexer = lex::Lexer::new(program, &mut runtime);
for _ in lexer {}
}

View file

@ -34,6 +34,9 @@ const fn _check_val_size() {
}
}
const TRUE: Value = Value::Bool(true);
const FALSE: Value = Value::Bool(false);
struct Vm<'bc> {
_blocks: &'bc [FnBlock<'bc>],
current: &'bc FnBlock<'bc>,
@ -100,12 +103,42 @@ impl<'bc> Vm<'bc> {
(Value::Bool(a), Value::Bool(b)) => Ok(Value::Bool(a || b)),
_ => Err("bad type"),
})?,
Instr::CmpGreater => todo!(),
Instr::CmpGreaterEq => todo!(),
Instr::CmpLess => todo!(),
Instr::CmpLessEq => todo!(),
Instr::CmpEq => todo!(),
Instr::CmpNotEq => todo!(),
Instr::CmpGreater => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a > b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() > b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpGreaterEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a >= b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() >= b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpLess => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a < b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() < b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpLessEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a <= b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a.as_str() <= b.as_str())),
_ => Err("bad type"),
})?,
Instr::CmpEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Null, Value::Null) => Ok(TRUE),
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a == b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a == b)),
(Value::Object(_a), Value::Object(_b)) => todo!(),
(Value::Array, Value::Array) => Ok(TRUE),
_ => Err("bad type"),
})?,
Instr::CmpNotEq => self.bin_op(|lhs, rhs| match (lhs, rhs) {
(Value::Null, Value::Null) => Ok(FALSE),
(Value::Num(a), Value::Num(b)) => Ok(Value::Bool(a != b)),
(Value::String(a), Value::String(b)) => Ok(Value::Bool(a != b)),
(Value::Object(_a), Value::Object(_b)) => todo!(),
(Value::Array, Value::Array) => Ok(FALSE),
_ => Err("bad type"),
})?,
Instr::Print => {
let val = self.stack.pop().unwrap();
println!("{}", val);