Check-in [5e924caac9]
Not logged in
Overview
SHA1 Hash:5e924caac94e53130e36a75af11b4314fc4945e6
Date: 2010-11-23 19:37:54
User: kinaba
Comment:added AST-rewriting macro sample.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified polemy/_common.d from [6dc6c83f7a158b25] to [30ee89adced74f7a].

1 /** 1 /** 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 * These modules are globaly used inside Polemy. | 5 * The list of modules globaly used inside Polemy. 6 */ 6 */ 7 module polemy._common; 7 module polemy._common; > 8 // basic utilities 8 public import tricks.test; 9 public import tricks.test; 9 public import tricks.tricks; 10 public import tricks.tricks; 10 public import std.algorithm; 11 public import std.algorithm; 11 public import std.array; 12 public import std.array; > 13 public import std.range; 12 public import std.bigint; 14 public import std.bigint; > 15 // debugging 13 public import std.conv : text; 16 public import std.conv : text; 14 public import std.exception; < 15 public import std.range; < 16 public import std.stdio : DBG = writeln; 17 public import std.stdio : DBG = writeln; > 18 // meta programming > 19 public import std.exception; 17 public import std.traits; 20 public import std.traits; 18 public import std.typecons; 21 public import std.typecons; 19 public import std.typetuple; 22 public import std.typetuple;

Modified polemy/layer.d from [f9c1f3b744698d72] to [59dc790b72d21ee0].

21 21 22 /// True if it is macro-like layer that basically generates syntax tree 22 /// True if it is macro-like layer that basically generates syntax tree 23 23 24 bool isMacroishLayer( Layer lay ) 24 bool isMacroishLayer( Layer lay ) 25 { 25 { 26 return lay==MacroLayer || lay==RawMacroLayer; 26 return lay==MacroLayer || lay==RawMacroLayer; 27 } 27 } > 28 > 29 unittest > 30 { > 31 assert( !isMacroishLayer(SystemLayer) ); > 32 assert( !isMacroishLayer(ValueLayer) ); > 33 assert( isMacroishLayer(MacroLayer) ); > 34 assert( isMacroishLayer(RawMacroLayer) ); > 35 } 28 36 29 /// True if in the specified layer @lay(...) has no effect and merely produces a 37 /// True if in the specified layer @lay(...) has no effect and merely produces a 30 38 31 bool isNoLayerChangeLayer( Layer lay ) 39 bool isNoLayerChangeLayer( Layer lay ) 32 { 40 { 33 return lay==RawMacroLayer; 41 return lay==RawMacroLayer; 34 } 42 } > 43 > 44 unittest > 45 { > 46 assert( !isNoLayerChangeLayer(SystemLayer) ); > 47 assert( !isNoLayerChangeLayer(ValueLayer) ); > 48 assert( !isNoLayerChangeLayer(MacroLayer) ); > 49 assert( isNoLayerChangeLayer(RawMacroLayer) ); > 50 }

Modified polemy/lex.d from [ad028a2a26b6c7bc] to [b98d4e6a5ceb6fb9].

379 } 379 } 380 } 380 } 381 381 382 unittest 382 unittest 383 { 383 { 384 assert( isForwardRange!(PositionedReader!string) ); 384 assert( isForwardRange!(PositionedReader!string) ); 385 assert( is(ElementType!(PositionedReader!string) == dchar) ); 385 assert( is(ElementType!(PositionedReader!string) == dchar) ); > 386 { > 387 auto pr = PositionedReader!string("abc","",1,1); > 388 assert_eq(pr.currentPosition().column, 1); pr.popFront; > 389 assert_eq(pr.currentPosition().column, 2); pr.popFront; > 390 assert_eq(pr.currentPosition().column, 3); pr.popFront; > 391 } > 392 { > 393 auto pr = PositionedReader!string("\n\r\n\n","",1,1); > 394 assert_eq(pr.currentPosition().lineno, 1); pr.popFront; > 395 assert_eq(pr.currentPosition().lineno, 2); pr.popFront; > 396 assert_eq(pr.currentPosition().lineno, 3); pr.popFront; > 397 } 386 } 398 }

