diff general/repl.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/repl.m	Sat Jan 12 19:21:22 2013 +0000
@@ -0,0 +1,34 @@
+% repl - Read-eval-print loop.
+%
+% repl ::
+%    string      ~'command line prompt',
+%    E:struct  ~'structure fields to put in current environment',
+% -> {...}.
+%
+% Runs an embedded command interpreter much like the Matlab top-level.
+% The second argument E is available as a value with the name E in the environment.
+% The environment can be examined using WHO or WHOS as usual, but there will be
+% some other variables visible that are internal to repl - they all end with two
+% underscores so they can be easily recognised.
+% The repl can return any number of values, but the values must be placed
+% by calling the ret(...) function within the repl.
+ 
+function varargout=repl(pr__,E)
+	fprintf('\n\nEntering REPL. Type "return","exit", or "quit" to finish.\n');
+	fprintf('The function ret(..) sets return values.\n\n');
+	cont__=true;
+	returns__={};
+	while cont__,
+		str__=input([pr__,'>> '],'s');
+		if strcmp(str__,'quit') break; end
+		if strcmp(str__,'exit') break; end
+		if strcmp(str__,'return') break; end
+		try, eval(str__)
+		catch ex__
+			fprintf('\n%s\n',getReport(ex__));
+		end
+	end
+	varargout=returns__;
+
+	function ret(varargin), returns__=varargin; cont__=false; end
+end