Implement small tests. Failing on parse.
This commit is contained in:
parent
c818e462b8
commit
9aaabb4c07
7 changed files with 49 additions and 143 deletions
38
Cargo.lock
generated
38
Cargo.lock
generated
|
|
@ -21,9 +21,6 @@ name = "macros"
|
|||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"map",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -37,38 +34,3 @@ dependencies = [
|
|||
"deimos",
|
||||
"glam",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"
|
||||
|
|
|
|||
2
deimos
2
deimos
|
|
@ -1 +1 @@
|
|||
Subproject commit f3922ff84da34746f845f5b817573944f8991fa4
|
||||
Subproject commit 385a08b094e43ff12b60a41a90ab99b4b18cea0c
|
||||
48
src/lib.rs
48
src/lib.rs
|
|
@ -13,22 +13,40 @@ mod tasks;
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::vm::Machine;
|
||||
use deimos::gc::Allocates;
|
||||
use crate::{RunError, Needle};
|
||||
|
||||
#[test]
|
||||
fn simple() {
|
||||
let mut machine = match Machine::new(|_| ()) {
|
||||
Err(error) => panic!("{}", error),
|
||||
Ok(machine) => machine,
|
||||
};
|
||||
let closure = match machine.load(include_str!("tests/script.lua")) {
|
||||
Err(error) => panic!("{}", error),
|
||||
Ok(closure) => closure,
|
||||
};
|
||||
println!("{:?}", machine.freelist().get(&closure).prototype);
|
||||
match machine.enter(closure) {
|
||||
Err(error) => panic!("{}", error),
|
||||
_ => {}
|
||||
macro_rules! unwrap {
|
||||
($e:expr) => {
|
||||
match $e {
|
||||
Err(error) => panic!("{}", error),
|
||||
Ok(result) => result,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn output(machine: &mut Machine<String>, mut needle: Needle) -> Result<(),RunError> {
|
||||
let arg = needle.pop().ok_or(RunError::new("Expected string, got nil."))?;
|
||||
let str = arg.try_string().ok_or(RunError::new("Expected string."))?;
|
||||
machine.state.push_str(str.as_str());
|
||||
println!("{}",str.as_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn output_test(file: &str) -> Result<Machine<String>,Box<dyn std::error::Error>> {
|
||||
let mut machine = Machine::new(|_| String::new())?;
|
||||
machine.set_global_function("output",output)?;
|
||||
let closure = machine.load(file)?;
|
||||
machine.enter(closure)?;
|
||||
Ok(machine)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hello() {
|
||||
assert_eq!(unwrap!(output_test(include_str!("tests/hello.lua"))).state,"hello")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many() {
|
||||
println!("{}",unwrap!(output_test(include_str!("tests/script.lua"))).state)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,9 +85,6 @@ impl<C> Traverse<C> for Table
|
|||
where
|
||||
C: MarsAllocator,
|
||||
{
|
||||
fn collect(self) {
|
||||
todo!()
|
||||
}
|
||||
fn traverse(id: &Id<Table>, context: &mut C) {
|
||||
for i in 0..context.freelist().get(id).table.map.len() {
|
||||
let entry = context.freelist().get(id).table.map[i].entry.clone();
|
||||
|
|
|
|||
1
src/tests/hello.lua
Normal file
1
src/tests/hello.lua
Normal file
|
|
@ -0,0 +1 @@
|
|||
output "hello"
|
||||
|
|
@ -2,4 +2,4 @@ local tab = {}
|
|||
for i = 1, 1000000 do
|
||||
tab[i] = true
|
||||
end
|
||||
debug(tab)
|
||||
output(#tab)
|
||||
98
src/vm.rs
98
src/vm.rs
|
|
@ -91,10 +91,6 @@ where
|
|||
object.traverse(context);
|
||||
}
|
||||
}
|
||||
|
||||
fn collect(self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -126,10 +122,6 @@ where
|
|||
Upvalue::traverse(&context.freelist().get(id).upvalues[i].clone(), context);
|
||||
}
|
||||
}
|
||||
|
||||
fn collect(self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Callable {
|
||||
|
|
@ -236,8 +228,12 @@ impl TableEntry for CachedStringEntry {
|
|||
}
|
||||
|
||||
impl Equivalent<&str> for CachedStringEntry {
|
||||
fn matches(&self, string: &&str) -> bool {
|
||||
self.0.as_ref().unwrap().0.as_ref().eq(*string)
|
||||
fn matches(&self, other: &&str) -> bool {
|
||||
if let Some(ref string) = self.0 {
|
||||
string.as_str() == *other
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -286,7 +282,7 @@ impl Value {
|
|||
_ => true,
|
||||
}
|
||||
}
|
||||
pub(crate) fn unwrap_str(&mut self) -> Rc<str> {
|
||||
pub fn unwrap_str(&mut self) -> Rc<str> {
|
||||
match self {
|
||||
Value::CachedString(hashed) => hashed.0.clone(),
|
||||
Value::String(hashed) => hashed.0.clone(),
|
||||
|
|
@ -392,64 +388,12 @@ impl<V> Equivalent<HashedString> for MapEntry<&str,V> { // For the constant maps
|
|||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! getter {
|
||||
(@$state:ident,$index:literal=>$f:expr,$($x:literal=>$y:expr),*) => {
|
||||
let temp: MetaOutFn<$state> = $f;
|
||||
$crate::getter!(@$($x=>$y)*)
|
||||
};
|
||||
(@$state:ident,$index:literal=>$f:expr) => {
|
||||
let temp: MetaOutFn<$state> = $f;
|
||||
};
|
||||
(@) => {
|
||||
|
||||
};
|
||||
($ident:tt,$state:ident,{$($map:tt)*}) => {
|
||||
|machine,id,index,needle| {
|
||||
use $crate::deimos::map::Entry;
|
||||
use $crate::deimos::map::MapEntry;
|
||||
$crate::getter!(@$state,$($map)*);
|
||||
static MAP: &[
|
||||
$crate::deimos::map::Entry<
|
||||
$crate::deimos::map::MapEntry<&'static str,$crate::MetaOutFn<$state>>
|
||||
>] = &$crate::deimos::macros::map!{$($map)*};
|
||||
if let Some(string) = index.clone().try_string() {
|
||||
if let Some(func) = $crate::deimos::map::get(MAP,string) {
|
||||
(func.0.unwrap().1)(machine,id,needle)
|
||||
} else {
|
||||
Err(RunError::from(format!("No field {:?} for {}",index,stringify!($ident))))
|
||||
}
|
||||
} else {
|
||||
Err(RunError::from(format!("No field {:?} for {}",index,stringify!($ident))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! setter {
|
||||
($ident:tt,$state:tt,$($map:tt)*) => {
|
||||
|machine,id,index,value,needle| {
|
||||
static MAP: &[Entry<MapEntry<&'static str,$crate::MetaInFn<$state>>>] = &$($map)*;
|
||||
if let Some(string) = index.clone().try_string() {
|
||||
if let Some(func) = $crate::deimos::map::get(MAP,string) {
|
||||
(func.0.unwrap().1)(machine,id,value,needle)
|
||||
} else {
|
||||
Err(RunError::from(format!("No field {:?} for {}",index,stringify!($ident))))
|
||||
}
|
||||
} else {
|
||||
Err(RunError::from(format!("No field {:?} for {}",index,stringify!($ident))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Metas are a faster alternative to metatables for foreign objects
|
||||
pub type MetaGetFn<S> = fn(&mut Machine<S>, AnyId, Value, Needle) -> Result<Value, RunError>;
|
||||
pub type MetaSetFn<S> = fn(&mut Machine<S>, AnyId, Value, Value, Needle) -> Result<(), RunError>;
|
||||
pub type MetaInFn<S> = fn(&mut Machine<S>, AnyId, Value, Needle) -> Result<(), RunError>;
|
||||
pub type MetaOutFn<S> = fn(&mut Machine<S>, AnyId, Needle) -> Result<Value, RunError>;
|
||||
pub type MetaTraverseFn<S> = fn(&mut Machine<S>, AnyId) -> Result<(), RunError>;
|
||||
|
||||
pub struct Meta<S> {
|
||||
pub string: MetaOutFn<S>,
|
||||
pub number: MetaOutFn<S>,
|
||||
|
|
@ -457,7 +401,6 @@ pub struct Meta<S> {
|
|||
pub set: MetaSetFn<S>,
|
||||
pub traverse: MetaTraverseFn<S>,
|
||||
}
|
||||
|
||||
impl<S> Default for Meta<S> {
|
||||
fn default() -> Self {
|
||||
Meta {
|
||||
|
|
@ -488,10 +431,6 @@ pub struct Machine<S> {
|
|||
impl<S> MarsAllocator for Machine<S> {}
|
||||
|
||||
impl<S> Allocates<ForeignFunction<S>> for Machine<S> {
|
||||
fn collect() {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn freelist(&mut self) -> &mut FreeList<ForeignFunction<S>> {
|
||||
&mut self.natives
|
||||
}
|
||||
|
|
@ -502,35 +441,19 @@ where
|
|||
Machine<S>: MarsAllocator,
|
||||
{
|
||||
fn traverse(id: &Id<ForeignFunction<S>>, _context: &mut Machine<S>) {}
|
||||
|
||||
fn collect(self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Allocates<Table> for Machine<S> {
|
||||
fn collect() {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn freelist(&mut self) -> &mut FreeList<Table> {
|
||||
&mut self.tables
|
||||
}
|
||||
}
|
||||
impl<S> Allocates<Closure> for Machine<S> {
|
||||
fn collect() {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn freelist(&mut self) -> &mut FreeList<Closure> {
|
||||
&mut self.closures
|
||||
}
|
||||
}
|
||||
impl<S> Allocates<Upvalue> for Machine<S> {
|
||||
fn collect() {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn freelist(&mut self) -> &mut FreeList<Upvalue> {
|
||||
&mut self.upvalues
|
||||
}
|
||||
|
|
@ -1351,6 +1274,11 @@ impl<S> Machine<S> {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MarsError(String);
|
||||
|
||||
impl std::error::Error for MarsError {
|
||||
|
||||
}
|
||||
|
||||
impl Display for MarsError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue