Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade version of nom to 8.0.0,and delete redundant code #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "monkey_exe"
path = "src/main.rs"

[dependencies]
nom = "^7.1.1"
nom = "8.0.0-alpha2"
clap = "~2.31.2"
rustyline = "9.1.2"
rustyline-derive = "0.6.0"
4 changes: 2 additions & 2 deletions lib/evaluator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn add_builtin(name: &str, param_num: usize, func: BuiltinFunction) -> (Ident, O
}

fn bprint_fn(args: Vec<Object>) -> Result<Object, String> {
match args.get(0) {
match args.first() {
Some(Object::String(t)) => {
println!("{}", t);
Ok(Object::Null)
Expand All @@ -45,7 +45,7 @@ fn bprint_fn(args: Vec<Object>) -> Result<Object, String> {
}

fn blen_fn(args: Vec<Object>) -> Result<Object, String> {
match args.get(0) {
match args.first() {
Some(Object::String(s)) => Ok(Object::Integer(s.len() as i64)),
Some(Object::Array(arr)) => Ok(Object::Integer(arr.len() as i64)),
_ => Err(String::from("invalid arguments for len")),
Expand Down
16 changes: 5 additions & 11 deletions lib/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ impl Evaluator {
let old_env = Rc::clone(&self.env);
let mut new_env = Environment::new_with_outer(Rc::clone(f_env));
let zipped = params.into_iter().zip(args);
for (_, (Ident(name), o)) in zipped.enumerate() {
zipped.for_each(|(Ident(name), o)| {
new_env.set(&name, o);
}
});
self.env = Rc::new(RefCell::new(new_env));
let object = self.eval_blockstmt(body);
self.env = old_env;
Expand Down Expand Up @@ -379,7 +379,7 @@ mod tests {
fn compare(input: &[u8], object: Object) {
let (_, r) = Lexer::lex_tokens(input).unwrap();
let tokens = Tokens::new(&r);
let (_, result_parse) = Parser::parse_tokens(tokens).unwrap();
let (_, result_parse) = MyParser::parse_tokens(tokens).unwrap();
let mut evaluator = Evaluator::new();
let eval = evaluator.eval_program(result_parse);
assert_eq!(eval, object);
Expand Down Expand Up @@ -723,10 +723,7 @@ mod tests {
(input_beg.clone() + "let s = \"two\"; h[s]").as_bytes(),
Object::Integer(2),
);
compare(
(input_beg.clone() + "h[3]").as_bytes(),
Object::Integer(3),
);
compare((input_beg.clone() + "h[3]").as_bytes(), Object::Integer(3));
compare(
(input_beg.clone() + "h[2 + 2]").as_bytes(),
Object::Integer(4),
Expand All @@ -739,10 +736,7 @@ mod tests {
(input_beg.clone() + "h[5 < 1]").as_bytes(),
Object::Boolean(false),
);
compare(
(input_beg.clone() + "h[100]").as_bytes(),
Object::Null,
);
compare((input_beg.clone() + "h[100]").as_bytes(), Object::Null);
compare(
(input_beg.clone() + "h[[]]").as_bytes(),
Object::Error("[] is not hashable".to_string()),
Expand Down
31 changes: 18 additions & 13 deletions lib/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nom::bytes::complete::{tag, take};
use nom::character::complete::{alpha1, alphanumeric1, digit1, multispace0};
use nom::combinator::{map, map_res, recognize};
use nom::multi::many0;
use nom::sequence::{delimited, pair};
use nom::sequence::delimited;
use nom::*;

use std::str;
Expand All @@ -15,8 +15,8 @@ use crate::lexer::token::*;

macro_rules! syntax {
($func_name: ident, $tag_string: literal, $output_token: expr) => {
fn $func_name<'a>(s: &'a [u8]) -> IResult<&[u8], Token> {
map(tag($tag_string), |_| $output_token)(s)
fn $func_name(s: &[u8]) -> IResult<&[u8], Token> {
map(tag($tag_string), |_| $output_token).parse(s)
}
};
}
Expand Down Expand Up @@ -49,7 +49,8 @@ pub fn lex_operator(input: &[u8]) -> IResult<&[u8], Token> {
lesser_operator_equal,
greater_operator,
lesser_operator,
))(input)
))
.parse(input)
}

// punctuations
Expand All @@ -74,7 +75,8 @@ pub fn lex_punctuations(input: &[u8]) -> IResult<&[u8], Token> {
rbrace_punctuation,
lbracket_punctuation,
rbracket_punctuation,
))(input)
))
.parse(input)
}

// Strings
Expand Down Expand Up @@ -106,17 +108,17 @@ fn complete_byte_slice_str_from_utf8(c: &[u8]) -> Result<&str, Utf8Error> {
str::from_utf8(c)
}
fn string(input: &[u8]) -> IResult<&[u8], String> {
delimited(tag("\""), map_res(pis, convert_vec_utf8), tag("\""))(input)
delimited(tag("\""), map_res(pis, convert_vec_utf8), tag("\"")).parse(input)
}

fn lex_string(input: &[u8]) -> IResult<&[u8], Token> {
map(string, Token::StringLiteral)(input)
map(string, Token::StringLiteral).parse(input)
}

// Reserved or ident
fn lex_reserved_ident(input: &[u8]) -> IResult<&[u8], Token> {
map_res(
recognize(pair(
recognize((
alt((alpha1, tag("_"))),
many0(alt((alphanumeric1, tag("_")))),
)),
Expand All @@ -133,7 +135,8 @@ fn lex_reserved_ident(input: &[u8]) -> IResult<&[u8], Token> {
_ => Token::Ident(syntax.to_string()),
})
},
)(input)
)
.parse(input)
}

fn complete_str_from_str<F: FromStr>(c: &str) -> Result<F, F::Err> {
Expand All @@ -148,12 +151,13 @@ fn lex_integer(input: &[u8]) -> IResult<&[u8], Token> {
complete_str_from_str,
),
Token::IntLiteral,
)(input)
)
.parse(input)
}

// Illegal tokens
fn lex_illegal(input: &[u8]) -> IResult<&[u8], Token> {
map(take(1usize), |_| Token::Illegal)(input)
map(take(1usize), |_| Token::Illegal).parse(input)
}

fn lex_token(input: &[u8]) -> IResult<&[u8], Token> {
Expand All @@ -164,11 +168,12 @@ fn lex_token(input: &[u8]) -> IResult<&[u8], Token> {
lex_reserved_ident,
lex_integer,
lex_illegal,
))(input)
))
.parse(input)
}

