From 71d0cbe6cf84ee2e17ddd817d3f92b98caed2821 Mon Sep 17 00:00:00 2001 From: Jonnie Diegelman Date: Tue, 15 Sep 2020 18:22:46 -0400 Subject: [PATCH 1/2] Add repl mode --- Project.toml | 3 ++- src/MATLAB.jl | 10 ++++++++++ src/matstr.jl | 12 +++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index e3ebe92..3bf77a9 100644 --- a/Project.toml +++ b/Project.toml @@ -2,10 +2,11 @@ name = "MATLAB" uuid = "10e44e05-a98a-55b3-a45b-ba969058deb6" repo = "https://github.com/JuliaInterop/MATLAB.jl.git" license = "MIT" -version = "v0.7.0" +version = "v0.8.0" [deps] Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" +ReplMaker = "b873ce64-0db9-51f5-a568-4457d8e49576" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [extras] diff --git a/src/MATLAB.jl b/src/MATLAB.jl index 68046e5..f010508 100644 --- a/src/MATLAB.jl +++ b/src/MATLAB.jl @@ -2,6 +2,7 @@ module MATLAB using Base.Sys: islinux, iswindows, isapple using Libdl +using ReplMaker using SparseArrays import Base: eltype, close, size, copy, ndims, unsafe_convert @@ -45,6 +46,7 @@ include("mxarray.jl") include("matfile.jl") include("engine.jl") include("matstr.jl") +# include("repl.jl") if iswindows() # workaround "primary message table for module 77" error @@ -166,6 +168,14 @@ function __init__() mat_put_variable[] = matfunc(:matPutVariable) mat_get_dir[] = matfunc(:matGetDir) + + if isinteractive() + initrepl(str -> Meta.parse("MATLAB.replmat\"$str\""); + prompt_text = ">> ", + start_key = ">", + mode_name = "MATLAB-Mode", + ) + end end diff --git a/src/matstr.jl b/src/matstr.jl index 835f171..9f7d379 100644 --- a/src/matstr.jl +++ b/src/matstr.jl @@ -94,7 +94,7 @@ function check_assignment(interp, i) return (assigned, used) end -function do_mat_str(ex) +function do_mat_str(ex, interactive=false) # Hack to do interpolation interp = Meta.parse(string("\"\"\"", replace(ex, "\"\"\"" => "\\\"\"\""), "\"\"\"")) if isa(interp, String) @@ -151,7 +151,9 @@ function do_mat_str(ex) # Add a semicolon to the end of the last statement to suppress output isa(interp[end], String) && (interp[end] = rstrip(interp[end])) - push!(interp, ";") + if !interactive || !isempty(assignedvars) + push!(interp, ";") + end # Figure out if `ans` exists in code to avoid an error if it doesn't push!(interp, "\nmatlab_jl_has_ans = exist('ans', 'var');") @@ -164,7 +166,7 @@ function do_mat_str(ex) # Clear variables we created :(eval_string($(string("clear ", join(union(usedvars, assignedvars), " "), ";")))) end) - if get_variable(:matlab_jl_has_ans) != 0 + if $(!interactive) && get_variable(:matlab_jl_has_ans) != 0 # Return ans if it was set get_variable(:ans) end @@ -174,3 +176,7 @@ end macro mat_str(ex) do_mat_str(ex) end + +macro replmat_str(ex) + do_mat_str(ex, true) +end \ No newline at end of file From 8ca53a4b65a423e3983554ae0041845b95f5982a Mon Sep 17 00:00:00 2001 From: Jonnie Diegelman Date: Tue, 15 Sep 2020 18:26:11 -0400 Subject: [PATCH 2/2] Add repl mode --- src/MATLAB.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/MATLAB.jl b/src/MATLAB.jl index f010508..0341ea4 100644 --- a/src/MATLAB.jl +++ b/src/MATLAB.jl @@ -46,7 +46,6 @@ include("mxarray.jl") include("matfile.jl") include("engine.jl") include("matstr.jl") -# include("repl.jl") if iswindows() # workaround "primary message table for module 77" error @@ -173,7 +172,7 @@ function __init__() initrepl(str -> Meta.parse("MATLAB.replmat\"$str\""); prompt_text = ">> ", start_key = ">", - mode_name = "MATLAB-Mode", + mode_name = "MATLAB-Mode", ) end end