@@ -12,6 +12,7 @@ mod lambda;
12
12
mod reader;
13
13
mod persistent_list;
14
14
mod persistent_vector;
15
+ mod repl;
15
16
16
17
use environment:: Environment ;
17
18
@@ -29,15 +30,25 @@ use crate::persistent_list::{PersistentList,ToPersistentList};
29
30
use crate :: persistent_vector:: { PersistentVector , ToPersistentVector } ;
30
31
use crate :: value:: Value ;
31
32
33
+ use nom:: Err :: Incomplete ;
34
+ use nom:: error:: convert_error;
35
+ use nom:: Needed :: Size ;
36
+
37
+
32
38
fn main ( )
33
39
{
40
+ println ! ( "Clojure RS 0.0.1" ) ;
41
+
34
42
// Register our macros / functions ahead of time
35
43
let add_fn = rust_core:: AddFn { } ;
36
44
let str_fn = rust_core:: StrFn { } ;
37
45
let do_fn = rust_core:: DoFn { } ;
38
46
let nth_fn = rust_core:: NthFn { } ;
39
47
let do_macro = rust_core:: DoMacro { } ;
40
48
let concat_fn = rust_core:: ConcatFn { } ;
49
+ let print_string_fn = rust_core:: PrintStringFn { } ;
50
+ // Hardcoded fns
51
+ let lexical_eval_fn = Value :: LexicalEvalFn { } ;
41
52
// Hardcoded macros
42
53
let let_macro = Value :: LetMacro { } ;
43
54
let quote_macro = Value :: QuoteMacro { } ;
@@ -59,24 +70,38 @@ fn main()
59
70
environment. insert ( Symbol :: intern ( "fn" ) , fn_macro. to_rc_value ( ) ) ;
60
71
environment. insert ( Symbol :: intern ( "defmacro" ) , defmacro_macro. to_rc_value ( ) ) ;
61
72
environment. insert ( Symbol :: intern ( "eval" ) , eval_fn. to_rc_value ( ) ) ;
73
+ environment. insert ( Symbol :: intern ( "lexical-eval" ) , lexical_eval_fn. to_rc_value ( ) ) ;
62
74
environment. insert ( Symbol :: intern ( "nth" ) , nth_fn. to_rc_value ( ) ) ;
63
75
environment. insert ( Symbol :: intern ( "concat" ) , concat_fn. to_rc_value ( ) ) ;
76
+ environment. insert ( Symbol :: intern ( "print-string" ) , print_string_fn. to_rc_value ( ) ) ;
77
+ //
78
+ // Read in clojure.core
79
+ //
80
+ repl:: try_eval_file ( & environment, "./src/clojure/core.clj" ) ;
64
81
//
65
82
// Start repl
66
83
//
67
84
let stdin = io:: stdin ( ) ;
68
85
print ! ( "user=> " ) ;
86
+ let mut remaining_input_buffer = String :: from ( "" ) ;
69
87
for line in stdin. lock ( ) . lines ( ) {
70
88
let line = line. unwrap ( ) ;
71
- let mut remaining_input = line. as_bytes ( ) ;
89
+ remaining_input_buffer. push_str ( & line) ;
90
+ let mut remaining_input_bytes = remaining_input_buffer. as_bytes ( ) ;
72
91
loop {
73
- let next_read_parse = reader:: try_read ( remaining_input ) ;
92
+ let next_read_parse = reader:: try_read ( remaining_input_bytes ) ;
74
93
match next_read_parse {
75
- Ok ( ( _remaining_input , value) ) => {
94
+ Ok ( ( _remaining_input_bytes , value) ) => {
76
95
print ! ( "{} " , value. eval( Rc :: clone( & environment) ) . to_string_explicit( ) ) ;
77
- remaining_input = _remaining_input;
96
+ remaining_input_bytes = _remaining_input_bytes;
97
+ } ,
98
+ Err ( Incomplete ( _) ) => {
99
+ remaining_input_buffer = String :: from_utf8 ( remaining_input_bytes. to_vec ( ) ) . unwrap ( ) ;
100
+ break ;
78
101
} ,
79
- _ => {
102
+ err => {
103
+ print ! ( "{}" , Value :: Condition ( format!( "Reader Error: {:?}" , err) ) ) ;
104
+ remaining_input_buffer = String :: from ( "" ) ;
80
105
break ;
81
106
}
82
107
}
0 commit comments