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
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)
}
}
}