Skip to content

Commit 152b03e

Browse files
committed
ci: add ci
Signed-off-by: Sn0rt <[email protected]>
1 parent 0b4ea00 commit 152b03e

File tree

5 files changed

+91
-59
lines changed

5 files changed

+91
-59
lines changed

.github/workflows/test.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: rust test
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
push:
7+
branches: [ main ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
test:
14+
name: Test
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Install Rust toolchain
20+
uses: actions-rs/toolchain@v1
21+
with:
22+
profile: minimal
23+
toolchain: stable
24+
override: true
25+
26+
- name: Cache dependencies
27+
uses: actions/cache@v3
28+
with:
29+
path: |
30+
~/.cargo/registry
31+
~/.cargo/git
32+
target
33+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
34+
35+
- name: Run tests
36+
uses: actions-rs/cargo@v1
37+
with:
38+
command: test
39+
args: --verbose
40+
41+
- name: Run clippy
42+
uses: actions-rs/cargo@v1
43+
with:
44+
command: clippy
45+
args: -- -D warnings

Cargo.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lambda_parse.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ fn parse_atomic_expression(tokens: &mut Peekable<IntoIter<String>>) -> Result<La
232232
"multiply" => Ok(parse_lambda("λm. λn. λf. m (n f)").unwrap()),
233233

234234
// logic operations
235-
"and" => parse_binary_op(tokens, |a, b| LambdaExpression::And(a, b)),
236-
"or" => parse_binary_op(tokens, |a, b| LambdaExpression::Or(a, b)),
235+
"and" => parse_binary_op(tokens, LambdaExpression::And),
236+
"or" => parse_binary_op(tokens, LambdaExpression::Or),
237237
"not" => parse_unary_op(tokens, LambdaExpression::Not),
238238

239239
// Y
@@ -251,7 +251,7 @@ fn parse_atomic_expression(tokens: &mut Peekable<IntoIter<String>>) -> Result<La
251251
let expr = parse_atomic_expression(tokens)?;
252252
Ok(LambdaExpression::IsZero(Rc::new(expr)))
253253
},
254-
"*" => parse_binary_op(tokens, |a, b| LambdaExpression::Multiply(a, b)),
254+
"*" => parse_binary_op(tokens, LambdaExpression::Multiply),
255255

256256
// branch
257257
"ifthenelse" => parse_ifthenelse(tokens, LambdaExpression::IfThenElse),
@@ -301,7 +301,7 @@ where
301301

