Parser changes - add precedences, move definitions

Move impls of Display for AST subtypes closer to where they are defined
This commit is contained in:
greg 2017-01-03 19:10:48 -08:00
parent 7eb48fb4ef
commit 4a7b570603

View File

@ -20,12 +20,24 @@ use std::collections::VecDeque;
// op := '+', '-', etc. // op := '+', '-', etc.
// //
pub type AST = Vec<Statement>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Statement { pub enum Statement {
ExprNode(Expression), ExprNode(Expression),
FuncDefNode(Function), FuncDefNode(Function),
} }
impl fmt::Display for Statement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Statement::*;
match *self {
ExprNode(ref expr) => write!(f, "{}", expr),
FuncDefNode(_) => write!(f, "UNIMPLEMENTED"),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Function { pub struct Function {
pub prototype: Prototype, pub prototype: Prototype,
@ -52,16 +64,6 @@ pub enum Expression {
While(Box<Expression>, Vec<Expression>), While(Box<Expression>, Vec<Expression>),
} }
impl fmt::Display for Statement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Statement::*;
match *self {
ExprNode(ref expr) => write!(f, "{}", expr),
FuncDefNode(_) => write!(f, "UNIMPLEMENTED"),
}
}
}
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::*;
@ -77,8 +79,6 @@ impl fmt::Display for Expression {
} }
} }
pub type AST = Vec<Statement>;
type Precedence = u8; type Precedence = u8;
// TODO make this support incomplete parses // TODO make this support incomplete parses
@ -126,6 +126,8 @@ impl Parser {
"/" => 20, "/" => 20,
"%" => 20, "%" => 20,
"=" => 1, "=" => 1,
"==" => 40,
">" | ">=" | "<" | "<=" => 30,
_ => 255, _ => 255,
} }
} }