Check-in [3464a035ec]
Not logged in
Overview
SHA1 Hash:3464a035ec56f3b045c3de08f56e6c19b722d5c1
Date: 2010-11-21 01:35:14
User: kinaba
Comment:source code cleanup
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified .poseidon from [c60d98a3acceffca] to [3a1ec7254a3a05f2].

30 30 <source> 31 31 <name>d2stacktrace\dbghelp.d</name> 32 32 <name>d2stacktrace\stacktrace.d</name> 33 33 <name>main.d</name> 34 34 <name>polemy\_common.d</name> 35 35 <name>polemy\ast.d</name> 36 36 <name>polemy\eval.d</name> 37 + <name>polemy\failure.d</name> 37 38 <name>polemy\lex.d</name> 38 39 <name>polemy\parse.d</name> 39 40 <name>polemy\value.d</name> 40 41 <name>tricks\test.d</name> 41 42 <name>tricks\tricks.d</name> 42 43 </source> 43 44 <interface />

Modified doc/candydoc/modules.ddoc from [d7e1d5353bbe670c] to [16e1e2b29a0a959b].

1 1 MODULES = 2 2 $(MODULE main) 3 3 $(MODULE tricks.tricks) 4 4 $(MODULE tricks.test) 5 5 $(MODULE polemy._common) 6 + $(MODULE polemy.failure) 6 7 $(MODULE polemy.lex) 7 8 $(MODULE polemy.parse) 8 9 $(MODULE polemy.ast) 9 10 $(MODULE polemy.eval) 10 11 $(MODULE polemy.value)

Modified main.d from [d5ef45952651bda8] to [3594122dc7a67c3c].

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

Modified polemy/_common.d from [ae51765dfb783149] to [05624ab3274c99f8].

9 9 public import tricks.tricks; 10 10 public import std.algorithm; 11 11 public import std.array; 12 12 public import std.bigint; 13 13 public import std.conv : text; 14 14 public import std.exception; 15 15 public import std.range; 16 -public import std.stdio : writeln; // for debugging... 16 +public import std.stdio : DBG = writeln;

Modified polemy/ast.d from [f8a685431329b03d] to [351d6a36d1e1b0e0].

2 2 * Authors: k.inaba 3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Syntax tree for Polemy programming language. 6 6 */ 7 7 module polemy.ast; 8 8 import polemy._common; 9 -import polemy.lex; 9 +import polemy.failure; 10 10 11 11 /// 12 12 abstract class AST 13 13 { 14 14 immutable LexPosition pos; 15 15 mixin SimpleConstructor; 16 16 mixin SimplePatternMatch;

Modified polemy/eval.d from [360da420d41dfbbf] to [97875541a42dbc7a].

2 2 * Authors: k.inaba 3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Evaluator for Polemy programming language. 6 6 */ 7 7 module polemy.eval; 8 8 import polemy._common; 9 -import polemy.lex : LexPosition; 9 +import polemy.failure; 10 10 import polemy.ast; 11 11 import polemy.parse; 12 12 import polemy.value; 13 13 import std.typecons; 14 14 import std.stdio; 15 15 16 16 ///

Added polemy/failure.d version [ceae1b272549ae1a]

1 +/** 2 + * Authors: k.inaba 3 + * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 + * 5 + * Error Information for Polemy Programming Language 6 + */ 7 +module polemy.failure; 8 +import polemy._common; 9 + 10 +/// Represents a position in source codes 11 + 12 +class LexPosition 13 +{ 14 + immutable string filename; /// name of the source file 15 + immutable int lineno; /// 1-origin 16 + immutable int column; /// 1-origin 17 + 18 + mixin SimpleClass; 19 + override string toString() const 20 + { 21 + return sprintf!("%s:%d:%d")(filename, lineno, column); 22 + } 23 + 24 + static immutable LexPosition dummy; 25 + static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 26 +} 27 + 28 +unittest 29 +{ 30 + auto p = new LexPosition("hello.cpp", 123, 45); 31 + 32 + assert_eq( p.filename, "hello.cpp" ); 33 + assert_eq( p.lineno, 123 ); 34 + assert_eq( p.column, 45 ); 35 + assert_eq( text(p), "hello.cpp:123:45" ); 36 + 37 + assert( !__traits(compiles, new LexPosition) ); 38 + assert( !__traits(compiles, p.filename="foo") ); 39 + assert( !__traits(compiles, p.lineno =789) ); 40 + assert( !__traits(compiles, p.column =222) ); 41 + 42 + auto q = new LexPosition("hello.cpp", 123, 46); 43 + assert_lt( p, q ); 44 + assert_ne( p, q ); 45 +} 46 + 47 +/*mixin*/ 48 +template ExceptionWithPosition() 49 +{ 50 + const LexPosition pos; 51 + this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) 52 + { 53 + if(pos is null) 54 + super(sprintf!("[???????] %s")(msg), file, line, next); 55 + else 56 + super(sprintf!("[%s] %s")(pos, msg), file, line, next); 57 + this.pos = pos; 58 + } 59 +} 60 + 61 +class UnexpectedEOF : Exception { mixin ExceptionWithPosition; } /// EOF during lexing/parsing 62 +class LexException : Exception { mixin ExceptionWithPosition; } /// Lexer errors 63 +class ParseException : Exception { mixin ExceptionWithPosition; } /// Parser errors 64 +class RuntimeException : Exception { mixin ExceptionWithPosition; } /// Evaluator errors

