Skip to content

Commit

Permalink
Mover wrappers to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Dec 29, 2024
1 parent 112eefb commit e11d35b
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 173 deletions.
174 changes: 1 addition & 173 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,179 +33,7 @@ mod rules;
mod segment;
mod tape;
mod tree;

/**************************************/

mod wrappers {
use pyo3::{pyclass, pyfunction, pymethods};

use crate::{
blocks::opt_block,
graph::is_connected,
instrs::{CompProg, Params, Parse as _, State},
machine::quick_term_or_rec,
reason::{
cant_blank, cant_halt, cant_spin_out,
BackwardResult as BackwardResultRs, BackwardResult::*,
Depth, Step,
},
segment::{
segment_cant_blank, segment_cant_halt,
segment_cant_spin_out,
},
};

#[expect(non_camel_case_types)]
#[pyclass]
pub enum BackwardResult {
refuted { step: Step },
init {},
linrec {},
spinout {},
step_limit {},
depth_limit {},
}

use BackwardResult::*;

#[pymethods]
impl BackwardResult {
const fn is_refuted(&self) -> bool {
matches!(self, Self::refuted { .. })
}

const fn is_settled(&self) -> bool {
matches!(self, Self::refuted { .. } | init {})
}

const fn __str__(&self) -> &str {
match self {
refuted { .. } => "refuted",
init {} => "init",
linrec {} => "linrec",
spinout {} => "spinout",
step_limit {} => "step_limit",
depth_limit {} => "depth_limit",
}
}
}

impl From<BackwardResultRs> for BackwardResult {
fn from(result: BackwardResultRs) -> Self {
match result {
Refuted(step) => Self::refuted { step },
Init => Self::init {},
LinRec => Self::linrec {},
Spinout => Self::spinout {},
StepLimit => Self::step_limit {},
DepthLimit => Self::depth_limit {},
}
}
}

#[pyfunction]
pub fn py_cant_halt(prog: &str, depth: Depth) -> BackwardResult {
cant_halt(&CompProg::from_str(prog), depth).into()
}

#[pyfunction]
pub fn py_cant_blank(prog: &str, depth: Depth) -> BackwardResult {
cant_blank(&CompProg::from_str(prog), depth).into()
}

#[pyfunction]
pub fn py_cant_spin_out(
prog: &str,
depth: Depth,
) -> BackwardResult {
cant_spin_out(&CompProg::from_str(prog), depth).into()
}

#[pyfunction]
pub fn py_is_connected(prog: &str, states: State) -> bool {
is_connected(&CompProg::from_str(prog), states)
}

#[pyfunction]
pub fn py_opt_block(prog: &str, steps: usize) -> usize {
opt_block(&CompProg::from_str(prog), steps)
}

#[pyfunction]
pub fn py_quick_term_or_rec(prog: &str, sim_lim: usize) -> bool {
quick_term_or_rec(&CompProg::from_str(prog), sim_lim, false)
}

#[pyfunction]
pub fn py_segment_cant_halt(
prog: &str,
segs: usize,
) -> Option<usize> {
let prog = &CompProg::from_str(prog);

let (states, colors) = prog
.keys()
.fold((0, 0), |acc, &(a, b)| (acc.0.max(a), acc.1.max(b)));

let params = (1 + states, 1 + colors);

segment_cant_halt(prog, params, segs)
}

#[pyfunction]
pub fn py_segment_cant_blank(
prog: &str,
segs: usize,
) -> Option<usize> {
let prog = &CompProg::from_str(prog);

let (states, colors) = prog
.keys()
.fold((0, 0), |acc, &(a, b)| (acc.0.max(a), acc.1.max(b)));

let params = (1 + states, 1 + colors);

segment_cant_blank(prog, params, segs)
}

#[pyfunction]
pub fn py_segment_cant_spin_out(
prog: &str,
segs: usize,
) -> Option<usize> {
let prog = &CompProg::from_str(prog);

let (states, colors) = prog
.keys()
.fold((0, 0), |acc, &(a, b)| (acc.0.max(a), acc.1.max(b)));

let params = (1 + states, 1 + colors);

segment_cant_spin_out(prog, params, segs)
}

#[pyfunction]
#[pyo3(signature = (comp, params=None))]
#[expect(clippy::needless_pass_by_value)]
pub fn py_show_comp(
comp: CompProg,
params: Option<Params>,
) -> String {
show_comp(&comp, params)
}

#[pyfunction]
pub fn tcompile(prog: &str) -> CompProg {
CompProg::from_str(prog)
}

pub fn show_comp(
comp: &CompProg,
params: Option<Params>,
) -> String {
comp.show(params)
}
}
mod wrappers;

