Basic parsing infrastructure .. doesn't actually do anything

This commit is contained in:
Eryn Wells 2016-12-29 16:58:02 -05:00
parent 1d19a3f722
commit e1f2c6b88b
5 changed files with 66 additions and 18 deletions

View file

@ -2,4 +2,41 @@
* Eryn Wells <eryn@erynwells.me>
*/
pub use self::nodes::Program;
mod nodes;
use lexer::Lexer;
pub struct Parser {
lexer: Lexer,
}
impl Parser {
pub fn new(lexer: Lexer) -> Parser {
Parser { lexer: lexer }
}
pub fn parse(&self) -> nodes::Program {
self.parse_program()
}
}
impl Parser {
fn parse_program(&self) -> nodes::Program {
let program = Program::new();
}
}
#[cfg(test)]
mod tests {
use super::*;
use lexer::Lexer;
#[test]
fn parses_empty_input() {
let parser = Parser::new(Lexer::new(""));
let parsed = parser.parse();
assert_eq!(parsed, Program::new());
}
}

View file

@ -5,20 +5,25 @@
use std::fmt;
use types::{Boolean, Number};
use super::TreeDebug;
use super::expression::Expression;
pub trait ConstantValue {}
impl ConstantValue for Boolean {}
impl ConstantValue for Number {}
#[derive(Debug)]
pub struct Constant<V: ConstantValue> {
pub struct Constant<'a, V: ConstantValue> {
parent: Option<&'a Expression>,
value: V
}
impl<V: ConstantValue + fmt::Debug> TreeDebug for Constant<V> {}
impl<V: ConstantValue> Constant<V> {
pub fn new(value: V) -> Constant<V> {
Constant { value: value }
impl<'a, V: ConstantValue> Constant<'a, V> {
pub fn new(value: V) -> Constant<'a, V> {
Constant { parent: None, value: value }
}
}
impl<'a, V: ConstantValue + fmt::Debug> fmt::Debug for Constant<'a, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Constant {{ {:?} }}", self.value)
}
}

View file

@ -0,0 +1,5 @@
/* parser/nodes/expression.rs
* Eryn Wells <eryn@erynwells.me>
*/
pub trait Expression {}

View file

@ -5,20 +5,13 @@
use std::fmt;
pub use self::constant::Constant;
pub use self::program::Program;
mod constant;
mod expression;
mod program;
use self::constant::ConstantValue;
trait Expression {}
impl fmt::Debug for Expression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Expression")
}
}
impl TreeDebug for Expression {}
impl<T: constant::ConstantValue> Expression for Constant<T> {}
use self::expression::Expression;
/// Conveniently print out a node in the tree
trait TreeDebug: fmt::Debug {

View file

@ -5,12 +5,19 @@
use std::fmt;
use super::TreeDebug;
use super::Expression;
use super::expression::Expression;
pub struct Program {
forms: Vec<Box<Expression>>,
}
impl Program {
pub fn new() -> Program {
Program { forms: Vec::new() }
}
}
/*
impl fmt::Debug for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.tree_fmt(f, 0)
@ -30,3 +37,4 @@ impl TreeDebug for Program {
result
}
}
*/