use an enum for OpDesc spec

This commit is contained in:
Bennet Bleßmann
2024-07-31 23:48:27 +02:00
parent 2814d79b56
commit 409f56ea37
9 changed files with 216 additions and 148 deletions

View File

@@ -1419,11 +1419,11 @@ mod tests {
let mut wam = MachineState::new();
let mut op_dir = default_op_dir();
op_dir.insert((atom!("+"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("-"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("-"), Fixity::Pre), OpDesc::build_with(200, FY as u8));
op_dir.insert((atom!("*"), Fixity::In), OpDesc::build_with(400, YFX as u8));
op_dir.insert((atom!("/"), Fixity::In), OpDesc::build_with(400, YFX as u8));
op_dir.insert((atom!("+"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("-"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("-"), Fixity::Pre), OpDesc::build_with(200, FY));
op_dir.insert((atom!("*"), Fixity::In), OpDesc::build_with(400, YFX));
op_dir.insert((atom!("/"), Fixity::In), OpDesc::build_with(400, YFX));
let term_write_result =
parse_and_write_parsed_term_to_heap(&mut wam, "3 + 4 - 1 + 2.", &op_dir).unwrap();

View File

@@ -555,10 +555,8 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
}
}
ModuleExport::OpDecl(op_decl) => {
let op_dir_value_opt = op_dir.swap_remove(&(
op_decl.name,
fixity(op_decl.op_desc.get_spec() as u32),
));
let op_dir_value_opt = op_dir
.swap_remove(&(op_decl.name, op_decl.op_desc.get_spec().fixity()));
if let Some(op_desc) = op_dir_value_opt {
retraction_info.push_record(op_retractor(*op_decl, op_desc));

View File

@@ -264,11 +264,11 @@ mod tests {
let mut wam = MachineState::new();
let mut op_dir = default_op_dir();
op_dir.insert((atom!("+"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("-"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("*"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("/"), Fixity::In), OpDesc::build_with(400, YFX as u8));
op_dir.insert((atom!("="), Fixity::In), OpDesc::build_with(700, XFX as u8));
op_dir.insert((atom!("+"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("-"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("*"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("/"), Fixity::In), OpDesc::build_with(400, YFX));
op_dir.insert((atom!("="), Fixity::In), OpDesc::build_with(700, XFX));
{
parse_and_write_parsed_term_to_heap(&mut wam, "f(X,X).", &op_dir).unwrap();
@@ -485,10 +485,10 @@ mod tests {
let mut wam = MachineState::new();
let mut op_dir = default_op_dir();
op_dir.insert((atom!("+"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("-"), Fixity::In), OpDesc::build_with(500, YFX as u8));
op_dir.insert((atom!("*"), Fixity::In), OpDesc::build_with(400, YFX as u8));
op_dir.insert((atom!("/"), Fixity::In), OpDesc::build_with(400, YFX as u8));
op_dir.insert((atom!("+"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("-"), Fixity::In), OpDesc::build_with(500, YFX));
op_dir.insert((atom!("*"), Fixity::In), OpDesc::build_with(400, YFX));
op_dir.insert((atom!("/"), Fixity::In), OpDesc::build_with(400, YFX));
{
parse_and_write_parsed_term_to_heap(&mut wam, "f(X,X).", &op_dir).unwrap();

View File

@@ -11,18 +11,12 @@ use indexmap::IndexSet;
use std::cell::Cell;
use std::convert::TryFrom;
pub(crate) fn to_op_decl(prec: u16, spec: OpDeclSpec, name: Atom) -> OpDecl {
OpDecl::new(OpDesc::build_with(prec, spec), name)
}
pub(crate) fn to_op_decl(prec: u16, spec: Atom, name: Atom) -> Result<OpDecl, CompilationError> {
match spec {
atom!("xfx") => Ok(OpDecl::new(OpDesc::build_with(prec, XFX as u8), name)),
atom!("xfy") => Ok(OpDecl::new(OpDesc::build_with(prec, XFY as u8), name)),
atom!("yfx") => Ok(OpDecl::new(OpDesc::build_with(prec, YFX as u8), name)),
atom!("fx") => Ok(OpDecl::new(OpDesc::build_with(prec, FX as u8), name)),
atom!("fy") => Ok(OpDecl::new(OpDesc::build_with(prec, FY as u8), name)),
atom!("xf") => Ok(OpDecl::new(OpDesc::build_with(prec, XF as u8), name)),
atom!("yf") => Ok(OpDecl::new(OpDesc::build_with(prec, YF as u8), name)),
_ => Err(CompilationError::InconsistentEntry),
}
pub(crate) fn to_op_decl_spec(spec: Atom) -> Result<OpDeclSpec, CompilationError> {
OpDeclSpec::try_from(spec).map_err(|_err| CompilationError::InconsistentEntry)
}
fn setup_op_decl(mut terms: Vec<Term>, atom_tbl: &AtomTable) -> Result<OpDecl, CompilationError> {
@@ -38,6 +32,8 @@ fn setup_op_decl(mut terms: Vec<Term>, atom_tbl: &AtomTable) -> Result<OpDecl, C
_ => return Err(CompilationError::InconsistentEntry),
};
let spec = to_op_decl_spec(spec)?;
let prec = match terms.pop().unwrap() {
Term::Literal(_, Literal::Fixnum(bi)) => match u16::try_from(bi.get_num()) {
Ok(n) if n <= 1200 => n,
@@ -46,7 +42,7 @@ fn setup_op_decl(mut terms: Vec<Term>, atom_tbl: &AtomTable) -> Result<OpDecl, C
_ => return Err(CompilationError::InconsistentEntry),
};
to_op_decl(prec, spec, name)
Ok(to_op_decl(prec, spec, name))
}
fn setup_predicate_indicator(term: &mut Term) -> Result<PredicateKey, CompilationError> {

View File

@@ -24,7 +24,6 @@ use crate::machine::machine_errors::*;
use crate::machine::machine_indices::*;
use crate::machine::machine_state::*;
use crate::machine::partial_string::*;
use crate::machine::preprocessor::to_op_decl;
use crate::machine::stack::*;
use crate::machine::streams::*;
use crate::machine::{get_structure_index, Machine, VERIFY_ATTR_INTERRUPT_LOC};
@@ -102,6 +101,8 @@ use warp::hyper::{HeaderMap, Method};
use warp::{Buf, Filter};
use super::libraries;
use super::preprocessor::to_op_decl;
use super::preprocessor::to_op_decl_spec;
#[cfg(feature = "repl")]
pub(crate) fn get_key() -> KeyEvent {
@@ -3987,19 +3988,6 @@ impl Machine {
pub(crate) fn get_next_op_db_ref(&mut self) {
let prec = self.deref_register(1);
fn get_spec(op_spec: u8) -> Atom {
match op_spec as u32 {
XFX => atom!("xfx"),
XFY => atom!("xfy"),
YFX => atom!("yfx"),
FX => atom!("fx"),
FY => atom!("fy"),
XF => atom!("xf"),
YF => atom!("yf"),
_ => unreachable!(),
}
}
let h = self.machine_st.heap.len();
fn write_op_functors_to_heap(
@@ -4016,7 +4004,7 @@ impl Machine {
continue;
}
let spec_atom = get_spec(op_desc.get_spec());
let spec_atom = op_desc.get_spec().get_spec();
heap.extend(functor!(
atom!("op"),
@@ -4038,17 +4026,14 @@ impl Machine {
let orig_op = self.deref_register(3);
let spec_num = if spec.get_tag() == HeapCellValueTag::Atom {
(match cell_as_atom!(spec) {
atom!("xfx") => XFX,
atom!("xfy") => XFY,
atom!("yfx") => YFX,
atom!("fx") => FX,
atom!("fy") => FY,
atom!("xf") => XF,
_ => unreachable!(),
}) as u8
Some(
OpDeclSpec::try_from(cell_as_atom!(spec))
.ok()
.filter(|spec| matches!(spec, XFX | XFY | YFX | FX | FY | XF))
.expect("we should only get valid values != YF here"),
)
} else {
0
None
};
let num_functors = if !orig_op.is_var() {
@@ -4111,7 +4096,7 @@ impl Machine {
}
if (!orig_op.is_var() && atom_as_cell!(name) != orig_op)
|| (!spec.is_var() && other_spec != spec_num)
|| (!spec.is_var() && Some(other_spec) != spec_num)
{
return None;
}
@@ -5051,8 +5036,9 @@ impl Machine {
}
);
let result = to_op_decl(priority, specifier, op)
let result = to_op_decl_spec(specifier)
.map_err(SessionError::from)
.map(|specifier| to_op_decl(priority, specifier, op))
.and_then(|mut op_decl| {
if op_decl.op_desc.get_prec() == 0 {
op_decl.remove(&mut self.indices.op_dir);