error checking (it's a start anyway)

This commit is contained in:
psf 2022-03-27 01:05:23 -07:00
parent 72dae5a249
commit e204ae6da7

View file

@ -15,7 +15,7 @@ enum State {
enum Post {
Nothing,
EatWord,
EatLine,
WarmReset,
}
#[derive(Debug)]
@ -171,13 +171,13 @@ fn tick(c: &mut Core) {
}
None => {
println!(" ' cannot find {}", name);
c.post = Post::EatLine;
c.post = Post::WarmReset;
}
}
}
_ => {
println!(" ' needs an argument");
c.post = Post::EatLine;
c.post = Post::WarmReset;
}
}
}
@ -214,8 +214,14 @@ fn push(c: &mut Core, val: u16) {
}
fn pop(c: &mut Core) -> u16 {
c.tds -= 1;
return c.dstack[c.tds];
if c.tds == 0 {
println!(" stack underflow");
c.post = Post::WarmReset; // note: could get overwritten later :(
return 0; // half-assed, should really return straight to interpreter
} else {
c.tds -= 1;
return c.dstack[c.tds];
}
}
fn dup(c: &mut Core) {
@ -342,25 +348,25 @@ fn lit(c: &mut Core) {
fn add(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v1 + v2);
push(c, v1.wrapping_add(v2));
}
fn sub(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v2 - v1);
push(c, v2.wrapping_sub(v1));
}
fn mul(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v1 * v2);
push(c, v1.saturating_mul(v2));
}
fn div(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v2 / v1);
push(c, v2.saturating_div(v1));
}
// --- Outer interpreter ---
@ -406,7 +412,12 @@ fn outer(c: &mut Core, s: &str) {
}
}
}
Err(_) => { if t != "" { println!("{}?", t) }}
Err(_) => {
if t != "" {
println!("{}?", t);
c.post = Post::WarmReset;
}
}
}
}
}
@ -415,7 +426,11 @@ fn outer(c: &mut Core, s: &str) {
}
match c.post {
Post::EatWord => { _ = tokens.next(); }
Post::EatLine => { break; }
Post::WarmReset => {
c.tds = 0;
c.trs = 0;
break; // discard rest of input line
}
Post::Nothing => { }
};
}