Skip to content

Commit c599a52

Browse files
committed
amend tests; now support for alternative evaluation
1 parent 95ff17d commit c599a52

5 files changed

Lines changed: 222 additions & 88 deletions

File tree

src/compiler/mod.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) fn eval(state: TiState) -> Result<Vec<TiState>, EvalError> {
115115
res.push(temp.clone());
116116
step(&mut temp)?;
117117
do_admin(&mut temp);
118-
while !ti_final(&temp) {
118+
while !ti_final(&temp)? {
119119
res.push(temp.clone());
120120
step(&mut temp)?;
121121
do_admin(&mut temp);
@@ -126,9 +126,9 @@ pub(crate) fn eval(state: TiState) -> Result<Vec<TiState>, EvalError> {
126126

127127
fn step(state: &mut TiState) -> Result<(), EvalError> {
128128
let (stack, dump, heap, _, _) = state;
129-
println!("Stack: {:?}", stack);
130-
println!("Dump: {:?}", dump);
131-
println!("{:?}", heap);
129+
// println!("Stack: {:?}", stack);
130+
// println!("Dump: {:?}", dump);
131+
// println!("{:?}", heap);
132132

133133
let last_stack = *stack.last().ok_or(EvalError::EmptyStack)?;
134134

@@ -201,14 +201,14 @@ fn prim_step(state: &mut TiState, prim: Primitive) -> Result<(), EvalError> {
201201
let Node::Num(n) = arg1 else {
202202
stack.pop().ok_or(EvalError::EmptyStack)?;
203203
dump.push(stack.clone());
204-
*stack = args;
204+
*stack = vec![args[0]];
205205
return Ok(());
206206
};
207207
let arg2 = heap.lookup(args[1]).map_err(EvalError::Heap)?;
208208
let Node::Num(m) = arg2 else {
209209
stack.pop().ok_or(EvalError::EmptyStack)?;
210210
dump.push(stack.clone());
211-
*stack = args;
211+
*stack = vec![args[1]];
212212
return Ok(());
213213
};
214214
stack.pop().ok_or(EvalError::EmptyStack)?;
@@ -261,16 +261,19 @@ fn sc_step(
261261
Ok(())
262262
}
263263

264-
fn ti_final(state: &TiState) -> bool {
264+
fn ti_final(state: &TiState) -> Result<bool, EvalError> {
265265
let (stack, dump, heap, _, _) = state;
266-
dump.is_empty()
267-
&& match stack.len() {
268-
1 => heap
269-
.lookup(stack[0])
270-
.map(Node::is_data_node)
271-
.unwrap_or(false),
272-
0 => panic!("Empty stack!"),
273-
_ => false,
266+
if !dump.is_empty() {
267+
Ok(false)
268+
} else {
269+
match stack.len() {
270+
1 => heap
271+
.lookup(stack[0])
272+
.map(Node::is_data_node)
273+
.map_err(EvalError::Heap),
274+
0 => Err(EvalError::EmptyStack),
275+
_ => Ok(false),
276+
}
274277
}
275278
}
276279

src/core/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Heap<Node> {
118118
expr: CoreExpr,
119119
env: &mut ASSOC<Name, Addr>,
120120
) -> Result<Addr, HeapError> {
121-
println!("env: {:?}", env);
121+
// println!("env: {:?}", env);
122122
match expr {
123123
CoreExpr::Var(v) => env
124124
.iter()
@@ -150,7 +150,7 @@ impl Heap<Node> {
150150
root_addr: Addr,
151151
env: &mut ASSOC<Name, Addr>,
152152
) -> Result<(), HeapError> {
153-
println!("env: {:?}", env);
153+
// println!("env: {:?}", env);
154154
match body {
155155
CoreExpr::Var(a) => {
156156
let a_addr = env

src/lang/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub const PRELUDE_DEFS: &'static str = "I x = x; K x y = x; K1 x y = y; S f g x
5353

5454
type Token = (u32, String);
5555

56-
fn clex(input: String) -> Vec<Token> {
56+
pub(crate) fn clex(input: String) -> Vec<Token> {
5757
let mut tokens: Vec<Token> = Vec::new();
5858
let mut chars = input.chars();
5959
let mut c = chars.next();
@@ -125,7 +125,7 @@ fn clex(input: String) -> Vec<Token> {
125125

126126
const TWO_CHAR_OPS: [&'static str; 5] = ["==", "~=", ">=", "<=", "->"];
127127

128-
fn syntax(tokens: Vec<Token>) -> Result<Vec<CoreProgram>, SyntaxError> {
128+
pub(crate) fn syntax(tokens: Vec<Token>) -> Result<Vec<CoreProgram>, SyntaxError> {
129129
let ress = parser::program()(tokens)
130130
.into_iter()
131131
.filter(|(_, v)| v.is_empty())

src/main.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::compiler::{Node, ResultError, TiStats};
2+
use crate::lang::SyntaxError;
23
use std::path::PathBuf;
34

45
pub mod compiler;
@@ -23,9 +24,8 @@ fn run_file(p: PathBuf) -> Result<Vec<Result<(Vec<Node>, TiStats), ResultError>>
2324
.collect::<Vec<_>>())
2425
}
2526

26-
fn run(s: String) -> Result<Vec<Result<(Vec<Node>, TiStats), ResultError>>, ResultError> {
27-
let state = lang::parse_raw(s)
28-
.map_err(ResultError::Syntax)?
27+
fn run(s: String) -> Result<Vec<Result<(Vec<Node>, TiStats), ResultError>>, SyntaxError> {
28+
let state = lang::parse_raw(s)?
2929
.into_iter()
3030
.map(|p| compiler::compile(p).map_err(ResultError::Compile))
3131
.collect::<Vec<_>>();
@@ -40,10 +40,6 @@ fn run(s: String) -> Result<Vec<Result<(Vec<Node>, TiStats), ResultError>>, Resu
4040
}
4141

4242
fn main() {
43-
let program = String::from(
44-
r#"
45-
main = 4*5+(2-5)
46-
"#,
47-
);
43+
let program = String::from(r#"main = 4*5+(2-5)"#);
4844
println!("{:?}", run(program));
4945
}

0 commit comments

Comments
 (0)