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
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::process::exit;

use rustyline::error::ReadlineError;
use rustyline::DefaultEditor;

use crate::chunk::{Instr, ModuleChunk};
use crate::chunk::OpCode::OpReturn;
use crate::compiler::{CompilationResult, Compiler};
use crate::vm::{ExecutionMode, VMState, VM, Global};
use crate::InterpretResult::InterpretOK;

pub mod chunk;
pub mod compiler;
pub mod debug;
pub mod gc;
pub mod io;
pub mod log;
pub mod native;
pub mod precedence;
pub mod resolver;
pub mod run;
pub mod scanner;
pub mod utils;
pub mod value;
pub mod vm;

#[derive(Debug, PartialEq)]
pub enum InterpretResult {
    InterpretOK(VMState, Vec<ModuleChunk>),
    InterpretCompileError,
    InterpretRuntimeError,
}


pub const VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn fly(file: String, source: String, debug: bool, quiet: bool) -> InterpretResult {
    let mut compiler = Compiler::new_file(file, source, quiet, 0, DEBUG);
    let result = compiler.compile(debug);
    if result.is_none() {
        return InterpretResult::InterpretCompileError;
    }

    let result = result.unwrap();
    let mut vm = if debug {
        VM::new(ExecutionMode::Trace, result, quiet)
    } else {
        VM::new(ExecutionMode::Default, result, quiet)
    };
    vm.run()
}

pub fn repl() -> Result<(), ReadlineError> {
    let mut rl = DefaultEditor::new()?;
    if rl.load_history("history.txt").is_err() {
        println!("No previous history.");
    }
    let s = &String::new();
    let file_name = "script.phx".to_string();
    let mut vm = VM::new(if DEBUG { ExecutionMode::Trace } else { ExecutionMode::Default }, CompilationResult::default(), false);
    let mut state: Option<VMState> = None;
    let mut modules = Vec::new();
    let mut compiler = Compiler::new_file(file_name, s.clone(), false, 0, DEBUG);
    let mut last_state;
    let mut last_compiler;
    loop {
        // let readline = rl.readline(" \x1b[32m>>\x1b[0m");
        let readline = rl.readline(">>");
        match readline {
            Ok(mut line) => {
                last_state = state.clone();
                last_compiler = compiler.clone();
                rl.add_history_entry(line.as_str())
                    .expect("failed to add history");
                // let _result = fly(file_name.clone(), line, DEBUG, false);
                // dbg!(&compiler.current_module().scanner.code);
                line.push('\n');
                compiler.current_module().scanner.code.push_str(&line);
                // dbg!(&compiler.current_module().scanner.code);
                let result = compiler.compile(DEBUG);
                if result.is_none() {
                    compiler = last_compiler;
                    continue;
                }

                let mut result = result.unwrap();
                // pop the return instructions
                result.modules[0].functions[0].chunk.code.retain(|x| x.op_code != OpReturn);
                let line_num = result.modules[0].functions[0].chunk.code.last().unwrap().line_num;
                result.modules[0].functions[0].chunk.code.push(Instr { op_code: OpReturn, line_num });
                state = if let Some(mut s) = state {
                    while s.globals[0].len() < result.modules[0].identifiers.len() {
                        s.globals[0].push(Global::Uninit);
                    }
                    Some(s)
                } else { state };
                vm.modules_cache = result.modules;
                let s = vm.run_state(state.clone(), modules.clone());
                if let InterpretOK(mut s, m) = s {
                    s.current_frame.ip -= 1;
                    state = Some(s);
                    modules = m;
                    if DEBUG {
                        // println!("state: {:#?}, modules: {:#?}", state, modules);
                    }
                } else {
                    state = last_state;
                    compiler = last_compiler;
                }
            }
            Err(ReadlineError::Interrupted) => {
                // println!("CTRL-C");
                break;
            }
            Err(ReadlineError::Eof) => {
                // println!("CTRL-D");
                break;
            }
            Err(err) => {
                phoenix_error!("Error: {:?}", err);
            }
        }
    }
    rl.save_history("history.txt")?;
    println!("Saved history. Goodbye!");
    Ok(())
}

pub fn run_file(filename: String, debug: bool) -> InterpretResult {
    let path = Path::new(&filename);
    let path_display = path.display();

    let mut file = match File::open(path) {
        Ok(file) => file,
        Err(why) => {
            eprintln!("Failed to open {}: {}", path_display, why);
            exit(1);
        }
    };

    let mut s = String::new();
    match file.read_to_string(&mut s) {
        Ok(_) => fly(filename, s, debug, false),
        Err(why) => {
            eprintln!("Failed to read {}: {}", path_display, why);
            exit(1);
        }
    }
}

pub const DEBUG: bool = false;