remove LocalCodePtr::IndexingBuf

This commit is contained in:
Mark Thom
2021-12-15 23:41:00 -07:00
parent b056d40eba
commit 6f9b6a29c4
8 changed files with 233 additions and 216 deletions

View File

@@ -1,53 +1,19 @@
use crate::clause_types::*;
use crate::instructions::*;
use crate::machine::MachineState;
use crate::machine::machine_indices::*;
// TODO: remove this, replace with just 'Code'.
#[derive(Debug)]
pub struct CodeRepo {
pub(super) code: Code,
}
impl CodeRepo {
#[inline]
pub(super) fn new() -> Self {
CodeRepo { code: Code::new() }
}
#[inline]
pub(super) fn lookup_local_instr<'a>(&'a self, p: LocalCodePtr) -> RefOrOwned<'a, Line> {
match p {
LocalCodePtr::Halt => {
// exit with the interrupt exit code.
std::process::exit(1);
}
LocalCodePtr::DirEntry(p) => RefOrOwned::Borrowed(&self.code[p as usize]),
LocalCodePtr::IndexingBuf(p, o, i) => match &self.code[p] {
&Line::IndexingCode(ref indexing_lines) => match &indexing_lines[o] {
&IndexingLine::IndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::IndexedChoice(indexed_choice_instrs[i]))
}
&IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::DynamicIndexedChoice(indexed_choice_instrs[i]))
}
_ => {
unreachable!()
}
},
_ => {
unreachable!()
}
},
}
}
pub(super) fn lookup_instr<'a>(
&'a self,
last_call: bool,
p: &CodePtr,
) -> Option<RefOrOwned<'a, Line>> {
pub(super) fn lookup_instr<'a>(&'a self, machine_st: &MachineState, p: &CodePtr) -> Option<RefOrOwned<'a, Line>> {
match p {
&CodePtr::Local(local) => {
return Some(self.lookup_local_instr(local));
return Some(self.lookup_local_instr(machine_st, local));
}
&CodePtr::REPL(..) => None,
&CodePtr::BuiltInClause(ref built_in, _) => {
@@ -55,33 +21,56 @@ impl CodeRepo {
ClauseType::BuiltIn(built_in.clone()),
built_in.arity(),
0,
last_call
machine_st.last_call
);
Some(RefOrOwned::Owned(call_clause))
}
&CodePtr::CallN(arity, _, last_call) => {
let call_clause = call_clause!(ClauseType::CallN, arity, 0, last_call);
Some(RefOrOwned::Owned(call_clause))
}
&CodePtr::VerifyAttrInterrupt(p) => Some(RefOrOwned::Borrowed(&self.code[p])),
}
}
pub(super) fn find_living_dynamic_else(
&self,
mut p: usize,
cc: usize,
) -> Option<(usize, usize)> {
#[inline]
pub(super) fn lookup_local_instr<'a>(&'a self, machine_st: &MachineState, p: LocalCodePtr) -> RefOrOwned<'a, Line> {
match p {
LocalCodePtr::Halt => {
// exit with the interrupt exit code.
std::process::exit(1);
}
LocalCodePtr::DirEntry(p) => match &self.code[p] {
&Line::IndexingCode(ref indexing_lines) => {
match &indexing_lines[machine_st.oip as usize] {
&IndexingLine::IndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::IndexedChoice(indexed_choice_instrs[machine_st.iip as usize]))
}
&IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::DynamicIndexedChoice(indexed_choice_instrs[machine_st.iip as usize]))
}
_ => {
RefOrOwned::Borrowed(&self.code[p as usize])
}
}
}
_ => RefOrOwned::Borrowed(&self.code[p as usize]),
}
}
}
}
impl MachineState {
pub(super) fn find_living_dynamic_else(&self, code: &Code, mut p: usize) -> Option<(usize, usize)> {
loop {
match &self.code[p] {
match &code[p] {
&Line::Choice(ChoiceInstruction::DynamicElse(
birth,
death,
NextOrFail::Next(i),
)) => {
if birth < cc && Death::Finite(cc) <= death {
if birth < self.cc && Death::Finite(self.cc) <= death {
return Some((p, i));
} else if i > 0 {
p += i;
@@ -94,7 +83,7 @@ impl CodeRepo {
death,
NextOrFail::Fail(_),
)) => {
if birth < cc && Death::Finite(cc) <= death {
if birth < self.cc && Death::Finite(self.cc) <= death {
return Some((p, 0));
} else {
return None;
@@ -105,7 +94,7 @@ impl CodeRepo {
death,
NextOrFail::Next(i),
)) => {
if birth < cc && Death::Finite(cc) <= death {
if birth < self.cc && Death::Finite(self.cc) <= death {
return Some((p, i));
} else if i > 0 {
p += i;
@@ -118,7 +107,7 @@ impl CodeRepo {
death,
NextOrFail::Fail(_),
)) => {
if birth < cc && Death::Finite(cc) <= death {
if birth < self.cc && Death::Finite(self.cc) <= death {
return Some((p, 0));
} else {
return None;
@@ -134,18 +123,11 @@ impl CodeRepo {
}
}
pub(super) fn find_living_dynamic(
&self,
p: LocalCodePtr,
cc: usize,
) -> Option<(usize, usize, usize, bool)> {
let (p, oi, mut ii) = match p {
LocalCodePtr::IndexingBuf(p, oi, ii) => (p, oi, ii),
_ => unreachable!(),
};
pub(super) fn find_living_dynamic(&self, code: &Code, oi: u32, mut ii: u32) -> Option<(usize, u32, u32, bool)> {
let p = self.p.local().abs_loc();
let indexed_choice_instrs = match &self.code[p] {
Line::IndexingCode(ref indexing_code) => match &indexing_code[oi] {
let indexed_choice_instrs = match &code[p] {
Line::IndexingCode(ref indexing_code) => match &indexing_code[oi as usize] {
IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => {
indexed_choice_instrs
}
@@ -155,14 +137,14 @@ impl CodeRepo {
};
loop {
match &indexed_choice_instrs.get(ii) {
Some(&offset) => match &self.code[p + offset - 1] {
match &indexed_choice_instrs.get(ii as usize) {
Some(&offset) => match &code[p + offset - 1] {
&Line::Choice(ChoiceInstruction::DynamicInternalElse(
birth,
death,
next_or_fail,
)) => {
if birth < cc && Death::Finite(cc) <= death {
if birth < self.cc && Death::Finite(self.cc) <= death {
return Some((offset, oi, ii, next_or_fail.is_next()));
} else {
ii += 1;
@@ -175,3 +157,10 @@ impl CodeRepo {
}
}
}
impl CodeRepo {
#[inline]
pub(super) fn new() -> Self {
CodeRepo { code: Code::new() }
}
}