Run clippy

This commit is contained in:
Dane Johnson 2022-06-01 16:30:29 -05:00
parent 12c67d4d15
commit 2a7db71ded
3 changed files with 41 additions and 22 deletions

View File

@ -22,16 +22,34 @@ impl<'a> Env<'a> {
values: HashMap::new(),
}
}
pub fn lookup(&self, id: &str) -> Atom {
if let Some(a) = self.values.get(id) {
a.clone()
} else if let Some(parent) = self.parent {
parent.lookup(id)
} else {
panic!("Variable {} not in scope.", id)
}
}
}
pub fn eval(ast: &parser::Expr, env: &mut Env) -> parser::Atom {
pub fn eval(ast: &parser::Stmt, env: &mut Env) {
match &ast {
parser::Stmt::ReplPrint(expr) =>
println!("{}", eval_expr(expr, env)),
_ => todo!(),
}
}
pub fn eval_expr(ast: &parser::Expr, env: &Env) -> Atom {
match ast {
parser::Expr::Id(a) => env.lookup(a),
parser::Expr::Atom(a) => a.clone(),
parser::Expr::UnaryMinus(a) => -eval(&a, env),
parser::Expr::Plus(a, b) => eval(&a, env) + eval(&b, env),
parser::Expr::Minus(a, b) => eval(&a, env) - eval(&b, env),
parser::Expr::Mult(a, b) => eval(&a, env) * eval(&b, env),
parser::Expr::Div(a, b) => eval(&a, env) / eval(&b, env),
parser::Expr::UnaryMinus(a) => -eval_expr(a, env),
parser::Expr::Plus(a, b) => eval_expr(a, env) + eval_expr(b, env),
parser::Expr::Minus(a, b) => eval_expr(a, env) - eval_expr(b, env),
parser::Expr::Mult(a, b) => eval_expr(a, env) * eval_expr(b, env),
parser::Expr::Div(a, b) => eval_expr(a, env) / eval_expr(b, env),
_ => panic!("Couldn't evalute expression {{ {:?} }}", ast),
}
}

View File

@ -26,11 +26,7 @@ fn repl(cli: &Cli) {
if cli.parse_only {
println!("{:#?}", tree);
} else {
match &tree {
parser::Stmt::ReplPrint(expr) =>
println!("{}", evaluator::eval(expr, &mut global)),
_ => todo!(),
}
evaluator::eval(&tree, &mut global);
}
}
}

View File

@ -33,7 +33,7 @@ pub enum Atom {
impl fmt::Display for Atom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Atom::String(a) => write!(f, "{}", a),
Atom::String(a) => write!(f, "\"{}\"", a),
Atom::Num(a) => write!(f, "{}", a),
}
}
@ -143,16 +143,21 @@ fn preprocess(input: &str) -> String {
} else if c == '#' {
break;
} else {
use std::cmp::Ordering::*;
let curr = stack.last().unwrap();
if curr < &count {
stack.push(count);
output.push_str(">>>");
} else if curr > &count {
while stack.last().unwrap() > &count {
output.push_str("<<<");
output.push('\n');
stack.pop();
match curr.cmp(&count) {
Less => {
stack.push(count);
output.push_str(">>>");
},
Greater => {
while stack.last().unwrap() > &count {
output.push_str("<<<");
output.push('\n');
stack.pop();
}
}
Equal => (),
}
break;
}
@ -164,12 +169,12 @@ fn preprocess(input: &str) -> String {
}
pub fn parse(prgm: &str) -> Vec<Stmt> {
let prgm = preprocess(&prgm);
let prgm = preprocess(prgm);
deelang_parser::program(&prgm).unwrap()
}
pub fn parse_stmt(stmt: &str) -> Stmt {
let stmt = preprocess(&stmt);
let stmt = preprocess(stmt);
deelang_parser::stmt(&stmt).unwrap()
}