forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.rs
More file actions
86 lines (73 loc) · 2.16 KB
/
Copy pathrepl.rs
File metadata and controls
86 lines (73 loc) · 2.16 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
//! Read-Eval-Print Loop for Nulang.
use crate::compiler::compile;
use crate::parser::parse;
use crate::vm::VM;
/// Start the interactive REPL.
pub fn run_repl() {
println!("Nulang REPL v0.1.0");
println!("Type :quit to exit, :help for commands");
println!();
let mut vm = VM::new();
loop {
print!("nulang> ");
use std::io::Write;
std::io::stdout().flush().unwrap();
let mut line = String::new();
match std::io::stdin().read_line(&mut line) {
Ok(0) => break, // EOF
Ok(_) => {}
Err(e) => {
eprintln!("Error reading input: {}", e);
continue;
}
}
let line = line.trim();
if line.is_empty() {
continue;
}
// Commands
if line.starts_with(':') {
match line {
":quit" | ":q" => break,
":help" | ":h" => {
println!("Commands:");
println!(" :quit, :q Exit the REPL");
println!(" :help, :h Show this help");
println!(" :ast Show AST of last expression");
println!(" :bytecode Show bytecode of last expression");
}
_ => println!("Unknown command: {}. Type :help for available commands.", line),
}
continue;
}
// Parse
let ast = match parse(line) {
Ok(ast) => ast,
Err(e) => {
eprintln!("Parse error: {}", e);
continue;
}
};
// Compile
let module = compile(&ast);
// Execute
match vm.load_module(&module) {
Ok(_) => {
match vm.call_function("main", &[]) {
Ok(result) => println!("{:?}", result),
Err(e) => eprintln!("Runtime error: {}", e),
}
}
Err(e) => eprintln!("Load error: {}", e),
}
}
println!("Goodbye!");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_repl_creation() {
let _vm = VM::new();
}
}