Clippy pass

This commit is contained in:
Greg Shuflin 2021-10-26 13:37:03 -07:00
parent f71d3707c6
commit 3402cfe326
6 changed files with 40 additions and 44 deletions

View File

@ -70,15 +70,15 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
match &expr.kind { match &expr.kind {
NatLiteral(_) | FloatLiteral(_) | StringLiteral(_) | BoolLiteral(_) | Value(_) => (), NatLiteral(_) | FloatLiteral(_) | StringLiteral(_) | BoolLiteral(_) | Value(_) => (),
BinExp(_, lhs, rhs) => { BinExp(_, lhs, rhs) => {
walk_expression(v, &lhs); walk_expression(v, lhs);
walk_expression(v, &rhs); walk_expression(v, rhs);
} }
PrefixExp(_, arg) => { PrefixExp(_, arg) => {
walk_expression(v, &arg); walk_expression(v, arg);
} }
TupleLiteral(exprs) => { TupleLiteral(exprs) => {
for expr in exprs { for expr in exprs {
walk_expression(v, &expr); walk_expression(v, expr);
} }
} }
NamedStruct { name: _, fields } => { NamedStruct { name: _, fields } => {
@ -87,7 +87,7 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
} }
} }
Call { f, arguments } => { Call { f, arguments } => {
walk_expression(v, &f); walk_expression(v, f);
for arg in arguments.iter() { for arg in arguments.iter() {
match arg { match arg {
InvocationArgument::Positional(expr) => walk_expression(v, expr), InvocationArgument::Positional(expr) => walk_expression(v, expr),
@ -97,7 +97,7 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
} }
} }
Index { indexee, indexers } => { Index { indexee, indexers } => {
walk_expression(v, &indexee); walk_expression(v, indexee);
for indexer in indexers.iter() { for indexer in indexers.iter() {
walk_expression(v, indexer); walk_expression(v, indexer);
} }
@ -107,15 +107,15 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
body, body,
} => { } => {
if let Some(d) = discriminator.as_ref() { if let Some(d) = discriminator.as_ref() {
walk_expression(v, &d); walk_expression(v, d);
} }
walk_if_expr_body(v, &body.as_ref()); walk_if_expr_body(v, body.as_ref());
} }
WhileExpression { condition, body } => { WhileExpression { condition, body } => {
if let Some(d) = condition.as_ref() { if let Some(d) = condition.as_ref() {
walk_expression(v, d); walk_expression(v, d);
} }
walk_block(v, &body); walk_block(v, body);
} }
ForExpression { enumerators, body } => { ForExpression { enumerators, body } => {
for enumerator in enumerators { for enumerator in enumerators {
@ -131,11 +131,11 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
type_anno: _, type_anno: _,
body, body,
} => { } => {
walk_block(v, &body); walk_block(v, body);
} }
ListLiteral(exprs) => { ListLiteral(exprs) => {
for expr in exprs { for expr in exprs {
walk_expression(v, &expr); walk_expression(v, expr);
} }
} }
}; };
@ -161,9 +161,9 @@ pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
else_case, else_case,
} => { } => {
walk_pattern(v, pattern); walk_pattern(v, pattern);
walk_block(v, &then_case); walk_block(v, then_case);
if let Some(ref block) = else_case.as_ref() { if let Some(block) = else_case.as_ref() {
walk_block(v, &block) walk_block(v, block)
} }
} }
CondList(arms) => { CondList(arms) => {
@ -181,7 +181,7 @@ pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
Condition::Else => (), Condition::Else => (),
} }
if let Some(ref guard) = arm.guard { if let Some(ref guard) = arm.guard {
walk_expression(v, &guard); walk_expression(v, guard);
} }
walk_block(v, &arm.body); walk_block(v, &arm.body);
} }

View File

