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
187
188
189
190
|
#[derive(Debug, Clone, PartialEq)]
enum TokenType {
Lparen,
Rparen,
Lbrace,
Rbrace,
Lbracket,
Rbracket,
Colon,
Identifier(String),
Eof,
}
#[derive(Debug, Clone, Copy)]
struct Blame {
line: usize,
char: usize,
}
#[derive(Debug)]
struct Token {
token: TokenType,
blame: Blame,
}
struct Lexer;
impl Lexer {
pub fn analyze(program: &str) -> Vec<Token> {
let mut tokens: Vec<Token> = program
.lines()
.enumerate()
.flat_map(|(line, text)| Self::lex_line(line, text))
.collect();
tokens.push(Token {
token: TokenType::Eof,
blame: Blame {
line: tokens.len(),
char: 0,
},
});
tokens
}
fn lex_line(line: usize, text: &str) -> Vec<Token> {
let mut tokens = Vec::new();
let mut ident_start: Option<usize> = None;
let flush = |tokens: &mut Vec<Token>, ident_start: &mut Option<usize>, end: usize| {
if let Some(start) = ident_start.take() {
if end > start {
tokens.push(Token {
token: TokenType::Identifier(text[start..end].to_string()),
blame: Blame { line, char: start },
});
}
}
};
for (i, c) in text.char_indices() {
let singlechar = match c {
'(' => Some(TokenType::Lparen),
')' => Some(TokenType::Rparen),
'[' => Some(TokenType::Lbracket),
']' => Some(TokenType::Rbracket),
'{' => Some(TokenType::Lbrace),
'}' => Some(TokenType::Rbrace),
':' => Some(TokenType::Colon),
_ => None,
};
if c == ' ' || c == '\t' {
flush(&mut tokens, &mut ident_start, i);
continue;
}
if let Some(tt) = singlechar {
flush(&mut tokens, &mut ident_start, i);
tokens.push(Token {
token: tt,
blame: Blame { line, char: i },
});
continue;
}
if ident_start.is_none() {
ident_start = Some(i);
}
}
flush(&mut tokens, &mut ident_start, text.len());
tokens
}
}
enum Atomic {
Unit,
Bool,
Int,
}
enum SimpleType {
Arrow((Box<SimpleType>, Box<SimpleType>)),
Atom(Atomic),
}
type Identifier = String;
struct Binding {
name: Identifier,
arg_type: Option<SimpleType>,
}
enum ASTNode {
Abstraction {
binding: Binding,
body: Box<ASTNode>,
},
Application((Box<ASTNode>, Box<ASTNode>)),
Identifier(Identifier),
}
pub enum StepResult<T> {
Terminal(T),
Continue,
}
pub trait Steppable<T> {
fn small_step(&mut self) -> StepResult<T>;
}
pub struct Environment<'a, T> {
parent_scope: Option<&'a Environment<'a, T>>,
capture: Identifier,
substitution: &'a T,
}
impl<T> Environment<'_, T> {
fn add<'a>(&'a self, identifier: Identifier, substitution: &'a T) -> Environment<'a, T> {
return Environment {
parent_scope: Some(&self),
capture: identifier,
substitution: substitution,
};
}
fn get<'a>(&'a self, identifier: Identifier) -> Option<&'a T> {
if *identifier == *(self.capture) {
return Some(self.substitution);
}
return match self.parent_scope {
Some(scope) => (*scope).get(identifier),
None => None,
};
}
fn root<'a>(identifier: Identifier, substitution: &'a T) -> Environment<'a, T> {
return Environment {
parent_scope: None,
capture: identifier,
substitution: substitution,
};
}
}
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_env() {
let ident = Identifier::from("ident");
let ident2 = Identifier::from("ident2");
let val: i32 = 1;
let val2: i32 = 2;
let val3: i32 = 3;
let env = Environment::root(ident.clone(), &val);
assert_eq!(env.get(ident.clone()), Some(&val));
let env2 = env.add(ident2.clone(), &val2);
assert_eq!(env2.get(ident.clone()), Some(&val2));
let env3 = env2.add(ident.clone(), &val3);
assert_eq!(env3.get(ident2.clone()), Some(&val2));
assert_eq!(env3.get(ident.clone()), Some(&val3));
assert_eq!(env.get(ident.clone()), Some(&val));
}
}
|