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
pub use super::{Environment, Function, ResultType, RuntimeError};
pub use super::value::Value;
use super::Function::Native;
use std::collections::HashMap;
fn print(_: &mut Environment, args: &[Value]) -> ResultType {
println!("{}", args[0]);
Ok(Value::Nothing)
}
macro_rules! get_args {
($args:expr, $(arg $ps:pat,)* => $b:expr) => {
get_args!(index 0, $args, $(arg $ps,)* => $b)
};
(index $ind:expr, $args:expr, arg $p:pat, $(arg $ps:pat,)* => $b:expr) => {
{
let arg = &$args[$ind];
if let $p = *arg {
get_args!(index $ind+1, $args, $(arg $ps,)* => $b)
} else {
Err(RuntimeError(format!("invalid argument: {:?}", arg)))
}
}
};
(index $ind:expr, $args:expr, => $b:expr) => { $b };
}
mod turtle;
mod env;
mod types;
mod string;
macro_rules! map {
($($k:expr => $v:expr,) *) => {
{
let mut result = HashMap::new();
$(result.insert($k.to_string(), $v);)*
result
}
}
}
pub fn default_functions() -> HashMap<String, Function> {
map!{
"PRINT" => Native(1, print),
"FORWARD" => Native(1, turtle::forward),
"BACKWARD" => Native(1, turtle::backward),
"LEFT" => Native(1, turtle::left),
"RIGHT" => Native(1, turtle::right),
"COLOR" => Native(3, turtle::color),
"BGCOLOR" => Native(3, turtle::bgcolor),
"CLEAR" => Native(0, turtle::clear),
"PENDOWN" => Native(0, turtle::pendown),
"PENUP" => Native(0, turtle::penup),
"HOME" => Native(0, turtle::home),
"REALIGN" => Native(1, turtle::realign),
"HIDE" => Native(0, turtle::hide),
"SHOW" => Native(0, turtle::show),
"WRITE" => Native(1, turtle::write),
"FLOOD" => Native(0, turtle::flood),
"MAKE" => Native(2, env::make),
"GLOBAL" => Native(2, env::global),
"SCREENSHOT" => Native(1, env::screenshot),
"PROMPT" => Native(1, env::prompt),
"THROW" => Native(1, env::throw),
"HEAD" => Native(1, types::head),
"TAIL" => Native(1, types::tail),
"FIRST" => Native(1, types::head),
"BUTFIRST" => Native(1, types::tail),
"LENGTH" => Native(1, types::length),
"ISEMPTY" => Native(1, types::isempty),
"GETINDEX" => Native(2, types::getindex),
"FIND" => Native(2, types::find),
"NOT" => Native(1, types::not),
"TONUMBER" => Native(1, types::tonumber),
"TOSTRING" => Native(1, types::tostring),
"NOTHING" => Native(0, types::nothing),
"REPLACE" => Native(3, string::replace),
"CONTAINS" => Native(2, string::contains),
"CHARS" => Native(1, string::chars),
"SPLIT" => Native(2, string::split),
}
}