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
//! # ksl::eval::sentence
//!
//! Defines a function that evaluates a KSL sentence.
use crate::{
Environment,
eval::pair::{PairType, eval_pair},
token::{Token, TokenType},
value::Value,
};
/// Evaluation of a KSL sentence.
pub(crate) fn eval_sentence(tokens: &[Token], env: &Environment) -> Option<Value> {
let mut current_token: usize = 0;
if let Some(token) = tokens.get(current_token) {
match &token.value {
TokenType::Atom(a) => {
if let Some(tok) = tokens.get(current_token + 1) {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unknown token `{:?}` at ({}, {})."
),
tok.value, tok.position.0.0, tok.position.0.1
);
return None;
} else {
return Some(Value::Atom(a.clone()));
}
}
TokenType::String(s) => {
if let Some(tok) = tokens.get(current_token + 1) {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unknown token `{:?}` at ({}, {})."
),
tok.value, tok.position.0.0, tok.position.0.1
);
return None;
} else {
return Some(Value::String(s.clone()));
}
}
TokenType::Char(c) => {
if let Some(tok) = tokens.get(current_token + 1) {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unknown token `{:?}` at ({}, {})."
),
tok.value, tok.position.0.0, tok.position.0.1
);
return None;
} else {
return Some(Value::String(String::from(*c as char)));
}
}
TokenType::Number(n) => {
if let Some(tok) = tokens.get(current_token + 1) {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unknown token `{:?}` at ({}, {})."
),
tok.value, tok.position.0.0, tok.position.0.1
);
return None;
} else {
return Some(Value::Number(n.clone()));
}
}
TokenType::Identity(id) => {
if let Some(tok) = tokens.get(current_token + 1) {
current_token += 1;
match tok.value {
TokenType::FuncListOpen => {
let arguments = eval_pair(&tokens[(current_token + 1)..], env, PairType::Function);
if let Some((args, bias)) = arguments {
return if (current_token + bias + 1) < tokens.len() {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Invalid token `{:?}` at ({}, {})."
),
tokens[current_token + bias + 1].value,
tokens[current_token + bias + 1].position.0.0,
tokens[current_token + bias + 1].position.0.1
);
None
} else {
Some(Value::Apply(args, Box::new(Value::Identity(id.clone()))))
};
} else {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unable to get the arguments of function `{}` at ({}, {})."
),
id, tok.position.0.0, tok.position.0.1
);
return None;
}
}
_ => {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unknown token `{:?}` at ({}, {})."
),
tok.value, tok.position.0.0, tok.position.0.1
);
return None;
}
}
} else {
if let Some(v) = env.get(id) {
Some(v.clone())
} else {
eprintln!(
concat!("Error[ksl::eval::eval_sentence]: ", "Unknown symbol `{}`."),
id
);
None
}
}
}
TokenType::ListOpen => {
let contents = eval_pair(&tokens[(current_token + 1)..], env, PairType::List);
if let Some((vals, bias)) = contents {
return if (current_token + bias + 1) < tokens.len() {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Invalid token `{:?}` at ({}, {})."
),
tokens[current_token + bias + 1].value,
tokens[current_token + bias + 1].position.0.0,
tokens[current_token + bias + 1].position.0.1
);
None
} else {
Some(Value::List(vals))
};
} else {
return None;
}
}
_val => {
eprintln!(
concat!(
"Error[ksl::eval::eval_sentence]: ",
"Unknown token `{:?}` at ({}, {})."
),
token.value, token.position.0.0, token.position.0.1
);
return None;
}
}
} else {
None
}
}