/**************************************/

Expand Down
158 changes: 158 additions & 0 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use pyo3::{pyclass, pyfunction, pymethods};

use crate::{
blocks::opt_block,
graph::is_connected,
instrs::{CompProg, Params, Parse as _, State},
machine::quick_term_or_rec,
reason::{
cant_blank, cant_halt, cant_spin_out,
BackwardResult as BackwardResultRs, BackwardResult::*, Depth,
Step,
},
segment::{
segment_cant_blank, segment_cant_halt, segment_cant_spin_out,
},
};

/***************************************/

#[pyfunction]
pub fn py_is_connected(prog: &str, states: State) -> bool {
is_connected(&CompProg::from_str(prog), states)
}

#[pyfunction]
pub fn py_opt_block(prog: &str, steps: usize) -> usize {
opt_block(&CompProg::from_str(prog), steps)
}

#[pyfunction]
pub fn py_quick_term_or_rec(prog: &str, sim_lim: usize) -> bool {
quick_term_or_rec(&CompProg::from_str(prog), sim_lim, false)
}

#[pyfunction]
#[pyo3(signature = (comp, params=None))]
#[expect(clippy::needless_pass_by_value)]
pub fn py_show_comp(comp: CompProg, params: Option<Params>) -> String {
show_comp(&comp, params)
}

#[pyfunction]
pub fn tcompile(prog: &str) -> CompProg {
CompProg::from_str(prog)
}

pub fn show_comp(comp: &CompProg, params: Option<Params>) -> String {
comp.show(params)
}

/***************************************/

#[expect(non_camel_case_types)]
#[pyclass]
pub enum BackwardResult {
refuted { step: Step },
init {},
linrec {},
spinout {},
step_limit {},
depth_limit {},
}

use BackwardResult::*;

#[pymethods]
impl BackwardResult {
const fn is_refuted(&self) -> bool {
matches!(self, Self::refuted { .. })
}

const fn is_settled(&self) -> bool {
matches!(self, Self::refuted { .. } | init {})
}

const fn __str__(&self) -> &str {
match self {
refuted { .. } => "refuted",
init {} => "init",
linrec {} => "linrec",
spinout {} => "spinout",
step_limit {} => "step_limit",
depth_limit {} => "depth_limit",
}
}
}

impl From<BackwardResultRs> for BackwardResult {
fn from(result: BackwardResultRs) -> Self {
match result {
Refuted(step) => Self::refuted { step },
Init => Self::init {},
LinRec => Self::linrec {},
Spinout => Self::spinout {},
StepLimit => Self::step_limit {},
DepthLimit => Self::depth_limit {},
}
}
}

#[pyfunction]
pub fn py_cant_halt(prog: &str, depth: Depth) -> BackwardResult {
cant_halt(&CompProg::from_str(prog), depth).into()
}

#[pyfunction]
pub fn py_cant_blank(prog: &str, depth: Depth) -> BackwardResult {
cant_blank(&CompProg::from_str(prog), depth).into()
}

#[pyfunction]
pub fn py_cant_spin_out(prog: &str, depth: Depth) -> BackwardResult {
cant_spin_out(&CompProg::from_str(prog), depth).into()
}

/***************************************/

#[pyfunction]
pub fn py_segment_cant_halt(prog: &str, segs: usize) -> Option<usize> {
let prog = &CompProg::from_str(prog);

let (states, colors) = prog
.keys()
.fold((0, 0), |acc, &(a, b)| (acc.0.max(a), acc.1.max(b)));

let params = (1 + states, 1 + colors);

segment_cant_halt(prog, params, segs)
}

#[pyfunction]
pub fn py_segment_cant_blank(prog: &str, segs: usize) -> Option<usize> {
let prog = &CompProg::from_str(prog);

let (states, colors) = prog
.keys()
.fold((0, 0), |acc, &(a, b)| (acc.0.max(a), acc.1.max(b)));

let params = (1 + states, 1 + colors);

segment_cant_blank(prog, params, segs)
}

#[pyfunction]
pub fn py_segment_cant_spin_out(
prog: &str,
segs: usize,
) -> Option<usize> {
let prog = &CompProg::from_str(prog);

let (states, colors) = prog
.keys()
.fold((0, 0), |acc, &(a, b)| (acc.0.max(a), acc.1.max(b)));

let params = (1 + states, 1 + colors);

segment_cant_spin_out(prog, params, segs)
}

0 comments on commit e11d35b

Please sign in to comment.