36c517dfc4 2010-11-23 kinaba: /** 36c517dfc4 2010-11-23 kinaba: * Authors: k.inaba 36c517dfc4 2010-11-23 kinaba: * License: NYSL 0.9982 (http://www.kmonos.net/nysl/) 36c517dfc4 2010-11-23 kinaba: * 36c517dfc4 2010-11-23 kinaba: * Read-Eval-Print-Loop 36c517dfc4 2010-11-23 kinaba: */ 36c517dfc4 2010-11-23 kinaba: module polemy.repl; 36c517dfc4 2010-11-23 kinaba: import polemy.failure; 36c517dfc4 2010-11-23 kinaba: import polemy.layer; 36c517dfc4 2010-11-23 kinaba: import polemy.eval; 36c517dfc4 2010-11-23 kinaba: import polemy.runtime; 36c517dfc4 2010-11-23 kinaba: import polemy.value; 36c517dfc4 2010-11-23 kinaba: import polemy.valueconv; 36c517dfc4 2010-11-23 kinaba: import std.stdio; 36c517dfc4 2010-11-23 kinaba: import std.string; fc0b463347 2010-12-04 kinaba: import std.cstream; 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: enum VersionNoMajor = 0; /// Version Number 36c517dfc4 2010-11-23 kinaba: enum VersionNoMinor = 1; /// Version Number 36c517dfc4 2010-11-23 kinaba: enum VersionNoRev = 0; /// Version Number 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /// Read-Eval-Print-Loop 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: class REPL 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: /// Load the prelude environment 36c517dfc4 2010-11-23 kinaba: this(string[] args) 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: ev = new Evaluator; 36c517dfc4 2010-11-23 kinaba: ev.globalContext.set("argv", ValueLayer, d2polemy(args)); 36c517dfc4 2010-11-23 kinaba: enrollRuntimeLibrary(ev); 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /// Print the version number etc. 36c517dfc4 2010-11-23 kinaba: void greet() 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: writefln("Welcome to Polemy %d.%d.%d", VersionNoMajor, VersionNoMinor, VersionNoRev); 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /// Run one file on the global scope 36c517dfc4 2010-11-23 kinaba: void runFile(string filename) 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: ev.evalFile(filename); 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /// Repeat the singleInteraction 36c517dfc4 2010-11-23 kinaba: void replLoop() 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: while( singleInteraction() ) {} 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /// Read one line from stdin, and do some reaction 36c517dfc4 2010-11-23 kinaba: bool singleInteraction() 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: writef(">> ", lineno); 36c517dfc4 2010-11-23 kinaba: string line = readln(); fc0b463347 2010-12-04 kinaba: if( line.startsWith("exit") || line.startsWith("quit") || din.eof() ) 36c517dfc4 2010-11-23 kinaba: return false; 36c517dfc4 2010-11-23 kinaba: try { 36c517dfc4 2010-11-23 kinaba: if( tryRun(line) ) 36c517dfc4 2010-11-23 kinaba: writeln(lastVal); 36c517dfc4 2010-11-23 kinaba: } catch(Throwable e) { 36c517dfc4 2010-11-23 kinaba: writeln(e); 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: return true; 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: private: 36c517dfc4 2010-11-23 kinaba: Evaluator ev; 36c517dfc4 2010-11-23 kinaba: Table ctx; 36c517dfc4 2010-11-23 kinaba: string buf; 36c517dfc4 2010-11-23 kinaba: Value lastVal; 36c517dfc4 2010-11-23 kinaba: int lineno = 1; 36c517dfc4 2010-11-23 kinaba: int nextlineno = 1; 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: bool tryRun( string s ) 36c517dfc4 2010-11-23 kinaba: { 36c517dfc4 2010-11-23 kinaba: scope(failure) 36c517dfc4 2010-11-23 kinaba: { buf = ""; lineno = nextlineno; } 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: buf ~= s; 36c517dfc4 2010-11-23 kinaba: nextlineno ++; 36c517dfc4 2010-11-23 kinaba: try 36c517dfc4 2010-11-23 kinaba: { lastVal = ev.evalString(buf, "<REPL>", lineno); } 36c517dfc4 2010-11-23 kinaba: catch( UnexpectedEOF ) 36c517dfc4 2010-11-23 kinaba: { return false; } // wait 36c517dfc4 2010-11-23 kinaba: buf = ""; 36c517dfc4 2010-11-23 kinaba: lineno = nextlineno; 36c517dfc4 2010-11-23 kinaba: return true; 36c517dfc4 2010-11-23 kinaba: } 36c517dfc4 2010-11-23 kinaba: }