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
|
use super::lparser::Rule;
use std::rc::Rc;
pub fn from_parse_tree(parse_tree: &mut pest::iterators::Pairs<Rule>) -> Program {
let root = parse_tree.next().unwrap();
match root.as_rule() {
Rule::program => {
let statements_with_eoi: Vec<pest::iterators::Pair<Rule>> = root.into_inner().collect();
let statements_split = statements_with_eoi.split_last().unwrap();
let statements = statements_split.1.to_vec();
statements
.iter()
.map(|s| Statement::from_pair(s.clone()))
.collect()
}
_ => panic!("[ast] first Pair is not a program"),
}
}
pub type Program = Vec<Statement>;
#[derive(Debug)]
pub enum Statement {
Definition {
symbol: Symbol,
parameters: Vec<Symbol>,
expression: Rc<Expression>,
},
Expression(Rc<Expression>),
}
impl Statement {
fn from_pair(pair: pest::iterators::Pair<Rule>) -> Statement {
let def_or_expr = pair.into_inner().next().unwrap();
fn definition_from_pair(pair: pest::iterators::Pair<Rule>) -> Statement {
let mut inner = pair.into_inner();
let symbol = inner.next().unwrap().as_span().as_str();
let mut parameters: Vec<Symbol> = vec![];
let mut expression_opt: Option<Rc<Expression>> = None;
for p in inner {
match p.as_rule() {
Rule::symbol => parameters.push(p.as_span().as_str().to_owned()),
Rule::expression => {
let expression_inners: Vec<ExpressionInner> =
p.into_inner().map(ExpressionInner::from_pair).collect();
expression_opt = Some(expression_vec_to_tuple(&expression_inners));
}
_ => panic!(
"[ast] illegal rule {:#?} as child of definition",
p.as_rule()
),
}
}
let expression =
expression_opt.expect("[ast] no expression found as child of definition");
Statement::Definition {
symbol: symbol.to_owned(),
parameters,
expression,
}
}
match def_or_expr.as_rule() {
Rule::definition => definition_from_pair(def_or_expr),
Rule::expression => {
let expression_inners: Vec<ExpressionInner> = def_or_expr
.into_inner()
.map(ExpressionInner::from_pair)
.collect();
Statement::Expression(expression_vec_to_tuple(&expression_inners))
}
rule => panic!("[ast] can't make a statement from {:#?}", rule),
}
}
}
#[derive(Debug, Clone)]
pub enum ExpressionInner {
Symbol(Symbol),
IntegerLiteral(IntegerLiteral),
StringLiteral(StringLiteral),
Expression(Rc<Expression>),
}
impl ExpressionInner {
fn from_pair(pair: pest::iterators::Pair<Rule>) -> ExpressionInner {
match pair.as_rule() {
Rule::symbol => ExpressionInner::Symbol(string_from_pair(pair)),
Rule::integer_literal => ExpressionInner::IntegerLiteral(integer_from_pair(pair)),
Rule::string_literal => {
let s_with_quotes = string_from_pair(pair);
let mut chars = s_with_quotes.chars();
chars.next();
chars.next_back();
let s = chars.as_str();
ExpressionInner::StringLiteral(s.to_owned())
}
Rule::expression => {
let expression_inners: Vec<ExpressionInner> =
pair.into_inner().map(ExpressionInner::from_pair).collect();
ExpressionInner::Expression(expression_vec_to_tuple(&expression_inners))
}
rule => panic!("[ast] can't make an expression element from {:#?}", rule),
}
}
}
#[derive(Debug)]
pub enum Expression {
Unary(ExpressionInner),
Binary(ExpressionInner, ExpressionInner),
}
fn expression_vec_to_tuple(v: &[ExpressionInner]) -> Rc<Expression> {
match v.len() {
1 => Rc::new(Expression::Unary(v[0].clone())),
2 => Rc::new(Expression::Binary(v[0].clone(), v[1].clone())),
_ => {
let combined = ExpressionInner::Expression(Rc::new(Expression::Binary(
v[0].clone(),
v[1].clone(),
)));
let mut new_v = vec![combined];
for e in &v[2..] {
new_v.push(e.clone());
}
expression_vec_to_tuple(&new_v)
}
}
}
pub type IntegerLiteral = i64;
fn integer_from_pair(pair: pest::iterators::Pair<Rule>) -> i64 {
let s = pair.as_span().as_str();
s.parse().unwrap()
}
pub type StringLiteral = String;
fn string_from_pair(pair: pest::iterators::Pair<Rule>) -> String {
pair.as_span().as_str().to_owned()
}
pub type Symbol = String;
|