From 2752fa4beb56b963746277a15e72eece33d71fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 31 Oct 2020 19:13:07 +0100 Subject: [PATCH] fix unit tests, cargo fiox --- crsn/src/asm/parse/mod.rs | 11 ++++++ crsn/src/asm/parse/parse_data.rs | 2 +- crsn/src/builtin/exec.rs | 2 +- crsn/src/builtin/parse.rs | 55 ++++++++++++++++++++++------ crsn/src/runtime/run_thread.rs | 2 +- crsn/src/runtime/run_thread/state.rs | 2 +- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/crsn/src/asm/parse/mod.rs b/crsn/src/asm/parse/mod.rs index a52125c..eaec09d 100644 --- a/crsn/src/asm/parse/mod.rs +++ b/crsn/src/asm/parse/mod.rs @@ -53,6 +53,17 @@ pub struct ParserState { pub parsing_expr : bool, } +impl ParserState { + pub fn reset(&mut self) { + self.global_reg_aliases.clear(); + self.reg_aliases.clear(); + self.reg_alias_stack.clear(); + self.constants.clear(); + self.parsing_expr = false; + self.const_eval.reset(); + } +} + pub fn parse(source: &str, pos: &SourcePosition, parsers: &ParserContext) -> Result, CrsnError> { let (items, _pos) = expect_list(sexp::parse(source)?, true)?; diff --git a/crsn/src/asm/parse/parse_data.rs b/crsn/src/asm/parse/parse_data.rs index 3fcd7b6..0aacb9f 100644 --- a/crsn/src/asm/parse/parse_data.rs +++ b/crsn/src/asm/parse/parse_data.rs @@ -124,7 +124,7 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result Sexp { if args.is_empty() { sexp::list(&[A("spawn"), A(handle), A(proc)]) } else { - let mut v = vec![A("spawn"), A(handle), A(proc)]; + let mut v = vec![A("spawn"), A(handle), A(&proc.name)]; v.extend(args.iter().map(|r| A(r))); sexp::list(&v) } @@ -473,10 +473,15 @@ mod test { use sexp::SourcePosition; - use crate::asm::parse::{parse_instructions, ParserContext}; + use crate::asm::parse::{parse_instructions, ParserContext, ParserState}; use crate::asm::parse::sexp_expect::expect_list; use crate::builtin::BuiltinOps; use crate::module::OpTrait; + use std::cell::RefCell; + use std::sync::Arc; + use crate::runtime::run_thread::{ThreadInfo, ThreadToken, RunState}; + use crate::runtime::program::Program; + use crate::runtime::frame::REG_COUNT; #[test] fn roundtrip() { @@ -562,13 +567,42 @@ mod test { let parser = BuiltinOps::new(); - let parsers = &[parser]; + let parsers = Arc::new(vec![parser]); + + let ti = Arc::new(ThreadInfo { + id: ThreadToken(0), + uniq: Default::default(), + program: Program::new(vec![], parsers.clone()).unwrap(), + cycle_time: Default::default(), + scheduler_interval: Default::default(), + extensions: parsers.clone(), + }); + + let pcx = ParserContext { + parsers: &parsers, + state: RefCell::new(ParserState { + reg_aliases: Default::default(), + reg_alias_stack: vec![], + global_reg_aliases: Default::default(), + constants: Default::default(), + + // This is a fake thread to pass to constant expressions when evaluating them. + // This allows to evaluate nearly all instructions at compile time. + const_eval: RunState { + thread_info: ti.clone(), + cr: Default::default(), + parked: Default::default(), + global_regs: [0; REG_COUNT], + ext_data: Default::default(), + cr_deadline: None + }, + const_eval_ti: ti.clone(), + parsing_expr: false + }), + }; for (sample, expected) in samples { - let pcx = ParserContext { - parsers, - state: Default::default(), - }; + pcx.state.borrow_mut().reset(); println!("Parse: {}", sample); @@ -587,10 +621,7 @@ mod test { assert_eq!(expected, exported); println!(" - 2nd cycle"); - let pcx = ParserContext { - parsers, - state: Default::default(), - }; + pcx.state.borrow_mut().reset(); /* second cycle, nothing should change */ let s = sexp::parse(&format!("({})", exported)) diff --git a/crsn/src/runtime/run_thread.rs b/crsn/src/runtime/run_thread.rs index fb58990..632f403 100644 --- a/crsn/src/runtime/run_thread.rs +++ b/crsn/src/runtime/run_thread.rs @@ -126,7 +126,7 @@ impl RunThread { 'find: for (n, th) in self.state.parked.iter_mut().enumerate() { if th.handle == handle { if let CoroutineState::Finished(_) = &th.cr_state { - let mut crs = mem::replace(&mut th.cr_state, CoroutineState::Ready); + let crs = mem::replace(&mut th.cr_state, CoroutineState::Ready); self.state.parked.remove(n); // delete it found = true; diff --git a/crsn/src/runtime/run_thread/state.rs b/crsn/src/runtime/run_thread/state.rs index 5b3ffc2..0e0341f 100644 --- a/crsn/src/runtime/run_thread/state.rs +++ b/crsn/src/runtime/run_thread/state.rs @@ -72,7 +72,7 @@ impl Default for CoroutineState { impl RunState { /// Clear everything. Caution: when done at runtime, this effectively reboots the thread - pub fn clear_all(&mut self) { + pub fn reset(&mut self) { self.cr = Default::default(); self.parked = Default::default(); self.global_regs = Default::default();