Modified polemy/lex.d from [514757ca55e253b4] to [480bb741b8b1612b].

2 2 * Authors: k.inaba 3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Lexer for Polemy programming language. 6 6 */ 7 7 module polemy.lex; 8 8 import polemy._common; 9 +import polemy.failure; 9 10 import std.file : readText; 10 11 import std.ctype : isspace, isalnum; 11 12 12 -/*mixin*/ 13 -template ExceptionWithPosition() 14 -{ 15 - const LexPosition pos; 16 - this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) 17 - { 18 - if(pos is null) 19 - super(sprintf!"[??] %s"(msg), file, line, next); 20 - else 21 - super(sprintf!"[%s] %s"(pos, msg), file, line, next); 22 - this.pos = pos; 23 - } 24 -} 25 - 26 -/// Thrown when encountered an EOF in the middle of a lexical token 27 - 28 -class UnexpectedEOF : Exception 29 -{ 30 - mixin ExceptionWithPosition; 31 -} 32 - 33 -/// Thrown when encountered a lexical error 34 - 35 -class LexException : Exception 36 -{ 37 - mixin ExceptionWithPosition; 38 -}; 39 - 40 -/// Represents a position in source codes 41 - 42 -class LexPosition 43 -{ 44 - immutable string filename; /// name of the source file 45 - immutable int lineno; /// 1-origin 46 - immutable int column; /// 1-origin 47 - 48 - mixin SimpleClass; 49 - override string toString() const 50 - { return sprintf!"%s:%d:%d"(filename, lineno, column); } 51 - 52 - static immutable LexPosition dummy; 53 - static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 54 -} 55 - 56 -unittest 57 -{ 58 - auto p = new LexPosition("hello.cpp", 123, 45); 59 - 60 - assert_eq( p.filename, "hello.cpp" ); 61 - assert_eq( p.lineno, 123 ); 62 - assert_eq( p.column, 45 ); 63 - assert_eq( text(p), "hello.cpp:123:45" ); 64 - 65 - assert( !__traits(compiles, new LexPosition) ); 66 - assert( !__traits(compiles, p.filename="foo") ); 67 - assert( !__traits(compiles, p.lineno =789) ); 68 - assert( !__traits(compiles, p.column =222) ); 69 - 70 - auto q = new LexPosition("hello.cpp", 123, 46); 71 - assert_lt( p, q ); 72 - assert_ne( p, q ); 73 -} 74 - 75 13 /// Represents a lexer token 76 14 77 15 class Token 78 16 { 79 17 immutable LexPosition pos; /// Position where the token occurred in the source 80 18 immutable string str; /// The token string itself 81 19 immutable bool quoted; /// Was it a "quoted" token or unquoted?

Modified polemy/parse.d from [6bec0997c5764737] to [a024e7c2d40e70c4].

2 2 * Authors: k.inaba 3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Parser for Polemy programming language 6 6 */ 7 7 module polemy.parse; 8 8 import polemy._common; 9 +import polemy.failure; 9 10 import polemy.lex; 10 11 import polemy.ast; 11 12 12 -/// Thrown when encountered a syntax error 13 - 14 -class ParseException : Exception 15 -{ 16 - mixin ExceptionWithPosition; 17 -} 18 - 19 13 /// Parse a string and return its AST 20 14 /// Throws: ParseException, LexException, UnexpectedEOF 21 15 22 16 AST parseString(S, T...)(S str, T fn_ln_cn) 23 17 { 24 18 return parserFromString(str, fn_ln_cn).parse(); 25 19 }

Modified polemy/value.d from [ff42bce4fb7dc674] to [f1d2e31afdaaee9d].

2 2 * Authors: k.inaba 3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Runtime data structures for Polemy programming language. 6 6 */ 7 7 module polemy.value; 8 8 import polemy._common; 9 -import polemy.lex; 9 +import polemy.failure; 10 10 import polemy.ast; 11 11 12 -/// Raised when something went wrong in runtime 13 - 14 -class RuntimeException : Exception 15 -{ 16 - mixin ExceptionWithPosition; 17 -} 18 - 19 12 /// Runtime values of Polemy 20 13 21 14 abstract class Value 22 15 { 23 16 } 24 17 25 18 ///

Modified readme.txt from [b45d599d08f18ee0] to [6629940451babbd3].

46 46 47 47 > polemy foo.pmy 48 48 executes foo.pmy 49 49 50 50 > polemy -l foo.pmy 51 51 after executing foo.pmy, starts REPL 52 52 53 + > polemy -l foo.pmy -l bar.pmy buz.pmy 54 + executes foo.pmy, bar.bmy, and then buz.pmy 55 + 53 56 54 57 55 58 <<Syntax>> 56 59 57 60 Comment is "# ... \n" 58 61 59 62 E ::=