Check-in [80ff567c75]
Not logged in
Overview
SHA1 Hash:80ff567c7531e620a1714206c60a0b23c5c4a9a0
Date: 2010-11-08 21:26:39
User: kinaba
Comment:Testing easyAST.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified polemy/ast.d from [9b22488b3393157f] to [f03af1208bc8c434].

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 * Syntax tree for Polemy programming language. 5 * Syntax tree for Polemy programming language. 6 */ 6 */ 7 module polemy.ast; 7 module polemy.ast; 8 import polemy._common; 8 import polemy._common; 9 import polemy.lex : LexPosition; | 9 import polemy.lex; 10 10 11 alias Statement[] Program; 11 alias Statement[] Program; 12 12 13 abstract class Statement 13 abstract class Statement 14 { 14 { 15 immutable LexPosition pos; 15 immutable LexPosition pos; 16 mixin SimpleConstructor; 16 mixin SimpleConstructor; 17 } 17 } 18 18 19 class DeclStatement : Statement 19 class DeclStatement : Statement 20 { 20 { 21 string var; 21 string var; 22 Expression expr; 22 Expression expr; 23 mixin SimpleConstructor; < 24 mixin SimpleCompare; | 23 mixin SimpleClass; 25 } 24 } 26 25 27 class ExprStatement : Statement 26 class ExprStatement : Statement 28 { 27 { 29 Expression expr; 28 Expression expr; 30 mixin SimpleConstructor; < 31 mixin SimpleCompare; | 29 mixin SimpleClass; 32 } 30 } 33 31 34 abstract class Expression 32 abstract class Expression 35 { 33 { 36 immutable LexPosition pos; 34 immutable LexPosition pos; 37 mixin SimpleConstructor; 35 mixin SimpleConstructor; 38 mixin SimpleCompare; < 39 } 36 } 40 37 41 class StrLiteralExpression : Expression 38 class StrLiteralExpression : Expression 42 { 39 { 43 string data; 40 string data; 44 mixin SimpleConstructor; < 45 mixin SimpleCompare; | 41 mixin SimpleClass; 46 } 42 } 47 43 48 class IntLiteralExpression : Expression 44 class IntLiteralExpression : Expression 49 { 45 { 50 BigInt data; 46 BigInt data; 51 mixin SimpleConstructor; < 52 mixin SimpleCompare; | 47 mixin SimpleClass; 53 } 48 } 54 49 55 class VarExpression : Expression 50 class VarExpression : Expression 56 { 51 { 57 string var; 52 string var; 58 mixin SimpleConstructor; < 59 mixin SimpleCompare; | 53 mixin SimpleClass; 60 } 54 } 61 55 62 class AssignExpression : Expression 56 class AssignExpression : Expression 63 { 57 { 64 Expression lhs; 58 Expression lhs; 65 Expression rhs; 59 Expression rhs; 66 mixin SimpleConstructor; < 67 mixin SimpleCompare; | 60 mixin SimpleClass; 68 } 61 } 69 62 70 class FuncallExpression : Expression 63 class FuncallExpression : Expression 71 { 64 { 72 Expression fun; 65 Expression fun; 73 Expression[] args; 66 Expression[] args; 74 this(immutable LexPosition pos, Expression fun, Expression[] args...) 67 this(immutable LexPosition pos, Expression fun, Expression[] args...) 75 { super(pos); this.fun=fun; this.args=args.dup; } 68 { super(pos); this.fun=fun; this.args=args.dup; } 76 mixin SimpleCompare; | 69 mixin SimpleClass; 77 } 70 } 78 71 79 class FunLiteralExpression : Expression 72 class FunLiteralExpression : Expression 80 { 73 { 81 string[] params; 74 string[] params; 82 Program funbody; 75 Program funbody; 83 mixin SimpleConstructor; | 76 mixin SimpleClass; > 77 } > 78 > 79 /// Handy Generator for AST nodes. To use this, mixin EasyAst; > 80 84 mixin SimpleCompare; | 81 /*mixin*/ > 82 template EasyAst() > 83 { > 84 template genEast(T) > 85 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } > 86 > 87 alias genEast!DeclStatement decl; > 88 alias genEast!ExprStatement expr; > 89 alias genEast!VarExpression var; > 90 alias genEast!FuncallExpression funcall; > 91 alias genEast!IntLiteralExpression intl; > 92 alias genEast!StrLiteralExpression strl; > 93 alias genEast!FunLiteralExpression fun; 85 } 94 }

Modified polemy/lex.d from [5f52873e3ff7ae30] to [bee9af8d0f8f7348].

27 immutable int column; /// column, 1, 2, ... 27 immutable int column; /// column, 1, 2, ... 28 28 29 override string toString() const 29 override string toString() const 30 { return sprintf!"%s:%d:%d"(filename, lineno, column); } 30 { return sprintf!"%s:%d:%d"(filename, lineno, column); } 31 31 32 mixin SimpleConstructor; 32 mixin SimpleConstructor; 33 mixin SimpleCompare; 33 mixin SimpleCompare; > 34 > 35 static immutable LexPosition dummy; > 36 static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 34 } 37 } 35 38 36 unittest 39 unittest 37 { 40 { 38 auto p = new LexPosition("hello.cpp", 123, 45); 41 auto p = new LexPosition("hello.cpp", 123, 45); 39 auto q = new LexPosition("hello.cpp", 123, 46); 42 auto q = new LexPosition("hello.cpp", 123, 46); 40 43 ................................................................................................................................................................................ 57 { 60 { 58 immutable LexPosition pos; /// Position where the token occurred in t 61 immutable LexPosition pos; /// Position where the token occurred in t 59 immutable string str; /// The token string itself 62 immutable string str; /// The token string itself 60 immutable bool quoted; /// Was it a "quoted" token or unquoted? 63 immutable bool quoted; /// Was it a "quoted" token or unquoted? 61 64 62 mixin SimpleConstructor; 65 mixin SimpleConstructor; 63 mixin SimpleCompare; 66 mixin SimpleCompare; > 67 mixin SimpleToString; 64 } 68 } 65 69 66 unittest 70 unittest 67 { 71 { 68 auto p = new immutable(LexPosition)("hello.cpp", 123, 45); 72 auto p = new immutable(LexPosition)("hello.cpp", 123, 45); 69 auto t = new Token(p, "class", false); 73 auto t = new Token(p, "class", false); 70 auto u = new Token(p, "class", true); 74 auto u = new Token(p, "class", true);

Modified polemy/parse.d from [de91c770214a43a1] to [51dd3bd106fd9ecb].

303 { 303 { 304 auto p = parserFromString(` 304 auto p = parserFromString(` 305 var zzz = 100; # comment 305 var zzz = 100; # comment 306 zzz = zzz + zzz * "fo\no"; # comment 306 zzz = zzz + zzz * "fo\no"; # comment 307 42; 307 42; 308 `); 308 `); 309 309 310 auto s0 = new DeclStatement(null, "zzz", new IntLiteralExpression(null, | 310 mixin EasyAst; > 311 auto s0 = decl("zzz", intl(BigInt(100))); 311 auto s1 = new ExprStatement(null, new AssignExpression(null, | 312 auto s1 = expr( new AssignExpression(null, 312 new VarExpression(null, "zzz"), 313 new VarExpression(null, "zzz"), 313 new FuncallExpression(null, new VarExpression(null,"+"), 314 new FuncallExpression(null, new VarExpression(null,"+"), 314 new VarExpression(null, "zzz"), 315 new VarExpression(null, "zzz"), 315 new FuncallExpression(null, new VarExpression(null,"*"), 316 new FuncallExpression(null, new VarExpression(null,"*"), 316 new VarExpression(null, "zzz"), 317 new VarExpression(null, "zzz"), 317 new StrLiteralExpression(null, "fo\\no") 318 new StrLiteralExpression(null, "fo\\no") 318 )))); 319 )))); ................................................................................................................................................................................ 327 328 328 unittest 329 unittest 329 { 330 { 330 auto p = parserFromString(` 331 auto p = parserFromString(` 331 var f = fun(x,y){x+y;}; 332 var f = fun(x,y){x+y;}; 332 f(1,fun(abc){}(4)); 333 f(1,fun(abc){}(4)); 333 `); 334 `); > 335 mixin EasyAst; 334 Program prog = p.parseProgram(); 336 Program prog = p.parseProgram(); 335 assert( prog.length == 2 ); 337 assert( prog.length == 2 ); 336 assert( prog[0] == new DeclStatement(null, "f", new FunLiteralExpression | 338 assert( prog[0] == decl("f", fun( 337 ["x","y"], [new ExprStatement(null, | 339 ["x","y"], [expr(funcall(var("+"), var("x"), var("y")))] 338 new FuncallExpression(null, new VarExpression(null, "+") < 339 new VarExpression(null, "x"), new VarExpression(null, "y < 340 ))); | 340 ))); 341 assert( prog[1] == new ExprStatement(null, new FuncallExpression(null, | 341 assert( prog[1] == expr(funcall(var("f"), 342 new VarExpression(null, "f"), < 343 new IntLiteralExpression(null, BigInt(1)), | 342 intl(BigInt(1)), 344 new FuncallExpression(null, | 343 funcall( 345 new FunLiteralExpression(null, ["abc"], [ < 346 ]), < > 344 fun(["abc"], cast(Statement[])[]), 347 new IntLiteralExpression(null, BigInt(4)) | 345 intl(BigInt(4)) 348 )))); | 346 )))); 349 } 347 } 350 348 351 unittest 349 unittest 352 { 350 { 353 auto p = parserFromString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); x 351 auto p = parserFromString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); x 354 Program prog = p.parseProgram(); 352 Program prog = p.parseProgram(); 355 } 353 }

Modified polemy/tricks.d from [fcea1bf69fccc37f] to [73906cba073bce6c].

113 assert_throw!AssertError( assert_eq(new Temp, 2) ); 113 assert_throw!AssertError( assert_eq(new Temp, 2) ); 114 } 114 } 115 115 116 /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_n 116 /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_n 117 117 118 /// Mixing-in the bean constructor for a class 118 /// Mixing-in the bean constructor for a class 119 119 > 120 /*mixin*/ 120 /*mixin*/ template SimpleConstructor() | 121 template SimpleConstructor() 121 { 122 { 122 static if( is(typeof(super) == Object) || super.tupleof.length==0 ) 123 static if( is(typeof(super) == Object) || super.tupleof.length==0 ) 123 this( typeof(this.tupleof) params ) 124 this( typeof(this.tupleof) params ) 124 { 125 { 125 static if(this.tupleof.length>0) 126 static if(this.tupleof.length>0) 126 this.tupleof = params; 127 this.tupleof = params; 127 } 128 } ................................................................................................................................................................................ 169 mixin SimpleConstructor; 170 mixin SimpleConstructor; 170 } 171 } 171 }) ); 172 }) ); 172 } 173 } 173 174 174 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class 175 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class 175 176 > 177 /*mixin*/ 176 /*mixin*/ template SimpleCompare() | 178 template SimpleCompare() 177 { 179 { 178 override bool opEquals(Object rhs_) const 180 override bool opEquals(Object rhs_) const 179 { 181 { 180 if( auto rhs = cast(typeof(this))rhs_ ) 182 if( auto rhs = cast(typeof(this))rhs_ ) 181 { 183 { 182 foreach(i,_; this.tupleof) 184 foreach(i,_; this.tupleof) 183 if( this.tupleof[i] != rhs.tupleof[i] ) 185 if( this.tupleof[i] != rhs.tupleof[i] ) ................................................................................................................................................................................ 234 } 236 } 235 assert_throw!AssertError( new Temp(1,"foo") == new TempDummy(1,"foo") ); 237 assert_throw!AssertError( new Temp(1,"foo") == new TempDummy(1,"foo") ); 236 assert_throw!AssertError( new Temp(1,"foo") <= new TempDummy(1,"foo") ); 238 assert_throw!AssertError( new Temp(1,"foo") <= new TempDummy(1,"foo") ); 237 } 239 } 238 240 239 /// Mixing-in a simple toString method 241 /// Mixing-in a simple toString method 240 242 > 243 /*mixin*/ 241 /*mixin*/ template SimpleToString() | 244 template SimpleToString() 242 { 245 { 243 override string toString() 246 override string toString() 244 { 247 { 245 string str = sprintf!"%s("(typeof(this).stringof); 248 string str = sprintf!"%s("(typeof(this).stringof); 246 foreach(i,mem; this.tupleof) 249 foreach(i,mem; this.tupleof) 247 { 250 { 248 if(i) str ~= ","; 251 if(i) str ~= ","; ................................................................................................................................................................................ 264 string y; 267 string y; 265 BigInt z; 268 BigInt z; 266 mixin SimpleConstructor; 269 mixin SimpleConstructor; 267 mixin SimpleToString; 270 mixin SimpleToString; 268 } 271 } 269 assert_eq( (new Temp(1,"foo",BigInt(42))).toString(), "Temp(1,foo,42)" ) 272 assert_eq( (new Temp(1,"foo",BigInt(42))).toString(), "Temp(1,foo,42)" ) 270 } 273 } > 274 > 275 /// Everything is in > 276 > 277 /*mixin*/ > 278 template SimpleClass() > 279 { > 280 mixin SimpleConstructor; > 281 mixin SimpleCompare; > 282 mixin SimpleToString; > 283 }