Nodes for Program and Constant

This commit is contained in:
Eryn Wells 2016-12-29 14:25:43 -05:00
parent 1527d10a80
commit 7f6ef1ac1e
3 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,24 @@
/* parser/nodes/constant.rs
* Eryn Wells <eryn@erynwells.me>
*/
use std::fmt;
use types::{Boolean, Number};
use super::TreeDebug;
pub trait ConstantValue {}
impl ConstantValue for Boolean {}
impl ConstantValue for Number {}
#[derive(Debug)]
pub struct Constant<V: ConstantValue> {
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 }
}
}

View file

@ -2,6 +2,23 @@
* Eryn Wells <eryn@erynwells.me>
*/
use std::fmt;
pub use self::constant::Constant;
mod constant;
mod program;
trait TreeDebug: fmt::Debug {
fn tree_indent(indent: u8) -> String {
(0..10).fold(String::new(), |mut acc, i| {
acc.push(' ');
acc
})
}
fn tree_fmt(&self, f: &mut fmt::Formatter, indent: u8) -> fmt::Result {
write!(f, "{}{:?}", self.tree_indent(indent), self)
}
}

View file

@ -0,0 +1,26 @@
/* parser/nodes/program.rs
* Eryn Wells <eyrn@erynwells.me>
*/
use std::fmt;
use super::TreeDebug;
use super::Constant;
pub struct Program {
forms: Vec<Constant>,
}
impl TreeDebug for Program {
fn tree_fmt(&self, f: fmt::Formatter, indent: u8) -> fmt::Result {
let spaces = self.tree_indent(indent);
let mut result = write!(f, "{}Program", spaces);
for form in self.forms {
if result.is_err() {
break;
}
result = form.tree_fmt(f, indent + 2);
}
result
}
}