From 3a4f5ae840145b8fffb7afb3c01cd05fc0720fcf Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 6 Jan 2017 04:56:00 -0800 Subject: [PATCH] Change name Op -> OpTok So that I can make an Op type for the ASTNode --- src/parser.rs | 9 +++++++-- src/tokenizer.rs | 18 +++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index a39eeb8..80b7df3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,5 @@ use std::fmt; -use tokenizer::{Token, Kw, Op}; +use tokenizer::{Token, Kw, OpTok}; use tokenizer::Token::*; use std::collections::VecDeque; use std::rc::Rc; @@ -66,6 +66,11 @@ pub enum Expression { While(Box, Vec), } +#[derive(Debug, Clone)] +pub struct Op { + rep: Rc, +} + impl fmt::Display for Expression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::Expression::*; @@ -120,7 +125,7 @@ impl Parser { self.tokens.pop() } - fn get_precedence(&self, op: &Op) -> Precedence { + fn get_precedence(&self, op: &OpTok) -> Precedence { match &op.0[..] { "+" => 10, "-" => 10, diff --git a/src/tokenizer.rs b/src/tokenizer.rs index cce9b7e..9f82ade 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -17,12 +17,12 @@ pub enum Token { NumLiteral(f64), StrLiteral(Rc), Identifier(Rc), - Operator(Op), + Operator(OpTok), Keyword(Kw), } #[derive(Debug, Clone, PartialEq)] -pub struct Op(pub Rc); +pub struct OpTok(pub Rc); #[derive(Debug, Clone, PartialEq)] pub enum Kw { @@ -100,7 +100,7 @@ fn tokenize_operator(c: char, iter: &mut Peekable) -> Result) -> Result { @@ -161,19 +161,19 @@ mod tests { #[test] fn basic_tokeniziation_tests() { token_test!("let a = 3\n", - [Keyword(Kw::Let), Identifier(ref a), Operator(Op(ref b)), NumLiteral(3.0), Newline], + [Keyword(Kw::Let), Identifier(ref a), Operator(OpTok(ref b)), NumLiteral(3.0), Newline], **a == "a" && **b == "="); token_test!("2+1", - [NumLiteral(2.0), Operator(Op(ref a)), NumLiteral(1.0)], + [NumLiteral(2.0), Operator(OpTok(ref a)), NumLiteral(1.0)], **a == "+"); token_test!("2 + 1", - [NumLiteral(2.0), Operator(Op(ref a)), NumLiteral(1.0)], + [NumLiteral(2.0), Operator(OpTok(ref a)), NumLiteral(1.0)], **a == "+"); token_test!("2.3*49.2", - [NumLiteral(2.3), Operator(Op(ref a)), NumLiteral(49.2)], + [NumLiteral(2.3), Operator(OpTok(ref a)), NumLiteral(49.2)], **a == "*"); assert!(tokenize("2.4.5").is_err()); @@ -182,14 +182,14 @@ mod tests { #[test] fn string_test() { token_test!("null + \"a string\"", - [Keyword(Kw::Null), Operator(Op(ref a)), StrLiteral(ref b)], + [Keyword(Kw::Null), Operator(OpTok(ref a)), StrLiteral(ref b)], **a == "+" && **b == "a string"); } #[test] fn operator_test() { token_test!("a *> b", - [Identifier(ref a), Operator(Op(ref b)), Identifier(ref c)], + [Identifier(ref a), Operator(OpTok(ref b)), Identifier(ref c)], **a == "a" && **b == "*>" && **c == "b");