Skip to content

Commit

Permalink
v0.7.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ocots committed Mar 31, 2024
1 parent f7f23cc commit 3bf56b8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 90 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CTBase"
uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd"
authors = ["Olivier Cots <[email protected]>"]
version = "0.7.11"
version = "0.7.12"

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

[compat]
Expand All @@ -30,4 +31,5 @@ Plots = "1.38"
PrettyTables = "2.2"
Printf = "1.8"
Reexport = "1.2"
ReplMaker = "0.2"
julia = "1.8"
8 changes: 4 additions & 4 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using Printf # to print an OptimalControlModel
using DataStructures # OrderedDict for aliases
using Unicode # unicode primitives
using PrettyTables # to print a table
#using ReplMaker
using ReplMaker
using MacroTools: @capture, postwalk, striplines
using LinearAlgebra

Expand Down Expand Up @@ -114,7 +114,7 @@ include("differential_geometry.jl")
include("ctparser_utils.jl")
##include("ctparser.jl")
include("onepass.jl")
#include("repl.jl")
include("repl.jl")

# numeric types
export ctNumber, ctVector, Time, Times, TimesDisc
Expand Down Expand Up @@ -166,7 +166,7 @@ export replace_call, constraint_type
export @def

# repl
##export ct_repl
##isdefined(Base, :active_repl) && ct_repl()
export ct_repl
isdefined(Base, :active_repl) && ct_repl()

end
184 changes: 99 additions & 85 deletions src/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,114 +13,127 @@ end
ct_repl_datas_data::Vector{CTRepl}=Vector{CTRepl}()
end

ct_repl_is_set = false

"""
$(TYPEDSIGNATURES)
Create a ct REPL.
"""
function ct_repl(; debug=false, demo=false)
function ct_repl(; debug=false, demo=false, verbose=false)

# init: ct_repl_data, history
ct_repl_data = CTRepl()
ct_repl_data.debug = debug
ct_repl_data.__demo = demo
history::HistoryRepl = HistoryRepl(0, Vector{ModelRepl}())
if !ct_repl_is_set

# if demo, print a message
demo && println("\nWelcome to the demo of the ct REPL.\n")
#
global ct_repl_is_set = true

# advice to start by setting the name of the ocp and the solution
println("To start, you should set the name of the optimal control problem and the name of the solution.")
println("For example, you can type:\n")
println(" ct> NAME=(ocp, sol)\n")
# init: ct_repl_data, history
ct_repl_data = CTRepl()
ct_repl_data.debug = debug
ct_repl_data.__demo = demo
history::HistoryRepl = HistoryRepl(0, Vector{ModelRepl}())

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

# text invalid
txt_invalid = "\nInvalid expression.\n\nType HELP to see the list of commands or enter a " *
"valid expression to update the model."
# advice to start by setting the name of the ocp and the solution
println("To start, you should set the name of the optimal control problem and the name of the solution.")
println("For example, you can type:\n")
println(" ct> NAME=(ocp, sol)\n")

function parse_to_expr(s::AbstractString)

# remove spaces from s at the beginning and at the end
s = strip(s)
# add initial ct_repl_data to history
__add!(history, ct_repl_data)

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

# parse string
e = Meta.parse(s)
function parse_to_expr(s::AbstractString)

# remove spaces from s at the beginning and at the end
s = strip(s)

#
ct_repl_data.debug && println("\ndebug> parsing string: ", s)
ct_repl_data.debug && println("debug> expression parsed: ", e)
ct_repl_data.debug && println("debug> expression type: ", typeof(e))
ct_repl_data.debug && println("debug> dump of expression: ", dump(e))

# test if e is a command
@match e begin
:( $c = $a ) => begin
command = __transform_to_command(c)
ct_repl_data.debug && println("debug> command: ", command, " and argument: ", a)
command keys(COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, a, history))
end
:( $c ) => begin
command = __transform_to_command(c)
ct_repl_data.debug && println("debug> command: ", command)
command keys(COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, history))
end
_ => nothing
end
# check if it is a comment
startswith(s, "#") && return nothing

