-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.rs
More file actions
73 lines (64 loc) · 1.77 KB
/
read.rs
File metadata and controls
73 lines (64 loc) · 1.77 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
use rusty_linter::core::{CastVariant, qualifier_of_variant};
use crate::RuntimeError;
use crate::interpreter::interpreter_trait::InterpreterTrait;
pub fn run<S: InterpreterTrait>(interpreter: &mut S) -> Result<(), RuntimeError> {
// variables are passed by ref, so we can assign to them
let len = interpreter.context().variables().len();
for i in 0..len {
let target_type = qualifier_of_variant(interpreter.context().variables().get(i).unwrap())?;
let data_value = interpreter.data_segment().pop()?;
let casted_value = data_value.cast(target_type)?;
interpreter.context_mut()[i] = casted_value;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::interpreter::interpreter_trait::InterpreterTrait;
use crate::{assert_interpreter_err, assert_prints};
#[test]
fn data_read_print() {
let input = r#"
DATA "the answer is", 42
READ A$, B%
PRINT A$, B%
"#;
assert_prints!(input, "the answer is 42");
}
#[test]
fn read_into_property() {
let input = r#"
TYPE Card
Value AS INTEGER
END TYPE
DIM C AS Card
DATA 42
READ C.Value
PRINT C.Value
"#;
assert_prints!(input, "42");
}
#[test]
fn read_into_array_element() {
let input = r#"
DIM A(1 TO 5)
DATA 1, 5, 9, 6, 7, 3, 2
READ LO
READ HI
FOR I = LO TO HI
READ A(I)
PRINT A(I)
NEXT
"#;
assert_prints!(input, "9", "6", "7", "3", "2");
}
#[test]
fn cast_error_at_runtime() {
let input = r#"
DATA 42
READ A$
"#;
assert_interpreter_err!(input, RuntimeError::TypeMismatch, 3, 9);
}
}