Introduce Op type

For operator parsing
This commit is contained in:
greg 2016-01-15 03:27:24 -08:00
parent 47d56a7b44
commit 9a257f08d7
2 changed files with 28 additions and 6 deletions

View File

@ -1,5 +1,4 @@
use tokenizer::Token;
use tokenizer::Kw;
use tokenizer::{Token, Kw, Op};
/* Grammar
program := (statement delimiter ?)*
@ -45,6 +44,8 @@ pub enum Expression {
pub type AST = Vec<ASTNode>;
type Precedence = u8;
//TODO make this support incomplete parses
pub type ParseResult<T> = Result<T, ParseError>;
@ -77,6 +78,17 @@ impl Parser {
fn next(&mut self) -> Option<Token>{
self.tokens.pop()
}
fn get_precedence(op: Op) -> Precedence {
match &op.repr[..] {
"+" => 10,
"-" => 10,
"*" => 20,
"/" => 20,
"%" => 20,
_ => 255,
}
}
}
macro_rules! expect {
@ -192,8 +204,13 @@ impl Parser {
fn expression(&mut self) -> ParseResult<Expression> {
use tokenizer::Token::*;
let mut lhr: Expression = try!(self.primary_expression());
Ok(lhr)
let lhs: Expression = try!(self.primary_expression());
self.precedence_expr(lhs, 0)
}
fn precedence_expr(&mut self, lhs: Expression, min_precedence: u8) -> ParseResult<Expression> {
use tokenizer::Token::*;
Ok(lhs)
}
fn primary_expression(&mut self) -> ParseResult<Expression> {

View File

@ -10,10 +10,15 @@ pub enum Token {
NumLiteral(f64),
StrLiteral(String),
Identifier(String),
Op(String),
Operator(Op),
Keyword(Kw)
}
#[derive(Debug, Clone, PartialEq)]
pub struct Op {
pub repr: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Kw {
If,
@ -109,7 +114,7 @@ pub fn tokenize(input: &str) -> Option<Vec<Token>> {
break;
}
}
Op(buffer)
Operator(Op {repr: buffer })
} else {
let mut buffer = String::with_capacity(20);
buffer.push(c);