ocelot_interpreter/
interpret_script.rs1use crate::interpreter::Interpreter;
2use ocelot_ast::script::Script;
3use ocelot_base::result::OcelotResult;
4use ocelot_pal::pal::Pal;
5
6pub fn interpret_script(script: &Script, pal: &dyn Pal) -> OcelotResult<()> {
8 Interpreter::new(pal).interpret_script(script)
9}
10
11#[cfg(test)]
12mod tests {
13 use super::interpret_script;
14 use ocelot_ast::expression::Expression;
15 use ocelot_ast::expression_kind::ExpressionKind;
16 use ocelot_ast::item::Item;
17 use ocelot_ast::item_kind::ItemKind;
18 use ocelot_ast::println_statement::PrintlnStatement;
19 use ocelot_ast::script::Script;
20 use ocelot_ast::statement::Statement;
21 use ocelot_ast::statement_kind::StatementKind;
22 use ocelot_ast::string_literal_expression::StringLiteralExpression;
23 use ocelot_ast::test_item::TestItem;
24 use ocelot_base::span::Span;
25 use ocelot_pal::pal_mock::PalMock;
26
27 #[test]
28 fn interprets_println_string_literal() {
29 let script = Script::new(
30 vec![Item::new(
31 ItemKind::Statement(Statement::new(
32 StatementKind::Println(PrintlnStatement::new(Expression::new(
33 ExpressionKind::StringLiteral(StringLiteralExpression::new("hello")),
34 Span::new(8, 15),
35 ))),
36 Span::new(0, 17),
37 )),
38 Span::new(0, 17),
39 )],
40 Span::new(0, 17),
41 );
42 let pal = PalMock::new();
43
44 interpret_script(&script, &pal).unwrap();
45
46 assert_eq!(pal.take_printed_output(), "hello\n");
47 }
48
49 #[test]
50 fn ignores_test_items_during_normal_script_execution() {
51 let script = Script::new(
52 vec![
53 Item::new(
54 ItemKind::Statement(Statement::new(
55 StatementKind::Println(PrintlnStatement::new(Expression::new(
56 ExpressionKind::StringLiteral(StringLiteralExpression::new("setup")),
57 Span::new(8, 15),
58 ))),
59 Span::new(0, 17),
60 )),
61 Span::new(0, 17),
62 ),
63 Item::new(
64 ItemKind::Test(TestItem::new(
65 "prints hello",
66 vec![Statement::new(
67 StatementKind::Println(PrintlnStatement::new(Expression::new(
68 ExpressionKind::StringLiteral(StringLiteralExpression::new(
69 "hello",
70 )),
71 Span::new(32, 39),
72 ))),
73 Span::new(24, 41),
74 )],
75 Span::new(18, 43),
76 )),
77 Span::new(18, 43),
78 ),
79 ],
80 Span::new(0, 43),
81 );
82 let pal = PalMock::new();
83
84 interpret_script(&script, &pal).unwrap();
85
86 assert_eq!(pal.take_printed_output(), "setup\n");
87 }
88}