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

View File

@ -32,7 +32,7 @@ impl<'a> Reducer<'a> {
// First reduce all functions
// TODO once this works, maybe rewrite it using the Visitor
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
@ -42,12 +42,12 @@ impl<'a> Reducer<'a> {
let ast::Statement { id: item_id, kind, .. } = statement;
match &kind {
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, ..}) => {
let symbol = self.symbol_table.lookup_symbol(item_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) => {
//TODO expressions can in principle contain definitions, but I won't worry
//about it now
()
},
ast::StatementKind::Declaration(decl) => match decl {
ast::Declaration::FuncDecl(_, statements) => {
ast::StatementKind::Declaration(decl) => {
if let ast::Declaration::FuncDecl(_, statements) = decl {
self.insert_function_definition(item_id, statements);
},
_ => ()
}
},
ast::StatementKind::Import(..) => (),
ast::StatementKind::Module(_modspec) => {
//TODO handle modules
()
}
}
}
@ -95,7 +92,7 @@ impl<'a> Reducer<'a> {
ast::Declaration::Binding { constant, expr, ..} => {
let symbol = self.symbol_table.lookup_symbol(item_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
@ -294,7 +291,7 @@ impl<'a> Reducer<'a> {
LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())),
FunctionParam(n) => Expression::Lookup(Lookup::Param(n)),
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
tag: index as u32,
}),
@ -310,8 +307,8 @@ impl ast::Pattern {
Ok(match self {
ast::Pattern::Ignored => Pattern::Ignored,
ast::Pattern::TuplePattern(subpatterns) => {
let items: Vec<_> = subpatterns.iter().map(|pat| pat.reduce(symbol_table)).collect();
let items: Result<Vec<Pattern>, PatternError> = items.into_iter().collect();
let items: Result<Vec<Pattern>, PatternError> = subpatterns.iter()
.map(|pat| pat.reduce(symbol_table)).into_iter().collect();
let items = items?;
Pattern::Tuple {
tag: None,
@ -332,8 +329,8 @@ impl ast::Pattern {
ast::Pattern::TupleStruct(name, subpatterns) => {
let symbol = symbol_table.lookup_symbol(&name.id).unwrap();
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> = items.into_iter().collect();
let items: Result<Vec<Pattern>, PatternError> = subpatterns.iter().map(|pat| pat.reduce(symbol_table))
.into_iter().collect();
let items = items?;
Pattern::Tuple {
tag: Some(tag as u32),
@ -353,7 +350,7 @@ impl ast::Pattern {
}
},
SymbolSpec::LocalVariable => {
let def_id = symbol.def_id().unwrap().clone();
let def_id = symbol.def_id().unwrap();
Pattern::Binding(def_id)
},
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();
println!("{}({}) -> {:?}", id, name, callable);
}
println!("");
println!();
println!("Entrypoint:");
println!("-----------");
for stmt in self.entrypoint.iter() {

View File

@ -334,7 +334,7 @@ impl SymbolTable {
println!("In add_symbol(), adding: {:?}", symbol);
self.symbol_trie.insert(&fqsn);
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

View File

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

View File

@ -136,7 +136,7 @@ enum Primitive {
impl Primitive {
fn to_repl(&self) -> String {
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, .. } =>
format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl()))),
Primitive::Literal(lit) => match lit {
@ -341,7 +341,6 @@ impl<'a> State<'a> {
if arity as usize != args.len() {
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)
}
Callable::DataConstructor { type_id, arity, tag } => {