From 4a7b570603aa23f7b6495615a425c5c0bb0ef6a4 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 3 Jan 2017 19:10:48 -0800 Subject: [PATCH] Parser changes - add precedences, move definitions Move impls of Display for AST subtypes closer to where they are defined --- src/parser.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 4071f23..de90b7d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -20,12 +20,24 @@ use std::collections::VecDeque; // op := '+', '-', etc. // +pub type AST = Vec; + #[derive(Debug, Clone)] pub enum Statement { ExprNode(Expression), 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)] pub struct Function { pub prototype: Prototype, @@ -52,16 +64,6 @@ pub enum Expression { While(Box, Vec), } -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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::Expression::*; @@ -77,8 +79,6 @@ impl fmt::Display for Expression { } } -pub type AST = Vec; - type Precedence = u8; // TODO make this support incomplete parses @@ -126,6 +126,8 @@ impl Parser { "/" => 20, "%" => 20, "=" => 1, + "==" => 40, + ">" | ">=" | "<" | "<=" => 30, _ => 255, } }