/
main.rs
315 lines (284 loc) · 8.65 KB
/
main.rs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#![recursion_limit = "5000"]
#![deny(warnings)]
#![allow(clippy::needless_return)]
#[macro_use]
extern crate nom;
#[macro_use]
extern crate log;
#[cfg(not(target_arch = "wasm32"))]
extern crate pretty_env_logger;
#[macro_use]
extern crate error_chain;
#[cfg(not(target_arch = "wasm32"))]
extern crate clap;
extern crate regex;
#[macro_use]
extern crate nom_locate;
#[cfg(target_arch = "wasm32")]
#[macro_use]
extern crate yew;
#[cfg(target_arch = "wasm32")]
use yew::prelude::*;
#[cfg(target_arch = "wasm32")]
#[macro_use]
extern crate stdweb;
#[cfg(target_arch = "wasm32")]
use stdweb::web::IParentNode;
mod common;
mod parser;
mod runner;
#[cfg(not(target_arch = "wasm32"))]
use clap::{App, Arg};
#[cfg(not(target_arch = "wasm32"))]
use std::fs::File;
#[cfg(not(target_arch = "wasm32"))]
use std::io::{self, Read};
#[cfg(not(target_arch = "wasm32"))]
fn main() -> common::Result<()> {
pretty_env_logger::try_init().unwrap_or(());
let matches = App::new("Maiden")
.version("1.0")
.author("Tom Parker <palfrey@tevp.net>")
.about("Rockstar interpreter")
.arg(
Arg::with_name("INPUT")
.help("Sets the input file to use")
.required(true)
.index(1),
)
.get_matches();
let mut f = File::open(matches.value_of("INPUT").unwrap())?;
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
let program = parser::parse(&buffer)?;
runner::run(&program, &mut io::stdout())?;
Ok(())
}
#[cfg(target_arch = "wasm32")]
mod web;
#[cfg(target_arch = "wasm32")]
fn main() {
yew::initialize();
let app: App<web::Model> = App::new();
let app_element = stdweb::web::document()
.query_selector("#app")
.unwrap()
.unwrap();
app.mount(app_element);
yew::run_loop();
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::Expression;
use std::collections::HashMap;
use std::io::Cursor;
fn test_program(code: &str, end_variables: HashMap<String, Expression>, expected_output: &str) {
pretty_env_logger::try_init().unwrap_or(());
let program = parser::parse(code).unwrap();
info!("Commands: {:?}", program.commands);
let mut writer = Cursor::new(Vec::new());
let variables = runner::run(&program, &mut writer).unwrap();
writer.set_position(0);
let res = std::str::from_utf8(writer.get_ref()).unwrap();
if res != "" {
debug!("{}", res);
}
assert_eq!(expected_output, res);
assert_eq!(end_variables, variables);
}
// https://gist.github.com/DmitrySoshnikov/8439eac0a09d9fafe55a83c88d049117
macro_rules! hashmap(
{ $($key:expr => $value:expr),+, } => {
{
let mut m = ::std::collections::HashMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);
#[test]
fn test_counting() {
let program = "Limit is 100
Counter is 0
Fizz is 3
Buzz is 5
Until Counter is Limit
Build Counter up
End";
let end_variables = hashmap! {
"buzz" => Expression::Floating(5f64),
"limit" => Expression::Floating(100f64),
"counter" => Expression::Floating(100f64),
"fizz" => Expression::Floating(3f64),
};
test_program(program, end_variables, "");
}
#[test]
fn test_rocking_counting() {
let program = "Desire is a lovestruck ladykiller
My world is nothing
Fire is ice
Hate is water
Until my world is Desire,
Build my world up
";
let end_variables = hashmap! {
"my world" => Expression::Floating(100f64),
"fire" => Expression::Floating(3f64),
"hate" => Expression::Floating(5f64),
"desire" => Expression::Floating(100f64),
};
test_program(program, end_variables, "");
}
#[test]
fn rocking_fizzbuzz() {
let program = "Midnight takes your heart & your soul
While your heart is as high as your soul
Put your heart without your soul into your heart
Give back your heart
Desire is a lovestruck ladykiller
My world is nothing
Fire is ice
Hate is water
Until my world is Desire,
Build my world up
If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing
Shout \"FizzBuzz!\"
Take it to the top
If Midnight taking my world, Fire is nothing
Shout \"Fizz!\"
Take it to the top
If Midnight taking my world, Hate is nothing
Say \"Buzz!\"
Take it to the top
Whisper my world
";
let end_variables = hashmap! {
"my world" => Expression::Floating(100f64),
"fire" => Expression::Floating(3f64),
"hate" => Expression::Floating(5f64),
"desire" => Expression::Floating(100f64),
};
test_program(
program,
end_variables,
concat!(
"1\n2\nFizz!\n4\nBuzz!\nFizz!\n7\n8\nFizz!\nBuzz!\n11\nFizz!\n13\n14\nFizzBuzz!\n16\n17\nFizz!\n",
"19\nBuzz!\nFizz!\n22\n23\nFizz!\nBuzz!\n26\nFizz!\n28\n29\nFizzBuzz!\n31\n32\nFizz!\n34\nBuzz!\n",
"Fizz!\n37\n38\nFizz!\nBuzz!\n41\nFizz!\n43\n44\nFizzBuzz!\n46\n47\nFizz!\n49\nBuzz!\nFizz!\n52\n",
"53\nFizz!\nBuzz!\n56\nFizz!\n58\n59\nFizzBuzz!\n61\n62\nFizz!\n64\nBuzz!\nFizz!\n67\n68\nFizz!\n",
"Buzz!\n71\nFizz!\n73\n74\nFizzBuzz!\n76\n77\nFizz!\n79\nBuzz!\nFizz!\n82\n83\nFizz!\nBuzz!\n86\n",
"Fizz!\n88\n89\nFizzBuzz!\n91\n92\nFizz!\n94\nBuzz!\nFizz!\n97\n98\nFizz!\nBuzz!\n"
),
);
}
#[test]
fn multi_word_say() {
let end_variables = HashMap::new();
test_program(
"say \"shout let it all out\"",
end_variables,
"shout let it all out\n",
);
}
#[test]
fn multiple_uppercase_proper_variable() {
let end_variables = hashmap! {
"id" => Expression::Floating(3f64),
};
test_program("put 3 into ID", end_variables, "");
}
#[test]
fn double_increment() {
let end_variables = hashmap! {
"my world" => Expression::Floating(2f64),
};
test_program(
"Put 0 into my world\nBuild my world up, up",
end_variables,
"",
);
}
#[test]
fn double_decrement() {
let end_variables = hashmap! {
"the walls" => Expression::Floating(-2f64),
};
test_program(
"Put 0 into the walls\nKnock the walls down, down",
end_variables,
"",
);
}
#[test]
fn skip_else() {
let end_variables = hashmap! {
"foo" => Expression::String("foo".to_string()),
};
test_program(
"if nothing is nothing
Foo says foo
Else
Bar says bar
",
end_variables,
"",
);
}
#[test]
fn numeric_args() {
let err = test_error("Multiply taking 3, 5");
if let common::ErrorKind::MissingFunction(name, line) = err {
assert_eq!(name, "Multiply");
assert_eq!(line, 1);
} else {
assert!(false, err);
}
}
fn test_error(input: &str) -> common::ErrorKind {
pretty_env_logger::try_init().unwrap_or(());
let program = parser::parse(input).unwrap();
let mut writer = Cursor::new(Vec::new());
runner::run(&program, &mut writer).err().unwrap().0
}
#[test]
fn missing_variable() {
let err = test_error("Put Desire into my world");
if let common::ErrorKind::MissingVariable(name, line) = err {
assert_eq!(name, "Desire");
assert_eq!(line, 1);
} else {
assert!(false, err);
}
}
#[test]
fn end_of_if() {
let err = test_error("if 1 is 2");
if let common::ErrorKind::NoEndOfIf(line) = err {
assert_eq!(line, 1);
} else {
assert!(false, format!("{:?}", err));
}
}
#[test]
fn no_end_func() {
let err = test_error("What Remains takes the fighters, and a war");
if let common::ErrorKind::NoEndFunction(line) = err {
assert_eq!(line, 1);
} else {
assert!(false, format!("{:?}", err));
}
}
#[test]
fn no_end_loop() {
let err = test_error("until 1");
if let common::ErrorKind::NoEndLoop(line) = err {
assert_eq!(line, 1);
} else {
assert!(false, format!("{:?}", err));
}
}
}