-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_fn.rs
More file actions
106 lines (92 loc) · 2.8 KB
/
string_fn.rs
File metadata and controls
106 lines (92 loc) · 2.8 KB
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
use rusty_linter::core::QBNumberCast;
use rusty_parser::BuiltInFunction;
use rusty_variant::Variant;
use crate::RuntimeError;
use crate::interpreter::interpreter_trait::InterpreterTrait;
use crate::interpreter::variant_casts::VariantCasts;
pub fn run<S: InterpreterTrait>(interpreter: &mut S) -> Result<(), RuntimeError> {
let count: usize = interpreter.context()[0].to_non_negative_int()?;
let v = &interpreter.context()[1];
let s = run_with_variant(count, v)?;
interpreter
.context_mut()
.set_built_in_function_result(BuiltInFunction::String, s);
Ok(())
}
fn run_with_variant(count: usize, v: &Variant) -> Result<String, RuntimeError> {
if let Variant::VString(s) = v {
run_with_string_argument(count, s)
} else {
let ascii: i32 = v.try_cast()?;
run_with_ascii_code_argument(count, ascii)
}
}
fn run_with_string_argument(count: usize, s: &str) -> Result<String, RuntimeError> {
let first_char = s.chars().next().ok_or(RuntimeError::IllegalFunctionCall)?;
run_with_char(count, first_char)
}
fn run_with_ascii_code_argument(count: usize, ascii: i32) -> Result<String, RuntimeError> {
if (0..=255).contains(&ascii) {
let u: u8 = ascii as u8;
run_with_char(count, u as char)
} else {
Err(RuntimeError::IllegalFunctionCall)
}
}
fn run_with_char(count: usize, ch: char) -> Result<String, RuntimeError> {
Ok(std::iter::repeat_n(ch, count).collect())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::interpreter::interpreter_trait::InterpreterTrait;
use crate::{assert_interpreter_err, assert_prints};
#[test]
fn string_with_ascii_code() {
assert_prints!("PRINT STRING$(3, 33)", "!!!");
}
#[test]
fn string_with_string_argument() {
assert_prints!(r#"PRINT STRING$(4, "hello")"#, "hhhh");
}
#[test]
fn string_with_empty_string_argument() {
assert_interpreter_err!(
r#"PRINT STRING$(5, "")"#,
RuntimeError::IllegalFunctionCall,
1,
7
);
}
#[test]
fn string_with_zero_count() {
assert_prints!(r#"PRINT STRING$(0, "hello")"#, "");
}
#[test]
fn string_with_negative_count() {
assert_interpreter_err!(
r#"PRINT STRING$(-1, "hello")"#,
RuntimeError::IllegalFunctionCall,
1,
7
);
}
#[test]
fn string_with_negative_ascii_code() {
assert_interpreter_err!(
"PRINT STRING$(10, -1)",
RuntimeError::IllegalFunctionCall,
1,
7
);
}
#[test]
fn string_with_too_big_ascii_code() {
assert_interpreter_err!(
"PRINT STRING$(10, 256)",
RuntimeError::IllegalFunctionCall,
1,
7
);
}
}