@@ -13,114 +13,127 @@ end
13
13
ct_repl_datas_data:: Vector{CTRepl} = Vector {CTRepl} ()
14
14
end
15
15
16
+ ct_repl_is_set = false
17
+
16
18
"""
17
19
$(TYPEDSIGNATURES)
18
20
19
21
Create a ct REPL.
20
22
"""
21
- function ct_repl (; debug= false , demo= false )
23
+ function ct_repl (; debug= false , demo= false , verbose = false )
22
24
23
- # init: ct_repl_data, history
24
- ct_repl_data = CTRepl ()
25
- ct_repl_data. debug = debug
26
- ct_repl_data. __demo = demo
27
- history:: HistoryRepl = HistoryRepl (0 , Vector {ModelRepl} ())
25
+ if ! ct_repl_is_set
28
26
29
- # if demo, print a message
30
- demo && println ( " \n Welcome to the demo of the ct REPL. \n " )
27
+ #
28
+ global ct_repl_is_set = true
31
29
32
- # advice to start by setting the name of the ocp and the solution
33
- println (" To start, you should set the name of the optimal control problem and the name of the solution." )
34
- println (" For example, you can type:\n " )
35
- println (" ct> NAME=(ocp, sol)\n " )
30
+ # init: ct_repl_data, history
31
+ ct_repl_data = CTRepl ()
32
+ ct_repl_data. debug = debug
33
+ ct_repl_data. __demo = demo
34
+ history:: HistoryRepl = HistoryRepl (0 , Vector {ModelRepl} ())
36
35
37
- # add initial ct_repl_data to history
38
- __add! (history, ct_repl_data )
36
+ # if demo, print a message
37
+ demo && println ( " \n Welcome to the demo of the ct REPL. \n " )
39
38
40
- # text invalid
41
- txt_invalid = " \n Invalid expression.\n\n Type HELP to see the list of commands or enter a " *
42
- " valid expression to update the model."
39
+ # advice to start by setting the name of the ocp and the solution
40
+ println (" To start, you should set the name of the optimal control problem and the name of the solution." )
41
+ println (" For example, you can type:\n " )
42
+ println (" ct> NAME=(ocp, sol)\n " )
43
43
44
- function parse_to_expr (s:: AbstractString )
45
-
46
- # remove spaces from s at the beginning and at the end
47
- s = strip (s)
44
+ # add initial ct_repl_data to history
45
+ __add! (history, ct_repl_data)
48
46
49
- # check if it is a comment
50
- startswith (s, " #" ) && return nothing
47
+ # text invalid
48
+ txt_invalid = " \n Invalid expression.\n\n Type HELP to see the list of commands or enter a " *
49
+ " valid expression to update the model."
51
50
52
- # parse string
53
- e = Meta. parse (s)
51
+ function parse_to_expr (s:: AbstractString )
52
+
53
+ # remove spaces from s at the beginning and at the end
54
+ s = strip (s)
54
55
55
- #
56
- ct_repl_data. debug && println (" \n debug> parsing string: " , s)
57
- ct_repl_data. debug && println (" debug> expression parsed: " , e)
58
- ct_repl_data. debug && println (" debug> expression type: " , typeof (e))
59
- ct_repl_data. debug && println (" debug> dump of expression: " , dump (e))
60
-
61
- # test if e is a command
62
- @match e begin
63
- :( $ c = $ a ) => begin
64
- command = __transform_to_command (c)
65
- ct_repl_data. debug && println (" debug> command: " , command, " and argument: " , a)
66
- command ∈ keys (COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, a, history))
67
- end
68
- :( $ c ) => begin
69
- command = __transform_to_command (c)
70
- ct_repl_data. debug && println (" debug> command: " , command)
71
- command ∈ keys (COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, history))
72
- end
73
- _ => nothing
74
- end
56
+ # check if it is a comment
57
+ startswith (s, " #" ) && return nothing
75
58
76
- # check if s finishes with a ";". If yes then remove it and return nothing at the end
77
- return_nothing = endswith (s, " ;" ) ? true : false
78
- return_nothing && (s = s[1 : end - 1 ])
79
- e = Meta. parse (s)
59
+ # parse string
60
+ e = Meta. parse (s)
80
61
81
- #
82
- return_nothing && ct_repl_data. debug && println (" \n debug> new parsing string: " , s)
83
- return_nothing && ct_repl_data. debug && println (" debug> new expression parsed: " , e)
84
-
85
- if e isa Expr
86
-
87
- # eval ocp to test if the expression is valid
88
- ct_repl_data. debug && (println (" debug> try to add expression: " , e))
89
- try
90
- __eval_ocp (ct_repl_data, e) # test if code is valid: if not, an exception is thrown
91
- catch ex
92
- ct_repl_data. debug && (println (" debug> exception thrown: " , ex))
93
- println (txt_invalid)
94
- return nothing
62
+ #
63
+ ct_repl_data. debug && println (" \n debug> parsing string: " , s)
64
+ ct_repl_data. debug && println (" debug> expression parsed: " , e)
65
+ ct_repl_data. debug && println (" debug> expression type: " , typeof (e))
66
+ ct_repl_data. debug && println (" debug> dump of expression: " , dump (e))
67
+
68
+ # test if e is a command
69
+ @match e begin
70
+ :( $ c = $ a ) => begin
71
+ command = __transform_to_command (c)
72
+ ct_repl_data. debug && println (" debug> command: " , command, " and argument: " , a)
73
+ command ∈ keys (COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, a, history))
74
+ end
75
+ :( $ c ) => begin
76
+ command = __transform_to_command (c)
77
+ ct_repl_data. debug && println (" debug> command: " , command)
78
+ command ∈ keys (COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, history))
79
+ end
80
+ _ => nothing
95
81
end
96
-
97
- # update model
98
- __update! (ct_repl_data. model, e)
99
- ct_repl_data. debug && (println (" debug> expression valid, model updated." ))
100
82
101
- # add ct_repl_data to history
102
- __add! (history, ct_repl_data)
83
+ # check if s finishes with a ";". If yes then remove it and return nothing at the end
84
+ return_nothing = endswith (s, " ;" ) ? true : false
85
+ return_nothing && (s = s[1 : end - 1 ])
86
+ e = Meta. parse (s)
103
87
104
88
#
105
- return return_nothing ? nothing : __quote_ocp (ct_repl_data)
106
-
107
- else
89
+ return_nothing && ct_repl_data. debug && println (" \n debug> new parsing string: " , s)
90
+ return_nothing && ct_repl_data. debug && println (" debug> new expression parsed: " , e)
91
+
92
+ if e isa Expr
93
+
94
+ # eval ocp to test if the expression is valid
95
+ ct_repl_data. debug && (println (" debug> try to add expression: " , e))
96
+ try
97
+ __eval_ocp (ct_repl_data, e) # test if code is valid: if not, an exception is thrown
98
+ catch ex
99
+ ct_repl_data. debug && (println (" debug> exception thrown: " , ex))
100
+ println (txt_invalid)
101
+ return nothing
102
+ end
103
+
104
+ # update model
105
+ __update! (ct_repl_data. model, e)
106
+ ct_repl_data. debug && (println (" debug> expression valid, model updated." ))
107
+
108
+ # add ct_repl_data to history
109
+ __add! (history, ct_repl_data)
110
+
111
+ #
112
+ return return_nothing ? nothing : __quote_ocp (ct_repl_data)
113
+
114
+ else
108
115
109
- println (txt_invalid)
110
- return nothing
116
+ println (txt_invalid)
117
+ return nothing
111
118
112
- end
119
+ end
113
120
114
- end # parse_to_expr
121
+ end # parse_to_expr
115
122
116
- # makerepl command
117
- initrepl (parse_to_expr,
118
- prompt_text= " ct> " ,
119
- prompt_color = :magenta ,
120
- start_key= ' >' ,
121
- mode_name= " ct_mode" ,
122
- valid_input_checker= complete_julia,
123
- startup_text= false )
123
+ # makerepl command
124
+ initrepl (parse_to_expr,
125
+ prompt_text= " ct> " ,
126
+ prompt_color = :magenta ,
127
+ start_key= ' >' ,
128
+ mode_name= " ct_mode" ,
129
+ valid_input_checker= complete_julia,
130
+ startup_text= false )
131
+
132
+ else
133
+ if verbose
134
+ println (" ct repl is already set." )
135
+ end
136
+ end
124
137
125
138
end
126
139
286
299
287
300
# get code from model and an extra expression
288
301
function __code (model:: ModelRepl , e:: Expr )
302
+ println (" ici" )
289
303
model_ = deepcopy (model) # copy model
290
- __update! (model_, e) # update model_
304
+ __update! (model_, e) # update model_
291
305
return __code (model_) # get code
292
306
end
293
307
0 commit comments