Change name Op -> OpTok
So that I can make an Op type for the ASTNode
This commit is contained in:
parent
298194c42d
commit
3a4f5ae840
@ -1,5 +1,5 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use tokenizer::{Token, Kw, Op};
|
use tokenizer::{Token, Kw, OpTok};
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@ -66,6 +66,11 @@ pub enum Expression {
|
|||||||
While(Box<Expression>, Vec<Expression>),
|
While(Box<Expression>, Vec<Expression>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Op {
|
||||||
|
rep: Rc<String>,
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Expression {
|
impl fmt::Display for Expression {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
use self::Expression::*;
|
use self::Expression::*;
|
||||||
@ -120,7 +125,7 @@ impl Parser {
|
|||||||
self.tokens.pop()
|
self.tokens.pop()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_precedence(&self, op: &Op) -> Precedence {
|
fn get_precedence(&self, op: &OpTok) -> Precedence {
|
||||||
match &op.0[..] {
|
match &op.0[..] {
|
||||||
"+" => 10,
|
"+" => 10,
|
||||||
"-" => 10,
|
"-" => 10,
|
||||||
|
@ -17,12 +17,12 @@ pub enum Token {
|
|||||||
NumLiteral(f64),
|
NumLiteral(f64),
|
||||||
StrLiteral(Rc<String>),
|
StrLiteral(Rc<String>),
|
||||||
Identifier(Rc<String>),
|
Identifier(Rc<String>),
|
||||||
Operator(Op),
|
Operator(OpTok),
|
||||||
Keyword(Kw),
|
Keyword(Kw),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Op(pub Rc<String>);
|
pub struct OpTok(pub Rc<String>);
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Kw {
|
pub enum Kw {
|
||||||
@ -100,7 +100,7 @@ fn tokenize_operator(c: char, iter: &mut Peekable<Chars>) -> Result<Token, Token
|
|||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
buffer.push(c);
|
buffer.push(c);
|
||||||
buffer.extend(iter.peeking_take_while(|x| !char::is_alphanumeric(*x) && !char::is_whitespace(*x)));
|
buffer.extend(iter.peeking_take_while(|x| !char::is_alphanumeric(*x) && !char::is_whitespace(*x)));
|
||||||
Ok(Token::Operator(Op(Rc::new(buffer))))
|
Ok(Token::Operator(OpTok(Rc::new(buffer))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tokenize_number_or_period(c: char, iter: &mut Peekable<Chars>) -> Result<Token, TokenizeError> {
|
fn tokenize_number_or_period(c: char, iter: &mut Peekable<Chars>) -> Result<Token, TokenizeError> {
|
||||||
@ -161,19 +161,19 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn basic_tokeniziation_tests() {
|
fn basic_tokeniziation_tests() {
|
||||||
token_test!("let a = 3\n",
|
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 == "=");
|
**a == "a" && **b == "=");
|
||||||
|
|
||||||
token_test!("2+1",
|
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 == "+");
|
**a == "+");
|
||||||
|
|
||||||
token_test!("2 + 1",
|
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 == "+");
|
**a == "+");
|
||||||
|
|
||||||
token_test!("2.3*49.2",
|
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 == "*");
|
**a == "*");
|
||||||
|
|
||||||
assert!(tokenize("2.4.5").is_err());
|
assert!(tokenize("2.4.5").is_err());
|
||||||
@ -182,14 +182,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn string_test() {
|
fn string_test() {
|
||||||
token_test!("null + \"a string\"",
|
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");
|
**a == "+" && **b == "a string");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn operator_test() {
|
fn operator_test() {
|
||||||
token_test!("a *> b",
|
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");
|
**a == "a" && **b == "*>" && **c == "b");
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user