From d23e5bff358ff9d4a53de96be9868f6450c030d1 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 6 Jan 2017 05:18:52 -0800 Subject: [PATCH] Add an Op type for binop operators Soon this will get swapped in as the way that BinOps are evaluated --- src/parser.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 80b7df3..728efa5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,6 +3,7 @@ use tokenizer::{Token, Kw, OpTok}; use tokenizer::Token::*; use std::collections::VecDeque; use std::rc::Rc; +use std::convert::From; // Grammar // program := (statement delimiter ?)* @@ -66,11 +67,6 @@ 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::*; @@ -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; // TODO make this support incomplete parses