Differences From Artifact [cbf6e2570b5a90b0]:
- File
main.d
- 2010-11-23 09:36:27 - part of checkin [b97bd4f713] on branch trunk - automatic AST to table encoder (user: kinaba) [annotate]
To Artifact [f44d615cc4428107]:
- File
main.d
- 2010-11-23 10:09:03 - part of checkin [36c517dfc4] on branch trunk - refactored d-value and polemy-value conversion (user: kinaba) [annotate]
1 /** 1 /**
2 * Authors: k.inaba 2 * Authors: k.inaba
3 * License: NYSL 0.9982 (http://www.kmonos.net/nysl/) 3 * License: NYSL 0.9982 (http://www.kmonos.net/nysl/)
4 * 4 *
5 * Entry point for Polemy interpreter. 5 * Entry point for Polemy interpreter.
6 */ 6 */
7 module main; 7 module main;
> 8 import polemy.repl;
8 import std.stdio; 9 import std.stdio;
9 import std.algorithm; 10 import std.algorithm;
10 import std.array; 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, VersionNo <
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 /// Advance args[] to point the argument list fed to the script. 13 /// Advance args[] to point the argument list fed to the script.
94 /// Returns the name of the source file to run, or returns "" if 14 /// Returns the name of the source file to run, or returns "" if
95 /// no filename was given. Also, returns to libs[] the list of 15 /// no filename was given. Also, returns to libs[] the list of
96 /// library source to load. 16 /// library source to load.
97 17
98 string parseArgv(ref string[] args, out string[] libs) 18 string parseArgv(ref string[] args, out string[] libs)
................................................................................................................................................................................
118 /// Entry point. 38 /// Entry point.
119 39
120 void main( string[] args ) 40 void main( string[] args )
121 { 41 {
122 string[] libs; 42 string[] libs;
123 string src = parseArgv(args, libs); 43 string src = parseArgv(args, libs);
124 44
125 auto r = new REPL; | 45 auto r = new REPL(args);
126 if( src.empty ) 46 if( src.empty )
127 r.greet(); 47 r.greet();
128 foreach(lb; libs) 48 foreach(lb; libs)
129 r.runFile(lb); 49 r.runFile(lb);
130 if( src.empty ) 50 if( src.empty )
131 r.replLoop(); 51 r.replLoop();
132 else 52 else
133 r.runFile(src); 53 r.runFile(src);
134 } 54 }