Skip to content

Commit a937a74

Browse files
authored
Merge pull request #10 from Jules-Bertholet/clone-war
Some more refactorings
2 parents 9be5d80 + 243daff commit a937a74

17 files changed

+846
-1038
lines changed

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ clap = { version = "4.4.8", features = ["derive"] }
2424
assert_cmd = "2.0.12"
2525
similar-asserts = "1.5.0"
2626
tempdir = "0.3.7"
27+
28+
[lints.rust]
29+
rust_2018_idioms = "warn"
30+
31+
[lints.clippy]
32+
semicolon_if_nothing_returned = "warn"

src/bin/bin.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct Args {
4040
additional: Vec<String>,
4141
}
4242

43-
pub fn interpret(args: Args, compiled_data: CompiledData) {
43+
pub fn interpret(args: Args, compiled_data: &CompiledData) {
4444
let Args {
4545
geom: _,
4646
file1,
@@ -57,7 +57,7 @@ pub fn interpret(args: Args, compiled_data: CompiledData) {
5757

5858
let read = compiled_data.interpret(read, &out1, &out2, &additional);
5959

60-
read.run_with_threads(threads)
60+
read.run_with_threads(threads);
6161
}
6262

6363
fn main() {
@@ -67,19 +67,19 @@ fn main() {
6767

6868
let (tokens, mut errs) = lexer::lexer().parse_recovery(&*geom);
6969

70-
let parse_errs = if let Some(tokens) = &tokens {
70+
let parse_errs = if let Some(tokens) = tokens {
7171
let (ast, parse_errs) = parser().parse_recovery(Stream::from_iter(
7272
tokens.len()..tokens.len() + 1,
73-
tokens.clone().into_iter(),
73+
tokens.into_iter(),
7474
));
7575

76-
if let Some((ast, _)) = &ast {
77-
let res = compile(ast.clone());
76+
if let Some(ast) = ast {
77+
let res = compile(ast);
7878

7979
if let Err(e) = res {
8080
errs.push(Simple::custom(e.span, e.msg));
8181
} else {
82-
interpret(args, res.ok().unwrap());
82+
interpret(args, &res.ok().unwrap());
8383
}
8484
};
8585

@@ -157,5 +157,5 @@ fn main() {
157157
};
158158

159159
report.finish().print(Source::from(&geom)).unwrap();
160-
})
160+
});
161161
}

src/geometry/compile/definitions.rs

+45-59
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
use super::{
2-
functions::{compile_fn, CompiledFunction},
3-
utils::*,
4-
};
5-
6-
use std::{collections::HashMap, ops::Deref};
1+
use std::collections::HashMap;
72

8-
use crate::parser::{Expr, Spanned};
3+
use crate::{
4+
compile::{
5+
functions::{compile_fn, CompiledFunction},
6+
utils::*,
7+
},
8+
parser::{Definition, Expr},
9+
S,
10+
};
911

10-
// validate definitions there should be no labels, just labeled geom peices and functions
11-
fn validate_definition(expr: Spanned<Expr>, label: &str) -> Result<GeometryMeta, Error> {
12-
let mut stack: Vec<Spanned<CompiledFunction>> = vec![];
13-
let mut expr = expr;
12+
/// validate definitions there should be no labels, just labeled geom peices and functions
13+
fn validate_definition(mut expr: S<Expr>, label: &str) -> Result<GeometryMeta, Error> {
14+
let mut stack: Vec<S<CompiledFunction>> = vec![];
1415

1516
loop {
1617
match expr.0 {
1718
Expr::Function(fn_, gp) => {
1819
// parse the function and validate it
19-
expr = gp.deref().clone();
20+
expr = gp.unboxed();
2021
stack.push(compile_fn(fn_, expr.clone())?); // here is where we can compile the functions
2122
}
2223
Expr::Label(_) => {
@@ -29,8 +30,8 @@ fn validate_definition(expr: Spanned<Expr>, label: &str) -> Result<GeometryMeta,
2930
}
3031
}
3132

32-
let gp = if let (Expr::GeomPiece(type_, size), span) = expr {
33-
(
33+
let gp = if let S(Expr::GeomPiece(type_, size), span) = expr {
34+
S(
3435
GeometryPiece {
3536
type_,
3637
size,
@@ -44,56 +45,41 @@ fn validate_definition(expr: Spanned<Expr>, label: &str) -> Result<GeometryMeta,
4445

4546
let gp = GeometryMeta { expr: gp, stack };
4647

47-
validate_expr(gp)
48+
gp.validate_expr().map(|()| gp)
4849
}
4950

50-
pub fn compile_definitions(expr: Spanned<Expr>) -> Result<HashMap<String, GeometryMeta>, Error> {
51-
let (expr, expr_span) = expr;
51+
pub fn compile_definitions(
52+
S(defs, _): S<Vec<S<Definition>>>,
53+
) -> Result<HashMap<String, GeometryMeta>, Error> {
54+
let mut map = HashMap::new();
5255

53-
if let Expr::Definitions(defs) = expr {
54-
let mut map = HashMap::new();
56+
let mut err: Option<Error> = None;
5557

56-
let mut err: Option<Error> = None;
57-
58-
for (def, def_span) in defs {
59-
if let Expr::LabeledGeomPiece(label, expr) = def.clone() {
60-
let expr = expr.deref().clone();
61-
let label = label.deref().clone();
62-
63-
if let Expr::Label((l, span)) = label {
64-
let res = validate_definition(expr.clone(), &l);
65-
if let Err(e) = res {
66-
err = Some(e);
67-
break;
68-
} else if map.insert(l.clone(), res.ok().unwrap()).is_some() {
69-
err = Some(Error {
70-
// span labels
71-
span,
72-
msg: format!(
73-
"Repeated label in definition block: \"{}\" already defined",
74-
l
75-
),
76-
});
77-
break;
78-
}
79-
} else {
80-
err = Some(Error {
81-
span: def_span,
82-
msg: format!("Expected a Labeled Geometry piece, found: {}", def),
83-
})
84-
}
85-
}
86-
}
87-
88-
if let Some(e) = err {
89-
return Err(e);
58+
for S(
59+
Definition {
60+
label: S(label_str, label_span),
61+
expr,
62+
},
63+
_,
64+
) in defs
65+
{
66+
let res = validate_definition(expr, &label_str);
67+
if let Err(e) = res {
68+
err = Some(e);
69+
break;
70+
} else if map.insert(label_str.clone(), res.ok().unwrap()).is_some() {
71+
err = Some(Error {
72+
// span labels
73+
span: label_span,
74+
msg: format!("Repeated label in definition block: \"{label_str}\" already defined"),
75+
});
76+
break;
9077
}
78+
}
9179

92-
Ok(map)
93-
} else {
94-
Err(Error {
95-
span: expr_span,
96-
msg: format!("Expected a definition block, found: {}", expr),
97-
})
80+
if let Some(e) = err {
81+
return Err(e);
9882
}
83+
84+
Ok(map)
9985
}

src/geometry/compile/functions.rs

+27-30
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
//! expressions with label references a seperate
55
//! place for this to happen will be useful
66
7-
use std::ops::Deref;
8-
97
use crate::{
10-
parser::{Expr, Function, Spanned},
11-
Nucleotide,
8+
compile::utils::{Error, GeometryMeta, GeometryPiece},
9+
parser::{Expr, Function},
10+
Nucleotide, S,
1211
};
1312

14-
use super::utils::{validate_expr, Error, GeometryMeta, GeometryPiece};
15-
1613
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1714
pub enum CompiledFunction {
1815
Reverse,
@@ -27,18 +24,16 @@ pub enum CompiledFunction {
2724
PadTo(usize, Nucleotide),
2825
PadToLeft(usize, Nucleotide),
2926
Normalize,
30-
Map(String, Vec<Spanned<CompiledFunction>>),
31-
MapWithMismatch(String, Vec<Spanned<CompiledFunction>>, usize),
27+
Map(String, Vec<S<CompiledFunction>>),
28+
MapWithMismatch(String, Vec<S<CompiledFunction>>, usize),
3229
FilterWithinDist(String, usize),
3330
Hamming(usize),
3431
}
3532

3633
pub fn compile_fn(
37-
fn_: Spanned<Function>,
38-
parent_expr: Spanned<Expr>,
39-
) -> Result<Spanned<CompiledFunction>, Error> {
40-
let (fn_, span) = fn_;
41-
let (parent_expr, expr_span) = parent_expr;
34+
S(fn_, span): S<Function>,
35+
S(parent_expr, expr_span): S<Expr>,
36+
) -> Result<S<CompiledFunction>, Error> {
4237
let comp_fn = match fn_ {
4338
Function::Reverse => CompiledFunction::Reverse,
4439
Function::ReverseComp => CompiledFunction::ReverseComp,
@@ -54,35 +49,35 @@ pub fn compile_fn(
5449
Function::Normalize => CompiledFunction::Normalize,
5550
Function::MapWithMismatch(path, expr, mismatch) => CompiledFunction::MapWithMismatch(
5651
path,
57-
compile_inner_expr(expr.deref().clone(), (parent_expr, expr_span))?,
52+
compile_inner_expr(expr.unboxed(), S(parent_expr, expr_span))?,
5853
mismatch,
5954
),
6055
Function::Map(path, expr) => CompiledFunction::Map(
6156
path,
62-
compile_inner_expr(expr.deref().clone(), (parent_expr, expr_span))?,
57+
compile_inner_expr(expr.unboxed(), S(parent_expr, expr_span))?,
6358
),
6459
Function::FilterWithinDist(path, mismatch) => {
6560
CompiledFunction::FilterWithinDist(path, mismatch)
6661
}
6762
Function::Hamming(n) => CompiledFunction::Hamming(n),
6863
};
6964

70-
Ok((comp_fn, span))
65+
Ok(S(comp_fn, span))
7166
}
7267

7368
fn compile_inner_expr(
74-
mut expr: Spanned<Expr>,
75-
parent_expr: Spanned<Expr>,
76-
) -> Result<Vec<Spanned<CompiledFunction>>, Error> {
69+
mut expr: S<Expr>,
70+
parent_expr: S<Expr>,
71+
) -> Result<Vec<S<CompiledFunction>>, Error> {
7772
// if we are here in a map then the expr passed into the expr should be a geom_piece or labeled geom_piece
7873
// either way we can extract the size and type of it
79-
let mut stack: Vec<Spanned<CompiledFunction>> = Vec::new();
74+
let mut stack: Vec<S<CompiledFunction>> = Vec::new();
8075

8176
loop {
8277
match expr.0 {
8378
Expr::Self_ => break,
8479
Expr::Function(inner_fn, inner_expr) => {
85-
expr = inner_expr.deref().clone();
80+
expr = inner_expr.unboxed();
8681
let inner_fn = compile_fn(inner_fn.clone(), expr.clone());
8782
if inner_fn.is_ok() {
8883
stack.push(inner_fn.ok().unwrap());
@@ -103,11 +98,11 @@ fn compile_inner_expr(
10398
}
10499

105100
let geom_piece = {
106-
let (mut expr, span) = parent_expr;
101+
let S(mut expr, span) = parent_expr;
107102
loop {
108103
match expr {
109104
Expr::LabeledGeomPiece(_, b) => {
110-
let (gp, _) = b.deref();
105+
let S(gp, _) = b.unboxed();
111106
expr = gp.clone();
112107
}
113108
Expr::GeomPiece(_, _) => break,
@@ -125,16 +120,18 @@ fn compile_inner_expr(
125120
} else {
126121
return Err(Error {
127122
span,
128-
msg: format!("Expected geometry peice found: {}", expr),
123+
msg: format!("Expected geometry peice found: {expr}"),
129124
});
130125
}
131126
};
132127

133-
// check this and return esult from this function
134-
validate_expr(GeometryMeta {
135-
expr: (geom_piece, expr.1),
136-
stack: stack.clone(),
137-
})?;
128+
// check this and return result from this function
129+
let gm = GeometryMeta {
130+
expr: S(geom_piece, expr.1),
131+
stack,
132+
};
133+
134+
gm.validate_expr()?;
138135

139-
Ok(stack)
136+
Ok(gm.stack)
140137
}

0 commit comments

Comments
 (0)