302302
fn parse_abstraction(tokens: &mut Peekable<IntoIter<String>>) -> Result<LambdaExpression, ParseError> {
303303
let mut parameters = Vec::new();
304-
while let Some(token) = tokens.next() {
304+
for token in tokens.by_ref() {
305305
if token == "." {
306306
break;
307307
}

src/lambda_vm.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::lambda_parse::{parse_lambda, LambdaExpression};
22
use std::rc::Rc;
33
use uuid::Uuid;
4-
use log::{debug, trace, info};
4+
use log::{debug, trace};
55
use colored::*;
66

77
pub struct VM {
@@ -83,7 +83,7 @@ impl VM {
8383
debug!("{}IsZero: {}", " ".repeat(depth), expr.to_string());
8484
let eval_inner = self.eval_recursive(inner, depth + 1);
8585
match &*eval_inner {
86-
LambdaExpression::Abstraction { parameter: f, body } => {
86+
LambdaExpression::Abstraction { parameter: _f, body } => {
8787
match &**body {
8888
LambdaExpression::Abstraction { parameter: x, body: inner_body } => {
8989
if **inner_body == LambdaExpression::Variable(x.clone()) {
@@ -157,7 +157,7 @@ impl VM {
157157
let eval_left = self.eval_recursive(left, depth + 1);
158158
let eval_right = self.eval_recursive(right, depth + 1);
159159
match (&*eval_left, &*eval_right) {
160-
(LambdaExpression::Number(n1), LambdaExpression::Number(n2)) => {
160+
(LambdaExpression::Number(_n1), LambdaExpression::Number(_n2)) => {
161161
// Implement multiplication for your number representation
162162
// This is a placeholder and needs to be implemented correctly
163163
Rc::new(LambdaExpression::Number(Rc::new(LambdaExpression::Number(
@@ -240,12 +240,6 @@ impl VM {
240240
debug!("{}YCombinator: {}", " ".repeat(depth), expr.to_string());
241241
self.eval_y_combinator(f, depth)
242242
}
243-
244-
// show error and not support yet
245-
_ => {
246-
debug!("{}Unsupported expression: {}", " ".repeat(depth), expr.to_string());
247-
Rc::new(expr.clone())
248-
}
249243
};
250244

251245
if *result == *expr {
@@ -281,7 +275,7 @@ impl VM {
281275
var: &str,
282276
replacement: &LambdaExpression,
283277
) -> LambdaExpression {
284-
let result = match expr {
278+
match expr {
285279
LambdaExpression::Variable(name) if name == var => replacement.clone(),
286280
LambdaExpression::Variable(_) => expr.clone(),
287281
LambdaExpression::Number(_) => expr.clone(),
@@ -350,8 +344,7 @@ impl VM {
350344
LambdaExpression::YCombinator(inner) => {
351345
LambdaExpression::YCombinator(Rc::new(self.substitute(inner, var, replacement)))
352346
}
353-
};
354-
result
347+
}
355348
}
356349

357350
fn alpha_convert(&self, param: &str, body: &LambdaExpression) -> (String, LambdaExpression) {
@@ -360,6 +353,7 @@ impl VM {
360353
(new_param, new_body)
361354
}
362355

356+
#[allow(clippy::only_used_in_recursion)]
363357
fn occurs_free(&self, var: &str, expr: &LambdaExpression) -> bool {
364358
match expr {
365359
LambdaExpression::Variable(name) => name == var,
@@ -402,6 +396,12 @@ impl VM {
402396
}
403397
}
404398

399+
impl Default for VM {
400+
fn default() -> Self {
401+
Self::new()
402+
}
403+
}
404+
405405
pub fn church_encode(n: u64) -> LambdaExpression {
406406
let body = (0..n).fold(LambdaExpression::Variable("x".to_string()), |acc, _| {
407407
LambdaExpression::Application {
@@ -421,18 +421,17 @@ pub fn church_encode(n: u64) -> LambdaExpression {
421421
pub fn church_decode(expr: &LambdaExpression) -> Result<u64, String> {
422422
fn count_applications(expr: &LambdaExpression) -> u64 {
423423
match expr {
424-
LambdaExpression::Application { function, argument } => {
424+
LambdaExpression::Application { function: _, argument } => {
425425
1 + count_applications(argument)
426426
}
427-
LambdaExpression::Variable(_) => 0,
428427
_ => 0,
429428
}
430429
}
431430

432431
match expr {
433-
LambdaExpression::Abstraction { parameter: f, body } => match &**body {
432+
LambdaExpression::Abstraction { parameter: _f, body } => match &**body {
434433
LambdaExpression::Abstraction {
435-
parameter: x,
434+
parameter: _x,
436435
body: inner_body,
437436
} => Ok(count_applications(inner_body)),
438437
_ => Err(format!(
@@ -478,28 +477,16 @@ mod tests {
478477
use super::*;
479478
use crate::lambda_parse::parse_lambda;
480479

480+
// check expr === value return boolean
481481
fn is_church_numeral(expr: &LambdaExpression, value: u64) -> bool {
482482
match expr {
483-
LambdaExpression::Abstraction { parameter: f, body } => match &**body {
484-
LambdaExpression::Abstraction {
485-
parameter: x,
486-
body: inner_body,
487-
} => {
488-
let mut current = inner_body;
489-
let mut count = 0;
490-
while let LambdaExpression::Application { function, argument } = &**current {
491-
if !matches!(&**function, LambdaExpression::Variable(name) if name == f) {
492-
return false;
493-
}
494-
current = argument;
495-
count += 1;
496-
}
497-
matches!(&**current, LambdaExpression::Variable(name) if name == x)
498-
&& count == value
499-
}
500-
_ => false,
483+
LambdaExpression::Abstraction { parameter: _f, body } => match &**body {
484+
LambdaExpression::Abstraction { parameter: _x, body: _inner_body } => {
485+
church_decode(expr).map_or(false, |n| n == value)
486+
},
487+
_ => false
501488
},
502-
_ => false,
489+
_ => false
503490
}
504491
}
505492

src/repl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::lambda_parse::{parse_lambda, LambdaExpression};
22
use crate::lambda_vm::{VM, church_decode};
33
use rustyline::error::ReadlineError;
44
use rustyline::Editor;
5-
use log::{info, error, debug, trace, LevelFilter};
5+
use log::{debug, LevelFilter};
66
use colored::*;
77
use std::rc::Rc;
88

@@ -16,7 +16,7 @@ fn print_help() {
1616
println!("║ {:<20} - Clear the screen ║", ":clear".yellow());
1717
println!("║ {:<20} - Show the last result ║", ":last".yellow());
1818
println!("║ {:<20} - Set log level ║", ":log <level>".yellow());
19-
println!("{}", "║ (error, warn, info, debug, trace) ║");
19+
println!("║ (error, warn, info, debug, trace) ║");
2020
println!("{}", "╠═══════════════════════════════════════════════════╣".cyan());
2121
println!("{}", "║ Enter a lambda calculus expression to evaluate it ║".cyan());
2222
println!("{}", "╚═══════════════════════════════════════════════════╝".cyan());

0 commit comments

Comments
 (0)