use std::fmt;
use sindra::Identifier;
use sindra::node::Node;
use ast::{Block, Parameter};
use PType;
use visitor::interp::ExtFuncIdent;
#[derive(Clone, Debug, PartialEq)]
pub enum Symbol {
BuiltinType {
name: Identifier,
ty: PType,
},
Variable {
name: Identifier,
ty: Option<PType>,
},
Function {
name: Identifier,
ret_ty: Option<PType>,
body: FunctionBody,
params: Vec<Node<Parameter>>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum FunctionBody {
External(ExtFuncIdent),
Ast(Node<Block>),
}
impl Symbol {
pub fn builtin(name: Identifier, ty: PType) -> Symbol {
Symbol::BuiltinType {
name: name,
ty: ty
}
}
pub fn function(name: Identifier, ty: Option<PType>, body: Node<Block>,
params: Vec<Node<Parameter>>) -> Symbol {
Symbol::Function {
name: name,
ret_ty: ty,
body: FunctionBody::Ast(body),
params: params,
}
}
pub fn ext_function(name: Identifier, ty: Option<PType>, body: ExtFuncIdent,
params: Vec<Node<Parameter>>) -> Symbol {
Symbol::Function {
name: name,
ret_ty: ty,
body: FunctionBody::External(body),
params: params,
}
}
pub fn variable(name: Identifier, ty: Option<PType>) -> Symbol {
Symbol::Variable {
name: name,
ty: ty
}
}
pub fn is_stdlib_func(&self) -> bool {
match *self {
Symbol::Function { body: FunctionBody::External (_), .. } => true,
_ => false,
}
}
}
impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter) -> ::std::result::Result<(), fmt::Error> {
let (kind, name, ty) = match *self {
Symbol::Variable { ref name, ref ty } => ("var", name, ty.clone()),
Symbol::Function { ref name, ref ret_ty, .. } => ("fn", name, ret_ty.clone()),
Symbol::BuiltinType { ref name, ref ty } => ("bi", name, Some(ty.clone())),
};
match ty {
Some(ty) => write!(f, "{} {}: {}", kind, name, ty),
None => write!(f, "{} {}", kind, name)
}
}
}