forked from MightyPork/crsn
refactors
This commit is contained in:
@@ -28,7 +28,7 @@ impl CrsnExtension for BuiltinOps {
|
|||||||
"builtin"
|
"builtin"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result<ParseOpRes, Error> {
|
fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result<ParseOpRes<Op>, Error> {
|
||||||
Ok(ParseOpRes::Parsed(Op::BuiltIn(match keyword {
|
Ok(ParseOpRes::Parsed(Op::BuiltIn(match keyword {
|
||||||
"nop" => {
|
"nop" => {
|
||||||
BuiltinOp::Nop
|
BuiltinOp::Nop
|
||||||
|
|||||||
+10
-3
@@ -14,13 +14,20 @@ use crate::runtime::run_thread::ThreadInfo;
|
|||||||
mod eval_res;
|
mod eval_res;
|
||||||
|
|
||||||
/// Result type returned from the op parser. This is the Ok variant of a Result.
|
/// Result type returned from the op parser. This is the Ok variant of a Result.
|
||||||
pub enum ParseOpRes {
|
pub enum ParseOpRes<T> {
|
||||||
/// Parsing successful.
|
/// Parsing successful.
|
||||||
Parsed(Op),
|
Parsed(T),
|
||||||
/// Instruction not recognized, but there was no error.
|
/// Instruction not recognized, but there was no error.
|
||||||
Unknown(ArgParser),
|
Unknown(ArgParser),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ParseOpRes<Op> {
|
||||||
|
/// 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 {
|
pub trait OpTrait: Debug + Send + Sync + 'static {
|
||||||
fn execute(&self, ti: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault>;
|
fn execute(&self, ti: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault>;
|
||||||
}
|
}
|
||||||
@@ -34,7 +41,7 @@ pub trait CrsnExtension: Debug + Send + Sync + 'static {
|
|||||||
/// the argument list and either return Ok or Err.
|
/// the argument list and either return Ok or Err.
|
||||||
///
|
///
|
||||||
/// If the instruction keyword is not recognized, return Unknown with the unchanged argument list.
|
/// If the instruction keyword is not recognized, return Unknown with the unchanged argument list.
|
||||||
fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result<ParseOpRes, Error>;
|
fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result<ParseOpRes<Op>, Error>;
|
||||||
|
|
||||||
/// Drop an object referenced by a handle
|
/// Drop an object referenced by a handle
|
||||||
fn drop_obj(&self,
|
fn drop_obj(&self,
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ impl CrsnExtension for ArithOps {
|
|||||||
"arith"
|
"arith"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result<ParseOpRes, Error> {
|
fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result<ParseOpRes<Op>, Error> {
|
||||||
Ok(ParseOpRes::Parsed(Op::Ext(Box::new(match keyword {
|
Ok(ParseOpRes::parsed(match keyword {
|
||||||
"cmp" => {
|
"cmp" => {
|
||||||
ArithOp::Compare {
|
ArithOp::Compare {
|
||||||
a: args.next_rd()?,
|
a: args.next_rd()?,
|
||||||
@@ -450,6 +450,6 @@ impl CrsnExtension for ArithOps {
|
|||||||
_other => {
|
_other => {
|
||||||
return Ok(ParseOpRes::Unknown(args));
|
return Ok(ParseOpRes::Unknown(args));
|
||||||
}
|
}
|
||||||
}))))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ impl CrsnExtension for StackOps {
|
|||||||
"stacks"
|
"stacks"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result<ParseOpRes, Error> {
|
fn parse_op(&self, keyword: &str, mut args: ArgParser) -> Result<ParseOpRes<Op>, Error> {
|
||||||
Ok(ParseOpRes::Parsed(Op::Ext(Box::new(match keyword {
|
Ok(ParseOpRes::parsed(match keyword {
|
||||||
"stack" => {
|
"stack" => {
|
||||||
StackOp::NewStack {
|
StackOp::NewStack {
|
||||||
dst: args.next_wr()?,
|
dst: args.next_wr()?,
|
||||||
@@ -51,7 +51,7 @@ impl CrsnExtension for StackOps {
|
|||||||
_other => {
|
_other => {
|
||||||
return Ok(ParseOpRes::Unknown(args));
|
return Ok(ParseOpRes::Unknown(args));
|
||||||
}
|
}
|
||||||
}))))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drop_obj(&self, _ti: &ThreadInfo, state: &mut RunState, handle: Value) -> Result<Option<()>, Fault>
|
fn drop_obj(&self, _ti: &ThreadInfo, state: &mut RunState, handle: Value) -> Result<Option<()>, Fault>
|
||||||
|
|||||||
Reference in New Issue
Block a user