Skip to content

Commit 3bf56b8

Browse files
committed
v0.7.12
1 parent f7f23cc commit 3bf56b8

File tree

3 files changed

+106
-90
lines changed

3 files changed

+106
-90
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CTBase"
22
uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd"
33
authors = ["Olivier Cots <[email protected]>"]
4-
version = "0.7.11"
4+
version = "0.7.12"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -16,6 +16,7 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1616
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
1717
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1818
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
19+
ReplMaker = "b873ce64-0db9-51f5-a568-4457d8e49576"
1920
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
2021

2122
[compat]
@@ -30,4 +31,5 @@ Plots = "1.38"
3031
PrettyTables = "2.2"
3132
Printf = "1.8"
3233
Reexport = "1.2"
34+
ReplMaker = "0.2"
3335
julia = "1.8"

src/CTBase.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using Printf # to print an OptimalControlModel
2424
using DataStructures # OrderedDict for aliases
2525
using Unicode # unicode primitives
2626
using PrettyTables # to print a table
27-
#using ReplMaker
27+
using ReplMaker
2828
using MacroTools: @capture, postwalk, striplines
2929
using LinearAlgebra
3030

@@ -114,7 +114,7 @@ include("differential_geometry.jl")
114114
include("ctparser_utils.jl")
115115
##include("ctparser.jl")
116116
include("onepass.jl")
117-
#include("repl.jl")
117+
include("repl.jl")
118118

119119
# numeric types
120120
export ctNumber, ctVector, Time, Times, TimesDisc
@@ -166,7 +166,7 @@ export replace_call, constraint_type
166166
export @def
167167

168168
# repl
169-
##export ct_repl
170-
##isdefined(Base, :active_repl) && ct_repl()
169+
export ct_repl
170+
isdefined(Base, :active_repl) && ct_repl()
171171

172172
end

src/repl.jl

Lines changed: 99 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,114 +13,127 @@ end
1313
ct_repl_datas_data::Vector{CTRepl}=Vector{CTRepl}()
1414
end
1515

16+
ct_repl_is_set = false
17+
1618
"""
1719
$(TYPEDSIGNATURES)
1820
1921
Create a ct REPL.
2022
"""
21-
function ct_repl(; debug=false, demo=false)
23+
function ct_repl(; debug=false, demo=false, verbose=false)
2224

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
2826

29-
# if demo, print a message
30-
demo && println("\nWelcome to the demo of the ct REPL.\n")
27+
#
28+
global ct_repl_is_set = true
3129

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}())
3635

37-
# add initial ct_repl_data to history
38-
__add!(history, ct_repl_data)
36+
# if demo, print a message
37+
demo && println("\nWelcome to the demo of the ct REPL.\n")
3938

40-
# text invalid
41-
txt_invalid = "\nInvalid expression.\n\nType 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")
4343

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)
4846

49-
# check if it is a comment
50-
startswith(s, "#") && return nothing
47+
# text invalid
48+
txt_invalid = "\nInvalid expression.\n\nType HELP to see the list of commands or enter a " *
49+
"valid expression to update the model."
5150

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)
5455

55-
#
56-
ct_repl_data.debug && println("\ndebug> 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
7558

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)
8061

81-
#
82-
return_nothing && ct_repl_data.debug && println("\ndebug> 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("\ndebug> 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
9581
end
96-
97-
# update model
98-
__update!(ct_repl_data.model, e)
99-
ct_repl_data.debug && (println("debug> expression valid, model updated."))
10082

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)
10387

10488
#
105-
return return_nothing ? nothing : __quote_ocp(ct_repl_data)
106-
107-
else
89+
return_nothing && ct_repl_data.debug && println("\ndebug> 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
108115

109-
println(txt_invalid)
110-
return nothing
116+
println(txt_invalid)
117+
return nothing
111118

112-
end
119+
end
113120

114-
end # parse_to_expr
121+
end # parse_to_expr
115122

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
124137

125138
end
126139

@@ -286,8 +299,9 @@ end
286299

287300
# get code from model and an extra expression
288301
function __code(model::ModelRepl, e::Expr)
302+
println("ici")
289303
model_ = deepcopy(model) # copy model
290-
__update!(model_, e) # update model_
304+
__update!(model_, e) # update model_
291305
return __code(model_) # get code
292306
end
293307

0 commit comments

Comments
 (0)