diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-17 22:23:07 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-17 22:23:07 -0500 |
| commit | 718887c3019c49fc807bc18fbd5feb975ec03c85 (patch) | |
| tree | 8b0d1d2e501541c015e66f4cde0510bdd94db195 | |
| parent | db4c329aff4621e05b92a55be4f18173f5a4f655 (diff) | |
| download | mal-718887c3019c49fc807bc18fbd5feb975ec03c85.tar.gz mal-718887c3019c49fc807bc18fbd5feb975ec03c85.zip | |
Ruby,Python,C#: readline history fixes. Ruby include path.
| -rw-r--r-- | cs/getline.cs | 5 | ||||
| -rw-r--r-- | docs/TODO | 5 | ||||
| -rw-r--r-- | python/mal_readline.py | 2 | ||||
| -rw-r--r-- | ruby/Makefile | 2 | ||||
| -rw-r--r-- | ruby/mal_readline.rb | 18 | ||||
| -rw-r--r-- | ruby/step0_repl.rb | 5 | ||||
| -rw-r--r-- | ruby/step1_read_print.rb | 5 | ||||
| -rw-r--r-- | ruby/step2_eval.rb | 5 | ||||
| -rw-r--r-- | ruby/step3_env.rb | 5 | ||||
| -rw-r--r-- | ruby/step4_if_fn_do.rb | 5 | ||||
| -rw-r--r-- | ruby/step5_tco.rb | 5 | ||||
| -rw-r--r-- | ruby/step6_file.rb | 5 | ||||
| -rw-r--r-- | ruby/step7_quote.rb | 5 | ||||
| -rw-r--r-- | ruby/step8_macros.rb | 5 | ||||
| -rw-r--r-- | ruby/step9_interop.rb | 5 | ||||
| -rw-r--r-- | ruby/stepA_more.rb | 5 |
16 files changed, 59 insertions, 28 deletions
diff --git a/cs/getline.cs b/cs/getline.cs index b6dcf07..c11a11d 100644 --- a/cs/getline.cs +++ b/cs/getline.cs @@ -895,8 +895,9 @@ namespace Mono.Terminal { throw new ArgumentException ("size"); if (app != null){ - string dir = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); + string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal); //Console.WriteLine (dir); + /* if (!Directory.Exists (dir)){ try { Directory.CreateDirectory (dir); @@ -906,6 +907,8 @@ namespace Mono.Terminal { } if (app != null) histfile = Path.Combine (dir, app) + ".history"; + */ + histfile = Path.Combine (dir, ".mal-history"); } history = new string [size]; @@ -17,13 +17,13 @@ All: --------------------------------------------- Bash: + - explore using ${!prefix*} syntax (more like make impl) C: - come up with better way to do 20 vararg code C#: - step9_interop - - use same history file Clojure: @@ -37,7 +37,6 @@ Make: - allow '_' in make variable names - errors should propagate up from within load-file - Mal: - line numbers in errors - step6_file: command line arguments @@ -52,8 +51,6 @@ Postscript: Python: Ruby: - - save history and use same history file - - setup require search path --------------------------------------------- diff --git a/python/mal_readline.py b/python/mal_readline.py index 2add6b5..fc22b58 100644 --- a/python/mal_readline.py +++ b/python/mal_readline.py @@ -8,7 +8,9 @@ else: rl = raw_input def readline(prompt="user> "): + global history_loaded if not history_loaded: + history_loaded = True try: with open(histfile, "r") as hf: for line in hf.readlines(): diff --git a/ruby/Makefile b/ruby/Makefile index bfaa826..71ab92c 100644 --- a/ruby/Makefile +++ b/ruby/Makefile @@ -1,6 +1,6 @@ TESTS = -SOURCES_BASE = types.rb reader.rb printer.rb +SOURCES_BASE = mal_readline.rb types.rb reader.rb printer.rb SOURCES_LISP = env.rb core.rb stepA_more.rb SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) diff --git a/ruby/mal_readline.rb b/ruby/mal_readline.rb new file mode 100644 index 0000000..63c5571 --- /dev/null +++ b/ruby/mal_readline.rb @@ -0,0 +1,18 @@ +require "readline" + +$history_loaded = false +$histfile = "#{ENV['HOME']}/.mal-history" + +def _readline(prompt) + if not $history_loaded + $history_loaded = true + File.readlines($histfile).each {|l| Readline::HISTORY.push(l.chomp)} + end + + if line = Readline.readline(prompt, true) + File.open($histfile, 'a+') {|f| f.write(line+"\n")} + return line + else + return nil + end +end diff --git a/ruby/step0_repl.rb b/ruby/step0_repl.rb index dd32b27..21d534a 100644 --- a/ruby/step0_repl.rb +++ b/ruby/step0_repl.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" # read def READ(str) @@ -20,6 +21,6 @@ def REP(str) return PRINT(EVAL(READ(str), {})) end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") puts REP(line) end diff --git a/ruby/step1_read_print.rb b/ruby/step1_read_print.rb index 20dd450..617323e 100644 --- a/ruby/step1_read_print.rb +++ b/ruby/step1_read_print.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -23,7 +24,7 @@ def REP(str) return PRINT(EVAL(READ(str), {})) end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP(line) rescue Exception => e diff --git a/ruby/step2_eval.rb b/ruby/step2_eval.rb index 9318099..1454d8c 100644 --- a/ruby/step2_eval.rb +++ b/ruby/step2_eval.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -54,7 +55,7 @@ repl_env[:-] = lambda {|a,b| a - b} repl_env[:*] = lambda {|a,b| a * b} repl_env[:/] = lambda {|a,b| a / b} -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step3_env.rb b/ruby/step3_env.rb index 7266424..88811f0 100644 --- a/ruby/step3_env.rb +++ b/ruby/step3_env.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -66,7 +67,7 @@ repl_env.set(:-, lambda {|a,b| a - b}) repl_env.set(:*, lambda {|a,b| a * b}) repl_env.set(:/, lambda {|a,b| a / b}) -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step4_if_fn_do.rb b/ruby/step4_if_fn_do.rb index 559f81e..4ae0d86 100644 --- a/ruby/step4_if_fn_do.rb +++ b/ruby/step4_if_fn_do.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -85,7 +86,7 @@ repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step5_tco.rb b/ruby/step5_tco.rb index c6b5782..e2fe5e7 100644 --- a/ruby/step5_tco.rb +++ b/ruby/step5_tco.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -94,7 +95,7 @@ repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step6_file.rb b/ruby/step6_file.rb index 6ca0d6d..45de088 100644 --- a/ruby/step6_file.rb +++ b/ruby/step6_file.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -101,7 +102,7 @@ if ARGV.size > 0 } exit 0 end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step7_quote.rb b/ruby/step7_quote.rb index 78ef117..e91058b 100644 --- a/ruby/step7_quote.rb +++ b/ruby/step7_quote.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -121,7 +122,7 @@ if ARGV.size > 0 } exit 0 end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step8_macros.rb b/ruby/step8_macros.rb index db34159..dd02c4f 100644 --- a/ruby/step8_macros.rb +++ b/ruby/step8_macros.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -148,7 +149,7 @@ if ARGV.size > 0 } exit 0 end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/step9_interop.rb b/ruby/step9_interop.rb index ba35146..2c56de4 100644 --- a/ruby/step9_interop.rb +++ b/ruby/step9_interop.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -150,7 +151,7 @@ if ARGV.size > 0 } exit 0 end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e diff --git a/ruby/stepA_more.rb b/ruby/stepA_more.rb index a2fc5cc..697caa3 100644 --- a/ruby/stepA_more.rb +++ b/ruby/stepA_more.rb @@ -1,4 +1,5 @@ -require "readline" +$: << File.expand_path(File.dirname(__FILE__)) +require "mal_readline" require "types" require "reader" require "printer" @@ -166,7 +167,7 @@ if ARGV.size > 0 } exit 0 end -while line = Readline.readline("user> ", true) +while line = _readline("user> ") begin puts REP[line] rescue Exception => e |
