Skip to content

Commit f8bbd78

Browse files
committed
1 parent eb2b549 commit f8bbd78

File tree

2 files changed

+47
-94
lines changed

2 files changed

+47
-94
lines changed

src/main.rs

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,107 +18,20 @@ mod value;
1818

1919
use environment::Environment;
2020

21-
use std::collections::HashMap;
22-
use std::fs::File;
2321
use std::io::BufRead;
24-
use std::io::{self, Read};
22+
use std::io::{self};
23+
use std::io::Write;
2524
use std::rc::Rc;
26-
use std::str::FromStr;
2725

28-
use crate::maps::*;
29-
use crate::persistent_list::{PersistentList, ToPersistentList};
30-
use crate::persistent_list_map::*;
31-
use crate::persistent_vector::{PersistentVector, ToPersistentVector};
3226
use crate::value::Value;
3327
use crate::value::{Evaluable, ToValue};
34-
use rust_core::{AddFn, StrFn};
3528
use symbol::Symbol;
3629

37-
use nom::error::convert_error;
3830
use nom::Err::Incomplete;
39-
use nom::Needed::Size;
4031

4132
fn main() {
42-
println!("Clojure RS 0.0.1");
43-
44-
// Register our macros / functions ahead of time
45-
let add_fn = rust_core::AddFn {};
46-
let str_fn = rust_core::StrFn {};
47-
let do_fn = rust_core::DoFn {};
48-
let nth_fn = rust_core::NthFn {};
49-
let do_macro = rust_core::DoMacro {};
50-
let concat_fn = rust_core::ConcatFn {};
51-
let print_string_fn = rust_core::PrintStringFn {};
52-
// Hardcoded fns
53-
let lexical_eval_fn = Value::LexicalEvalFn {};
54-
// Hardcoded macros
55-
let let_macro = Value::LetMacro {};
56-
let quote_macro = Value::QuoteMacro {};
57-
let def_macro = Value::DefMacro {};
58-
let fn_macro = Value::FnMacro {};
59-
let defmacro_macro = Value::DefmacroMacro {};
60-
61-
let environment = Rc::new(Environment::new_main_environment());
62-
63-
let eval_fn = rust_core::EvalFn::new(Rc::clone(&environment));
64-
65-
environment.insert(Symbol::intern("+"), add_fn.to_rc_value());
66-
environment.insert(Symbol::intern("let"), let_macro.to_rc_value());
67-
environment.insert(Symbol::intern("str"), str_fn.to_rc_value());
68-
environment.insert(Symbol::intern("quote"), quote_macro.to_rc_value());
69-
environment.insert(Symbol::intern("do-fn*"), do_fn.to_rc_value());
70-
environment.insert(Symbol::intern("do"), do_macro.to_rc_value());
71-
environment.insert(Symbol::intern("def"), def_macro.to_rc_value());
72-
environment.insert(Symbol::intern("fn"), fn_macro.to_rc_value());
73-
environment.insert(Symbol::intern("defmacro"), defmacro_macro.to_rc_value());
74-
environment.insert(Symbol::intern("eval"), eval_fn.to_rc_value());
75-
environment.insert(
76-
Symbol::intern("lexical-eval"),
77-
lexical_eval_fn.to_rc_value(),
78-
);
79-
environment.insert(Symbol::intern("nth"), nth_fn.to_rc_value());
80-
environment.insert(Symbol::intern("concat"), concat_fn.to_rc_value());
81-
environment.insert(
82-
Symbol::intern("print-string"),
83-
print_string_fn.to_rc_value(),
84-
);
85-
//
86-
// Read in clojure.core
87-
//
88-
repl::try_eval_file(&environment, "./src/clojure/core.clj");
8933
//
9034
// Start repl
9135
//
92-
let stdin = io::stdin();
93-
print!("user=> ");
94-
let mut remaining_input_buffer = String::from("");
95-
for line in stdin.lock().lines() {
96-
let line = line.unwrap();
97-
remaining_input_buffer.push_str(&line);
98-
let mut remaining_input_bytes = remaining_input_buffer.as_bytes();
99-
loop {
100-
let next_read_parse = reader::try_read(remaining_input_bytes);
101-
match next_read_parse {
102-
Ok((_remaining_input_bytes, value)) => {
103-
print!(
104-
"{} ",
105-
value.eval(Rc::clone(&environment)).to_string_explicit()
106-
);
107-
remaining_input_bytes = _remaining_input_bytes;
108-
}
109-
Err(Incomplete(_)) => {
110-
remaining_input_buffer =
111-
String::from_utf8(remaining_input_bytes.to_vec()).unwrap();
112-
break;
113-
}
114-
err => {
115-
print!("{}", Value::Condition(format!("Reader Error: {:?}", err)));
116-
remaining_input_buffer = String::from("");
117-
break;
118-
}
119-
}
120-
}
121-
println!();
122-
print!("user=> ");
123-
}
36+
repl::repl();
12437
}

src/repl.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
use std::fs::File;
22
use std::io::BufRead;
33
use std::io::BufReader;
4-
use std::io::{self, Read};
5-
use std::str::FromStr;
4+
use std::io::{self};
5+
use std::io::Write;
66

77
use crate::environment::Environment;
88
use crate::reader;
99
use crate::value::Evaluable;
1010
use crate::value::Value;
1111
use std::rc::Rc;
1212

13-
use nom::error::convert_error;
1413
use nom::Err::Incomplete;
1514
use nom::Needed::Size;
1615

1716
//
1817
// Will possibly just add this to our environment, or turn this into a parallel of clojure.lang.RT
1918
//
20-
2119
pub fn try_eval_file(environment: &Rc<Environment>, filepath: &str) -> Result<(), io::Error> {
2220
let core = File::open(filepath)?;
2321

@@ -56,3 +54,45 @@ pub fn try_eval_file(environment: &Rc<Environment>, filepath: &str) -> Result<()
5654

5755
Ok(())
5856
}
57+
// @TODO eventually, this will likely be implemented purely in Clojure
58+
/// Starts an entirely new session of Clojure RS
59+
pub fn repl() {
60+
println!("Clojure RS 0.0.1");
61+
62+
let environment = Environment::clojure_core_environment();
63+
let stdin = io::stdin();
64+
65+
print!("user=> ");
66+
let _ = io::stdout().flush();
67+
let mut remaining_input_buffer = String::from("");
68+
for line in stdin.lock().lines() {
69+
let line = line.unwrap();
70+
remaining_input_buffer.push_str(&line);
71+
let mut remaining_input_bytes = remaining_input_buffer.as_bytes();
72+
loop {
73+
let next_read_parse = reader::try_read(remaining_input_bytes);
74+
match next_read_parse {
75+
Ok((_remaining_input_bytes, value)) => {
76+
print!(
77+
"{} ",
78+
value.eval(Rc::clone(&environment)).to_string_explicit()
79+
);
80+
remaining_input_bytes = _remaining_input_bytes;
81+
}
82+
Err(Incomplete(_)) => {
83+
remaining_input_buffer =
84+
String::from_utf8(remaining_input_bytes.to_vec()).unwrap();
85+
break;
86+
}
87+
err => {
88+
print!("{}", Value::Condition(format!("Reader Error: {:?}", err)));
89+
remaining_input_buffer = String::from("");
90+
break;
91+
}
92+
}
93+
}
94+
println!();
95+
print!("user=> ");
96+
let _ = io::stdout().flush();
97+
}
98+
}

0 commit comments

Comments
 (0)