Add an Op type for binop operators

Soon this will get swapped in as the way that BinOps are evaluated
This commit is contained in:
greg 2017-01-06 05:18:52 -08:00
parent 3a4f5ae840
commit d23e5bff35

View File

@ -3,6 +3,7 @@ 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;
use std::convert::From;
// Grammar // Grammar
// program := (statement delimiter ?)* // program := (statement delimiter ?)*
@ -66,11 +67,6 @@ 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::*;
@ -86,6 +82,50 @@ impl fmt::Display for Expression {
} }
} }
#[derive(Debug, Clone)]
pub enum Op {
Add,
AddAssign,
Sub,
SubAssign,
Mul,
MulAssign,
Div,
DivAssign,
Mod,
Less,
LessEq,
Greater,
GreaterEq,
Equal,
Assign,
Custom(String),
}
impl<'a> From<&'a str> for Op {
fn from(s: &'a str) -> Op {
use self::Op::*;
match s {
"+" => Add,
"+=" => AddAssign,
"-" => Sub,
"-=" => SubAssign,
"*" => Mul,
"*=" => MulAssign,
"/" => Div,
"/=" => DivAssign,
"%" => Mod,
"<" => Less,
"<=" => LessEq,
">" => Greater,
">=" => GreaterEq,
"==" => Equal,
"=" => Assign,
op => Custom(op.to_string()),
}
}
}
type Precedence = u8; type Precedence = u8;
// TODO make this support incomplete parses // TODO make this support incomplete parses