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(), 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 { match ast {
parser::Expr::Id(a) => env.lookup(a),
parser::Expr::Atom(a) => a.clone(), parser::Expr::Atom(a) => a.clone(),
parser::Expr::UnaryMinus(a) => -eval(&a, env), parser::Expr::UnaryMinus(a) => -eval_expr(a, env),
parser::Expr::Plus(a, b) => eval(&a, env) + eval(&b, env), parser::Expr::Plus(a, b) => eval_expr(a, env) + eval_expr(b, env),
parser::Expr::Minus(a, b) => eval(&a, env) - eval(&b, env), parser::Expr::Minus(a, b) => eval_expr(a, env) - eval_expr(b, env),
parser::Expr::Mult(a, b) => eval(&a, env) * eval(&b, env), parser::Expr::Mult(a, b) => eval_expr(a, env) * eval_expr(b, env),
parser::Expr::Div(a, b) => eval(&a, env) / eval(&b, env), parser::Expr::Div(a, b) => eval_expr(a, env) / eval_expr(b, env),
_ => panic!("Couldn't evalute expression {{ {:?} }}", ast), _ => panic!("Couldn't evalute expression {{ {:?} }}", ast),
} }
} }

View File

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

View File

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