2
2
#![ allow( rustdoc:: missing_crate_level_docs) ] // it's an example
3
3
4
4
use eframe:: egui:: * ;
5
- use mech_core:: * ;
6
- use mech_syntax:: parser;
7
- use mech_interpreter:: * ;
5
+ use mech:: * ;
8
6
use mech_notebook:: * ;
9
7
use std:: sync:: Arc ;
10
8
11
9
const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
12
10
13
11
fn main ( ) -> eframe:: Result {
14
- env_logger:: init ( ) ; // Log to stderr (if you run with `RUST_LOG=debug`).
15
-
16
12
let icon = icon:: load_icon ( ) ;
17
13
18
-
19
14
let options = eframe:: NativeOptions {
20
- viewport : egui:: ViewportBuilder :: default ( ) . with_inner_size ( [ 1024.0 , 768.0 ] ) . with_icon ( Arc :: new ( icon) ) ,
21
- ..Default :: default ( )
15
+ viewport : egui:: ViewportBuilder :: default ( ) . with_inner_size ( [ 1024.0 , 768.0 ] ) . with_icon ( Arc :: new ( icon) ) ,
16
+ ..Default :: default ( )
22
17
} ;
23
18
24
- // Our application state:
25
- let mut terminal_input = String :: new ( ) ;
19
+ let mut input = String :: new ( ) ;
26
20
let mut terminal_output = String :: new ( ) ;
27
21
let mut text_edit_focus_id = egui:: Id :: new ( "terminal_input" ) ;
28
- let mut intrp = Interpreter :: new ( ) ;
22
+
23
+ let id = hash_str ( "mech-notebook" ) ;
24
+ let mut intrp = Interpreter :: new ( id) ;
25
+ let mut repl = MechRepl :: from ( intrp) ;
26
+
29
27
let mut scroll_to_bottom = false ;
30
28
terminal_output. push_str ( & format ! ( "Mech v{}\n " , VERSION ) ) ;
31
-
32
29
eframe:: run_simple_native ( "Mech Terminal" , options, move |ctx, _frame| {
33
30
34
31
let mut visuals = egui:: Visuals :: dark ( ) ;
@@ -38,7 +35,7 @@ fn main() -> eframe::Result {
38
35
39
36
40
37
let mut fonts = FontDefinitions :: default ( ) ;
41
- fonts. font_data . insert ( "FiraCode-Regular" . to_owned ( ) , FontData :: from_static ( include_bytes ! ( "../../fonts/FiraCode-Regular.ttf" ) ) ) ;
38
+ fonts. font_data . insert ( "FiraCode-Regular" . to_owned ( ) , Arc :: new ( FontData :: from_static ( include_bytes ! ( "../../fonts/FiraCode-Regular.ttf" ) ) ) ) ;
42
39
fonts. families . get_mut ( & FontFamily :: Proportional ) . unwrap ( ) . insert ( 0 , "FiraCode-Regular" . to_owned ( ) ) ;
43
40
ctx. set_fonts ( fonts) ;
44
41
@@ -60,31 +57,43 @@ fn main() -> eframe::Result {
60
57
ui. horizontal ( |ui| {
61
58
ui. label ( ">:" ) ;
62
59
let response = ui. add (
63
- egui:: TextEdit :: singleline ( & mut terminal_input )
60
+ egui:: TextEdit :: singleline ( & mut input )
64
61
. id ( text_edit_focus_id)
65
62
. frame ( false )
66
63
) ;
67
-
68
64
if response. lost_focus ( ) && ctx. input ( |i| i. key_pressed ( egui:: Key :: Enter ) ) {
69
- terminal_output. push_str ( & format ! ( ">: {}\n " , terminal_input) ) ;
70
- match parser:: parse ( & terminal_input) {
71
- Ok ( tree) => {
72
- let result = intrp. interpret ( & tree) ;
73
- let out_str = match result {
74
- Ok ( r) => format ! ( "{}\n " , r. pretty_print( ) ) ,
75
- Err ( err) => format ! ( "{:?}" , err) ,
76
- } ;
77
- terminal_output. push_str ( & out_str) ;
78
- scroll_to_bottom = true ;
65
+ terminal_output. push_str ( & format ! ( ">: {}\n " , input) ) ;
66
+ if input. chars ( ) . nth ( 0 ) == Some ( ':' ) {
67
+ match MechRepl :: parse_repl_command ( & input. as_str ( ) ) {
68
+ Ok ( ( _, repl_command) ) => {
69
+ match repl. execute_repl_command ( repl_command) {
70
+ Ok ( output) => {
71
+ terminal_output. push_str ( & format ! ( "{}\n " , output) ) ;
72
+ }
73
+ Err ( err) => {
74
+ terminal_output. push_str ( & format ! ( "{:?}\n " , err) ) ;
75
+ }
76
+ }
77
+ }
78
+ _ => todo ! ( ) ,
79
79
}
80
- Err ( err) => {
81
-
80
+ } else if input. trim ( ) == "" {
81
+ //continue;
82
+ } else {
83
+ let cmd = ReplCommand :: Code ( vec ! [ ( "repl" . to_string( ) , MechSourceCode :: String ( input. clone( ) ) ) ] ) ;
84
+ match repl. execute_repl_command ( cmd) {
85
+ Ok ( output) => {
86
+ terminal_output. push_str ( & format ! ( "{}\n " , output) ) ;
87
+ }
88
+ Err ( err) => {
89
+ terminal_output. push_str ( & format ! ( "{:?}\n " , err) ) ;
90
+ }
82
91
}
83
92
}
84
- terminal_input . clear ( ) ;
93
+ input . clear ( ) ;
85
94
}
86
95
response. request_focus ( ) ;
87
96
} ) ;
88
97
} ) ;
89
98
} )
90
- }
99
+ }
0 commit comments