From 33ec1461e4b6eb2443330d5a74474b23e4fff338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 6 Oct 2020 23:12:11 +0200 Subject: [PATCH] remove unnecessary cloning of SourcePosition, reduce SourcePosition size. clean, format --- crsn/src/asm/data/rd.rs | 2 +- crsn/src/asm/data/reg.rs | 13 ++++---- crsn/src/asm/data/wr.rs | 2 +- crsn/src/asm/error.rs | 6 ++-- crsn/src/asm/instr/cond.rs | 7 +++-- crsn/src/asm/instr/flatten.rs | 11 ++++--- crsn/src/asm/instr/mod.rs | 2 +- crsn/src/asm/instr/op.rs | 5 +-- crsn/src/asm/mod.rs | 7 +++-- crsn/src/asm/parse/arg_parser.rs | 4 +-- crsn/src/asm/parse/mod.rs | 4 +-- crsn/src/asm/parse/parse_cond.rs | 4 +-- crsn/src/asm/parse/parse_data.rs | 31 +++++++++---------- crsn/src/asm/parse/parse_instr.rs | 14 ++++----- crsn/src/asm/parse/parse_op.rs | 17 +++++----- crsn/src/asm/parse/parse_routine.rs | 14 ++++----- crsn/src/asm/patches/mod.rs | 20 ++++++------ crsn/src/builtin/defs.rs | 3 +- crsn/src/builtin/exec.rs | 7 ++--- crsn/src/builtin/mod.rs | 11 ++++--- crsn/src/builtin/parse.rs | 48 +++++++++++++++-------------- crsn/src/module/mod.rs | 9 +++--- crsn/src/runtime/program.rs | 9 +++--- crsn/src/utils/mod.rs | 5 +-- crsn_arith/src/exec.rs | 10 +++--- crsn_arith/src/lib.rs | 2 +- crsn_arith/src/parse.rs | 34 ++++++++++---------- crsn_screen/src/exec.rs | 16 +++------- crsn_screen/src/lib.rs | 2 +- crsn_screen/src/parse.rs | 10 +++--- crsn_stacks/src/exec.rs | 12 ++++---- crsn_stacks/src/lib.rs | 2 +- crsn_stacks/src/parse.rs | 4 +-- launcher/src/main.rs | 2 +- lib/spanned_sexp/.travis.yml | 28 ++++++++--------- lib/spanned_sexp/Cargo.toml | 6 ++-- lib/spanned_sexp/src/error.rs | 14 ++++----- lib/spanned_sexp/src/lib.rs | 2 +- lib/spanned_sexp/src/test.rs | 6 ++-- 39 files changed, 203 insertions(+), 202 deletions(-) diff --git a/crsn/src/asm/data/rd.rs b/crsn/src/asm/data/rd.rs index 435c267..589bb62 100644 --- a/crsn/src/asm/data/rd.rs +++ b/crsn/src/asm/data/rd.rs @@ -1,4 +1,4 @@ -use std::fmt::{Debug, Formatter, Display}; +use std::fmt::{Debug, Display, Formatter}; use std::fmt; use crate::asm::data::{DataDisp, Mask, RdData, Register}; diff --git a/crsn/src/asm/data/reg.rs b/crsn/src/asm/data/reg.rs index b14c593..a43acba 100644 --- a/crsn/src/asm/data/reg.rs +++ b/crsn/src/asm/data/reg.rs @@ -1,7 +1,8 @@ use std::fmt::{self, Display, Formatter}; -use crate::asm::error::CrsnError; use sexp::SourcePosition; + +use crate::asm::error::CrsnError; use crate::asm::patches::ErrWithPos; /// Register name @@ -25,27 +26,27 @@ impl Display for Register { } } -pub fn parse_reg(name: &str, at: SourcePosition) -> Result { +pub fn parse_reg(name: &str, at: &SourcePosition) -> Result { // TODO deduplicate code if let Some(rn) = name.strip_prefix("arg") { if rn.chars().find(|c: &char| !c.is_ascii_digit()).is_some() { - return Err(CrsnError::Parse(format!("Bad register: {}", name).into(), at))?; + return Err(CrsnError::Parse(format!("Bad register: {}", name).into(), at.clone()))?; } let val: u8 = rn.parse().err_pos(at)?; Ok(Register::Arg(val)) } else if let Some(rn) = name.strip_prefix("res") { if rn.chars().find(|c: &char| !c.is_ascii_digit()).is_some() { - return Err(CrsnError::Parse(format!("Bad register: {}", name).into(), at))?; + return Err(CrsnError::Parse(format!("Bad register: {}", name).into(), at.clone()))?; } let val: u8 = rn.parse().err_pos(at)?; Ok(Register::Res(val)) } else if let Some(rn) = name.strip_prefix("r") { if rn.chars().find(|c: &char| !c.is_ascii_digit()).is_some() { - return Err(CrsnError::Parse(format!("Bad register: {}", name).into(), at))?; + return Err(CrsnError::Parse(format!("Bad register: {}", name).into(), at.clone()))?; } let val: u8 = rn.parse().err_pos(at)?; Ok(Register::Gen(val)) } else { - Err(CrsnError::Parse(format!("Bad reg name: {}", name).into(), at))? + Err(CrsnError::Parse(format!("Bad reg name: {}", name).into(), at.clone()))? } } diff --git a/crsn/src/asm/data/wr.rs b/crsn/src/asm/data/wr.rs index 75509d6..8a7d8e8 100644 --- a/crsn/src/asm/data/wr.rs +++ b/crsn/src/asm/data/wr.rs @@ -1,4 +1,4 @@ -use std::fmt::{Debug, Formatter, Display}; +use std::fmt::{Debug, Display, Formatter}; use std::fmt; use crate::asm::data::{DataDisp, Mask, Rd, WrData}; diff --git a/crsn/src/asm/error.rs b/crsn/src/asm/error.rs index 12744a5..0f8bc49 100644 --- a/crsn/src/asm/error.rs +++ b/crsn/src/asm/error.rs @@ -1,13 +1,13 @@ use std::borrow::Cow; -use std::num::ParseIntError; +use std::error::Error; use thiserror::Error; +use sexp::SourcePosition; + use crate::asm::data::{Mask, Register}; use crate::asm::data::literal::Label; use crate::asm::instr::Cond; -use sexp::SourcePosition; -use std::error::Error; /// csn_asm unified error type #[derive(Error, Debug)] diff --git a/crsn/src/asm/instr/cond.rs b/crsn/src/asm/instr/cond.rs index e4f7774..4aa68ab 100644 --- a/crsn/src/asm/instr/cond.rs +++ b/crsn/src/asm/instr/cond.rs @@ -1,9 +1,10 @@ use std::fmt::{self, Display, Formatter}; use std::ops::Not; -use crate::asm::error::CrsnError; use sexp::SourcePosition; +use crate::asm::error::CrsnError; + /// Condition flag #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] pub enum Cond { @@ -46,7 +47,7 @@ pub enum Cond { NotCarry, } -pub fn parse_cond(text: &str, pos : SourcePosition) -> Result { +pub fn parse_cond(text: &str, pos: &SourcePosition) -> Result { Ok(match text.trim_end_matches('?') { "eq" | "=" | "==" => Cond::Equal, "ne" | "<>" | "!=" | "≠" => Cond::NotEqual, @@ -67,7 +68,7 @@ pub fn parse_cond(text: &str, pos : SourcePosition) -> Result { "ov" | "^" => Cond::Overflow, "nov" | "!ov" | "!^" => Cond::NotOverflow, _ => { - return Err(CrsnError::Parse(format!("Unknown cond: {}", text).into(), pos)); + return Err(CrsnError::Parse(format!("Unknown cond: {}", text).into(), pos.clone())); } }) } diff --git a/crsn/src/asm/instr/flatten.rs b/crsn/src/asm/instr/flatten.rs index 95f1a27..07d03b7 100644 --- a/crsn/src/asm/instr/flatten.rs +++ b/crsn/src/asm/instr/flatten.rs @@ -1,5 +1,8 @@ use std::collections::HashMap; -use std::sync::atomic::{AtomicU32}; +use std::fmt::Debug; +use std::sync::atomic::AtomicU32; + +use sexp::SourcePosition; use crate::asm::data::{Rd, RdData}; use crate::asm::data::literal::{Label, Value}; @@ -8,11 +11,9 @@ use crate::asm::instr::{Cond, InstrWithBranches, Op, Routine}; use crate::asm::instr::op::OpKind; use crate::builtin::defs::Barrier; use crate::builtin::defs::BuiltinOp; -use std::fmt::Debug; -use sexp::SourcePosition; /// A trait for something that can turn into multiple instructions -pub trait Flatten : Debug { +pub trait Flatten: Debug { fn flatten(self: Box, label_num: &AtomicU32) -> Result, CrsnError>; fn pos(&self) -> SourcePosition; @@ -154,7 +155,7 @@ pub fn labels_to_skips(ops: Vec) -> Result, CrsnError> { let skip = *dest as isize - n as isize + skipped; cleaned.push(Op { cond: op.cond, - pos : op.pos.clone(), + pos: op.pos.clone(), kind: OpKind::BuiltIn(BuiltinOp::Skip(Rd::new(RdData::Immediate(skip as Value)))), }); } else { diff --git a/crsn/src/asm/instr/mod.rs b/crsn/src/asm/instr/mod.rs index 28190e9..f71e181 100644 --- a/crsn/src/asm/instr/mod.rs +++ b/crsn/src/asm/instr/mod.rs @@ -1,9 +1,9 @@ pub use cond::Cond; pub use flatten::Flatten; pub use op::Op; +use sexp::SourcePosition; use crate::asm::data::literal::RoutineName; -use sexp::SourcePosition; pub mod op; pub mod cond; diff --git a/crsn/src/asm/instr/op.rs b/crsn/src/asm/instr/op.rs index e432da4..9a96bc2 100644 --- a/crsn/src/asm/instr/op.rs +++ b/crsn/src/asm/instr/op.rs @@ -1,11 +1,12 @@ use std::fmt::Debug; +use sexp::{Atom, Sexp, SourcePosition}; + use crate::asm::instr::Cond; use crate::builtin::defs::BuiltinOp; use crate::module::{EvalRes, OpTrait}; use crate::runtime::fault::Fault; use crate::runtime::run_thread::{info::ThreadInfo, state::RunState}; -use sexp::{Sexp, Atom, SourcePosition}; /// A higher level simple opration #[derive(Debug)] @@ -18,7 +19,7 @@ pub enum OpKind { #[derive(Debug)] pub struct Op { pub cond: Option, - pub pos : SourcePosition, + pub pos: SourcePosition, pub kind: OpKind, } diff --git a/crsn/src/asm/mod.rs b/crsn/src/asm/mod.rs index 7217054..292713e 100644 --- a/crsn/src/asm/mod.rs +++ b/crsn/src/asm/mod.rs @@ -1,11 +1,12 @@ use std::cell::RefCell; use std::sync::Arc; +use sexp::SourcePosition; + +use crate::asm::instr::flatten::labels_to_skips; use crate::asm::parse::{ParserContext, ParserState}; use crate::module::CrsnExtension; use crate::runtime::program::Program; -use crate::asm::instr::flatten::labels_to_skips; -use sexp::SourcePosition; pub mod data; pub mod error; @@ -24,7 +25,7 @@ pub fn assemble(source: &str, parsers: Arc>>) -> Resu }), }; - let ops = parse::parse(source, SourcePosition::default(), &pcx)?; + let ops = parse::parse(source, &SourcePosition::default(), &pcx)?; let ops = labels_to_skips(ops)?; Ok(Program::new(ops, parsers)?) diff --git a/crsn/src/asm/parse/arg_parser.rs b/crsn/src/asm/parse/arg_parser.rs index 1bbfb7e..a5d911c 100644 --- a/crsn/src/asm/parse/arg_parser.rs +++ b/crsn/src/asm/parse/arg_parser.rs @@ -11,7 +11,7 @@ use crate::asm::patches::NextOrErr; pub struct TokenParser<'a> { orig_len: usize, args: Vec, - start_pos: SourcePosition, + start_pos: &'a SourcePosition, pub pcx: &'a ParserContext<'a>, } @@ -29,7 +29,7 @@ impl<'a> IntoIterator for TokenParser<'a> { impl<'a> TokenParser<'a> { /// Create a new argument parser - pub fn new(mut args: Vec, start_pos: SourcePosition, pcx: &'a ParserContext) -> Self { + pub fn new(mut args: Vec, start_pos: &'a SourcePosition, pcx: &'a ParserContext) -> Self { args.reverse(); Self { orig_len: args.len(), diff --git a/crsn/src/asm/parse/mod.rs b/crsn/src/asm/parse/mod.rs index cf87193..bcfbbd0 100644 --- a/crsn/src/asm/parse/mod.rs +++ b/crsn/src/asm/parse/mod.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::sync::atomic::AtomicU32; pub use parse_instr::parse_instructions; +use sexp::SourcePosition; use crate::asm::data::literal::{ConstantName, RegisterAlias, Value}; use crate::asm::data::Register; @@ -10,7 +11,6 @@ use crate::asm::error::CrsnError; use crate::asm::instr::Op; use crate::asm::parse::sexp_expect::expect_list; use crate::module::CrsnExtension; -use sexp::SourcePosition; pub mod parse_cond; pub mod parse_instr; @@ -39,7 +39,7 @@ pub struct ParserState { pub constants: HashMap, } -pub fn parse(source: &str, pos: SourcePosition, parsers: &ParserContext) -> Result, CrsnError> { +pub fn parse(source: &str, pos: &SourcePosition, parsers: &ParserContext) -> Result, CrsnError> { let (items, _pos) = expect_list(sexp::parse(source)?, true)?; /* numbered labels start with a weird high number diff --git a/crsn/src/asm/parse/parse_cond.rs b/crsn/src/asm/parse/parse_cond.rs index bbbeb9e..0205b17 100644 --- a/crsn/src/asm/parse/parse_cond.rs +++ b/crsn/src/asm/parse/parse_cond.rs @@ -9,12 +9,12 @@ use crate::asm::patches::TryRemove; pub fn parse_cond_branch(tok: Sexp, parsers: &ParserContext) -> Result<(Cond, Box), CrsnError> { let (mut list, pos) = expect_list(tok, false)?; - let (kw, kw_pos) = expect_string_atom(list.remove_or_err(0, pos.clone(), "Missing \"cond?\" keyword in conditional branch")?)?; + let (kw, kw_pos) = expect_string_atom(list.remove_or_err(0, &pos, "Missing \"cond?\" keyword in conditional branch")?)?; if !kw.ends_with('?') { return Err(CrsnError::Parse(format!("Condition must end with '?': {}", kw).into(), kw_pos)); } - Ok((cond::parse_cond(&kw, kw_pos)?, parse_instructions(list.into_iter(), pos, parsers)?)) + Ok((cond::parse_cond(&kw, &kw_pos)?, parse_instructions(list.into_iter(), &pos, parsers)?)) } diff --git a/crsn/src/asm/parse/parse_data.rs b/crsn/src/asm/parse/parse_data.rs index 436fc31..bc0e6d4 100644 --- a/crsn/src/asm/parse/parse_data.rs +++ b/crsn/src/asm/parse/parse_data.rs @@ -8,7 +8,6 @@ use crate::asm::data::literal::{ConstantName, Label, RegisterAlias, Value}; use crate::asm::error::CrsnError; use crate::asm::parse::ParserContext; use crate::asm::parse::sexp_expect::expect_string_atom; -use std::num::TryFromIntError; use crate::asm::patches::ErrWithPos; fn is_valid_identifier(name: &str) -> bool { @@ -46,14 +45,14 @@ pub fn parse_constant_name(name: Sexp) -> Result<(ConstantName, SourcePosition), pub fn parse_label(name: Sexp) -> Result { // trace!("parse label: {:?}", name); let (name, namepos) = expect_string_atom(name)?; - Ok(parse_label_str(&name, namepos)?) + Ok(parse_label_str(&name, &namepos)?) } -pub fn parse_label_str(name: &str, pos: SourcePosition) -> Result { +pub fn parse_label_str(name: &str, pos: &SourcePosition) -> Result { let label = name.trim_start_matches(':'); Ok(if label.starts_with('#') { - let val = parse_u64(&label[1..], pos.clone())?; + let val = parse_u64(&label[1..], pos)?; Label::Numbered(u32::try_from(val).err_pos(pos)?) } else { Label::Named(label.to_string()) @@ -67,7 +66,7 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result { + Sexp::Atom(Atom::I(val), _pos) => { Ok(DataDisp::Immediate(unsafe { std::mem::transmute(val) })) } Sexp::Atom(Atom::S(s), pos) => { @@ -92,7 +91,7 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result Result Result Result { match tok { - Sexp::Atom(Atom::I(val), pos) => { + Sexp::Atom(Atom::I(val), _pos) => { Ok(unsafe { std::mem::transmute(val) }) } Sexp::Atom(Atom::S(s), pos) => { @@ -131,7 +130,7 @@ pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result { return Ok(*val); } - Ok(unsafe { std::mem::transmute(parse_i64(&s, pos)?) }) + Ok(unsafe { std::mem::transmute(parse_i64(&s, &pos)?) }) } other => { Err(CrsnError::Parse(format!("bad value format: {:?}", other).into(), other.pos().clone())) @@ -140,7 +139,7 @@ pub fn parse_value(tok: Sexp, pcx: &ParserContext) -> Result { } -pub fn parse_u64(literal: &str, pos: SourcePosition) -> Result { +pub fn parse_u64(literal: &str, pos: &SourcePosition) -> Result { // trace!("parse u64 from {}", literal); let mut without_underscores = Cow::Borrowed(literal); if without_underscores.contains('_') { @@ -156,21 +155,21 @@ pub fn parse_u64(literal: &str, pos: SourcePosition) -> Result { } } -pub fn parse_i64(literal: &str, pos: SourcePosition) -> Result { +pub fn parse_i64(literal: &str, pos: &SourcePosition) -> Result { // trace!("parse i64 from {}", literal); if let Some(_value) = literal.strip_prefix("-") { - Ok(-1 * i64::try_from(parse_u64(literal, pos.clone())?).err_pos(pos)?) + Ok(-1 * i64::try_from(parse_u64(literal, pos)?).err_pos(pos)?) } else { - Ok(i64::try_from(parse_u64(literal, pos.clone())?).err_pos(pos)?) + Ok(i64::try_from(parse_u64(literal, pos)?).err_pos(pos)?) } } pub fn parse_rd(tok: Sexp, pcx: &ParserContext) -> Result { let pos = tok.pos().clone(); - Ok(Rd::new(RdData::try_from(parse_data_disp(tok, pcx)?).err_pos(pos)?)) + Ok(Rd::new(RdData::try_from(parse_data_disp(tok, pcx)?).err_pos(&pos)?)) } pub fn parse_wr(tok: Sexp, pcx: &ParserContext) -> Result { let pos = tok.pos().clone(); - Ok(Wr::new(WrData::try_from(parse_data_disp(tok, pcx)?).err_pos(pos)?)) + Ok(Wr::new(WrData::try_from(parse_data_disp(tok, pcx)?).err_pos(&pos)?)) } diff --git a/crsn/src/asm/parse/parse_instr.rs b/crsn/src/asm/parse/parse_instr.rs index 7b78183..8f24234 100644 --- a/crsn/src/asm/parse/parse_instr.rs +++ b/crsn/src/asm/parse/parse_instr.rs @@ -7,12 +7,12 @@ use crate::asm::parse::parse_cond::parse_cond_branch; use crate::asm::parse::parse_routine::parse_routine; use crate::asm::parse::ParserContext; use crate::asm::parse::sexp_expect::{expect_list, expect_string_atom}; +use crate::asm::patches::NextOrErr; use crate::module::ParseRes; use super::parse_op::parse_op; -use crate::asm::patches::NextOrErr; -pub fn parse_instructions(items: impl Iterator, pos: SourcePosition, pcx: &ParserContext) -> Result, CrsnError> { +pub fn parse_instructions(items: impl Iterator, pos: &SourcePosition, pcx: &ParserContext) -> Result, CrsnError> { let mut parsed = vec![]; for expr in items { let (tokens, listpos) = expect_list(expr, false)?; @@ -21,13 +21,13 @@ pub fn parse_instructions(items: impl Iterator, pos: SourcePosition, let (name, namepos) = expect_string_atom(toki.next_or_err(listpos.clone(), "Expected instruction name token")?)?; if name == "proc" { - parsed.push(parse_routine(toki, pos.clone(), pcx)?); + parsed.push(parse_routine(toki, pos, pcx)?); continue; } - let mut token_parser = TokenParser::new(toki.collect(), listpos.clone(), pcx); + let mut token_parser = TokenParser::new(toki.collect(), &listpos, pcx); for p in pcx.parsers { - token_parser = match p.parse_syntax(pos.clone(), &name, token_parser) { + token_parser = match p.parse_syntax(pos, &name, token_parser) { Ok(ParseRes::Parsed(op)) => return Ok(op), Ok(ParseRes::ParsedNone) => return Ok(Box::new(())), Ok(ParseRes::Unknown(to_reuse)) => { @@ -45,7 +45,7 @@ pub fn parse_instructions(items: impl Iterator, pos: SourcePosition, // Get back the original iterator let toki = token_parser.into_iter(); - let arg_tokens = TokenParser::new(toki.clone().take_while(|e| e.is_atom()).collect(), listpos.clone(), pcx); + let arg_tokens = TokenParser::new(toki.clone().take_while(|e| e.is_atom()).collect(), &listpos, pcx); let branch_tokens = toki .skip_while(|e| e.is_atom()) .take_while(|e| e.is_list()); @@ -62,7 +62,7 @@ pub fn parse_instructions(items: impl Iterator, pos: SourcePosition, } }; - if let Some(op) = parse_op(name.as_str(), arg_tokens, namepos.clone())? { + if let Some(op) = parse_op(name.as_str(), arg_tokens, &namepos)? { parsed.push(Box::new(InstrWithBranches { op, pos: namepos, diff --git a/crsn/src/asm/parse/parse_op.rs b/crsn/src/asm/parse/parse_op.rs index 1ceb58c..bf26826 100644 --- a/crsn/src/asm/parse/parse_op.rs +++ b/crsn/src/asm/parse/parse_op.rs @@ -1,32 +1,33 @@ +use sexp::SourcePosition; + use crate::asm::error::CrsnError; use crate::asm::instr::cond::parse_cond; use crate::asm::instr::Op; use crate::asm::parse::arg_parser::TokenParser; -use crate::module::ParseRes; use crate::builtin::BuiltinOps; -use sexp::SourcePosition; +use crate::module::ParseRes; -pub fn parse_op<'a>(mut keyword: &str, mut arg_tokens: TokenParser<'a>, spos: SourcePosition) -> Result, CrsnError> { +pub fn parse_op<'a>(mut keyword: &str, mut arg_tokens: TokenParser<'a>, spos: &SourcePosition) -> Result, CrsnError> { // Include built-in instructions let builtins = [BuiltinOps::new()]; let mut cond = None; if let Some(pos) = keyword.find('.') { - cond = Some(parse_cond(&keyword[(pos + 1)..], spos.clone())?); + cond = Some(parse_cond(&keyword[(pos + 1)..], spos)?); keyword = &keyword[..pos]; } for p in builtins.iter().chain(arg_tokens.pcx.parsers) { - arg_tokens = match p.parse_op(spos.clone(), keyword, arg_tokens) { + arg_tokens = match p.parse_op(spos, keyword, arg_tokens) { Ok(ParseRes::Parsed(kind)) => return Ok(Some(Op { cond, - pos: spos, + pos: spos.clone(), kind, })), Ok(ParseRes::ParsedNone) => return Ok(None), Ok(ParseRes::Unknown(to_reuse)) => { if to_reuse.parsing_started() { - return Err(CrsnError::Parse(format!("Module \"{}\" started parsing {}, but returned Unknown!", p.name(), keyword).into(), spos)); + return Err(CrsnError::Parse(format!("Module \"{}\" started parsing {}, but returned Unknown!", p.name(), keyword).into(), spos.clone())); } to_reuse } @@ -36,5 +37,5 @@ pub fn parse_op<'a>(mut keyword: &str, mut arg_tokens: TokenParser<'a>, spos: So } } - return Err(CrsnError::Parse(format!("Unknown instruction: {}", keyword).into(), spos)); + return Err(CrsnError::Parse(format!("Unknown instruction: {}", keyword).into(), spos.clone())); } diff --git a/crsn/src/asm/parse/parse_routine.rs b/crsn/src/asm/parse/parse_routine.rs index 34819aa..e849cff 100644 --- a/crsn/src/asm/parse/parse_routine.rs +++ b/crsn/src/asm/parse/parse_routine.rs @@ -7,14 +7,14 @@ use crate::asm::parse::{parse_instructions, ParserContext}; use crate::asm::parse::arg_parser::TokenParser; use crate::asm::parse::parse_data::parse_reg_alias; use crate::asm::parse::sexp_expect::expect_string_atom; -use crate::builtin::parse::parse_routine_name; use crate::asm::patches::NextOrErr; +use crate::builtin::parse::parse_routine_name; -pub fn parse_routine(mut toki: impl Iterator + Clone, rt_pos: SourcePosition, pcx: &ParserContext) -> Result, CrsnError> { +pub fn parse_routine(mut toki: impl Iterator + Clone, rt_pos: &SourcePosition, pcx: &ParserContext) -> Result, CrsnError> { let (name, namepos) = expect_string_atom(toki.next_or_err(rt_pos.clone(), "Expected routine name")?)?; - let mut name = parse_routine_name(name, namepos)?; + let mut name = parse_routine_name(name, &namepos)?; - let arg_tokens = TokenParser::new(toki.clone().take_while(|e| e.is_atom()).collect(), rt_pos.clone(), pcx); + let arg_tokens = TokenParser::new(toki.clone().take_while(|e| e.is_atom()).collect(), rt_pos, pcx); // If arity is explicitly given, then either no named argument must be provided, // or their count must match the arity. If no arity is given, then arity is determined @@ -22,7 +22,7 @@ pub fn parse_routine(mut toki: impl Iterator + Clone, rt_pos: SourceP if name.arity == 0 && arg_tokens.len() != 0 { name.arity = arg_tokens.len() as u8; } else if arg_tokens.len() != 0 && name.arity as usize != arg_tokens.len() { - return Err(CrsnError::Parse(format!("arity mismatch in routine {}", name.name).into(), rt_pos)); + return Err(CrsnError::Parse(format!("arity mismatch in routine {}", name.name).into(), rt_pos.clone())); } let toki = toki.skip_while(|e| e.is_atom()); @@ -44,7 +44,7 @@ pub fn parse_routine(mut toki: impl Iterator + Clone, rt_pos: SourceP } } - let body = parse_instructions(toki, rt_pos.clone(), pcx)?; + let body = parse_instructions(toki, rt_pos, pcx)?; { let mut pstate = pcx.state.borrow_mut(); @@ -54,7 +54,7 @@ pub fn parse_routine(mut toki: impl Iterator + Clone, rt_pos: SourceP return Ok(Box::new(Routine { name, - pos: rt_pos, + pos: rt_pos.clone(), body, })); } diff --git a/crsn/src/asm/patches/mod.rs b/crsn/src/asm/patches/mod.rs index aa299b5..b36202c 100644 --- a/crsn/src/asm/patches/mod.rs +++ b/crsn/src/asm/patches/mod.rs @@ -1,12 +1,12 @@ use sexp::SourcePosition; + use crate::asm::error::CrsnError; -use std::borrow::Cow; pub trait TryRemove { type Item; fn try_remove(&mut self, index: usize) -> Option; - fn remove_or_err(&mut self, index: usize, pos: SourcePosition, err : &'static str) -> Result; + fn remove_or_err(&mut self, index: usize, pos: &SourcePosition, err: &'static str) -> Result; } impl TryRemove for Vec { @@ -20,10 +20,10 @@ impl TryRemove for Vec { } } - fn remove_or_err(&mut self, index: usize, pos: SourcePosition, err : &'static str) -> Result { + fn remove_or_err(&mut self, index: usize, pos: &SourcePosition, err: &'static str) -> Result { match self.try_remove(index) { None => { - Err(CrsnError::Parse(err.into(), pos)) + Err(CrsnError::Parse(err.into(), pos.clone())) } Some(removed) => Ok(removed) } @@ -31,10 +31,10 @@ impl TryRemove for Vec { } pub trait NextOrErr { - fn next_or_err(&mut self, pos: SourcePosition, err : &'static str) -> Result; + fn next_or_err(&mut self, pos: SourcePosition, err: &'static str) -> Result; } -impl> NextOrErr for K { +impl> NextOrErr for K { fn next_or_err(&mut self, pos: SourcePosition, err: &'static str) -> Result { match self.next() { None => { @@ -46,14 +46,14 @@ impl> NextOrErr for K { } pub trait ErrWithPos { - fn err_pos(self, pos : SourcePosition) -> Result; + fn err_pos(self, pos: &SourcePosition) -> Result; } -impl ErrWithPos for Result { - fn err_pos(self, pos : SourcePosition) -> Result { +impl ErrWithPos for Result { + fn err_pos(self, pos: &SourcePosition) -> Result { match self { Ok(v) => Ok(v), - Err(e) => Err(CrsnError::ParseOther(Box::new(e), pos)) + Err(e) => Err(CrsnError::ParseOther(Box::new(e), pos.clone())) } } } diff --git a/crsn/src/builtin/defs.rs b/crsn/src/builtin/defs.rs index 686f432..db3de9d 100644 --- a/crsn/src/builtin/defs.rs +++ b/crsn/src/builtin/defs.rs @@ -1,8 +1,9 @@ +use sexp::SourcePosition; + use crate::asm::data::{Rd, RdObj, Wr}; use crate::asm::data::literal::{DebugMsg, Label, RoutineName}; use crate::asm::instr::Op; use crate::asm::instr::op::OpKind; -use sexp::SourcePosition; #[derive(Debug)] pub enum Barrier { diff --git a/crsn/src/builtin/exec.rs b/crsn/src/builtin/exec.rs index 7574c6a..45a0db5 100644 --- a/crsn/src/builtin/exec.rs +++ b/crsn/src/builtin/exec.rs @@ -1,5 +1,7 @@ use std::time::Duration; +use sexp::Sexp; + use crate::asm::data::{Rd, RdData}; use crate::asm::data::literal::Addr; use crate::asm::instr::Cond; @@ -8,11 +10,6 @@ use crate::module::{EvalRes, OpTrait}; use crate::runtime::fault::Fault; use crate::runtime::frame::StackFrame; use crate::runtime::run_thread::{state::RunState, ThreadInfo}; -use sexp::Sexp; - - - - impl OpTrait for BuiltinOp { fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result { diff --git a/crsn/src/builtin/mod.rs b/crsn/src/builtin/mod.rs index 0fa9a9f..fdf1ea9 100644 --- a/crsn/src/builtin/mod.rs +++ b/crsn/src/builtin/mod.rs @@ -1,9 +1,10 @@ -use crate::module::{CrsnExtension, ParseRes}; -use crate::asm::parse::arg_parser::TokenParser; -use crate::asm::instr::op::OpKind; -use crate::asm::error::CrsnError; use sexp::SourcePosition; +use crate::asm::error::CrsnError; +use crate::asm::instr::op::OpKind; +use crate::asm::parse::arg_parser::TokenParser; +use crate::module::{CrsnExtension, ParseRes}; + pub mod defs; pub mod exec; pub mod parse; @@ -22,7 +23,7 @@ impl CrsnExtension for BuiltinOps { "builtin" } - fn parse_op<'a>(&self, pos: SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { + fn parse_op<'a>(&self, pos: &SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { parse::parse_op(pos, keyword, args) } } diff --git a/crsn/src/builtin/parse.rs b/crsn/src/builtin/parse.rs index 873fd90..ec5a57f 100644 --- a/crsn/src/builtin/parse.rs +++ b/crsn/src/builtin/parse.rs @@ -5,15 +5,14 @@ use crate::asm::data::reg::parse_reg; use crate::asm::error::CrsnError; use crate::asm::instr::op::OpKind; use crate::asm::parse::arg_parser::TokenParser; -use crate::asm::parse::parse_data::{parse_constant_name, parse_label, parse_rd, parse_reg_alias, parse_value, parse_label_str}; +use crate::asm::parse::parse_data::{parse_constant_name, parse_label, parse_label_str, parse_rd, parse_reg_alias, parse_value}; use crate::asm::parse::sexp_expect::expect_string_atom; +use crate::asm::patches::ErrWithPos; use crate::builtin::defs::{Barrier, BuiltinOp}; -use crate::module::{ParseRes}; +use crate::module::ParseRes; use crate::utils::A; -use crate::asm::patches::ErrWithPos; - -pub(crate) fn parse_op<'a>(op_pos: SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { +pub(crate) fn parse_op<'a>(op_pos: &SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { let pcx = args.pcx; Ok(ParseRes::Parsed(OpKind::BuiltIn(match keyword { @@ -35,7 +34,7 @@ pub(crate) fn parse_op<'a>(op_pos: SourcePosition, keyword: &str, mut args: Toke let (alias, aliaspos) = parse_reg_alias(args.next_or_err()?)?; trace!("alias={:?}", alias); let (rn, rpos) = args.next_string()?; - let register = parse_reg(&rn, rpos.clone())?; + let register = parse_reg(&rn, &rpos)?; trace!("register={:?}", alias); let mut pstate = pcx.state.borrow_mut(); @@ -124,7 +123,7 @@ pub(crate) fn parse_op<'a>(op_pos: SourcePosition, keyword: &str, mut args: Toke "routine" => { let name = args.next_string()?; - BuiltinOp::Routine(parse_routine_name(name.0, name.1)?) + BuiltinOp::Routine(parse_routine_name(name.0, &name.1)?) } "skip" => { @@ -200,7 +199,7 @@ pub(crate) fn parse_op<'a>(op_pos: SourcePosition, keyword: &str, mut args: Toke other => { if let Some(label) = other.strip_prefix(':') { - BuiltinOp::Label(parse_label_str(label, op_pos)?) + BuiltinOp::Label(parse_label_str(label, &op_pos)?) } else { return Ok(ParseRes::Unknown(args)); } @@ -208,7 +207,7 @@ pub(crate) fn parse_op<'a>(op_pos: SourcePosition, keyword: &str, mut args: Toke }))) } -pub(crate) fn parse_routine_name(name: String, pos: SourcePosition) -> Result { +pub(crate) fn parse_routine_name(name: String, pos: &SourcePosition) -> Result { let (name, arity) = if let Some(n) = name.find('/') { ( (&name[0..n]).to_string(), @@ -238,7 +237,7 @@ pub(crate) fn to_sexp(op: &BuiltinOp) -> Sexp { inner.extend(args.iter().map(A)); sexp::list(&inner) } - }, + } BuiltinOp::Ret(values) => { if values.is_empty() { sexp::list(&[A("ret")]) @@ -287,16 +286,15 @@ pub(crate) fn to_sexp(op: &BuiltinOp) -> Sexp { #[cfg(test)] mod test { - - use std::sync::atomic::AtomicU32; - use crate::asm::instr::{Flatten}; + use sexp::SourcePosition; + + use crate::asm::instr::Flatten; use crate::asm::parse::{parse_instructions, ParserContext}; use crate::asm::parse::sexp_expect::expect_list; - - use crate::module::OpTrait; use crate::builtin::BuiltinOps; + use crate::module::OpTrait; #[test] fn roundtrip() { @@ -306,10 +304,10 @@ mod test { ("(nop)", "(nop)"), ("(halt)", "(halt)"), ("(sleep 1000)", "(sleep 1000)"), - ("(:x)\ - (j :x)", "(skip 0)"), - ("(:#7)\ - (j :#7)", "(skip 0)"), + ("(:x)", "(:x)"), + ("(j :x)", "(j :x)"), + ("(:#7)", "(:#7)"), + ("(j :#7)", "(j :#7)"), ("(fj :x)", "(fj :x)"), ("(skip 0)", "(skip 0)"), ("(skip r0)", "(skip r0)"), @@ -367,9 +365,9 @@ mod test { /* first cycle */ let s = sexp::parse(&format!("({})", sample)) .expect("parse sexp"); - let list = expect_list(Some(s), false).unwrap(); + let list = expect_list(s, false).unwrap(); let num = AtomicU32::new(0); - let parsed = parse_instructions(list.into_iter(), &pcx) + let parsed = parse_instructions(list.0.into_iter(), &SourcePosition::default(), &pcx) .expect("parse instr").flatten(&num) .expect("flatten").remove(0); @@ -379,13 +377,17 @@ mod test { assert_eq!(expected, exported); println!(" - 2nd cycle"); + let pcx = ParserContext { + parsers, + state: Default::default(), + }; /* second cycle, nothing should change */ let s = sexp::parse(&format!("({})", exported)) .expect("parse sexp (2c)"); - let list = expect_list(Some(s), false).unwrap(); + let list = expect_list(s, false).unwrap(); let num = AtomicU32::new(0); - let parsed = parse_instructions(list.into_iter(), &pcx) + let parsed = parse_instructions(list.0.into_iter(), &SourcePosition::default(), &pcx) .expect("parse instr (2c)").flatten(&num) .expect("flatten (2c)").remove(0); diff --git a/crsn/src/module/mod.rs b/crsn/src/module/mod.rs index 77ff1ac..2861ace 100644 --- a/crsn/src/module/mod.rs +++ b/crsn/src/module/mod.rs @@ -1,8 +1,9 @@ #![allow(unused_variables)] -use std::fmt::{Debug}; +use std::fmt::Debug; pub use eval_res::EvalRes; +use sexp::{Sexp, SourcePosition}; use crate::asm::data::literal::Value; use crate::asm::data::Mask; @@ -13,8 +14,6 @@ use crate::asm::parse::arg_parser::TokenParser; use crate::runtime::fault::Fault; use crate::runtime::run_thread::state::RunState; use crate::runtime::run_thread::ThreadInfo; -use sexp::{Sexp, SourcePosition}; - mod eval_res; @@ -53,12 +52,12 @@ pub trait CrsnExtension: Debug + Send + Sync + 'static { /// If the instruction keyword is not recognized, return Unknown with the unchanged argument list. /// /// pcx is available on the arg_tokens parser - fn parse_op<'a>(&self, pos: SourcePosition, keyword: &str, arg_tokens: TokenParser<'a>) -> Result, CrsnError>; + fn parse_op<'a>(&self, pos: &SourcePosition, keyword: &str, arg_tokens: TokenParser<'a>) -> Result, CrsnError>; /// Parse a generic S-expression (non-op) /// /// pcx is available on the arg_tokens parser - fn parse_syntax<'a>(&self, pos: SourcePosition, keyword: &str, tokens: TokenParser<'a>) + fn parse_syntax<'a>(&self, pos: &SourcePosition, keyword: &str, tokens: TokenParser<'a>) -> Result>, CrsnError> { Ok(ParseRes::Unknown(tokens)) diff --git a/crsn/src/runtime/program.rs b/crsn/src/runtime/program.rs index 279e43e..1ddebdc 100644 --- a/crsn/src/runtime/program.rs +++ b/crsn/src/runtime/program.rs @@ -1,14 +1,15 @@ use std::collections::HashMap; use std::sync::Arc; +use sexp::SourcePosition; + use crate::asm::data::literal::{Addr, Label, RoutineName}; +use crate::asm::error::CrsnError; use crate::asm::instr::Op; use crate::asm::instr::op::OpKind; use crate::builtin::defs::{Barrier, BuiltinOp}; use crate::module::CrsnExtension; use crate::runtime::fault::Fault; -use sexp::SourcePosition; -use crate::asm::error::CrsnError; #[derive(Debug)] pub struct Program { @@ -96,9 +97,9 @@ impl Program { pos: SourcePosition { line: 0, column: 0, - index: 0 + index: 0, }, - cond: None + cond: None, } } else { &self.ops[addr.0 as usize] diff --git a/crsn/src/utils/mod.rs b/crsn/src/utils/mod.rs index 7a365ce..052b9a0 100644 --- a/crsn/src/utils/mod.rs +++ b/crsn/src/utils/mod.rs @@ -1,8 +1,9 @@ -pub use option_ext::UncheckedOptionExt; use std::fmt::Display; -use sexp::{Sexp, Atom}; use std::str::FromStr; +pub use option_ext::UncheckedOptionExt; +use sexp::{Atom, Sexp}; + mod option_ext; /// Convert a string token to Sexp diff --git a/crsn_arith/src/exec.rs b/crsn_arith/src/exec.rs index 7118d91..412473a 100644 --- a/crsn_arith/src/exec.rs +++ b/crsn_arith/src/exec.rs @@ -2,16 +2,16 @@ use std::ops::Rem; use num_traits::PrimInt; +use crsn::asm::data::{Rd, Wr}; use crsn::asm::instr::Cond; use crsn::module::{EvalRes, OpTrait}; use crsn::runtime::fault::Fault; use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; - -use crate::defs::ArithOp; -use crsn::sexp::Sexp; use crsn::sexp; +use crsn::sexp::Sexp; use crsn::utils::A; -use crsn::asm::data::{Rd, Wr}; + +use crate::defs::ArithOp; impl OpTrait for ArithOp { fn execute(&self, _ti: &ThreadInfo, state: &mut RunState) -> Result { @@ -238,7 +238,7 @@ impl OpTrait for ArithOp { } } -fn to_sexp_2_or_3(name: &str, dst : &Wr, a: &Rd, b: &Rd) -> Sexp { +fn to_sexp_2_or_3(name: &str, dst: &Wr, a: &Rd, b: &Rd) -> Sexp { if &dst.as_rd() == a { sexp::list(&[A(name), A(dst), A(b)]) } else { diff --git a/crsn_arith/src/lib.rs b/crsn_arith/src/lib.rs index ba06694..146fb98 100644 --- a/crsn_arith/src/lib.rs +++ b/crsn_arith/src/lib.rs @@ -22,7 +22,7 @@ impl CrsnExtension for ArithOps { "arith" } - fn parse_op<'a>(&self, pos: SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { + fn parse_op<'a>(&self, pos: &SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { parse::parse(pos, keyword, args) } } diff --git a/crsn_arith/src/parse.rs b/crsn_arith/src/parse.rs index 4248a11..e4a7fb0 100644 --- a/crsn_arith/src/parse.rs +++ b/crsn_arith/src/parse.rs @@ -3,11 +3,11 @@ use crsn::asm::error::CrsnError; use crsn::asm::instr::op::OpKind; use crsn::asm::parse::arg_parser::TokenParser; use crsn::module::ParseRes; +use crsn::sexp::SourcePosition; use crate::defs::ArithOp; -use crsn::sexp::SourcePosition; -pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { +pub(crate) fn parse<'a>(pos: &SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { Ok(ParseRes::ext(match keyword { "cmp" => { ArithOp::Compare { @@ -57,7 +57,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Add requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Add requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -80,7 +80,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Sub requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Sub requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -103,7 +103,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Mul requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Mul requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -130,7 +130,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("DivR requires 3 or 4 arguments".into(), pos)); + return Err(CrsnError::Parse("DivR requires 3 or 4 arguments".into(), pos.clone())); } } } @@ -156,7 +156,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Div requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Div requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -180,7 +180,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Mod requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Mod requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -203,7 +203,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("And requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("And requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -226,7 +226,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Or requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Or requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -249,7 +249,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Xor requires 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Xor requires 2 or 3 arguments".into(), pos.clone())); } } } @@ -270,7 +270,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Cpl requires 1 or 2 arguments".into(), pos)); + return Err(CrsnError::Parse("Cpl requires 1 or 2 arguments".into(), pos.clone())); } } } @@ -301,7 +301,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Rol requires 1, 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Rol requires 1, 2 or 3 arguments".into(), pos.clone())); } } } @@ -332,7 +332,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Ror requires 1, 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Ror requires 1, 2 or 3 arguments".into(), pos.clone())); } } } @@ -363,7 +363,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Lsl requires 1, 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Lsl requires 1, 2 or 3 arguments".into(), pos.clone())); } } } @@ -394,7 +394,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Lsr requires 1, 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Lsr requires 1, 2 or 3 arguments".into(), pos.clone())); } } } @@ -425,7 +425,7 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse } } _ => { - return Err(CrsnError::Parse("Asr requires 1, 2 or 3 arguments".into(), pos)); + return Err(CrsnError::Parse("Asr requires 1, 2 or 3 arguments".into(), pos.clone())); } } } diff --git a/crsn_screen/src/exec.rs b/crsn_screen/src/exec.rs index 1afd177..63bac12 100644 --- a/crsn_screen/src/exec.rs +++ b/crsn_screen/src/exec.rs @@ -1,19 +1,19 @@ use std::ops::Sub; use std::time::{Duration, Instant}; -use minifb::{ScaleMode, Window, WindowOptions, MouseMode, Key, MouseButton}; +use minifb::{Key, MouseButton, MouseMode, ScaleMode, Window, WindowOptions}; use crsn::asm::data::literal::Value; use crsn::asm::instr::Cond; use crsn::module::{EvalRes, OpTrait}; use crsn::runtime::fault::Fault; use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; - -use crate::defs::ScreenOp; -use crsn::sexp::Sexp; use crsn::sexp; +use crsn::sexp::Sexp; use crsn::utils::A; +use crate::defs::ScreenOp; + #[derive(Debug)] struct Opts { auto_blit: bool, @@ -299,7 +299,7 @@ fn blit(backend: &mut Backend) { backend.last_render = Instant::now(); } -fn num2key(num : Value) -> Option { +fn num2key(num: Value) -> Option { let remap = [ Key::Key0, Key::Key1, @@ -311,7 +311,6 @@ fn num2key(num : Value) -> Option { Key::Key7, Key::Key8, Key::Key9, - Key::A, // 10 Key::B, Key::C, @@ -361,7 +360,6 @@ fn num2key(num : Value) -> Option { Key::Up, Key::Apostrophe, Key::Backquote, - Key::Backslash, // 57 Key::Comma, Key::Equal, @@ -370,20 +368,17 @@ fn num2key(num : Value) -> Option { Key::Period, Key::RightBracket, Key::Semicolon, - Key::Slash, // 65 Key::Backspace, Key::Delete, Key::End, Key::Enter, - Key::Escape, // 70 Key::Home, Key::Insert, Key::Menu, Key::PageDown, Key::PageUp, - Key::Pause, // 76 Key::Space, Key::Tab, @@ -394,7 +389,6 @@ fn num2key(num : Value) -> Option { Key::RightShift, Key::LeftCtrl, Key::RightCtrl, - Key::NumPad0, // 86 Key::NumPad1, Key::NumPad2, diff --git a/crsn_screen/src/lib.rs b/crsn_screen/src/lib.rs index 9b569f5..f4f8c3c 100644 --- a/crsn_screen/src/lib.rs +++ b/crsn_screen/src/lib.rs @@ -25,7 +25,7 @@ impl CrsnExtension for ScreenOps { "screen" } - fn parse_op<'a>(&self, pos: SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { + fn parse_op<'a>(&self, pos: &SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { parse::parse(pos, keyword, args) } } diff --git a/crsn_screen/src/parse.rs b/crsn_screen/src/parse.rs index f73c20b..8ccbb7e 100644 --- a/crsn_screen/src/parse.rs +++ b/crsn_screen/src/parse.rs @@ -1,13 +1,13 @@ -use crsn::asm::data::{Rd}; +use crsn::asm::data::Rd; use crsn::asm::error::CrsnError; use crsn::asm::instr::op::OpKind; use crsn::asm::parse::arg_parser::TokenParser; use crsn::module::ParseRes; +use crsn::sexp::SourcePosition; use crate::defs::ScreenOp; -use crsn::sexp::SourcePosition; -pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { +pub(crate) fn parse<'a>(_pos: &SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { Ok(ParseRes::ext(match keyword { "sc-init" => { ScreenOp::ScreenInit { @@ -65,14 +65,14 @@ pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParse "sc-key" => { ScreenOp::TestKey { pressed: args.next_wr()?, - code: args.next_rd()? + code: args.next_rd()?, } } "sc-mbtn" => { ScreenOp::TestMouse { pressed: args.next_wr()?, - button: args.next_rd()? + button: args.next_rd()?, } } diff --git a/crsn_stacks/src/exec.rs b/crsn_stacks/src/exec.rs index 2a6cdc9..f11554f 100644 --- a/crsn_stacks/src/exec.rs +++ b/crsn_stacks/src/exec.rs @@ -1,16 +1,16 @@ use std::collections::{HashMap, VecDeque}; +use crsn::asm::data::{Rd, RdObj, Wr}; use crsn::asm::data::literal::Value; use crsn::asm::instr::Cond; use crsn::module::{EvalRes, OpTrait}; use crsn::runtime::fault::Fault; use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; - -use crate::defs::StackOp; -use crsn::sexp::Sexp; use crsn::sexp; +use crsn::sexp::Sexp; use crsn::utils::A; -use crsn::asm::data::{RdObj, Rd, Wr}; + +use crate::defs::StackOp; #[derive(Debug, Default)] struct Stacks { @@ -66,7 +66,7 @@ pub(crate) fn drop_obj(state: &mut RunState, handle: Value) -> Result Ok(stacks.store.remove(&handle).map(|_| ())) } -fn prepare_push(state: &mut RunState, obj: &RdObj, src: &Rd, pushfn : impl FnOnce(&mut VecDeque, Value) -> ()) -> Result<(), Fault> { +fn prepare_push(state: &mut RunState, obj: &RdObj, src: &Rd, pushfn: impl FnOnce(&mut VecDeque, Value) -> ()) -> Result<(), Fault> { state.clear_status(); let handle = state.read_obj(*obj)?; let val = state.read(*src)?; @@ -79,7 +79,7 @@ fn prepare_push(state: &mut RunState, obj: &RdObj, src: &Rd, pushfn : impl FnOnc Ok(()) } -fn prepare_pop(state: &mut RunState, dst: &Wr, obj: &RdObj, popfn : impl FnOnce(&mut VecDeque) -> Option) -> Result<(), Fault> { +fn prepare_pop(state: &mut RunState, dst: &Wr, obj: &RdObj, popfn: impl FnOnce(&mut VecDeque) -> Option) -> Result<(), Fault> { state.clear_status(); let handle = state.read_obj(*obj)?; diff --git a/crsn_stacks/src/lib.rs b/crsn_stacks/src/lib.rs index 6059f4e..424a813 100644 --- a/crsn_stacks/src/lib.rs +++ b/crsn_stacks/src/lib.rs @@ -25,7 +25,7 @@ impl CrsnExtension for StackOps { "stacks" } - fn parse_op<'a>(&self, pos: SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { + fn parse_op<'a>(&self, pos: &SourcePosition, keyword: &str, args: TokenParser<'a>) -> Result, CrsnError> { parse::parse(pos, keyword, args) } diff --git a/crsn_stacks/src/parse.rs b/crsn_stacks/src/parse.rs index 0458180..5fb3199 100644 --- a/crsn_stacks/src/parse.rs +++ b/crsn_stacks/src/parse.rs @@ -2,11 +2,11 @@ use crsn::asm::error::CrsnError; use crsn::asm::instr::op::OpKind; use crsn::asm::parse::arg_parser::TokenParser; use crsn::module::ParseRes; +use crsn::sexp::SourcePosition; use crate::defs::StackOp; -use crsn::sexp::SourcePosition; -pub(crate) fn parse<'a>(pos: SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { +pub(crate) fn parse<'a>(_pos: &SourcePosition, keyword: &str, mut args: TokenParser<'a>) -> Result, CrsnError> { Ok(ParseRes::ext(match keyword { "stack" => { StackOp::NewStack { diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 95e0965..748c0f5 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -10,11 +10,11 @@ use clappconfig::clap::ArgMatches; use serde::{Deserialize, Serialize}; use crsn::asm::data::literal::Addr; +use crsn::module::OpTrait; use crsn::runtime::run_thread::{RunThread, ThreadToken}; use crsn_arith::ArithOps; use crsn_screen::ScreenOps; use crsn_stacks::StackOps; -use crsn::module::OpTrait; mod read_file; mod serde_duration_millis; diff --git a/lib/spanned_sexp/.travis.yml b/lib/spanned_sexp/.travis.yml index a4430b7..9a98210 100644 --- a/lib/spanned_sexp/.travis.yml +++ b/lib/spanned_sexp/.travis.yml @@ -8,9 +8,9 @@ addons: - libelf-dev - libdw-dev rust: -- nightly + - nightly os: -- linux + - linux env: global: @@ -21,16 +21,16 @@ before_script: - pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH script: -- travis-cargo build -- travis-cargo test -- travis-cargo bench -- travis-cargo doc + - travis-cargo build + - travis-cargo test + - travis-cargo bench + - travis-cargo doc after_success: -- | - [ $TRAVIS_BRANCH = master ] && - [ $TRAVIS_PULL_REQUEST = false ] && - echo '' > target/doc/index.html && - git clone --depth 1 https://github.com/davisp/ghp-import && - ./ghp-import/ghp-import -n target/doc && - git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages -- travis-cargo coveralls --no-sudo + - | + [ $TRAVIS_BRANCH = master ] && + [ $TRAVIS_PULL_REQUEST = false ] && + echo '' > target/doc/index.html && + git clone --depth 1 https://github.com/davisp/ghp-import && + ./ghp-import/ghp-import -n target/doc && + git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages + - travis-cargo coveralls --no-sudo diff --git a/lib/spanned_sexp/Cargo.toml b/lib/spanned_sexp/Cargo.toml index 4742a78..7ba146a 100644 --- a/lib/spanned_sexp/Cargo.toml +++ b/lib/spanned_sexp/Cargo.toml @@ -4,12 +4,12 @@ version = "1.1.4" authors = ["Clark Gaebel "] documentation = "https://cgaebel.github.io/sexp" -homepage = "https://github.com/cgaebel/sexp" -repository = "https://github.com/cgaebel/sexp" +homepage = "https://github.com/cgaebel/sexp" +repository = "https://github.com/cgaebel/sexp" readme = "README.md" -keywords = [ "sexp", "parsing", "s-expression", "file-format" ] +keywords = ["sexp", "parsing", "s-expression", "file-format"] description = "A small, simple, self-contained, s-expression parser and pretty-printer." diff --git a/lib/spanned_sexp/src/error.rs b/lib/spanned_sexp/src/error.rs index 34ab7aa..1807365 100644 --- a/lib/spanned_sexp/src/error.rs +++ b/lib/spanned_sexp/src/error.rs @@ -12,11 +12,11 @@ pub struct Error { #[derive(Debug, PartialEq, Clone, Default)] pub struct SourcePosition { /// The line number on which the error occurred. - pub line: usize, + pub line: u32, /// The column number on which the error occurred. - pub column: usize, + pub column: u32, /// The index in the given string which caused the error. - pub index: usize, + pub index: u32, } /// Since errors are the uncommon case, they're boxed. This keeps the size of @@ -56,9 +56,9 @@ pub(crate) fn get_line_and_column(s: &str, pos: usize) -> SourcePosition { } } SourcePosition { - line, - column: cmp::max(col, 0) as usize, - index: pos, + line: line as u32, + column: cmp::max(col, 0) as u32, + index: pos as u32, } } @@ -66,7 +66,7 @@ pub(crate) fn get_line_and_column(s: &str, pos: usize) -> SourcePosition { fn err_impl(message: &'static str, s: &str, pos: &usize) -> Err { Box::new(Error { message, - pos: get_line_and_column(s, *pos) + pos: get_line_and_column(s, *pos), }) } diff --git a/lib/spanned_sexp/src/lib.rs b/lib/spanned_sexp/src/lib.rs index ee0ba18..bbbaa9c 100644 --- a/lib/spanned_sexp/src/lib.rs +++ b/lib/spanned_sexp/src/lib.rs @@ -11,9 +11,9 @@ use std::borrow::Cow; use std::fmt; use std::str::{self, FromStr}; +use error::{ERes, err, spos}; pub use error::Error; pub use error::SourcePosition; -use error::{ERes, err, spos}; #[cfg(test)] mod test; diff --git a/lib/spanned_sexp/src/test.rs b/lib/spanned_sexp/src/test.rs index f96beca..f32f2ce 100644 --- a/lib/spanned_sexp/src/test.rs +++ b/lib/spanned_sexp/src/test.rs @@ -27,8 +27,8 @@ fn test_pp() { fn test_tight_parens() { let s = "(hello(world))"; let sexp = parse(s).unwrap(); - assert_eq!(sexp, Sexp::List(vec![Sexp::Atom(Atom::S("hello".into()), None), - Sexp::List(vec![Sexp::Atom(Atom::S("world".into()), None)], None)], None)); + assert_eq!(sexp, Sexp::List(vec![Sexp::Atom(Atom::S("hello".into()), Default::default()), + Sexp::List(vec![Sexp::Atom(Atom::S("world".into()), Default::default())], Default::default())], Default::default())); let s = "(this (has)tight(parens))"; let s2 = "( this ( has ) tight ( parens ) )"; assert_eq!(parse(s).unwrap(), parse(s2).unwrap()); @@ -66,5 +66,5 @@ fn line_and_col_test() { fn sexp_size() { // I just want to see when this changes, in the diff. use std::mem; - assert_eq!(mem::size_of::(), mem::size_of::() * 5); + assert_eq!(mem::size_of::(), mem::size_of::() * 6); }