clippy fixes

This commit is contained in:
2022-02-22 22:40:45 +01:00
parent ce8435e3dc
commit 1b434f6b30
6 changed files with 34 additions and 43 deletions

View File

@@ -288,7 +288,7 @@ impl Expr {
Expression { Expression {
type_: None, type_: None,
expr: self, expr: self,
span: span, span,
} }
} }
} }

View File

@@ -66,7 +66,7 @@ pub fn emit(script: &ast::Script, module_name: &str, options: &Options) -> Vec<u
function_map.insert(name.clone(), function_map.len() as u32); function_map.insert(name.clone(), function_map.len() as u32);
EntityType::Function( EntityType::Function(
*function_types *function_types
.get(&(params.clone(), result.clone())) .get(&(params.clone(), *result))
.unwrap() as u32, .unwrap() as u32,
) )
} }
@@ -255,7 +255,7 @@ fn collect_function_types(script: &ast::Script) -> HashMap<FunctionTypeKey, usiz
{ {
let index = types.len(); let index = types.len();
types types
.entry((params.clone(), result.clone())) .entry((params.clone(), *result))
.or_insert(index); .or_insert(index);
} }
} }

View File

@@ -9,20 +9,18 @@ pub fn resolve_includes(script: &mut ast::Script, path: &Path) -> Result<()> {
let script_dir = path.parent().expect("Script path has no parent"); let script_dir = path.parent().expect("Script path has no parent");
for data in &mut script.data { for data in &mut script.data {
for values in &mut data.data { for values in &mut data.data {
match values { if let ast::DataValues::File {
ast::DataValues::File { ref path,
ref path, ref mut data,
ref mut data, } = values
} => { {
let mut full_path = script_dir.to_path_buf(); let mut full_path = script_dir.to_path_buf();
full_path.push(path); full_path.push(path);
File::open(&full_path) File::open(&full_path)
.map_err(|e| { .map_err(|e| {
anyhow!("Failed to load data from {}: {}", full_path.display(), e) anyhow!("Failed to load data from {}: {}", full_path.display(), e)
})? })?
.read_to_end(data)?; .read_to_end(data)?;
}
_ => (),
} }
} }
} }

View File

@@ -20,10 +20,7 @@ pub struct Options {
impl Options { impl Options {
pub fn with_debug(self) -> Self { pub fn with_debug(self) -> Self {
Options { Options { debug: true }
debug: true,
..self
}
} }
} }
@@ -36,7 +33,7 @@ pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<Vec<u8>
} }
pub fn compile_str(input: &str, path: &Path, options: Options) -> Result<Vec<u8>> { pub fn compile_str(input: &str, path: &Path, options: Options) -> Result<Vec<u8>> {
let mut script = match parser::parse(&input) { let mut script = match parser::parse(input) {
Ok(script) => script, Ok(script) => script,
Err(_) => bail!("Parse failed"), Err(_) => bail!("Parse failed"),
}; };
@@ -44,7 +41,7 @@ pub fn compile_str(input: &str, path: &Path, options: Options) -> Result<Vec<u8>
includes::resolve_includes(&mut script, path)?; includes::resolve_includes(&mut script, path)?;
constfold::fold_script(&mut script); constfold::fold_script(&mut script);
if let Err(_) = typecheck::tc_script(&mut script, &input) { if typecheck::tc_script(&mut script, input).is_err() {
bail!("Type check failed"); bail!("Type check failed");
} }
let wasm = emit::emit( let wasm = emit::emit(

View File

@@ -183,7 +183,8 @@ fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
u64::from_str_radix(&n, 16).map_err(|err| Simple::custom(span, err.to_string())) u64::from_str_radix(&n, 16).map_err(|err| Simple::custom(span, err.to_string()))
}) })
.or(text::int(10).try_map(|n: String, span: Span| { .or(text::int(10).try_map(|n: String, span: Span| {
u64::from_str_radix(&n, 10).map_err(|err| Simple::custom(span, err.to_string())) n.parse::<u64>()
.map_err(|err| Simple::custom(span, err.to_string()))
})) }))
.boxed(); .boxed();
@@ -358,7 +359,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
let branch = just(Token::Branch) let branch = just(Token::Branch)
.ignore_then(identifier) .ignore_then(identifier)
.map(|label| ast::Expr::Branch(label)); .map(ast::Expr::Branch);
let branch_if = just(Token::BranchIf) let branch_if = just(Token::BranchIf)
.ignore_then(expression.clone()) .ignore_then(expression.clone())
@@ -377,7 +378,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
.or(just(Token::Inline).to(ast::LetType::Inline))) .or(just(Token::Inline).to(ast::LetType::Inline)))
.or_not(), .or_not(),
) )
.then(identifier.clone()) .then(identifier)
.then(just(Token::Ctrl(':')).ignore_then(type_parser()).or_not()) .then(just(Token::Ctrl(':')).ignore_then(type_parser()).or_not())
.then( .then(
just(Token::Op("=".to_string())) just(Token::Op("=".to_string()))
@@ -394,7 +395,6 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
.boxed(); .boxed();
let assign = identifier let assign = identifier
.clone()
.then_ignore(just(Token::Op("=".to_string()))) .then_ignore(just(Token::Op("=".to_string())))
.then(expression.clone()) .then(expression.clone())
.map(|(name, value)| ast::Expr::Assign { .map(|(name, value)| ast::Expr::Assign {
@@ -422,7 +422,6 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
.boxed(); .boxed();
let function_call = identifier let function_call = identifier
.clone()
.then( .then(
expression expression
.clone() .clone()
@@ -676,7 +675,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
}) })
.boxed(); .boxed();
let op_first = op_bit op_bit
.clone() .clone()
.then( .then(
just(Token::Op("<|".to_string())) just(Token::Op("<|".to_string()))
@@ -691,9 +690,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
} }
.with_span(span) .with_span(span)
}) })
.boxed(); .boxed()
op_first
}); });
expression_out = Some(expression.clone()); expression_out = Some(expression.clone());
@@ -718,7 +715,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
} }
ast::Expr::Block { ast::Expr::Block {
statements: statements.into_iter().map(|(expr, _)| expr).collect(), statements: statements.into_iter().map(|(expr, _)| expr).collect(),
final_expression: final_expression.map(|e| Box::new(e)), final_expression: final_expression.map(Box::new),
} }
.with_span(span) .with_span(span)
}) })
@@ -740,7 +737,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
let import_global = just(Token::Global) let import_global = just(Token::Global)
.ignore_then(just(Token::Mut).or_not()) .ignore_then(just(Token::Mut).or_not())
.then(identifier.clone()) .then(identifier)
.then_ignore(just(Token::Ctrl(':'))) .then_ignore(just(Token::Ctrl(':')))
.then(type_parser()) .then(type_parser())
.map(|((mut_opt, name), type_)| ast::ImportType::Variable { .map(|((mut_opt, name), type_)| ast::ImportType::Variable {
@@ -751,7 +748,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
.boxed(); .boxed();
let import_function = just(Token::Fn) let import_function = just(Token::Fn)
.ignore_then(identifier.clone()) .ignore_then(identifier)
.then( .then(
type_parser() type_parser()
.separated_by(just(Token::Ctrl(','))) .separated_by(just(Token::Ctrl(',')))
@@ -783,7 +780,6 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
.boxed(); .boxed();
let parameter = identifier let parameter = identifier
.clone()
.then_ignore(just(Token::Ctrl(':'))) .then_ignore(just(Token::Ctrl(':')))
.then(type_parser()) .then(type_parser())
.boxed(); .boxed();
@@ -792,7 +788,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
.or_not() .or_not()
.then(just(Token::Ident("start".to_string())).or_not()) .then(just(Token::Ident("start".to_string())).or_not())
.then_ignore(just(Token::Fn)) .then_ignore(just(Token::Fn))
.then(identifier.clone()) .then(identifier)
.then( .then(
parameter parameter
.separated_by(just(Token::Ctrl(','))) .separated_by(just(Token::Ctrl(',')))
@@ -820,7 +816,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
let global = just(Token::Global) let global = just(Token::Global)
.ignore_then(just(Token::Mut).or_not()) .ignore_then(just(Token::Mut).or_not())
.then(identifier.clone()) .then(identifier)
.then(just(Token::Ctrl(':')).ignore_then(type_parser()).or_not()) .then(just(Token::Ctrl(':')).ignore_then(type_parser()).or_not())
.then(just(Token::Op("=".to_string())).ignore_then(expression.clone())) .then(just(Token::Op("=".to_string())).ignore_then(expression.clone()))
.then_ignore(just(Token::Ctrl(';'))) .then_ignore(just(Token::Ctrl(';')))
@@ -850,7 +846,7 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
) )
.map(|(type_, values)| ast::DataValues::Array { type_, values }); .map(|(type_, values)| ast::DataValues::Array { type_, values });
let data_string = string.clone().map(|s| ast::DataValues::String(s)); let data_string = string.clone().map(ast::DataValues::String);
let data_file = just(Token::Ident("file".to_string())) let data_file = just(Token::Ident("file".to_string()))
.ignore_then( .ignore_then(

View File

@@ -160,7 +160,7 @@ pub fn tc_script(script: &mut ast::Script, source: &str) -> Result<()> {
context.locals.locals[index].index = Some((locals_start + id) as u32); context.locals.locals[index].index = Some((locals_start + id) as u32);
} }
f.locals = std::mem::replace(&mut context.locals, ast::Locals::default()); f.locals = std::mem::take(&mut context.locals);
if f.body.type_ != f.type_ { if f.body.type_ != f.type_ {
result = type_mismatch(f.type_, &f.span, f.body.type_, &f.body.span, source); result = type_mismatch(f.type_, &f.span, f.body.type_, &f.body.span, source);
@@ -333,7 +333,7 @@ fn type_mismatch(
"Expected type {:?}...", "Expected type {:?}...",
type1 type1
.map(|t| format!("{:?}", t)) .map(|t| format!("{:?}", t))
.unwrap_or("void".to_string()) .unwrap_or_else(|| "void".to_string())
)) ))
.with_color(Color::Yellow), .with_color(Color::Yellow),
) )
@@ -343,7 +343,7 @@ fn type_mismatch(
"...but found type {}", "...but found type {}",
type2 type2
.map(|t| format!("{:?}", t)) .map(|t| format!("{:?}", t))
.unwrap_or("void".to_string()) .unwrap_or_else(|| "void".to_string())
)) ))
.with_color(Color::Red), .with_color(Color::Red),
) )
@@ -406,7 +406,7 @@ fn missing_label(span: &Span, source: &str) -> Result<()> {
.finish() .finish()
.eprint(Source::from(source)) .eprint(Source::from(source))
.unwrap(); .unwrap();
return Err(()); Err(())
} }
fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()> { fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()> {