forked from CeleritasCelery/rune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
executable file
·150 lines (136 loc) · 3.34 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
#[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 editfns;
mod emacs;
mod eval;
mod fileio;
mod floatfns;
mod fns;
mod interpreter;
mod keymap;
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 rune_core::macros::root;
use std::io::{self, Write};
fn main() {
let roots = &RootSet::default();
let cx = &mut Context::new(roots);
root!(env, Env::default(), cx);
let args = Args::parse();
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.load {
load(env, cx);
}
if args.repl {
repl(env, cx);
}
}
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) {
println!("Hello, world!");
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) => {
println!("Error: {e}");
buffer.clear();
continue;
}
};
root!(obj, cx);
match interpreter::eval(obj, None, env, cx) {
Ok(val) => println!("{val}"),
Err(e) => {
print!("Error: {e}");
if let Ok(e) = e.downcast::<EvalError>() {
e.print_backtrace();
}
}
}
buffer.clear();
}
}
fn load(env: &mut Rt<Env>, cx: &mut Context) {
buffer::get_buffer_create(cx.add("*scratch*"), Some(NIL), cx).unwrap();
let bootstrap: Gc<&LispString> = cx.add_as("lisp/bootstrap.el");
root!(bootstrap, cx);
match crate::lread::load(bootstrap, None, None, cx, env) {
Ok(val) => print!("{val}"),
Err(e) => {
print!("Error: {e}");
if let Ok(e) = e.downcast::<EvalError>() {
e.print_backtrace();
}
}
}
}
#[derive(Default)]
struct Args {
load: bool,
repl: bool,
}
impl Args {
fn empty(&self) -> bool {
!self.load && !self.repl
}
fn parse() -> Self {
let mut args = Args::default();
for arg in std::env::args() {
match arg.as_str() {
"--repl" => args.repl = true,
"--load" => args.load = true,
x => println!("unknown arg: {x}"),
}
}
if args.empty() {
args.load = true;
}
args
}
}