Created
April 21, 2015 01:21
-
-
Save IainNZ/e115e59612e770ba74cd to your computer and use it in GitHub Desktop.
JuMP REPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using JuMP | |
_model = Model() | |
import Base: LineEdit, REPL | |
function handle_line(line) | |
if line[1:3] == "Var" | |
Base.parse_input_line("@defVar(_model,$(line[5:end]))") | |
elseif line[1:3] == "Max" || line[1:3] == "Min" | |
Base.parse_input_line("@setObjective(_model,$(line[1:3]),$(line[5:end]))") | |
elseif line[1:3] == "Sol" | |
Base.parse_input_line("solve(_model)") | |
else | |
Base.parse_input_line("@addConstraint(_model,$(line))") | |
end | |
end | |
function run_JuMP_repl() | |
prompt = LineEdit.Prompt("JuMP > ") | |
repl = Base.active_repl | |
prompt.on_done = REPL.respond(handle_line,repl,prompt) | |
main_mode = repl.interface.modes[1] | |
push!(repl.interface.modes,prompt) | |
hp = main_mode.hist | |
hp.mode_mapping[:jump] = prompt | |
prompt.hist = hp | |
const jump_keymap = Dict{Any,Any}( | |
'<' => function (s,args...) | |
if isempty(s) | |
if !haskey(s.mode_state,prompt) | |
s.mode_state[prompt] = LineEdit.init_state(repl.t,prompt) | |
end | |
LineEdit.transition(s,prompt) | |
else | |
LineEdit.edit_insert(s,'<') | |
end | |
end | |
) | |
search_prompt, skeymap = LineEdit.setup_search_keymap(hp) | |
mk = REPL.mode_keymap(main_mode) | |
b = Dict{Any,Any}[skeymap, mk, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults] | |
prompt.keymap_dict = LineEdit.keymap(b) | |
main_mode.keymap_dict = LineEdit.keymap_merge(main_mode.keymap_dict, jump_keymap); | |
nothing | |
end | |
run_JuMP_repl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment