-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.rs
executable file
·156 lines (141 loc) · 3.45 KB
/
main.rs
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
#[cfg(all(not(target_env = "msvc"), not(miri)))]
#[global_allocator]
#[doc(hidden)]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
#[macro_use]
mod macros;
#[macro_use]
mod core;
#[macro_use]
mod debug;
mod alloc;
mod arith;
mod buffer;
mod bytecode;
mod casefiddle;
mod character;
mod data;
mod dired;
mod editfns;
mod emacs;
mod eval;
mod fileio;
mod filelock;
mod floatfns;
mod fns;
mod interpreter;
mod keymap;
mod library;
mod lread;
mod print;
mod reader;
mod search;
mod threads;
mod timefns;
use crate::core::{
env::{intern, sym, Env},
gc::{Context, RootSet, Rt},
object::{Gc, LispString, NIL},
};
use crate::eval::EvalError;
use clap::Parser;
use rune_core::macros::root;
use std::io::{self, Write};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short, long, value_name = "FILE")]
load: Vec<String>,
#[arg(short, long)]
repl: bool,
#[arg(short, long)]
no_bootstrap: bool,
}
fn main() -> Result<(), ()> {
let args = Args::parse();
let roots = &RootSet::default();
let cx = &mut Context::new(roots);
root!(env, new(Env), cx);
sym::init_symbols();
crate::core::env::init_variables(cx, env);
crate::data::defalias(intern("not", cx), (sym::NULL).into(), None)
.expect("null should be defined");
if !args.no_bootstrap {
bootstrap(env, cx)?;
}
for file in args.load {
load(&file, cx, env)?;
}
if args.repl {
repl(env, cx);
}
Ok(())
}
fn parens_closed(buffer: &str) -> bool {
let open = buffer.chars().filter(|&x| x == '(').count();
let close = buffer.chars().filter(|&x| x == ')').count();
open <= close
}
fn repl(env: &mut Rt<Env>, cx: &mut Context) {
let mut buffer = String::new();
let stdin = io::stdin();
loop {
print!("> ");
io::stdout().flush().unwrap();
stdin.read_line(&mut buffer).unwrap();
if buffer.trim() == "exit" {
return;
}
if buffer.trim().is_empty() {
continue;
}
if !parens_closed(&buffer) {
continue;
}
let (obj, _) = match reader::read(&buffer, cx) {
Ok(obj) => obj,
Err(e) => {
eprintln!("Error: {e}");
buffer.clear();
continue;
}
};
root!(obj, cx);
match interpreter::eval(obj, None, env, cx) {
Ok(val) => println!("{val}"),
Err(e) => {
eprintln!("Error: {e}");
if let Ok(e) = e.downcast::<EvalError>() {
e.print_backtrace();
}
}
}
buffer.clear();
}
}
fn load(file: &str, cx: &mut Context, env: &mut Rt<Env>) -> Result<(), ()> {
let file: Gc<&LispString> = cx.add_as(file);
root!(file, cx);
match crate::lread::load(file, None, None, cx, env) {
Ok(val) => {
println!("{val}");
Ok(())
}
Err(e) => {
eprintln!("Error: {e}");
if let Ok(e) = e.downcast::<EvalError>() {
e.print_backtrace();
}
Err(())
}
}
}
fn bootstrap(env: &mut Rt<Env>, cx: &mut Context) -> Result<(), ()> {
buffer::get_buffer_create(cx.add("*scratch*"), Some(NIL), cx).unwrap();
load("bootstrap.el", cx, env)
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Args::command().debug_assert()
}