use ast::{TransformedNode, AstLocation, Comment};
use ast::transform::{Transform, TransformContext, TransformResult};
use ast::expression::*;
use std::fmt;
use errors::{Result, ErrorKind};
#[derive(Clone)]
pub struct VarReference {
pub identifier: String,
pub location: AstLocation,
}
impl fmt::Debug for VarReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "${}", self.identifier)
}
}
impl Transform for VarReference {
#[allow(unused_variables)]
fn transform(&self, ctx: TransformContext)
-> Result<Option<TransformedNode>> {
match ctx.ref_scope().var(&self.identifier) {
Some(val) => Ok(Some(
TransformedNode::Value(val)
)),
None => Err(ErrorKind::MissingVarRef(format!("{:?}", self), self.location()).into())
}
}
fn location(&self) -> AstLocation {
self.location.clone()
}
}
#[derive(Debug, Clone)]
pub struct VarDefinition {
pub identifier: String,
pub values: Box<ExpressionNode>,
pub location: AstLocation,
pub comment: Option<Comment>,
}
impl Transform for VarDefinition {
fn transform(&self, ctx: TransformContext)
-> Result<Option<TransformedNode>> {
if let Some(transformed_value) = self.values.transform(ctx.clone())? {
ctx.mut_scope()
.push_var(self.identifier.clone(), transformed_value.return_value());
}
Ok(None)
}
fn location(&self) -> AstLocation {
self.location.clone()
}
}