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

View File

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