Skip to content

Commit

Permalink
Move matching & command parsing up into values
Browse files Browse the repository at this point in the history
  • Loading branch information
rdaum committed Nov 15, 2024
1 parent 6e97778 commit a92d312
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 59 deletions.
2 changes: 1 addition & 1 deletion crates/daemon/src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use crate::rpc_hosts::Hosts;
use crate::rpc_session::RpcSession;
use moor_db::DatabaseFlavour;
use moor_kernel::config::Config;
use moor_kernel::tasks::command_parse::preposition_to_string;
use moor_kernel::tasks::sessions::SessionError::DeliveryError;
use moor_kernel::tasks::sessions::{Session, SessionError};
use moor_kernel::tasks::TaskHandle;
use moor_kernel::SchedulerClient;
use moor_values::matching::command_parse::preposition_to_string;
use moor_values::model::{Named, ObjectRef, PropFlag, ValSet, VerbFlag};
use moor_values::tasks::SchedulerError::CommandExecutionError;
use moor_values::tasks::{CommandError, NarrativeEvent, SchedulerError, TaskId};
Expand Down
9 changes: 4 additions & 5 deletions crates/kernel/src/builtins/bf_verbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
use strum::EnumCount;
use tracing::{error, warn};

use crate::bf_declare;
use crate::builtins::BfRet::Ret;
use crate::builtins::{world_state_bf_err, BfCallState, BfErr, BfRet, BuiltinFunction};
use moor_compiler::offset_for_builtin;
use moor_compiler::program_to_tree;
use moor_compiler::unparse;
use moor_compiler::GlobalName;
use moor_compiler::Program;
use moor_compiler::{compile, to_literal};
use moor_values::matching::command_parse::{parse_preposition_spec, preposition_to_string};
use moor_values::model::ObjFlag;
use moor_values::model::VerbDef;
use moor_values::model::WorldStateError;
Expand All @@ -37,11 +41,6 @@ use moor_values::{v_empty_list, v_list, v_none, v_objid, v_str, v_string, Var};
use moor_values::{v_list_iter, Error};
use moor_values::{AsByteBuffer, Sequence};

use crate::bf_declare;
use crate::builtins::BfRet::Ret;
use crate::builtins::{world_state_bf_err, BfCallState, BfErr, BfRet, BuiltinFunction};
use crate::tasks::command_parse::{parse_preposition_spec, preposition_to_string};

