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 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 : LexPosition; 9 +import polemy.lex; 10 10 11 11 alias Statement[] Program; 12 12 13 13 abstract class Statement 14 14 { 15 15 immutable LexPosition pos; 16 16 mixin SimpleConstructor; 17 17 } 18 18 19 19 class DeclStatement : Statement 20 20 { 21 21 string var; 22 22 Expression expr; 23 - mixin SimpleConstructor; 24 - mixin SimpleCompare; 23 + mixin SimpleClass; 25 24 } 26 25 27 26 class ExprStatement : Statement 28 27 { 29 28 Expression expr; 30 - mixin SimpleConstructor; 31 - mixin SimpleCompare; 29 + mixin SimpleClass; 32 30 } 33 31 34 32 abstract class Expression 35 33 { 36 34 immutable LexPosition pos; 37 35 mixin SimpleConstructor; 38 - mixin SimpleCompare; 39 36 } 40 37 41 38 class StrLiteralExpression : Expression 42 39 { 43 40 string data; 44 - mixin SimpleConstructor; 45 - mixin SimpleCompare; 41 + mixin SimpleClass; 46 42 } 47 43 48 44 class IntLiteralExpression : Expression 49 45 { 50 46 BigInt data; 51 - mixin SimpleConstructor; 52 - mixin SimpleCompare; 47 + mixin SimpleClass; 53 48 } 54 49 55 50 class VarExpression : Expression 56 51 { 57 52 string var; 58 - mixin SimpleConstructor; 59 - mixin SimpleCompare; 53 + mixin SimpleClass; 60 54 } 61 55 62 56 class AssignExpression : Expression 63 57 { 64 58 Expression lhs; 65 59 Expression rhs; 66 - mixin SimpleConstructor; 67 - mixin SimpleCompare; 60 + mixin SimpleClass; 68 61 } 69 62 70 63 class FuncallExpression : Expression 71 64 { 72 65 Expression fun; 73 66 Expression[] args; 74 67 this(immutable LexPosition pos, Expression fun, Expression[] args...) 75 68 { super(pos); this.fun=fun; this.args=args.dup; } 76 - mixin SimpleCompare; 69 + mixin SimpleClass; 77 70 } 78 71 79 72 class FunLiteralExpression : Expression 80 73 { 81 74 string[] params; 82 75 Program funbody; 83 - mixin SimpleConstructor; 84 - mixin SimpleCompare; 76 + mixin SimpleClass; 77 +} 78 + 79 +/// Handy Generator for AST nodes. To use this, mixin EasyAst; 80 + 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 27 immutable int column; /// column, 1, 2, ... 28 28 29 29 override string toString() const 30 30 { return sprintf!"%s:%d:%d"(filename, lineno, column); } 31 31 32 32 mixin SimpleConstructor; 33 33 mixin SimpleCompare; 34 + 35 + static immutable LexPosition dummy; 36 + static this(){ dummy = new immutable(LexPosition)("<unnamed>",0,0); } 34 37 } 35 38 36 39 unittest 37 40 { 38 41 auto p = new LexPosition("hello.cpp", 123, 45); 39 42 auto q = new LexPosition("hello.cpp", 123, 46); 40 43 ................................................................................ 57 60 { 58 61 immutable LexPosition pos; /// Position where the token occurred in the source 59 62 immutable string str; /// The token string itself 60 63 immutable bool quoted; /// Was it a "quoted" token or unquoted? 61 64 62 65 mixin SimpleConstructor; 63 66 mixin SimpleCompare; 67 + mixin SimpleToString; 64 68 } 65 69 66 70 unittest 67 71 { 68 72 auto p = new immutable(LexPosition)("hello.cpp", 123, 45); 69 73 auto t = new Token(p, "class", false); 70 74 auto u = new Token(p, "class", true);

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

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

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

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