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
mod frontend;
mod intermediate;
mod backend;
mod runtime;
mod version;

pub use runtime::program::Program;
pub use runtime::state::State;
pub use runtime::object::Object;
pub use runtime::object::NativeModel;
pub use runtime::object::NativeModelInstance;
pub use runtime::object::Reference;

use backend::compiler::DefaultStorage;
use backend::compiler::compile_file;
use std::ops::{Deref, DerefMut};

pub mod helper {
    pub use crate::runtime::object::make_reference;
    pub use crate::backend::compiler::Storage;
}

pub mod debug {
    pub use crate::intermediate::CompileErrorList;
    pub use crate::runtime::program::RuntimeError;
    pub use crate::intermediate::Position;
}

pub struct Clover {
    storage: Box<dyn helper::Storage>
}

impl Clover {
    pub fn new_with_file_loader(storage: Box<dyn helper::Storage>) -> Clover {
        Clover {
            storage
        }
    }

    pub fn new() -> Clover {
        Clover {
            storage: Box::new(DefaultStorage::new())
        }
    }

    pub fn compile_file(&self, filename: &str) -> Result<Program, debug::CompileErrorList> {
        compile_file(filename, self.storage.deref())
    }

    pub fn save_program(&self, filename: &str, program: &Program) -> Result<(), debug::CompileErrorList> {

        let mut writer = self.storage.get_writer(filename)?;

        program.serialize(writer.deref_mut()).unwrap();

        Ok(())
    }

    pub fn load_program(&self, filename: &str) -> Result<Program, debug::CompileErrorList> {
        let mut reader = self.storage.get_reader(filename)?;

        Ok(Program::deserialize(&mut reader).unwrap())
    }

    pub fn create_state_by_filename(&self, filename: &str) -> Result<State, debug::CompileErrorList> {
        let program = self.compile_file(filename)?;

        Ok(program.into())
    }

    pub fn run(&self, program: Program) -> Result<Object, debug::RuntimeError> {
        let mut state: State = program.into();

        state.execute()
    }

}


#[cfg(test)]
mod tests {
    use crate::{Clover, State, Object};

    fn execute_function(state: &mut State, function_name: &str) {
        let mut function_index = None;

        for (i, name) in state.get_program().file_info.as_ref().unwrap().function_names.iter().enumerate() {
            if name != function_name {
                continue;
            };

            function_index = Some(i);
            break;
        };

        assert!(function_index.is_some(), "can not found function [{}] in [{}]", function_name, &state.get_program().file_info.as_ref().unwrap().filenames[0]);

        let result = state.execute_by_function_index(function_index.unwrap(), &[]);

        assert!(result.is_ok(), "error occur when executing function [{}] in [{}]", function_name, &state.get_program().file_info.as_ref().unwrap().filenames[0]);

        let object = result.unwrap();

        if let Object::Boolean(value) = object{
            assert!(value, "result is not bool when executing function [{}] in [{}]", function_name, &state.get_program().file_info.as_ref().unwrap().filenames[0]);
        } else {
            panic!("result is not bool when executing function [{}] in [{}]", function_name, &state.get_program().file_info.as_ref().unwrap().filenames[0]);
        };
    }

    fn execute(filename: &str, function_names: &[ &str ]) {
        let clover = Clover::new();

        let result = clover.create_state_by_filename(filename);

        assert!(result.is_ok(), "create state with with file [{}]", filename);

        let mut state = result.unwrap();

        for function_name in function_names {
            execute_function(&mut state, *function_name)
        };
    }

    #[test]
    fn integer_operations() {
        execute("tests/integer_operations.luck", &[ "add", "sub", "multiply", "divide" ]);
    }

    #[test]
    fn for_loop() {
        execute("tests/for_loop.luck", &[ "simple", "nests", "break_loop", "array", "for_model" ]);
    }

    #[test]
    fn error_handling() {
        execute("tests/error_handling.luck", &[ "in_same_function", "in_child_function" ]);
    }

    #[test]
    fn function() {
        execute("tests/function.luck", &[ "recursive", "with_return", "first_class_function", "instance_first_class_function" ]);
    }

    #[test]
    fn include() {
        execute("tests/include.luck", &[ "include_function", "include_with_nickname", "include_model" ]);
    }

    #[test]
    fn model() {
        execute("tests/model.luck", &[ "regular", "with_apply" ]);
    }

    #[test]
    fn local() {
        execute("tests/local.luck", &[ "in_file", "in_file_again", "in_function", "in_scope" ]);
    }
}