fn lex_tokens(input: &[u8]) -> IResult<&[u8], Vec<Token>> {
many0(delimited(multispace0, lex_token, multispace0))(input)
many0(delimited(multispace0, lex_token, multispace0)).parse(input)
}

pub struct Lexer;
Expand Down
63 changes: 11 additions & 52 deletions lib/lexer/token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use nom::*;
use std::iter::Enumerate;
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};

#[derive(PartialEq, Debug, Clone)]
pub enum Token {
Expand Down Expand Up @@ -61,14 +60,11 @@ impl<'a> Tokens<'a> {
}
}

impl<'a> InputLength for Tokens<'a> {
impl<'a> Input for Tokens<'a> {
#[inline]
fn input_len(&self) -> usize {
self.tok.len()
}
}

impl<'a> InputTake for Tokens<'a> {
#[inline]
fn take(&self, count: usize) -> Self {
Tokens {
Expand All @@ -93,55 +89,10 @@ impl<'a> InputTake for Tokens<'a> {
};
(second, first)
}
}

impl InputLength for Token {
#[inline]
fn input_len(&self) -> usize {
1
}
}

impl<'a> Slice<Range<usize>> for Tokens<'a> {
#[inline]
fn slice(&self, range: Range<usize>) -> Self {
Tokens {
tok: self.tok.slice(range.clone()),
start: self.start + range.start,
end: self.start + range.end,
}
}
}

impl<'a> Slice<RangeTo<usize>> for Tokens<'a> {
#[inline]
fn slice(&self, range: RangeTo<usize>) -> Self {
self.slice(0..range.end)
}
}

impl<'a> Slice<RangeFrom<usize>> for Tokens<'a> {
#[inline]
fn slice(&self, range: RangeFrom<usize>) -> Self {
self.slice(range.start..self.end - self.start)
}
}

impl<'a> Slice<RangeFull> for Tokens<'a> {
#[inline]
fn slice(&self, _: RangeFull) -> Self {
Tokens {
tok: self.tok,
start: self.start,
end: self.end,
}
}
}

impl<'a> InputIter for Tokens<'a> {
type Item = &'a Token;
type Iter = Enumerate<::std::slice::Iter<'a, Token>>;
type IterElem = ::std::slice::Iter<'a, Token>;
type IterIndices = Enumerate<::std::slice::Iter<'a, Token>>;
type Iter = ::std::slice::Iter<'a, Token>;

#[inline]
fn iter_indices(&self) -> Enumerate<::std::slice::Iter<'a, Token>> {
Expand All @@ -166,4 +117,12 @@ impl<'a> InputIter for Tokens<'a> {
Err(Needed::Unknown)
}
}

fn take_from(&self, index: usize) -> Self {
Self {
tok: self.tok.split_at(index).1,
start: index,
end: self.end,
}
}
}
Loading