Skip to content

Commit 040a11a

Browse files
committed
[refactoring] Format fix.
1 parent 9be5403 commit 040a11a

9 files changed

Lines changed: 262 additions & 272 deletions

File tree

src/environment/environment.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<A: Clone> Environment<A> {
102102

103103
pub fn get_all_variables(&self) -> Vec<(Name, A)> {
104104
let mut vars = Vec::new();
105-
105+
106106
// First get variables from local scopes (in reverse order to respect shadowing)
107107
for scope in self.stack.iter() {
108108
for (name, value) in &scope.variables {
@@ -111,14 +111,14 @@ impl<A: Clone> Environment<A> {
111111
}
112112
}
113113
}
114-
114+
115115
// Then get variables from global scope (if not already found)
116116
for (name, value) in &self.globals.variables {
117117
if !vars.iter().any(|(n, _)| n == name) {
118118
vars.push((name.clone(), value.clone()));
119119
}
120120
}
121-
121+
122122
vars
123123
}
124124
}
@@ -136,11 +136,11 @@ mod tests {
136136
assert_eq!(Some(&32), env.lookup(&"x".to_string()));
137137

138138
// Test nested scopes
139-
env.push(); // scope 1
139+
env.push(); // scope 1
140140
env.map_variable("y".to_string(), 23);
141-
env.map_variable("x".to_string(), 55); // shadows global x
141+
env.map_variable("x".to_string(), 55); // shadows global x
142142

143-
env.push(); // scope 2
143+
env.push(); // scope 2
144144
env.map_variable("z".to_string(), 44);
145145

146146
// Variables from all scopes should be accessible
@@ -152,18 +152,18 @@ mod tests {
152152
env.pop();
153153
assert_eq!(Some(&55), env.lookup(&"x".to_string())); // still in scope 1
154154
assert_eq!(Some(&23), env.lookup(&"y".to_string())); // still in scope 1
155-
assert_eq!(None, env.lookup(&"z".to_string())); // z is gone
155+
assert_eq!(None, env.lookup(&"z".to_string())); // z is gone
156156

157157
// Pop scope 1
158158
env.pop();
159159
assert_eq!(Some(&32), env.lookup(&"x".to_string())); // back to global x
160-
assert_eq!(None, env.lookup(&"y".to_string())); // y is gone
160+
assert_eq!(None, env.lookup(&"y".to_string())); // y is gone
161161
}
162162

163163
#[test]
164164
fn test_function_scoping() {
165165
let mut env: Environment<i32> = Environment::new();
166-
166+
167167
let global_func = Function {
168168
name: "global".to_string(),
169169
kind: Type::TVoid,
@@ -184,12 +184,12 @@ mod tests {
184184

185185
env.push();
186186
env.map_function(local_func.clone());
187-
187+
188188
assert!(env.lookup_function(&"global".to_string()).is_some()); // can see global
189-
assert!(env.lookup_function(&"local".to_string()).is_some()); // can see local
189+
assert!(env.lookup_function(&"local".to_string()).is_some()); // can see local
190190

191191
env.pop();
192192
assert!(env.lookup_function(&"global".to_string()).is_some()); // global still visible
193-
assert!(env.lookup_function(&"local".to_string()).is_none()); // local gone
193+
assert!(env.lookup_function(&"local".to_string()).is_none()); // local gone
194194
}
195195
}

src/interpreter/interpreter.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,21 +374,27 @@ fn call(
374374
// Bind arguments
375375
for (i, formal_arg) in func.params.iter().enumerate() {
376376
if i >= args.len() {
377-
return Err((format!(
378-
"[Runtime Error on '{}()'] missing argument '{}'.",
379-
env.scope_name(),
380-
formal_arg.argumentName
381-
), None));
377+
return Err((
378+
format!(
379+
"[Runtime Error on '{}()'] missing argument '{}'.",
380+
env.scope_name(),
381+
formal_arg.argumentName
382+
),
383+
None,
384+
));
382385
}
383386
let arg_value = eval(args[i].clone(), env)?;
384387
new_env.insert_variable(formal_arg.argumentName.clone(), arg_value);
385388
}
386389

387390
if args.len() > func.params.len() {
388-
return Err((format!(
389-
"[Runtime Error on '{}()'] too many arguments.",
390-
env.scope_name()
391-
), None));
391+
return Err((
392+
format!(
393+
"[Runtime Error on '{}()'] too many arguments.",
394+
env.scope_name()
395+
),
396+
None,
397+
));
392398
}
393399

394400
// Execute function

src/ir/ast.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ pub struct FormalArgument {
138138

139139
impl FormalArgument {
140140
pub fn new(argumentName: Name, argumentType: Type) -> Self {
141-
FormalArgument {
142-
argumentName,
143-
argumentType,
144-
}
141+
FormalArgument {
142+
argumentName,
143+
argumentType,
144+
}
145145
}
146146
}
147147

@@ -184,10 +184,7 @@ pub struct ValueConstructor {
184184

185185
impl ValueConstructor {
186186
pub fn new(name: Name, types: Vec<Type>) -> Self {
187-
ValueConstructor {
188-
name,
189-
types,
190-
}
187+
ValueConstructor { name, types }
191188
}
192189
}
193190

src/parser/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod parser_stmt;
55
pub mod parser_type;
66
use nom::{
77
branch::alt,
8-
bytes::complete::{tag},
8+
bytes::complete::tag,
99
character::complete::{char, multispace0},
1010
combinator::{map, opt},
1111
error::Error,
@@ -26,11 +26,11 @@ pub fn parse(input: &str) -> IResult<&str, Vec<Statement>> {
2626
multispace0,
2727
separated_list0(
2828
tuple((multispace0, char(';'), multispace0)),
29-
parse_statement
29+
parse_statement,
3030
),
31-
opt(tuple((multispace0, char(';')))), // optional trailing semicolon
32-
multispace0
31+
opt(tuple((multispace0, char(';')))), // optional trailing semicolon
32+
multispace0,
3333
)),
34-
|(_, statements, _, _)| statements
34+
|(_, statements, _, _)| statements,
3535
)(input)
36-
}
36+
}

src/parser/parser_expr.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ use nom::{
33
bytes::complete::{tag, take_while},
44
character::complete::{char, digit1, multispace0},
55
combinator::{map, map_res, opt, value, verify},
6+
error::Error,
67
multi::{fold_many0, separated_list0},
78
sequence::{delimited, pair, preceded, tuple},
89
IResult,
9-
error::Error,
1010
};
1111

1212
use std::str::FromStr;
1313

1414
use crate::ir::ast::Expression;
15-
use crate::parser::parser_common::{identifier, keyword, is_string_char};
15+
use crate::parser::parser_common::{identifier, is_string_char, keyword};
1616

1717
use crate::ir::ast::Function;
1818
use crate::ir::ast::Type;
@@ -81,10 +81,7 @@ fn parse_relational(input: &str) -> IResult<&str, Expression> {
8181
fn parse_add_sub(input: &str) -> IResult<&str, Expression> {
8282
let (input, init) = parse_term(input)?;
8383
fold_many0(
84-
pair(
85-
alt((operator("+"), operator("-"))),
86-
parse_term
87-
),
84+
pair(alt((operator("+"), operator("-"))), parse_term),
8885
move || init.clone(),
8986
|acc, (op, val)| match op {
9087
"+" => Expression::Add(Box::new(acc), Box::new(val)),
@@ -97,10 +94,7 @@ fn parse_add_sub(input: &str) -> IResult<&str, Expression> {
9794
fn parse_term(input: &str) -> IResult<&str, Expression> {
9895
let (input, init) = parse_factor(input)?;
9996
fold_many0(
100-
pair(
101-
alt((operator("*"), operator("/"))),
102-
parse_factor
103-
),
97+
pair(alt((operator("*"), operator("/"))), parse_factor),
10498
move || init.clone(),
10599
|acc, (op, val)| match op {
106100
"*" => Expression::Mul(Box::new(acc), Box::new(val)),
@@ -118,7 +112,11 @@ fn parse_factor(input: &str) -> IResult<&str, Expression> {
118112
parse_list,
119113
parse_function_call,
120114
parse_var,
121-
delimited(char::<&str, Error<&str>>('('), parse_expression, char::<&str, Error<&str>>(')')),
115+
delimited(
116+
char::<&str, Error<&str>>('('),
117+
parse_expression,
118+
char::<&str, Error<&str>>(')'),
119+
),
122120
))(input)
123121
}
124122

@@ -136,7 +134,7 @@ fn parse_number(input: &str) -> IResult<&str, Expression> {
136134
opt(char::<&str, Error<&str>>('-')),
137135
digit1,
138136
char::<&str, Error<&str>>('.'),
139-
digit1
137+
digit1,
140138
)),
141139
|(_, _, _, _)| true,
142140
),
@@ -150,17 +148,14 @@ fn parse_number(input: &str) -> IResult<&str, Expression> {
150148
);
151149

152150
let int_parser = map_res(
153-
tuple((
154-
opt(char::<&str, Error<&str>>('-')),
155-
digit1
156-
)),
151+
tuple((opt(char::<&str, Error<&str>>('-')), digit1)),
157152
|(sign, digits)| {
158153
let s = match sign {
159154
Some(_) => format!("-{}", digits),
160155
None => digits.to_string(),
161156
};
162157
i32::from_str(&s)
163-
}
158+
},
164159
);
165160

166161
alt((
@@ -201,29 +196,29 @@ pub fn parse_actual_arguments(input: &str) -> IResult<&str, Vec<Expression>> {
201196
char::<&str, Error<&str>>('('),
202197
separated_list0(
203198
tuple((multispace0, char::<&str, Error<&str>>(','), multispace0)),
204-
parse_expression
199+
parse_expression,
205200
),
206201
multispace0,
207-
char::<&str, Error<&str>>(')')
202+
char::<&str, Error<&str>>(')'),
208203
)),
209-
|(_, _, args, _, _)| args
204+
|(_, _, args, _, _)| args,
210205
)(input)
211206
}
212207

213208
fn parse_list(input: &str) -> IResult<&str, Expression> {
214209
let (input, _) = multispace0(input)?;
215210
let (input, _) = char('[')(input)?;
216211
let (input, _) = multispace0(input)?;
217-
212+
218213
let (input, elements) = separated_list0(
219214
delimited(multispace0, char(','), multispace0),
220-
parse_expression
215+
parse_expression,
221216
)(input)?;
222-
217+
223218
let (input, _) = multispace0(input)?;
224219
let (input, _) = char(']')(input)?;
225220
let (input, _) = multispace0(input)?;
226-
221+
227222
Ok((input, Expression::ListValue(elements)))
228223
}
229224

0 commit comments

Comments
 (0)