4198578702 2010-11-07 kinaba: /** 4198578702 2010-11-07 kinaba: * Authors: k.inaba 423f308350 2010-11-07 kinaba: * License: NYSL 0.9982 (http://www.kmonos.net/nysl/ 4198578702 2010-11-07 kinaba: * 4198578702 2010-11-07 kinaba: * Entry point for Polemy interpreter. 423f308350 2010-11-07 kinaba: */ 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: import std.stdio; 7de80acfb8 2010-11-09 kinaba: import std.algorithm; 7de80acfb8 2010-11-09 kinaba: import polemy.value; 7de80acfb8 2010-11-09 kinaba: import polemy.lex; 7de80acfb8 2010-11-09 kinaba: import polemy.parse; 7de80acfb8 2010-11-09 kinaba: import polemy.ast; 7de80acfb8 2010-11-09 kinaba: import polemy.eval; 820e7198cc 2010-11-07 kinaba: 38fcc662be 2010-11-10 kinaba: /// Tenuki Read-Eval-Print-Loop 7de80acfb8 2010-11-09 kinaba: class REPL 077506b38c 2010-11-08 kinaba: { 7de80acfb8 2010-11-09 kinaba: Table ctx; 7de80acfb8 2010-11-09 kinaba: string buf; 7de80acfb8 2010-11-09 kinaba: Value lastVal; 7de80acfb8 2010-11-09 kinaba: int lineno = 1; 7de80acfb8 2010-11-09 kinaba: int nextlineno = 1; 7de80acfb8 2010-11-09 kinaba: this() { ctx = createGlobalContext(); } 7de80acfb8 2010-11-09 kinaba: 7de80acfb8 2010-11-09 kinaba: bool tryRun( string s ) 7de80acfb8 2010-11-09 kinaba: { 2459e9a821 2010-11-09 kinaba: scope(failure) 2459e9a821 2010-11-09 kinaba: { buf = ""; lineno = nextlineno; } 2459e9a821 2010-11-09 kinaba: 7de80acfb8 2010-11-09 kinaba: buf ~= s; 2459e9a821 2010-11-09 kinaba: nextlineno ++; 2459e9a821 2010-11-09 kinaba: try 7465fcdd7f 2010-11-09 kinaba: { lastVal = eval(parseString(buf, "<REPL>", lineno), ctx, false, "@v"); } 2459e9a821 2010-11-09 kinaba: catch( UnexpectedEOF ) 2459e9a821 2010-11-09 kinaba: { return false; } // wait 2459e9a821 2010-11-09 kinaba: buf = ""; 2459e9a821 2010-11-09 kinaba: lineno = nextlineno; 7de80acfb8 2010-11-09 kinaba: return true; 7de80acfb8 2010-11-09 kinaba: } 7de80acfb8 2010-11-09 kinaba: 7de80acfb8 2010-11-09 kinaba: bool singleInteraction() 7de80acfb8 2010-11-09 kinaba: { 7de80acfb8 2010-11-09 kinaba: writef(">> ", lineno); 7de80acfb8 2010-11-09 kinaba: string line = readln(); 7de80acfb8 2010-11-09 kinaba: if( line.startsWith("exit") || line.startsWith("quit") ) 7de80acfb8 2010-11-09 kinaba: return false; 7de80acfb8 2010-11-09 kinaba: try { 7de80acfb8 2010-11-09 kinaba: if( tryRun(line) ) 7de80acfb8 2010-11-09 kinaba: writeln(lastVal); 7de80acfb8 2010-11-09 kinaba: } catch(Throwable e) { 7de80acfb8 2010-11-09 kinaba: writeln(e); 7de80acfb8 2010-11-09 kinaba: } 7de80acfb8 2010-11-09 kinaba: return true; 7de80acfb8 2010-11-09 kinaba: } 7de80acfb8 2010-11-09 kinaba: } 7de80acfb8 2010-11-09 kinaba: d78d700f7a 2010-11-09 kinaba: version(unittest) { d78d700f7a 2010-11-09 kinaba: bool success = false; d78d700f7a 2010-11-09 kinaba: static ~this(){ if(!success){writeln("(press enter to exit)"); readln();} } d78d700f7a 2010-11-09 kinaba: } d78d700f7a 2010-11-09 kinaba: 38fcc662be 2010-11-10 kinaba: /// Entry point. If args.length==1, invoke REPL. 38fcc662be 2010-11-10 kinaba: /// Otherwise interpret the argument as a filename. b11090f441 2010-11-08 kinaba: void main( string[] args ) 423f308350 2010-11-07 kinaba: { d78d700f7a 2010-11-09 kinaba: version(unittest) success=true; d78d700f7a 2010-11-09 kinaba: 7de80acfb8 2010-11-09 kinaba: if( args.length <= 1 ) 7de80acfb8 2010-11-09 kinaba: { 7de80acfb8 2010-11-09 kinaba: writeln("Welcome to Polemy 0.1.0"); 7de80acfb8 2010-11-09 kinaba: for(auto r = new REPL; r.singleInteraction();) {} 7de80acfb8 2010-11-09 kinaba: } 7de80acfb8 2010-11-09 kinaba: else 7de80acfb8 2010-11-09 kinaba: { 7de80acfb8 2010-11-09 kinaba: evalFile(args[1]); 7de80acfb8 2010-11-09 kinaba: } 423f308350 2010-11-07 kinaba: }