Skip to content

Commit

Permalink
Merge pull request #12 from palfrey/rust-2018
Browse files Browse the repository at this point in the history
Upgrade to Rust 2018 (with NLL)
  • Loading branch information
palfrey committed Dec 6, 2018
2 parents 9f08dfd + 8e39b8a commit a04e57f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 55 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -2,6 +2,7 @@
name = "maiden"
version = "0.1.0"
authors = ["Tom Parker <palfrey@tevp.net>"]
edition = "2018"

[dependencies]
nom = "4"
Expand Down
3 changes: 2 additions & 1 deletion build.rs
Expand Up @@ -38,7 +38,8 @@ fn main() {
name = name,
test_name = test_name,
function = function
).unwrap();
)
.unwrap();
}
}
}
2 changes: 1 addition & 1 deletion src/common.rs
Expand Up @@ -182,7 +182,7 @@ pub struct Program {
pub functions: HashMap<String, Function>,
}

error_chain!{
error_chain! {
foreign_links {
Io(::std::io::Error);
}
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
@@ -1,6 +1,6 @@
#![recursion_limit = "5000"]
#![deny(warnings)]
#![cfg_attr(feature = "cargo-clippy", allow(needless_return))]
#![allow(clippy::needless_return)]

#[macro_use]
extern crate nom;
Expand Down Expand Up @@ -50,7 +50,8 @@ fn main() -> common::Result<()> {
.help("Sets the input file to use")
.required(true)
.index(1),
).get_matches();
)
.get_matches();
let mut f = File::open(matches.value_of("INPUT").unwrap())?;
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
Expand Down Expand Up @@ -78,7 +79,7 @@ fn main() {
#[cfg(test)]
mod tests {
use super::*;
use common::Expression;
use crate::common::Expression;
use std::collections::HashMap;
use std::io::Cursor;

Expand Down Expand Up @@ -214,7 +215,7 @@ mod tests {

#[test]
fn double_increment() {
let end_variables = hashmap!{
let end_variables = hashmap! {
"my world" => Expression::Floating(2f64),
};
test_program(
Expand All @@ -226,7 +227,7 @@ mod tests {

#[test]
fn double_decrement() {
let end_variables = hashmap!{
let end_variables = hashmap! {
"the walls" => Expression::Floating(-2f64),
};
test_program(
Expand Down
58 changes: 31 additions & 27 deletions src/parser.rs
@@ -1,8 +1,8 @@
// because nom macros
#![cfg_attr(feature = "cargo-clippy", allow(double_parens))]
#![cfg_attr(feature = "cargo-clippy", allow(double_comparisons))]
#![allow(clippy::double_parens)]
#![allow(clippy::double_comparisons)]

use common::*;
use crate::common::*;
use nom;
use nom::types::CompleteStr;
use std::collections::HashMap;
Expand Down Expand Up @@ -312,7 +312,7 @@ named!(not_expr(Span) -> SymbolType,
)
);

#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // FIXME: break this up a bit
#[allow(clippy::cyclomatic_complexity)] // FIXME: break this up a bit
fn multiply_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
let (rest, (mut res, mut times)) = do_parse!(
input,
Expand All @@ -338,21 +338,22 @@ fn multiply_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
return Ok((rest, res));
}

#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // FIXME: break this up a bit
#[allow(clippy::cyclomatic_complexity)] // FIXME: break this up a bit
fn add_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
let (rest, (mut res, mut adds)) = do_parse!(
input,
me: multiply_expr
>> adds: many0!(do_parse!(
take_while1!(is_space)
>> op: alt_complete!(
alt_complete!(
tag_no_case!("without") | tag_no_case!("minus")
) => {|_| SymbolType::Subtract} |
alt_complete!(
tag_no_case!("with") | tag_no_case!("plus")
) => {|_| SymbolType::Add}
) >> take_while1!(is_space)
alt_complete!(
tag_no_case!("without") | tag_no_case!("minus")
) => {|_| SymbolType::Subtract} |
alt_complete!(
tag_no_case!("with") | tag_no_case!("plus")
) => {|_| SymbolType::Add}
)
>> take_while1!(is_space)
>> other_me: multiply_expr
>> (op, other_me)
))
Expand All @@ -365,7 +366,7 @@ fn add_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
return Ok((rest, res));
}

#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // FIXME: break this up a bit
#[allow(clippy::cyclomatic_complexity)] // FIXME: break this up a bit
fn inequality_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
let (rest, (mut res, mut ineqs)) = do_parse!(
input,
Expand All @@ -389,21 +390,22 @@ fn inequality_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
return Ok((rest, res));
}

#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // FIXME: break this up a bit
#[allow(clippy::cyclomatic_complexity)] // FIXME: break this up a bit
fn equality_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
let (rest, (mut res, mut eqs)) = do_parse!(
input,
ie: inequality_expr
>> eqs: many0!(do_parse!(
take_while1!(is_space)
>> kind: alt_complete!(
alt_complete!(
tag!("is") | tag!("was") | tag!("are") | tag!("were")
) => {|_| SymbolType::Is} |
alt_complete!(
tag_no_case!("ain't") | tag_no_case!("aint")
) => {|_| SymbolType::Aint}
) >> take_while1!(is_space)
alt_complete!(
tag!("is") | tag!("was") | tag!("are") | tag!("were")
) => {|_| SymbolType::Is} |
alt_complete!(
tag_no_case!("ain't") | tag_no_case!("aint")
) => {|_| SymbolType::Aint}
)
>> take_while1!(is_space)
>> other_ie: inequality_expr
>> (kind, other_ie)
))
Expand All @@ -423,10 +425,11 @@ fn boolean_expr(input: Span) -> nom::IResult<Span, Vec<SymbolType>> {
>> bqs: many0!(do_parse!(
take_while1!(is_space)
>> kind: alt_complete!(
tag_no_case!("and") => {|_| SymbolType::And} |
tag_no_case!("or") => {|_| SymbolType::Or} |
tag_no_case!("nor") => {|_| SymbolType::Nor}
) >> take_while1!(is_space)
tag_no_case!("and") => {|_| SymbolType::And} |
tag_no_case!("or") => {|_| SymbolType::Or} |
tag_no_case!("nor") => {|_| SymbolType::Nor}
)
>> take_while1!(is_space)
>> other_ee: equality_expr
>> (kind, other_ee)
))
Expand Down Expand Up @@ -938,7 +941,7 @@ fn build_next(commands: &mut Vec<CommandLine>, loop_starts: &mut Vec<usize>) ->
return Command::Next { loop_start };
}

#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // FIXME: break this up a bit
#[allow(clippy::cyclomatic_complexity)] // FIXME: break this up a bit
pub fn parse(input: &str) -> Result<Program> {
let raw_lines = lines(&input)?;
if !raw_lines.0.fragment.is_empty() && raw_lines.0.fragment.chars().any(|c| !c.is_whitespace())
Expand Down Expand Up @@ -1365,7 +1368,8 @@ mod tests {
"ladykiller".to_string()
]),
0
).unwrap(),
)
.unwrap(),
Expression::Floating(100f64)
);
}
Expand Down
38 changes: 20 additions & 18 deletions src/runner.rs
@@ -1,4 +1,4 @@
use common::*;
use crate::common::*;
use std;
use std::collections::HashMap;
use std::io::{self, Write};
Expand Down Expand Up @@ -81,9 +81,11 @@ fn run_binop_shortcut(
}
}
},
Expression::String(_) => if let Expression::String(_) = res_second {
return Ok(f(state, &res_first, &res_second)?);
},
Expression::String(_) => {
if let Expression::String(_) = res_second {
return Ok(f(state, &res_first, &res_second)?);
}
}
Expression::Mysterious => {
if let Expression::Mysterious = res_second {
return Ok(true);
Expand Down Expand Up @@ -182,9 +184,11 @@ fn run_mathbinop(
let second_value = *i;
return Ok(Expression::Floating(f(0f64, second_value)));
}
Expression::String(ref s_s) => if let Expression::Add(_, _) = op {
return Ok(Expression::String(format!("null{}", s_s)));
},
Expression::String(ref s_s) => {
if let Expression::Add(_, _) = op {
return Ok(Expression::String(format!("null{}", s_s)));
}
}
Expression::Null => {
return Ok(Expression::Floating(f(0f64, 0f64)));
}
Expand Down Expand Up @@ -289,7 +293,7 @@ fn call_function(
return run_core(&mut new_state, program, func.location + 1);
}

#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // FIXME: break this up a bit
#[allow(clippy::cyclomatic_complexity)] // FIXME: break this up a bit
fn run_expression(
state: &mut State,
program: &Program,
Expand Down Expand Up @@ -449,16 +453,14 @@ fn run_expression(
pub fn run(program: &Program, writer: &mut Write) -> Result<HashMap<String, Expression>> {
let pc = 0;
let mut variables: HashMap<String, Expression> = HashMap::new();
{
let mut state = State {
variables: &mut variables,
writer,
current_line: 0,
depth: 0,
pronoun: None,
};
run_core(&mut state, program, pc)?;
} // FIXME: Drop once NLL is merged
let mut state = State {
variables: &mut variables,
writer,
current_line: 0,
depth: 0,
pronoun: None,
};
run_core(&mut state, program, pc)?;
return Ok(variables);
}

Expand Down
6 changes: 3 additions & 3 deletions src/web.rs
@@ -1,6 +1,6 @@
use common;
use parser;
use runner;
use crate::common;
use crate::parser;
use crate::runner;
use std;
use yew::prelude::*;

Expand Down

0 comments on commit a04e57f

Please sign in to comment.