@ -32,7 +32,7 @@ impl<'a> Reducer<'a> {
// First reduce all functions // First reduce all functions
// TODO once this works, maybe rewrite it using the Visitor // TODO once this works, maybe rewrite it using the Visitor
for statement in ast.statements.iter() { for statement in ast.statements.iter() {
self.top_level_statement(&statement); self.top_level_statement(statement);
} }
// Then compute the entrypoint statements (which may reference previously-computed // Then compute the entrypoint statements (which may reference previously-computed
@ -42,12 +42,12 @@ impl<'a> Reducer<'a> {
let ast::Statement { id: item_id, kind, .. } = statement; let ast::Statement { id: item_id, kind, .. } = statement;
match &kind { match &kind {
ast::StatementKind::Expression(expr) => { ast::StatementKind::Expression(expr) => {
entrypoint.push(Statement::Expression(self.expression(&expr))); entrypoint.push(Statement::Expression(self.expression(expr)));
}, },
ast::StatementKind::Declaration(ast::Declaration::Binding { name: _, constant, expr, ..}) => { ast::StatementKind::Declaration(ast::Declaration::Binding { name: _, constant, expr, ..}) => {
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); let symbol = self.symbol_table.lookup_symbol(item_id).unwrap();
let def_id = symbol.def_id().unwrap(); let def_id = symbol.def_id().unwrap();
entrypoint.push(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(&expr) }); entrypoint.push(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(expr) });
}, },
_ => () _ => ()
} }
@ -65,18 +65,15 @@ impl<'a> Reducer<'a> {
ast::StatementKind::Expression(_expr) => { ast::StatementKind::Expression(_expr) => {
//TODO expressions can in principle contain definitions, but I won't worry //TODO expressions can in principle contain definitions, but I won't worry
//about it now //about it now
()
}, },
ast::StatementKind::Declaration(decl) => match decl { ast::StatementKind::Declaration(decl) => {
ast::Declaration::FuncDecl(_, statements) => { if let ast::Declaration::FuncDecl(_, statements) = decl {
self.insert_function_definition(item_id, statements); self.insert_function_definition(item_id, statements);
}, }
_ => ()
}, },
ast::StatementKind::Import(..) => (), ast::StatementKind::Import(..) => (),
ast::StatementKind::Module(_modspec) => { ast::StatementKind::Module(_modspec) => {
//TODO handle modules //TODO handle modules
()
} }
} }
} }
@ -95,7 +92,7 @@ impl<'a> Reducer<'a> {
ast::Declaration::Binding { constant, expr, ..} => { ast::Declaration::Binding { constant, expr, ..} => {
let symbol = self.symbol_table.lookup_symbol(item_id).unwrap(); let symbol = self.symbol_table.lookup_symbol(item_id).unwrap();
let def_id = symbol.def_id().unwrap(); let def_id = symbol.def_id().unwrap();
Some(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(&expr) }) Some(Statement::Binding { id: def_id, constant: *constant, expr: self.expression(expr) })
}, },
_ => None _ => None
@ -294,7 +291,7 @@ impl<'a> Reducer<'a> {
LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())), LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())),
FunctionParam(n) => Expression::Lookup(Lookup::Param(n)), FunctionParam(n) => Expression::Lookup(Lookup::Param(n)),
DataConstructor { index, arity, type_id } => Expression::Callable(Callable::DataConstructor { DataConstructor { index, arity, type_id } => Expression::Callable(Callable::DataConstructor {
type_id: type_id.clone(), type_id,
arity: arity as u32, //TODO fix up these modifiers arity: arity as u32, //TODO fix up these modifiers
tag: index as u32, tag: index as u32,
}), }),
@ -310,8 +307,8 @@ impl ast::Pattern {
Ok(match self { Ok(match self {
ast::Pattern::Ignored => Pattern::Ignored, ast::Pattern::Ignored => Pattern::Ignored,
ast::Pattern::TuplePattern(subpatterns) => { ast::Pattern::TuplePattern(subpatterns) => {
let items: Vec<_> = subpatterns.iter().map(|pat| pat.reduce(symbol_table)).collect(); let items: Result<Vec<Pattern>, PatternError> = subpatterns.iter()
let items: Result<Vec<Pattern>, PatternError> = items.into_iter().collect(); .map(|pat| pat.reduce(symbol_table)).into_iter().collect();
let items = items?; let items = items?;
Pattern::Tuple { Pattern::Tuple {
tag: None, tag: None,
@ -332,8 +329,8 @@ impl ast::Pattern {
ast::Pattern::TupleStruct(name, subpatterns) => { ast::Pattern::TupleStruct(name, subpatterns) => {
let symbol = symbol_table.lookup_symbol(&name.id).unwrap(); let symbol = symbol_table.lookup_symbol(&name.id).unwrap();
if let SymbolSpec::DataConstructor { index: tag, type_id: _, arity: _ } = symbol.spec() { if let SymbolSpec::DataConstructor { index: tag, type_id: _, arity: _ } = symbol.spec() {
let items: Vec<_> = subpatterns.iter().map(|pat| pat.reduce(symbol_table)).collect(); let items: Result<Vec<Pattern>, PatternError> = subpatterns.iter().map(|pat| pat.reduce(symbol_table))
let items: Result<Vec<Pattern>, PatternError> = items.into_iter().collect(); .into_iter().collect();
let items = items?; let items = items?;
Pattern::Tuple { Pattern::Tuple {
tag: Some(tag as u32), tag: Some(tag as u32),
@ -353,7 +350,7 @@ impl ast::Pattern {
} }
}, },
SymbolSpec::LocalVariable => { SymbolSpec::LocalVariable => {
let def_id = symbol.def_id().unwrap().clone(); let def_id = symbol.def_id().unwrap();
Pattern::Binding(def_id) Pattern::Binding(def_id)
}, },
spec => return Err(format!("Unexpected VarOrName symbol: {:?}", spec).into()) spec => return Err(format!("Unexpected VarOrName symbol: {:?}", spec).into())

View File

@ -28,7 +28,7 @@ impl ReducedIR {
let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name(); let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name();
println!("{}({}) -> {:?}", id, name, callable); println!("{}({}) -> {:?}", id, name, callable);
} }
println!(""); println!();
println!("Entrypoint:"); println!("Entrypoint:");
println!("-----------"); println!("-----------");
for stmt in self.entrypoint.iter() { for stmt in self.entrypoint.iter() {

View File

@ -334,7 +334,7 @@ impl SymbolTable {
println!("In add_symbol(), adding: {:?}", symbol); println!("In add_symbol(), adding: {:?}", symbol);
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol.clone()); self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.id_to_symbol.insert(id.clone(), symbol.clone()); self.id_to_symbol.insert(id.clone(), symbol);
} }
/// Walks the AST, matching the ID of an identifier used in some expression to /// Walks the AST, matching the ID of an identifier used in some expression to

View File

@ -81,7 +81,7 @@ impl<'a> ScopeResolver<'a> {
let name_type = self.lexical_scopes.lookup(&local_name); let name_type = self.lexical_scopes.lookup(&local_name);
match name_type { match name_type {
Some(NameType::Import(fqsn)) => { Some(NameType::Import(fqsn)) => {
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); let symbol = self.symbol_table.fqsn_to_symbol.get(fqsn);
if let Some(symbol) = symbol { if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone()); self.symbol_table.id_to_symbol.insert(id.clone(), symbol.clone());
} }
@ -94,7 +94,7 @@ impl<'a> ScopeResolver<'a> {
self.symbol_table.add_symbol(id, fqsn, spec); self.symbol_table.add_symbol(id, fqsn, spec);
} }
Some(NameType::LocalVariable(item_id)) => { Some(NameType::LocalVariable(item_id)) => {
let symbol = self.symbol_table.id_to_symbol.get(&item_id).cloned(); let symbol = self.symbol_table.id_to_symbol.get(item_id).cloned();
if let Some(symbol) = symbol { if let Some(symbol) = symbol {
self.symbol_table.id_to_symbol.insert(id.clone(), symbol); self.symbol_table.id_to_symbol.insert(id.clone(), symbol);
} }
@ -193,7 +193,7 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
Declaration::Binding { name, .. } => { Declaration::Binding { name, .. } => {
if let Some(fn_name) = cur_function_name { if let Some(fn_name) = cur_function_name {
// We are within a function scope // We are within a function scope
let fqsn = Fqsn { scopes: vec![Scope::Name(fn_name.clone()), Scope::Name(name.clone())] }; let fqsn = Fqsn { scopes: vec![Scope::Name(fn_name), Scope::Name(name.clone())] };
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable); self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
self.lexical_scopes.insert(name.clone(), NameType::LocalVariable(id.clone())); self.lexical_scopes.insert(name.clone(), NameType::LocalVariable(id.clone()));
} }
@ -231,7 +231,7 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
} }
IfExpression { discriminator, body } => { IfExpression { discriminator, body } => {
if let Some(d) = discriminator.as_ref() { if let Some(d) = discriminator.as_ref() {
walk_expression(self, &d); walk_expression(self, d);
} }
let mut resolver = ScopeResolver { let mut resolver = ScopeResolver {
lexical_scopes: self.lexical_scopes.new_scope(Some(ScopeType::PatternMatch)), lexical_scopes: self.lexical_scopes.new_scope(Some(ScopeType::PatternMatch)),
@ -255,9 +255,9 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
else_case, else_case,
} => { } => {
walk_pattern(new_resolver, pattern); walk_pattern(new_resolver, pattern);
walk_block(new_resolver, &then_case); walk_block(new_resolver, then_case);
if let Some(ref block) = else_case.as_ref() { if let Some(block) = else_case.as_ref() {
walk_block(new_resolver, &block) walk_block(new_resolver, block)
} }
} }
IfExpressionBody::CondList(arms) => { IfExpressionBody::CondList(arms) => {
@ -275,7 +275,7 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
Condition::Else => (), Condition::Else => (),
} }
if let Some(ref guard) = arm.guard { if let Some(ref guard) = arm.guard {
walk_expression(new_resolver, &guard); walk_expression(new_resolver, guard);
} }
walk_block(new_resolver, &arm.body); walk_block(new_resolver, &arm.body);
} }
@ -306,7 +306,7 @@ impl<'a> ASTVisitor for ScopeResolver<'a> {
let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] }; let fqsn = Fqsn { scopes: vec![lscope, Scope::Name(local_name.clone())] };
//let local_name = fqsn.local_name(); //let local_name = fqsn.local_name();
self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable); self.symbol_table.add_symbol(id, fqsn, SymbolSpec::LocalVariable);
self.lexical_scopes.insert(local_name.clone(), NameType::LocalVariable(id.clone())); self.lexical_scopes.insert(local_name, NameType::LocalVariable(id.clone()));
} else { } else {
let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() }; let fqsn = Fqsn { scopes: components.iter().map(|name| Scope::Name(name.clone())).collect() };
let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn); let symbol = self.symbol_table.fqsn_to_symbol.get(&fqsn);

View File

@ -136,7 +136,7 @@ enum Primitive {
impl Primitive { impl Primitive {
fn to_repl(&self) -> String { fn to_repl(&self) -> String {
match self { match self {
Primitive::Object { type_id, items, .. } if items.len() == 0 => format!("{}", type_id.local_name()), Primitive::Object { type_id, items, .. } if items.is_empty() => type_id.local_name().to_string(),
Primitive::Object { type_id, items, .. } => Primitive::Object { type_id, items, .. } =>
format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl()))), format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl()))),
Primitive::Literal(lit) => match lit { Primitive::Literal(lit) => match lit {
@ -341,7 +341,6 @@ impl<'a> State<'a> {
if arity as usize != args.len() { if arity as usize != args.len() {
return Err(format!("Lambda expression requries {} arguments, only {} provided", arity, args.len()).into()); return Err(format!("Lambda expression requries {} arguments, only {} provided", arity, args.len()).into());
} }
let body = body.clone(); //TODO again ideally, no cloning here
self.apply_function(body, args) self.apply_function(body, args)
} }
Callable::DataConstructor { type_id, arity, tag } => { Callable::DataConstructor { type_id, arity, tag } => {