Skip to content

Commit f69c86e

Browse files
refactor: prefer default over new
1 parent 5ff086c commit f69c86e

File tree

5 files changed

+8
-49
lines changed

5 files changed

+8
-49
lines changed

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn process_path(path: &OsStr, args: &Args) -> Result<(), Box<dyn Error>> {
107107
let mut ast = get_abstract_syntax_tree(&code, args.abstract_tree)?;
108108

109109
// Infer as many types as we can in a single pass and return to the start
110-
let mut context = Context::new();
110+
let mut context = Context::default();
111111
ast.infer(&mut context);
112112
context.reset_position();
113113

src/ast/context.rs

+2-23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ast::{SymbolTable, VariableType};
1515
///
1616
/// Stores the current function name if we are inside one, the return types of functions, their
1717
/// argument names and types, and any external `#include`s that are needed.
18-
#[derive(Debug)]
18+
#[derive(Debug, Default)]
1919
pub struct Context {
2020
/// Stores the currently defined variables and deals with scoping rules
2121
symbol_table: SymbolTable,
@@ -34,21 +34,6 @@ pub struct Context {
3434
}
3535

3636
impl Context {
37-
/// Creates a new Context.
38-
///
39-
/// This assumes that nothing has been learnt about the program yet.
40-
pub fn new() -> Context {
41-
Context {
42-
symbol_table: SymbolTable::new(),
43-
current_function: None,
44-
function_return_types: HashMap::new(),
45-
function_argument_types: HashMap::new(),
46-
function_argument_names: HashMap::new(),
47-
includes: HashSet::new(),
48-
header_includes: HashSet::new(),
49-
}
50-
}
51-
5237
/// Push a new scope into the `SymbolTable`.
5338
pub fn push_scope(&mut self) {
5439
self.symbol_table.push_scope();
@@ -313,7 +298,7 @@ impl Context {
313298
/// ```
314299
/// use ptc::ast::Context;
315300
///
316-
/// let mut context = Context::new();
301+
/// let mut context = Context::default();
317302
/// assert!(context.in_global_scope());
318303
///
319304
/// context.push_scope();
@@ -326,9 +311,3 @@ impl Context {
326311
self.symbol_table.in_global_scope()
327312
}
328313
}
329-
330-
impl Default for Context {
331-
fn default() -> Context {
332-
Context::new()
333-
}
334-
}

src/ast/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Infer for Suite {
8181
/// use ptc::ast::{Context, DataType, Literal, VariableType};
8282
///
8383
/// let node = Literal::Float { value: 0.5 };
84-
/// let mut context = Context::new();
84+
/// let mut context = Context::default();
8585
/// assert_eq!(node.get_type(&mut context), Some(VariableType::Float));
8686
/// ```
8787
pub trait DataType {

src/ast/symboltable.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl VariableInformation {
2828
}
2929

3030
/// Stores a single scope layer with the variables defined within it.
31-
#[derive(Debug)]
31+
#[derive(Debug, Default)]
3232
pub struct Scope {
3333
/// The variables defined in this scope
3434
variables: HashMap<String, VariableInformation>,
@@ -39,25 +39,13 @@ pub struct Scope {
3939
}
4040

4141
impl Scope {
42-
/// Creates a new Scope.
43-
///
44-
/// Initialises a new scope with an empty `HashMap` for Variables, no known subscopes and marks
45-
/// it as unexplored thusfar.
46-
pub fn new() -> Scope {
47-
Scope {
48-
variables: HashMap::new(),
49-
subscopes: Vec::new(),
50-
explored: false,
51-
}
52-
}
53-
5442
/// Push a new scope into the symbol table, returning the new index that it was inserted into.
5543
pub fn push_scope(&mut self, indices: &[usize]) -> usize {
5644
if let Some((head, tail)) = indices.split_first() {
5745
return self.subscopes[*head].push_scope(tail);
5846
}
5947

60-
self.subscopes.push(Scope::new());
48+
self.subscopes.push(Scope::default());
6149
self.subscopes.len() - 1
6250
}
6351

@@ -186,7 +174,7 @@ impl Scope {
186174
}
187175

188176
/// Defines the `SymbolTable` used in the Context.
189-
#[derive(Debug)]
177+
#[derive(Debug, Default)]
190178
pub struct SymbolTable {
191179
/// The global scope, which all scopes descend from
192180
scope: Scope,
@@ -195,14 +183,6 @@ pub struct SymbolTable {
195183
}
196184

197185
impl SymbolTable {
198-
/// Creates a new `SymbolTable` with an empty global scope.
199-
pub fn new() -> SymbolTable {
200-
SymbolTable {
201-
scope: Scope::new(),
202-
active: Vec::new(),
203-
}
204-
}
205-
206186
/// Push a new scope into the currently active one. This implies we are going into a new level
207187
/// of nesting, such as a function definition, if statement or while statement.
208188
pub fn push_scope(&mut self) {

tests/codegen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn get_output(input: &str) -> String {
77
let lexer = ptc::lexer::Lexer::new(input.char_indices());
88
let mut ast = parser.parse(lexer).unwrap();
99

10-
let mut context = Context::new();
10+
let mut context = Context::default();
1111
ast.infer(&mut context);
1212

1313
context.reset_position();

0 commit comments

Comments
 (0)