Diff
Not logged in

Differences From Artifact [cbf6e2570b5a90b0]:

To Artifact [f44d615cc4428107]:


1 1 /** 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 module main; 8 +import polemy.repl; 8 9 import std.stdio; 9 10 import std.algorithm; 10 11 import std.array; 11 -import polemy.value; 12 -import polemy.failure; 13 -import polemy.layer; 14 -import polemy.parse; 15 -import polemy.ast; 16 -import polemy.eval; 17 -import polemy.runtime; 18 - 19 -enum VersionNoMajor = 0; 20 -enum VersionNoMinor = 1; 21 -enum VersionNoRev = 0; 22 - 23 -/// Read-Eval-Print-Loop 24 - 25 -class REPL 26 -{ 27 -Evaluator ev; 28 - /// Load the prelude environment 29 - this() 30 - { 31 - ev = new Evaluator; 32 - enrollRuntimeLibrary(ev); 33 - } 34 - 35 - /// Print the version number etc. 36 - void greet() 37 - { 38 - writefln("Welcome to Polemy %d.%d.%d", VersionNoMajor, VersionNoMinor, VersionNoRev); 39 - } 40 - 41 - /// Run one file on the global scope 42 - void runFile(string filename) 43 - { 44 - ev.evalFile(filename); 45 - } 46 - 47 - /// Repeat the singleInteraction 48 - void replLoop() 49 - { 50 - while( singleInteraction() ) {} 51 - } 52 - 53 - /// Read one line from stdin, and do some reaction 54 - bool singleInteraction() 55 - { 56 - writef(">> ", lineno); 57 - string line = readln(); 58 - if( line.startsWith("exit") || line.startsWith("quit") ) 59 - return false; 60 - try { 61 - if( tryRun(line) ) 62 - writeln(lastVal); 63 - } catch(Throwable e) { 64 - writeln(e); 65 - } 66 - return true; 67 - } 68 - 69 -private: 70 - Table ctx; 71 - string buf; 72 - Value lastVal; 73 - int lineno = 1; 74 - int nextlineno = 1; 75 - 76 - bool tryRun( string s ) 77 - { 78 - scope(failure) 79 - { buf = ""; lineno = nextlineno; } 80 - 81 - buf ~= s; 82 - nextlineno ++; 83 - try 84 - { lastVal = ev.evalString(buf, "<REPL>", lineno); } 85 - catch( UnexpectedEOF ) 86 - { return false; } // wait 87 - buf = ""; 88 - lineno = nextlineno; 89 - return true; 90 - } 91 -} 92 12 93 13 /// Advance args[] to point the argument list fed to the script. 94 14 /// Returns the name of the source file to run, or returns "" if 95 15 /// no filename was given. Also, returns to libs[] the list of 96 16 /// library source to load. 97 17 98 18 string parseArgv(ref string[] args, out string[] libs) ................................................................................ 118 38 /// Entry point. 119 39 120 40 void main( string[] args ) 121 41 { 122 42 string[] libs; 123 43 string src = parseArgv(args, libs); 124 44 125 - auto r = new REPL; 45 + auto r = new REPL(args); 126 46 if( src.empty ) 127 47 r.greet(); 128 48 foreach(lb; libs) 129 49 r.runFile(lb); 130 50 if( src.empty ) 131 51 r.replLoop(); 132 52 else 133 53 r.runFile(src); 134 54 }