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: 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: { 7de80acfb8 2010-11-09 kinaba: nextlineno ++; 7de80acfb8 2010-11-09 kinaba: buf ~= s; 7de80acfb8 2010-11-09 kinaba: try { 7de80acfb8 2010-11-09 kinaba: AST a = parseString(buf, "<REPL>", lineno); 7de80acfb8 2010-11-09 kinaba: buf = ""; 7de80acfb8 2010-11-09 kinaba: lineno = nextlineno; 7de80acfb8 2010-11-09 kinaba: lastVal = eval(a, ctx); 7de80acfb8 2010-11-09 kinaba: } catch( LexException ) { 7de80acfb8 2010-11-09 kinaba: // always EOF exception, so wait next 7de80acfb8 2010-11-09 kinaba: return false; 7de80acfb8 2010-11-09 kinaba: } catch( ParseException e ) { 7de80acfb8 2010-11-09 kinaba: if( find(e.msg, "EOF") ) // ultra ad-hoc 7de80acfb8 2010-11-09 kinaba: return false; 7de80acfb8 2010-11-09 kinaba: throw 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: 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: } 077506b38c 2010-11-08 kinaba: } 077506b38c 2010-11-08 kinaba: b11090f441 2010-11-08 kinaba: void main( string[] args ) 423f308350 2010-11-07 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: }