Modified polemy/parse.d from [9df8a8a07696ff22] to [b6858e7b65012cb1].

152 // [TODO] make this customizable from program 152 // [TODO] make this customizable from program 153 private static string[][] operator_perferences = [ 153 private static string[][] operator_perferences = [ 154 ["||"], 154 ["||"], 155 ["&&"], 155 ["&&"], 156 ["!="], 156 ["!="], 157 ["=="], 157 ["=="], 158 ["<","<=",">",">="], 158 ["<","<=",">",">="], 159 ["|"], | 159 // ["|"], 160 ["^"], | 160 // ["^"], 161 ["&"], | 161 // ["&"], 162 ["<<", ">>"], | 162 // ["<<", ">>", "<<<", ">>>"], 163 ["+","-"], 163 ["+","-"], 164 ["~"], 164 ["~"], 165 ["*","/","%"], 165 ["*","/","%"], 166 ["^^","**"], | 166 // ["^^","**"], 167 [".",".?"] 167 [".",".?"] 168 ]; 168 ]; 169 169 170 AST E(size_t level) 170 AST E(size_t level) 171 { 171 { 172 /// Expression ::= (Binary left-associative operators over) Func 172 /// Expression ::= (Binary left-associative operators over) Func 173 173 ................................................................................................................................................................................ 192 return Funcall(); 192 return Funcall(); 193 else 193 else 194 return rec(E(level+1)); 194 return rec(E(level+1)); 195 } 195 } 196 196 197 AST Funcall() 197 AST Funcall() 198 { 198 { 199 /// Funcall ::= BaseExpression ["(" Expression%"," ")"]* | 199 /// Funcall ::= BaseExpression ["(" Expression%"," ")" | "{" ENT 200 200 201 auto e = BaseExpression(); 201 auto e = BaseExpression(); 202 for(;;) 202 for(;;) 203 if( tryEat("(") ) 203 if( tryEat("(") ) 204 { 204 { 205 auto pos = currentPosition(); 205 auto pos = currentPosition(); 206 AST[] args; 206 AST[] args; ................................................................................................................................................................................ 222 else 222 else 223 break; 223 break; 224 return e; 224 return e; 225 } 225 } 226 226 227 AST parseTableSetAfterBrace(AST e) 227 AST parseTableSetAfterBrace(AST e) 228 { 228 { > 229 /// TableSet ::= "{" (ID ":" E) % "," "}" > 230 229 if( tryEat("}") ) 231 if( tryEat("}") ) 230 return e; 232 return e; 231 auto pos = currentPosition(); 233 auto pos = currentPosition(); 232 for(;;) 234 for(;;) 233 { 235 { 234 string key = eatId("for table key", AllowQuoted); 236 string key = eatId("for table key", AllowQuoted); 235 eat(":", "after table key"); 237 eat(":", "after table key"); ................................................................................................................................................................................ 314 } 316 } 315 scope(exit) lex.popFront; 317 scope(exit) lex.popFront; 316 return new Var(pos, lex.front.str); 318 return new Var(pos, lex.front.str); 317 } 319 } 318 320 319 AST parsePatternMatch(LexPosition pos) 321 AST parsePatternMatch(LexPosition pos) 320 { 322 { 321 // case( pmExpr )cases | 323 // case "(" pmExpr ")" CASES 322 //==> 324 //==> 323 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir 325 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir 324 eat("(", "after case"); 326 eat("(", "after case"); 325 AST pmExpr = E(0); 327 AST pmExpr = E(0); 326 eat(")", "after case"); 328 eat(")", "after case"); 327 string pmVar = freshVarName(); 329 string pmVar = freshVarName(); 328 string pmTryFirst = freshVarName(); 330 string pmTryFirst = freshVarName(); ................................................................................................................................................................................ 657 case( 1 ) 659 case( 1 ) 658 when(x){1} 660 when(x){1} 659 `)); 661 `)); 660 assert_nothrow(parseString(` 662 assert_nothrow(parseString(` 661 case( 1 ) 663 case( 1 ) 662 when({aaaa:_}){1} 664 when({aaaa:_}){1} 663 `)); 665 `)); > 666 assert_nothrow(parseString(` > 667 case( 1 ) > 668 when({aaaa:@value(x)}){1} > 669 when({aaaa:{bbb:_}, ccc:123}){1} > 670 `)); 664 } 671 }

