Check-in [7de80acfb8]
Not logged in
Overview
SHA1 Hash:7de80acfb85fc35b0fd078a22a4cd585f787aeda
Date: 2010-11-09 15:02:32
User: kinaba
Comment:Added ultra tenuki REPL
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified main.d from [404b54e69646e17a] to [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 }

Modified polemy/eval.d from [1a40d715cf0caee4] to [c3d65954914654a5].

86 86 /// Entry point of this module 87 87 88 88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 89 89 { 90 90 return eval( polemy.parse.parseString(str, fn_ln_cn) ); 91 91 } 92 92 93 -Tuple!(Value,"val",Table,"ctx") evalFile(S, T...)(S filenae, T ln_cn) 93 +Tuple!(Value,"val",Table,"ctx") evalFile(S, T...)(S filename, T ln_cn) 94 94 { 95 95 return eval( polemy.parse.parseFile(filename, ln_cn) ); 96 96 } 97 97 98 98 Tuple!(Value,"val",Table,"ctx") eval(AST e) 99 99 { 100 100 Table ctx = createGlobalContext();

Modified polemy/lex.d from [df057f5d35e6bc8a] to [bda39a8af81ee5d9].

11 11 12 12 /// Exception from this module 13 13 14 14 class LexException : Exception 15 15 { 16 16 const LexPosition pos; 17 17 18 - this( const LexPosition pos, string msg, string file="", int line=0 ) 19 - { super(sprintf!"[%s] %s"(pos, msg), file, line); this.pos = pos; } 18 + this( const LexPosition pos, string msg, string file="", int line=0, Throwable next=null ) 19 + { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; } 20 20 }; 21 21 22 22 /// Represents a position in a source code 23 23 24 24 class LexPosition 25 25 { 26 26 immutable string filename; /// name of the source file ................................................................................ 34 34 static immutable LexPosition dummy; 35 35 static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 36 36 } 37 37 38 38 unittest 39 39 { 40 40 auto p = new LexPosition("hello.cpp", 123, 45); 41 - auto q = new LexPosition("hello.cpp", 123, 46); 42 41 43 42 assert_eq( p.filename, "hello.cpp" ); 44 43 assert_eq( p.lineno, 123 ); 45 44 assert_eq( p.column, 45 ); 46 45 assert_eq( to!string(p), "hello.cpp:123:45" ); 47 - assert_lt( p, q ); 48 - assert_ne( p, q ); 49 46 50 47 assert( !__traits(compiles, new LexPosition) ); 51 48 assert( !__traits(compiles, p.filename="foo") ); 52 49 assert( !__traits(compiles, p.lineno =789) ); 53 50 assert( !__traits(compiles, p.column =222) ); 51 + 52 + auto q = new LexPosition("hello.cpp", 123, 46); 53 + assert_lt( p, q ); 54 + assert_ne( p, q ); 54 55 } 55 56 56 57 /// Represents a lexer token 57 58 58 59 class Token 59 60 { 60 61 immutable LexPosition pos; /// Position where the token occurred in the source ................................................................................ 157 158 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); } 158 159 } 159 160 160 161 string readQuoted(const LexPosition pos){char[] buf; return readQuoted(pos,buf);} 161 162 string readQuoted(const LexPosition pos, ref char[] buf) 162 163 { 163 164 if( reader.empty ) 164 - throw new LexException(pos, "EOF found while lexing a quoted-string"); 165 + throw genex!LexException(pos, "EOF found while lexing a quoted-string"); 165 166 dchar c = reader.front; 166 167 reader.popFront; 167 168 if( c == '"' ) 168 169 return assumeUnique(buf); 169 170 if( c == '\\' && !reader.empty ) { 170 171 if( reader.front=='"' ) { 171 172 reader.popFront; ................................................................................ 223 224 } 224 225 } 225 226 } 226 227 227 228 unittest 228 229 { 229 230 assert( std.range.isForwardRange!(Lexer) ); 231 + assert( is(ElementType!(Lexer) == Token) ); 230 232 } 231 233 232 234 unittest 233 235 { 234 236 auto lex = lexerFromString("this is a \t\r\n pen :-( @@; "); 235 237 Token[] ts = std.array.array(lex); 236 238

Modified polemy/parse.d from [fe5c9fee07b98701] to [f48294ade1f2cd3c].

11 11 12 12 /// Exception from this module 13 13 14 14 class ParseException : Exception 15 15 { 16 16 const LexPosition pos; 17 17 18 - private this( const LexPosition pos, string msg ) 19 - { super(sprintf!"%s [%s]"(msg, pos)); this.pos = pos; } 18 + this( const LexPosition pos, string msg, string file="", int line=0, Throwable next=null ) 19 + { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; } 20 20 } 21 21 22 22 private auto createException(Lexer)(Lexer lex, string msg) 23 23 { return new ParseException(lex.empty?null:lex.front.pos, msg); } 24 24 25 25 /// Entry points of this module 26 26 27 27 auto parseString(S, T...)(S str, T fn_ln_cn) 28 28 { return parserFromString(str, fn_ln_cn).parse(); } 29 29 30 -auto parseFile(S, T...)(S filename,T ln_cn) 30 +auto parseFile(S, T...)(S filename, T ln_cn) 31 31 { return parserFromString(filename, ln_cn).parse(); } 32 32 33 33 /// Named Constructor of Parser 34 34 35 35 private auto parserFromLexer(Lexer)(Lexer lex) 36 36 { return new Parser!Lexer(lex); } 37 37

Modified tricks/tricks.d from [3f689d3451f8c1cc] to [4c996bb5859ed5ea].

17 17 auto writer = appender!string(); 18 18 formattedWrite(writer, fmt, params); 19 19 return writer.data; 20 20 } 21 21 22 22 unittest 23 23 { 24 - assert( sprintf!"%s == %d"("1+2", 3) == "1+2 == 3" ); 25 - assert( sprintf!"%s == %04d"("1+2", 3) == "1+2 == 0003" ); 24 + assert_eq( sprintf!"%s == %04d"("1+2", 3), "1+2 == 0003" ); 25 + assert_eq( sprintf!"%2$s == %1$s"("1+2", 5, 8), "5 == 1+2" ); 26 + assert_throw!Error( sprintf!"%s%s"(1) ); 26 27 } 27 28 28 29 /// Create an exception with automatically completed filename and lineno information 29 30 30 31 auto genex(ExceptionType, string fn=__FILE__, int ln=__LINE__, T...)(T params) 31 32 { 32 33 static if( T.length > 0 && is(T[$-1] : Throwable) )