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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
extern crate pest;
#[macro_use]
extern crate pest_derive;
#[derive(Parser)]
#[grammar = "scheme.pest"]
struct SchemeParser;
use pest::{Parser, RuleType};
use pest::iterators::{Pairs, Pair};
use pest::inputs::{Input,StringInput};
#[cfg(test)]
mod tests;
#[derive(Debug)]
pub enum Value{
List(Cons),
Symbol(String),
Number(usize),
Bool(bool),
Literal(String),
Null
}
impl Value{
pub fn car(self) -> Box<Value>{
match self{
Value::List(c) => c.car,
_ => panic!("Not a pair")
}
}
pub fn cdr(self) -> Box<Value>{
match self{
Value::List(c) => c.cdr,
_ => panic!("Not a pair")
}
}
pub fn is_symbol(&self) -> bool{
match self{
&Value::Symbol(_) => true,
_ => false
}
}
pub fn is_number(&self) -> bool{
match self{
&Value::Number(_) => true,
_ => false
}
}
pub fn is_true(&self) -> bool{
match self{
&Value::Bool(val) => val,
_ => false
}
}
pub fn is_false(&self) -> bool{
!self.is_true()
}
pub fn unwrap_symbol(self) -> String{
match self{
Value::Symbol(symb) => symb,
_ => panic!("value is not a symbol")
}
}
pub fn unwrap_number(self) -> usize{
match self{
Value::Number(n) => n,
_ => panic!("value is not a number")
}
}
pub fn unwrap_literal(self) -> String{
match self{
Value::Literal(lit) => lit,
_ => panic!("value is not a literal")
}
}
}
#[derive(Debug)]
pub struct Cons{
car: Box<Value>,
cdr: Box<Value>
}
trait PairMixin{
fn string_value(self) -> String;
fn usize_value(self) -> usize;
}
impl<R: RuleType,I: Input> PairMixin for Pair<R,I>{
fn string_value(self) -> String{
self.into_span().as_str().to_string()
}
fn usize_value(self) -> usize{
self.into_span().as_str().to_string().parse().unwrap()
}
}
trait PairsMixin<R: RuleType, I: Input>{
fn first_pair(self) -> Option<Pair<R,I>>;
}
impl<R: RuleType, I: Input> PairsMixin<R,I> for Pairs<R,I>{
fn first_pair(mut self) -> Option<Pair<R,I>>{
self.next()
}
}
pub fn parse(input: &'static str) -> Value{
let pairs = SchemeParser::parse_str(Rule::scheme, input).expect("Parsed output");
make_ast(pairs.first_pair().unwrap().into_inner().first_pair().unwrap())
}
fn make_ast(pair: Pair<Rule, StringInput>) -> Value{
match pair.as_rule(){
Rule::list => make_list(pair.into_inner()),
Rule::symbol => Value::Symbol(pair.string_value()),
Rule::number => Value::Number(pair.usize_value()),
Rule::true_lit => Value::Bool(true),
Rule::false_lit => Value::Bool(false),
Rule::literal => Value::Literal(pair.string_value()),
_ => panic!("Not covered expression {:?}. Please implement.", pair.as_rule())
}
}
fn make_list(mut pairs: Pairs<Rule, StringInput>) -> Value{
let element = pairs.next();
match element{
Some(el) => Value::List(Cons{car: Box::new(make_ast(el)),
cdr: Box::new(make_list(pairs))}),
None => Value::Null
}
}