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
|
use std::collections::HashMap;
use syn::punctuated::Pair;
use crate::chomp::ast;
use super::cst::{Alt, Call, Cat, Fix, Ident, ParenExpression, Term};
#[derive(Clone, Copy, Debug)]
pub enum Binding {
Variable(usize),
Parameter(usize),
Global,
}
#[derive(Debug)]
pub struct Context {
names: HashMap<String, Binding>,
vars: usize,
}
impl Context {
pub fn new<I: IntoIterator<Item = Ident>>(globals: &[Ident], params: I) -> Self {
let mut names = HashMap::new();
for global in globals {
names.insert(global.to_string(), Binding::Global);
}
for (index, param) in params.into_iter().enumerate() {
names.insert(param.to_string(), Binding::Parameter(index));
}
Self { names, vars: 0 }
}
pub fn lookup(&self, name: &Ident) -> Option<Binding> {
// we make variable binding cheaper by inserting wrong and pulling right.
match self.names.get(&name.to_string()).copied() {
Some(Binding::Variable(index)) => Some(Binding::Variable(self.vars - index - 1)),
x => x,
}
}
pub fn with_variable<F: FnOnce(&mut Self) -> R, R>(&mut self, name: &Ident, f: F) -> R {
let old = self
.names
.insert(name.to_string(), Binding::Variable(self.vars));
// we make variable binding cheaper by inserting wrong and pulling right.
// we should increment all values in names instead, but that's slow
self.vars += 1;
let res = f(self);
self.vars -= 1;
if let Some(val) = old {
self.names.insert(name.to_string(), val);
} else {
self.names.remove(&name.to_string());
}
res
}
}
pub trait Convert {
fn convert(self, context: &mut Context) -> Option<ast::Expression>;
}
impl Convert for Ident {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
let span = self.span();
match context.lookup(&self)? {
Binding::Variable(index) => Some(ast::Variable::new(self, index).into()),
Binding::Parameter(index) => Some(ast::Parameter::new(self, index).into()),
Binding::Global => Some(ast::Call::new(self, Vec::new(), Some(span)).into()),
}
}
}
impl Convert for Call {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
let span = self.span();
let args = self
.args
.into_iter()
.map(|arg| arg.convert(context))
.collect::<Option<_>>()?;
Some(ast::Call::new(self.name, args, span).into())
}
}
impl Convert for Fix {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
let span = self.span();
let expr = self.expr;
let inner = context.with_variable(&self.arg, |context| expr.convert(context))?;
Some(ast::Fix::new(self.arg, inner, span).into())
}
}
impl Convert for ParenExpression {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
self.expr.convert(context)
}
}
impl Convert for Term {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
match self {
Self::Epsilon(e) => Some(e.into()),
Self::Ident(i) => i.convert(context),
Self::Literal(l) => Some(l.into()),
Self::Call(c) => c.convert(context),
Self::Fix(f) => f.convert(context),
Self::Parens(p) => p.convert(context),
}
}
}
impl Convert for Cat {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
let mut iter = self.0.into_pairs();
let mut out = match iter.next().unwrap() {
Pair::Punctuated(t, p) => (t.convert(context)?, Some(p)),
Pair::End(t) => (t.convert(context)?, None),
};
for pair in iter {
let (fst, punct) = out;
out = match pair {
Pair::Punctuated(t, p) => (
ast::Cat::new(fst, punct, t.convert(context)?).into(),
Some(p),
),
Pair::End(t) => (ast::Cat::new(fst, punct, t.convert(context)?).into(), None),
};
}
Some(out.0)
}
}
impl Convert for Alt {
fn convert(self, context: &mut Context) -> Option<ast::Expression> {
let mut iter = self.0.into_pairs();
let mut out = match iter.next().unwrap() {
Pair::Punctuated(t, p) => (t.convert(context)?, Some(p)),
Pair::End(t) => (t.convert(context)?, None),
};
for pair in iter {
let (fst, punct) = out;
out = match pair {
Pair::Punctuated(t, p) => (
ast::Alt::new(fst, punct, t.convert(context)?).into(),
Some(p),
),
Pair::End(t) => (ast::Alt::new(fst, punct, t.convert(context)?).into(), None),
};
}
Some(out.0)
}
}
|