1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
//! Symbol type

use std::fmt;

use sindra::Identifier;
use sindra::node::Node;

use ast::{Block, Parameter};
use PType;

use visitor::interp::ExtFuncIdent;

/// Symbol object (for use in symbol tables).
#[derive(Clone, Debug, PartialEq)]
pub enum Symbol {
    /// Built-in types
    BuiltinType {
        /// Name of the built-in type (a keyword)
        name: Identifier,
        /// PType associated with the name
        ty: PType,
    },
    /// Variables
    Variable {
        /// Name of the variable
        name: Identifier,
        /// Type of this variable (an Option, since this type is not known at all times of the
        /// computation)
        ty: Option<PType>,
    },
    /// Functions
    Function {
        /// Name of the function
        name: Identifier,
        /// Return type of the function (an Option since this type is not known at all times of
        /// the computation)
        ret_ty: Option<PType>,
        /// Function Body
        body: FunctionBody,
        /// Function parameters,
        params: Vec<Node<Parameter>>,
    },
}

/// Function body types
#[derive(Debug, Clone, PartialEq)]
pub enum FunctionBody {
    /// External (library) function
    External(ExtFuncIdent),
    /// Ast-defined block
    Ast(Node<Block>),
}

impl Symbol {
    /// Create a builtin type Symbol, with specified type
    pub fn builtin(name: Identifier, ty: PType) -> Symbol {
        Symbol::BuiltinType {
            name: name,
            ty: ty
        }
    }
    /// Create a function Symbol, with specified type
    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,
        }
    }
    /// Create a function Symbol, with specified type
    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,
        }
    }
    /// Create a variable Symbol
    pub fn variable(name: Identifier, ty: Option<PType>) -> Symbol {
        Symbol::Variable {
            name: name,
            ty: ty
        }
    }
    /// Return true if symbol is a standard library function
    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)
        }
    }
}