Modified polemy/runtime.d from [7110e96fc2a7a412] to [6af5a0863b67aa85].

16 void enrollRuntimeLibrary( Evaluator e ) 16 void enrollRuntimeLibrary( Evaluator e ) 17 { 17 { 18 e.addPrimitive("+", ValueLayer, (IntValue lhs, IntValue rhs){return new 18 e.addPrimitive("+", ValueLayer, (IntValue lhs, IntValue rhs){return new 19 e.addPrimitive("-", ValueLayer, (IntValue lhs, IntValue rhs){return new 19 e.addPrimitive("-", ValueLayer, (IntValue lhs, IntValue rhs){return new 20 e.addPrimitive("*", ValueLayer, (IntValue lhs, IntValue rhs){return new 20 e.addPrimitive("*", ValueLayer, (IntValue lhs, IntValue rhs){return new 21 e.addPrimitive("/", ValueLayer, (IntValue lhs, IntValue rhs){return new 21 e.addPrimitive("/", ValueLayer, (IntValue lhs, IntValue rhs){return new 22 e.addPrimitive("%", ValueLayer, (IntValue lhs, IntValue rhs){return new 22 e.addPrimitive("%", ValueLayer, (IntValue lhs, IntValue rhs){return new > 23 e.addPrimitive("~", ValueLayer, (Value lhs, Value rhs){return new StrVa 23 e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new 24 e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new 24 e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new 25 e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new 25 e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntVa 26 e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntVa 26 e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntVa 27 e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntVa 27 e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntVa 28 e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntVa 28 e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntVa 29 e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntVa 29 e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntVa 30 e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntVa

Modified readme.txt from [d36ee595e22be49c] to [78aa08fe5254309c].

379 {} 0-ary create-empty-table 379 {} 0-ary create-empty-table 380 . 2-ary table-get 380 . 2-ary table-get 381 .? 2-ary table-has? 381 .? 2-ary table-has? 382 .= 3-ary table-set 382 .= 3-ary table-set 383 383 384 if 3-ary if-then-else 384 if 3-ary if-then-else 385 385 386 + - * / % || && 2-ary integer-operations (no short-circuit!) | 386 + - * / % || && 2-ary integer-operations (NOTE! no short-circuit for && an 387 < > <= >= == != 2-ary generic comparison 387 < > <= >= == != 2-ary generic comparison > 388 ~ 2-ary string concatenation (works also for non-string obje 388 389 389 print 1-ary print-to-stdout 390 print 1-ary print-to-stdout 390 391 391 _isint _isstr _isfun _isundefined _istable 1-ary dynamic-type-test 392 _isint _isstr _isfun _isundefined _istable 1-ary dynamic-type-test 392 393

Added sample/argv.pmy version [c595f8c10fb25b25]

> 1 print(argv)

Added sample/ast.pmy version [94485181e809cc7b]

> 1 def reverse(lst, acc) > 2 { > 3 case(lst) > 4 when( {car:a, cdr: d} ) { reverse(d, {car:a, cdr:acc}) } > 5 when( {} ) { acc } > 6 }; > 7 > 8 @macro reverseArgs(e) {@value( > 9 var ev = @macro(e); > 10 case(ev) > 11 when( {is:"app", fun:f, args:a} ) > 12 { > 13 ev {args: reverse(a, {})} > 14 } > 15 when( _ ) > 16 { > 17 ev > 18 } > 19 )}; > 20 > 21 def main() > 22 { > 23 print( reverseArgs(1 + 2) ); > 24 print( reverseArgs(1 - 2) ); > 25 }; > 26 > 27 main()