From 2269d759c62db490991515f6d1c09cece38fa010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 28 Sep 2020 20:44:37 +0200 Subject: [PATCH] refactors --- crsn/src/builtin/parse.rs | 2 +- crsn/src/module/mod.rs | 13 ++++++++++--- crsn_arith/src/parse.rs | 6 +++--- crsn_stacks/src/parse.rs | 6 +++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/crsn/src/builtin/parse.rs b/crsn/src/builtin/parse.rs index e28f911..7ae516d 100644 --- a/crsn/src/builtin/parse.rs +++ b/crsn/src/builtin/parse.rs @@ -28,7 +28,7 @@ impl CrsnExtension for BuiltinOps { "builtin" } - fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { + fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result, Error> { Ok(ParseOpRes::Parsed(Op::BuiltIn(match keyword { "nop" => { BuiltinOp::Nop diff --git a/crsn/src/module/mod.rs b/crsn/src/module/mod.rs index a7abd66..5d023b8 100644 --- a/crsn/src/module/mod.rs +++ b/crsn/src/module/mod.rs @@ -14,13 +14,20 @@ use crate::runtime::run_thread::ThreadInfo; mod eval_res; /// Result type returned from the op parser. This is the Ok variant of a Result. -pub enum ParseOpRes { +pub enum ParseOpRes { /// Parsing successful. - Parsed(Op), + Parsed(T), /// Instruction not recognized, but there was no error. Unknown(ArgParser), } +impl ParseOpRes { + /// Helper to construct an extension op + pub fn parsed(op : impl OpTrait) -> Self { + Self::Parsed(Op::Ext(Box::new(op))) + } +} + pub trait OpTrait: Debug + Send + Sync + 'static { fn execute(&self, ti: &ThreadInfo, state: &mut RunState) -> Result; } @@ -34,7 +41,7 @@ pub trait CrsnExtension: Debug + Send + Sync + 'static { /// the argument list and either return Ok or Err. /// /// If the instruction keyword is not recognized, return Unknown with the unchanged argument list. - fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result; + fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result, Error>; /// Drop an object referenced by a handle fn drop_obj(&self, diff --git a/crsn_arith/src/parse.rs b/crsn_arith/src/parse.rs index d641302..316d216 100644 --- a/crsn_arith/src/parse.rs +++ b/crsn_arith/src/parse.rs @@ -24,8 +24,8 @@ impl CrsnExtension for ArithOps { "arith" } - fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { - Ok(ParseOpRes::Parsed(Op::Ext(Box::new(match keyword { + fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result, Error> { + Ok(ParseOpRes::parsed(match keyword { "cmp" => { ArithOp::Compare { a: args.next_rd()?, @@ -450,6 +450,6 @@ impl CrsnExtension for ArithOps { _other => { return Ok(ParseOpRes::Unknown(args)); } - })))) + })) } } diff --git a/crsn_stacks/src/parse.rs b/crsn_stacks/src/parse.rs index 3f028df..ee5bbbf 100644 --- a/crsn_stacks/src/parse.rs +++ b/crsn_stacks/src/parse.rs @@ -26,8 +26,8 @@ impl CrsnExtension for StackOps { "stacks" } - fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result { - Ok(ParseOpRes::Parsed(Op::Ext(Box::new(match keyword { + fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result, Error> { + Ok(ParseOpRes::parsed(match keyword { "stack" => { StackOp::NewStack { dst: args.next_wr()?, @@ -51,7 +51,7 @@ impl CrsnExtension for StackOps { _other => { return Ok(ParseOpRes::Unknown(args)); } - })))) + })) } fn drop_obj(&self, _ti: &ThreadInfo, state: &mut RunState, handle: Value) -> Result, Fault>