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(); } 5afe8e3f26 2010-11-13 kinaba: this(string filename) { 5afe8e3f26 2010-11-13 kinaba: ctx = createGlobalContext(); 5afe8e3f26 2010-11-13 kinaba: eval(parseFile(filename), ctx, false, "@v"); 5afe8e3f26 2010-11-13 kinaba: } 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) ) 3f6f41b558 2010-11-20 kinaba: { 3f6f41b558 2010-11-20 kinaba: // for debugging. 3f6f41b558 2010-11-20 kinaba: //try { 3f6f41b558 2010-11-20 kinaba: // writeln(tableToAST("@v", cast(Table)lastVal)); 3f6f41b558 2010-11-20 kinaba: //} catch(Throwable e) { 3f6f41b558 2010-11-20 kinaba: // writeln(e); 3f6f41b558 2010-11-20 kinaba: //} 7de80acfb8 2010-11-09 kinaba: writeln(lastVal); 3f6f41b558 2010-11-20 kinaba: } 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: 38fcc662be 2010-11-10 kinaba: /// Entry point. If args.length==1, invoke REPL. 5afe8e3f26 2010-11-13 kinaba: /// If args.length==3 && args[1]=="-l" read args[2] and 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: { 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();) {} 5afe8e3f26 2010-11-13 kinaba: } 5afe8e3f26 2010-11-13 kinaba: else if( args.length>=3 && args[1]=="-l" ) 5afe8e3f26 2010-11-13 kinaba: { 5afe8e3f26 2010-11-13 kinaba: writeln("Welcome to Polemy 0.1.0"); 5afe8e3f26 2010-11-13 kinaba: for(auto r = new REPL(args[2]); 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: }