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 * 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 7 8 import std.stdio; 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)" ); | 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); 13 readln(); | 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 void main( string[] args ) 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 /// Entry point of this module 86 /// Entry point of this module 87 87 88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 89 { 89 { 90 return eval( polemy.parse.parseString(str, fn_ln_cn) ); 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 return eval( polemy.parse.parseFile(filename, ln_cn) ); 95 return eval( polemy.parse.parseFile(filename, ln_cn) ); 96 } 96 } 97 97 98 Tuple!(Value,"val",Table,"ctx") eval(AST e) 98 Tuple!(Value,"val",Table,"ctx") eval(AST e) 99 { 99 { 100 Table ctx = createGlobalContext(); 100 Table ctx = createGlobalContext();

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

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

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

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

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

17 auto writer = appender!string(); 17 auto writer = appender!string(); 18 formattedWrite(writer, fmt, params); 18 formattedWrite(writer, fmt, params); 19 return writer.data; 19 return writer.data; 20 } 20 } 21 21 22 unittest 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 /// Create an exception with automatically completed filename and lineno informa 29 /// Create an exception with automatically completed filename and lineno informa 29 30 30 auto genex(ExceptionType, string fn=__FILE__, int ln=__LINE__, T...)(T params) 31 auto genex(ExceptionType, string fn=__FILE__, int ln=__LINE__, T...)(T params) 31 { 32 { 32 static if( T.length > 0 && is(T[$-1] : Throwable) ) 33 static if( T.length > 0 && is(T[$-1] : Throwable) )