# check if s finishes with a ";". If yes then remove it and return nothing at the end
return_nothing = endswith(s, ";") ? true : false
return_nothing && (s = s[1:end-1])
e = Meta.parse(s)
# parse string
e = Meta.parse(s)

#
return_nothing && ct_repl_data.debug && println("\ndebug> new parsing string: ", s)
return_nothing && ct_repl_data.debug && println("debug> new expression parsed: ", e)

if e isa Expr

# eval ocp to test if the expression is valid
ct_repl_data.debug && (println("debug> try to add expression: ", e))
try
__eval_ocp(ct_repl_data, e) # test if code is valid: if not, an exception is thrown
catch ex
ct_repl_data.debug && (println("debug> exception thrown: ", ex))
println(txt_invalid)
return nothing
#
ct_repl_data.debug && println("\ndebug> parsing string: ", s)
ct_repl_data.debug && println("debug> expression parsed: ", e)
ct_repl_data.debug && println("debug> expression type: ", typeof(e))
ct_repl_data.debug && println("debug> dump of expression: ", dump(e))

# test if e is a command
@match e begin
:( $c = $a ) => begin
command = __transform_to_command(c)
ct_repl_data.debug && println("debug> command: ", command, " and argument: ", a)
command keys(COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, a, history))
end
:( $c ) => begin
command = __transform_to_command(c)
ct_repl_data.debug && println("debug> command: ", command)
command keys(COMMANDS_ACTIONS) && (return COMMANDS_ACTIONS[command](ct_repl_data, history))
end
_ => nothing
end

# update model
__update!(ct_repl_data.model, e)
ct_repl_data.debug && (println("debug> expression valid, model updated."))

# add ct_repl_data to history
__add!(history, ct_repl_data)
# check if s finishes with a ";". If yes then remove it and return nothing at the end
return_nothing = endswith(s, ";") ? true : false
return_nothing && (s = s[1:end-1])
e = Meta.parse(s)

#
return return_nothing ? nothing : __quote_ocp(ct_repl_data)

else
return_nothing && ct_repl_data.debug && println("\ndebug> new parsing string: ", s)
return_nothing && ct_repl_data.debug && println("debug> new expression parsed: ", e)

if e isa Expr

# eval ocp to test if the expression is valid
ct_repl_data.debug && (println("debug> try to add expression: ", e))
try
__eval_ocp(ct_repl_data, e) # test if code is valid: if not, an exception is thrown
catch ex
ct_repl_data.debug && (println("debug> exception thrown: ", ex))
println(txt_invalid)
return nothing
end

# update model
__update!(ct_repl_data.model, e)
ct_repl_data.debug && (println("debug> expression valid, model updated."))

# add ct_repl_data to history
__add!(history, ct_repl_data)

#
return return_nothing ? nothing : __quote_ocp(ct_repl_data)

else

println(txt_invalid)
return nothing
println(txt_invalid)
return nothing

end
end

end # parse_to_expr
end # parse_to_expr

# makerepl command
initrepl(parse_to_expr,
prompt_text="ct> ",
prompt_color = :magenta,
start_key='>',
mode_name="ct_mode",
valid_input_checker=complete_julia,
startup_text=false)
# makerepl command
initrepl(parse_to_expr,
prompt_text="ct> ",
prompt_color = :magenta,
start_key='>',
mode_name="ct_mode",
valid_input_checker=complete_julia,
startup_text=false)

else
if verbose
println("ct repl is already set.")
end
end

end

Expand Down Expand Up @@ -286,8 +299,9 @@ end

# get code from model and an extra expression
function __code(model::ModelRepl, e::Expr)
println("ici")
model_ = deepcopy(model) # copy model
__update!(model_, e) # update model_
__update!(model_, e) # update model_
return __code(model_) # get code
end

Expand Down

0 comments on commit 3bf56b8

Please sign in to comment.