Diff
Not logged in

Differences From Artifact [404b54e69646e17a]:

To Artifact [14a3f0cd527958ce]:


2 2 * Authors: k.inaba 3 3 * License: NYSL 0.9982 (http://www.kmonos.net/nysl/ 4 4 * 5 5 * Entry point for Polemy interpreter. 6 6 */ 7 7 8 8 import std.stdio; 9 +import std.algorithm; 10 +import polemy.value; 11 +import polemy.lex; 12 +import polemy.parse; 13 +import polemy.ast; 14 +import polemy.eval; 9 15 10 -version(unittest) static ~this() 16 +class REPL 11 17 { 12 - writeln( "(Press Enter to finish)" ); 13 - readln(); 18 + Table ctx; 19 + string buf; 20 + Value lastVal; 21 + int lineno = 1; 22 + int nextlineno = 1; 23 + this() { ctx = createGlobalContext(); } 24 + 25 + bool tryRun( string s ) 26 + { 27 + nextlineno ++; 28 + buf ~= s; 29 + try { 30 + AST a = parseString(buf, "<REPL>", lineno); 31 + buf = ""; 32 + lineno = nextlineno; 33 + lastVal = eval(a, ctx); 34 + } catch( LexException ) { 35 + // always EOF exception, so wait next 36 + return false; 37 + } catch( ParseException e ) { 38 + if( find(e.msg, "EOF") ) // ultra ad-hoc 39 + return false; 40 + throw e; 41 + } 42 + return true; 43 + } 44 + 45 + bool singleInteraction() 46 + { 47 + writef(">> ", lineno); 48 + string line = readln(); 49 + if( line.startsWith("exit") || line.startsWith("quit") ) 50 + return false; 51 + try { 52 + if( tryRun(line) ) 53 + writeln(lastVal); 54 + } catch(Throwable e) { 55 + writeln(e); 56 + } 57 + return true; 58 + } 14 59 } 15 60 16 61 void main( string[] args ) 17 62 { 63 + if( args.length <= 1 ) 64 + { 65 + writeln("Welcome to Polemy 0.1.0"); 66 + for(auto r = new REPL; r.singleInteraction();) {} 67 + } 68 + else 69 + { 70 + evalFile(args[1]); 71 + } 18 72 }