// verb_info (obj <object>, str <verb-desc>) -> {<owner>, <perms>, <names>}
fn bf_verb_info(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
if bf_args.args.len() != 2 {
Expand Down
1 change: 0 additions & 1 deletion crates/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub use moor_values::tasks::TaskId;

pub mod builtins;
pub mod config;
pub mod matching;
pub mod tasks;
pub mod textdump;
pub mod vm;
1 change: 0 additions & 1 deletion crates/kernel/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub use crate::tasks::tasks_db::{NoopTasksDb, TasksDb, TasksDbError};
use crate::vm::Fork;
use moor_values::tasks::{SchedulerError, TaskId};

pub mod command_parse;
pub mod scheduler;
pub mod sessions;

Expand Down
23 changes: 11 additions & 12 deletions crates/kernel/src/tasks/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,8 @@ use moor_values::model::{BinaryType, HasUuid, ObjectRef, ValSet, VerbAttrs};
use moor_values::model::{CommitResult, Perms};
use moor_values::model::{WorldState, WorldStateError};

use moor_values::tasks::{
AbortLimitReason, CommandError, SchedulerError, TaskId, VerbProgramError,
};
use moor_values::Error::{E_INVARG, E_INVIND, E_PERM};
use moor_values::{v_err, v_int, v_none, v_objid, v_string, Symbol, Var};
use moor_values::{AsByteBuffer, SYSTEM_OBJECT};
use moor_values::{Objid, Variant};

use crate::builtins::BuiltinRegistry;
use crate::config::Config;
use crate::matching::match_env::MatchEnvironmentParseMatcher;
use crate::matching::ws_match_env::WsMatchEnv;
use crate::tasks::command_parse::ParseMatcher;
use crate::tasks::scheduler_client::{SchedulerClient, SchedulerClientMsg};
use crate::tasks::sessions::{Session, SessionFactory, SystemControl};
use crate::tasks::suspension::{SuspensionQ, WakeCondition};
Expand All @@ -56,10 +45,20 @@ use crate::tasks::{
};
use crate::textdump::{make_textdump, TextdumpWriter};
use crate::vm::Fork;
use moor_values::matching::command_parse::ParseMatcher;
use moor_values::matching::match_env::MatchEnvironmentParseMatcher;
use moor_values::matching::ws_match_env::WsMatchEnv;
use moor_values::tasks::SchedulerError::{
CommandExecutionError, InputRequestNotFound, TaskAbortedCancelled, TaskAbortedError,
TaskAbortedException, TaskAbortedLimit, VerbProgramFailed,
};
use moor_values::tasks::{
AbortLimitReason, CommandError, SchedulerError, TaskId, VerbProgramError,
};
use moor_values::Error::{E_INVARG, E_INVIND, E_PERM};
use moor_values::{v_err, v_int, v_none, v_objid, v_string, Symbol, Var};
use moor_values::{AsByteBuffer, SYSTEM_OBJECT};
use moor_values::{Objid, Variant};

const SCHEDULER_TICK_TIME: Duration = Duration::from_millis(5);

Expand Down Expand Up @@ -1732,7 +1731,7 @@ fn match_object_ref(
Ok(obj)
}
ObjectRef::Match(object_name) => {
let match_env = WsMatchEnv { ws: tx, perms };
let match_env = WsMatchEnv::new(tx, perms);
let matcher = MatchEnvironmentParseMatcher {
env: match_env,
player,
Expand Down
11 changes: 4 additions & 7 deletions crates/kernel/src/tasks/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ use moor_values::{NOTHING, SYSTEM_OBJECT};

use crate::builtins::BuiltinRegistry;
use crate::config::Config;
use crate::matching::match_env::MatchEnvironmentParseMatcher;
use crate::matching::ws_match_env::WsMatchEnv;
use crate::tasks::command_parse::{parse_command, ParseCommandError, ParsedCommand};
use crate::tasks::sessions::Session;
use crate::tasks::task_scheduler_client::{TaskControlMsg, TaskSchedulerClient};
use crate::tasks::vm_host::{VMHostResponse, VmHost};
use crate::tasks::{ServerOptions, TaskStart, VerbCall};
use moor_values::matching::command_parse::{parse_command, ParseCommandError, ParsedCommand};
use moor_values::matching::match_env::MatchEnvironmentParseMatcher;
use moor_values::matching::ws_match_env::WsMatchEnv;

lazy_static! {
static ref HUH_SYM: Symbol = Symbol::mk("huh");
Expand Down Expand Up @@ -495,10 +495,7 @@ impl Task {
};

// Parse the command in the current environment.
let me = WsMatchEnv {
ws: world_state,
perms: player,
};
let me = WsMatchEnv::new(world_state, player);
let matcher = MatchEnvironmentParseMatcher { env: me, player };
let parsed_command = match parse_command(command, matcher) {
Ok(pc) => pc,
Expand Down
2 changes: 1 addition & 1 deletion crates/kernel/src/tasks/vm_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use moor_values::{v_none, Symbol};

use crate::builtins::BuiltinRegistry;
use crate::config::Config;
use crate::tasks::command_parse::ParsedCommand;
use crate::tasks::sessions::Session;
use crate::tasks::task_scheduler_client::TaskSchedulerClient;
use crate::tasks::vm_host::VMHostResponse::{AbortLimit, ContinueOk, DispatchFork, Suspend};
Expand All @@ -49,6 +48,7 @@ use crate::vm::vm_call::VerbProgram;
use crate::vm::VmExecParams;
use crate::vm::{ExecutionResult, Fork, VerbExecutionRequest};
use crate::vm::{FinallyReason, VMExecState};
use moor_values::matching::command_parse::ParsedCommand;

/// Return values from exec_interpreter back to the Task scheduler loop
pub enum VMHostResponse {
Expand Down
2 changes: 1 addition & 1 deletion crates/kernel/src/vm/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ use moor_values::{v_empty_str, Error};
use moor_values::{v_list, Objid};
use moor_values::{AsByteBuffer, Symbol};

use crate::tasks::command_parse::ParsedCommand;
use crate::vm::moo_frame::MooStackFrame;
use crate::vm::vm_call::VerbProgram;
use crate::vm::VerbExecutionRequest;
use moor_values::matching::command_parse::ParsedCommand;

lazy_static! {
static ref EVAL_SYMBOL: Symbol = Symbol::mk("eval");
Expand Down
2 changes: 1 addition & 1 deletion crates/kernel/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bytes::Bytes;
pub use exec_state::VMExecState;
use moor_compiler::{BuiltinId, Name};
use moor_compiler::{Offset, Program};
use moor_values::matching::command_parse::ParsedCommand;
use moor_values::model::VerbDef;
use moor_values::{Objid, Var};
pub use vm_call::VerbExecutionRequest;
Expand All @@ -33,7 +34,6 @@ pub use vm_unwind::FinallyReason;
// Exports to the rest of the kernel
use crate::builtins::BuiltinRegistry;
use crate::config::Config;
use crate::tasks::command_parse::ParsedCommand;
use crate::tasks::task_scheduler_client::TaskSchedulerClient;
use crate::tasks::VerbCall;
use crate::vm::activation::Activation;
Expand Down
2 changes: 1 addition & 1 deletion crates/kernel/src/vm/vm_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use moor_values::{v_int, Var};
use moor_values::{List, Objid};

use crate::builtins::{BfCallState, BfErr, BfRet};
use crate::tasks::command_parse::ParsedCommand;
use crate::tasks::sessions::Session;
use crate::tasks::VerbCall;
use crate::vm::activation::{Activation, Frame};
use crate::vm::vm_unwind::FinallyReason;
use crate::vm::{ExecutionResult, Fork};
use crate::vm::{VMExecState, VmExecParams};
use moor_values::matching::command_parse::ParsedCommand;

pub(crate) fn args_literal(args: &[Var]) -> String {
args.iter()
Expand Down
2 changes: 2 additions & 0 deletions crates/values/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ pub use var::{
pub use var::{Error, Objid, Symbol, VarType};

mod encode;
pub mod matching;
pub mod model;
pub mod tasks;
pub mod util;

mod var;

/// When encoding or decoding types to/from data or network, this is a version tag put into headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use std::string::ToString;

use bincode::{Decode, Encode};

use moor_values::model::WorldStateError;
use moor_values::model::{PrepSpec, Preposition};
use moor_values::util;
use moor_values::Objid;
use moor_values::{v_str, Var};
use crate::model::WorldStateError;
use crate::model::{PrepSpec, Preposition};
use crate::util;
use crate::Objid;
use crate::{v_str, Var};

#[derive(Clone, Eq, PartialEq, Debug, Decode, Encode)]
pub struct ParsedCommand {
Expand Down Expand Up @@ -96,7 +96,6 @@ pub enum ParseCommandError {
PermissionDenied,
}

#[tracing::instrument(skip(command_environment))]
pub fn parse_command<M>(
input: &str,
command_environment: M,
Expand Down Expand Up @@ -186,10 +185,10 @@ where

#[cfg(test)]
mod tests {
use moor_values::model::Preposition;
use moor_values::util::parse_into_words;
use moor_values::v_str;
use moor_values::FAILED_MATCH;
use crate::model::Preposition;
use crate::util::parse_into_words;
use crate::v_str;
use crate::FAILED_MATCH;

use crate::matching::match_env::MatchEnvironmentParseMatcher;
use crate::matching::mock_matching_env::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
// this program. If not, see <https://www.gnu.org/licenses/>.
//

use moor_values::model::WorldStateError;
use moor_values::model::{ObjSet, ValSet};
use moor_values::Objid;
use moor_values::{AMBIGUOUS, FAILED_MATCH, NOTHING};

use crate::tasks::command_parse::ParseMatcher;
use crate::matching::command_parse::ParseMatcher;
use crate::model::WorldStateError;
use crate::model::{ObjSet, ValSet};
use crate::Objid;
use crate::{AMBIGUOUS, FAILED_MATCH, NOTHING};

// This is the interface that the matching code needs to be able to call into the world state.
// Separated out so can be more easily mocked.
Expand Down Expand Up @@ -148,16 +147,15 @@ impl<M: MatchEnvironment> ParseMatcher for MatchEnvironmentParseMatcher<M> {

#[cfg(test)]
mod tests {
use moor_values::Objid;
use moor_values::{FAILED_MATCH, NOTHING};

use crate::matching::command_parse::ParseMatcher;
use crate::matching::match_env::{
do_match_object_names, MatchData, MatchEnvironmentParseMatcher,
};
use crate::matching::mock_matching_env::{
setup_mock_environment, MOCK_PLAYER, MOCK_ROOM1, MOCK_THING1, MOCK_THING2,
};
use crate::tasks::command_parse::ParseMatcher;
use crate::Objid;
use crate::{FAILED_MATCH, NOTHING};

#[test]
fn test_match_object_names_fail() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

use std::collections::{HashMap, HashSet};

use moor_values::model::{ObjSet, ValSet};
use moor_values::model::{ObjectRef, WorldStateError};
use moor_values::Objid;
use moor_values::NOTHING;
use crate::model::{ObjSet, ValSet};
use crate::model::{ObjectRef, WorldStateError};
use crate::Objid;
use crate::NOTHING;

use crate::matching::match_env::MatchEnvironment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// this program. If not, see <https://www.gnu.org/licenses/>.
//

pub mod command_parse;
pub mod match_env;
#[doc(hidden)]
pub mod mock_matching_env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// this program. If not, see <https://www.gnu.org/licenses/>.
//

use moor_values::model::ObjSet;
use moor_values::model::WorldState;
use moor_values::model::WorldStateError;
use moor_values::Objid;
use crate::model::ObjSet;
use crate::model::WorldState;
use crate::model::WorldStateError;
use crate::Objid;

use crate::matching::match_env::MatchEnvironment;

Expand All @@ -25,6 +25,11 @@ pub struct WsMatchEnv<'a> {
pub(crate) perms: Objid,
}

impl<'a> WsMatchEnv<'a> {
pub fn new(ws: &'a dyn WorldState, perms: Objid) -> Self {
Self { ws, perms }
}
}
impl<'a> MatchEnvironment for WsMatchEnv<'a> {
fn obj_valid(&self, oid: Objid) -> Result<bool, WorldStateError> {
self.ws.valid(oid)
Expand Down

0 comments on commit a92d312

Please sign in to comment.