From 1e9a15d01edaeaa6d1a892accedbcc87bae31260 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 18 Oct 2021 23:41:29 -0700 Subject: [PATCH] Some cipppy lints in reduced ast --- schala-lang/language/src/reduced_ast.rs | 31 ++++++++++---------- schala-lang/language/src/symbol_table/mod.rs | 17 ++++++++++- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 481ec9a..0b47ce9 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -129,12 +129,12 @@ impl<'a> Reducer<'a> { fn statement(&mut self, stmt: &Statement) -> Stmt { match &stmt.kind { - StatementKind::Expression(expr) => Stmt::Expr(self.expression(&expr)), - StatementKind::Declaration(decl) => self.declaration(&decl), + StatementKind::Expression(expr) => Stmt::Expr(self.expression(expr)), + StatementKind::Declaration(decl) => self.declaration(decl), StatementKind::Import(_) => Stmt::Noop, StatementKind::Module(modspec) => { for statement in modspec.contents.iter() { - self.statement(&statement); + self.statement(statement); } Stmt::Noop } @@ -156,7 +156,7 @@ impl<'a> Reducer<'a> { fn expression(&mut self, expr: &Expression) -> Expr { use crate::ast::ExpressionKind::*; - let ref input = expr.kind; + let input = &expr.kind; match input { NatLiteral(n) => Expr::Lit(Lit::Nat(*n)), FloatLiteral(f) => Expr::Lit(Lit::Float(*f)), @@ -178,8 +178,7 @@ impl<'a> Reducer<'a> { } fn value(&mut self, qualified_name: &QualifiedName) -> Expr { - let ref id = qualified_name.id; - let ref sym_name = match self.symbol_table.get_fqsn_from_id(id) { + let sym_name = match self.symbol_table.get_fqsn_from_id(&qualified_name.id) { Some(fqsn) => fqsn, None => return Expr::ReductionError(format!("FQSN lookup for Value {:?} failed", qualified_name)), }; @@ -191,14 +190,14 @@ impl<'a> Reducer<'a> { let Symbol { local_name, spec, .. } = match self.symbol_table.lookup_by_fqsn(&sym_name) { Some(s) => s, //None => return Expr::ReductionError(format!("Symbol {:?} not found", sym_name)), - None => return Expr::Sym(name.clone()) + None => return Expr::Sym(name) }; match spec { SymbolSpec::RecordConstructor { .. } => Expr::ReductionError(format!("AST reducer doesn't expect a RecordConstructor here")), SymbolSpec::DataConstructor { index, type_args, type_name } => Expr::Constructor { type_name: type_name.clone(), - name: name.clone(), + name, tag: index.clone(), arity: type_args.len(), }, @@ -316,7 +315,7 @@ impl<'a> Reducer<'a> { } } - fn binop(&mut self, binop: &BinOp, lhs: &Box, rhs: &Box) -> Expr { + fn binop(&mut self, binop: &BinOp, lhs: &Expression, rhs: &Expression) -> Expr { let operation = Builtin::from_str(binop.sigil()).ok(); match operation { Some(Builtin::Assignment) => Expr::Assign { @@ -334,7 +333,7 @@ impl<'a> Reducer<'a> { } } - fn prefix(&mut self, prefix: &PrefixOp, arg: &Box) -> Expr { + fn prefix(&mut self, prefix: &PrefixOp, arg: &Expression) -> Expr { let builtin: Option = TryFrom::try_from(prefix).ok(); match builtin { Some(op) => { @@ -356,7 +355,7 @@ impl<'a> Reducer<'a> { func: Func::UserDefined { name: Some(name.clone()), params: params.iter().map(|param| param.name.clone()).collect(), - body: self.block(&statements), + body: self.block(statements), } }, TypeDecl { .. } => Stmt::Noop, @@ -379,7 +378,7 @@ impl<'a> Reducer<'a> { fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec, symbol_table: &SymbolTable) -> Subpattern { use self::Pattern::*; let tag = symbol.map(|symbol| match symbol.spec { - SymbolSpec::DataConstructor { index, .. } => index.clone(), + SymbolSpec::DataConstructor { index, .. } => index, _ => panic!("Symbol is not a data constructor - this should've been caught in type-checking"), }); let bound_vars = inner_patterns.iter().map(|p| match p { @@ -445,7 +444,7 @@ impl Pattern { use self::Pattern::*; match self { TupleStruct(QualifiedName{ components, id }, inner_patterns) => { - let fqsn = symbol_table.get_fqsn_from_id(&id); + let fqsn = symbol_table.get_fqsn_from_id(id); match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) { Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table), None => { @@ -462,8 +461,8 @@ impl Pattern { VarOrName(QualifiedName { components, id }) => { // if fqsn is Some, treat this as a symbol pattern. If it's None, treat it // as a variable. - let fqsn = symbol_table.get_fqsn_from_id(&id); - match fqsn.and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) { + let fqsn = symbol_table.get_fqsn_from_id(id); + match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(fqsn)) { Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table), None => { let name = if components.len() == 1 { @@ -475,7 +474,7 @@ impl Pattern { tag: None, subpatterns: vec![], guard: None, - bound_vars: vec![Some(name.clone())], + bound_vars: vec![Some(name)], } } } diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 9b6b86c..614b031 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -270,11 +270,26 @@ impl SymbolTable { * later */ + /// The main entry point into the symbol table. This will traverse the AST in several + /// different ways and populate subtables with information that will be used further in the + /// compilation process. + pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> { + + self.populate_name_tables(ast)?; + self.resolve_symbol_ids(ast)?; + Ok(()) + } + + //TODO does the same thing scope resolution... but maybe I don't need that + fn resolve_symbol_ids(&mut self, ast: &ast::AST) -> Result<(), String> { + Ok(()) + } + /// This function traverses the AST and adds symbol table entries for /// constants, functions, types, and modules defined within. This simultaneously /// checks for dupicate definitions (and returns errors if discovered), and sets /// up name tables that will be used by further parts of the compiler - pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> { + fn populate_name_tables(&mut self, ast: &ast::AST) -> Result<(), String> { let mut scope_stack = vec![Scope::Top]; self.add_from_scope(ast.statements.as_ref(), &mut scope_stack) .map_err(|err| format!("{:?}", err))?;