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 2 * Authors: k.inaba 3 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 7 module polemy._common; 8 +// basic utilities 8 9 public import tricks.test; 9 10 public import tricks.tricks; 10 11 public import std.algorithm; 11 12 public import std.array; 13 +public import std.range; 12 14 public import std.bigint; 15 +// debugging 13 16 public import std.conv : text; 14 -public import std.exception; 15 -public import std.range; 16 17 public import std.stdio : DBG = writeln; 18 +// meta programming 19 +public import std.exception; 17 20 public import std.traits; 18 21 public import std.typecons; 19 22 public import std.typetuple;

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

21 21 22 22 /// True if it is macro-like layer that basically generates syntax tree 23 23 24 24 bool isMacroishLayer( Layer lay ) 25 25 { 26 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 37 /// True if in the specified layer @lay(...) has no effect and merely produces a syntax tree 30 38 31 39 bool isNoLayerChangeLayer( Layer lay ) 32 40 { 33 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 382 unittest 383 383 { 384 384 assert( isForwardRange!(PositionedReader!string) ); 385 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 152 // [TODO] make this customizable from program 153 153 private static string[][] operator_perferences = [ 154 154 ["||"], 155 155 ["&&"], 156 156 ["!="], 157 157 ["=="], 158 158 ["<","<=",">",">="], 159 - ["|"], 160 - ["^"], 161 - ["&"], 162 - ["<<", ">>"], 159 +// ["|"], 160 +// ["^"], 161 +// ["&"], 162 +// ["<<", ">>", "<<<", ">>>"], 163 163 ["+","-"], 164 164 ["~"], 165 165 ["*","/","%"], 166 - ["^^","**"], 166 +// ["^^","**"], 167 167 [".",".?"] 168 168 ]; 169 169 170 170 AST E(size_t level) 171 171 { 172 172 /// Expression ::= (Binary left-associative operators over) Funcall 173 173 ................................................................................ 192 192 return Funcall(); 193 193 else 194 194 return rec(E(level+1)); 195 195 } 196 196 197 197 AST Funcall() 198 198 { 199 - /// Funcall ::= BaseExpression ["(" Expression%"," ")"]* 199 + /// Funcall ::= BaseExpression ["(" Expression%"," ")" | "{" ENTRIES "}"]* 200 200 201 201 auto e = BaseExpression(); 202 202 for(;;) 203 203 if( tryEat("(") ) 204 204 { 205 205 auto pos = currentPosition(); 206 206 AST[] args; ................................................................................ 222 222 else 223 223 break; 224 224 return e; 225 225 } 226 226 227 227 AST parseTableSetAfterBrace(AST e) 228 228 { 229 + /// TableSet ::= "{" (ID ":" E) % "," "}" 230 + 229 231 if( tryEat("}") ) 230 232 return e; 231 233 auto pos = currentPosition(); 232 234 for(;;) 233 235 { 234 236 string key = eatId("for table key", AllowQuoted); 235 237 eat(":", "after table key"); ................................................................................ 314 316 } 315 317 scope(exit) lex.popFront; 316 318 return new Var(pos, lex.front.str); 317 319 } 318 320 319 321 AST parsePatternMatch(LexPosition pos) 320 322 { 321 - // case( pmExpr )cases 323 + // case "(" pmExpr ")" CASES 322 324 //==> 323 325 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFirst()) 324 326 eat("(", "after case"); 325 327 AST pmExpr = E(0); 326 328 eat(")", "after case"); 327 329 string pmVar = freshVarName(); 328 330 string pmTryFirst = freshVarName(); ................................................................................ 657 659 case( 1 ) 658 660 when(x){1} 659 661 `)); 660 662 assert_nothrow(parseString(` 661 663 case( 1 ) 662 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 16 void enrollRuntimeLibrary( Evaluator e ) 17 17 { 18 18 e.addPrimitive("+", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} ); 19 19 e.addPrimitive("-", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} ); 20 20 e.addPrimitive("*", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} ); 21 21 e.addPrimitive("/", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} ); 22 22 e.addPrimitive("%", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} ); 23 + e.addPrimitive("~", ValueLayer, (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.toString);} ); 23 24 e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || rhs.data!=0);} ); 24 25 e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && rhs.data!=0);} ); 25 26 e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs < rhs);} ); 26 27 e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs > rhs);} ); 27 28 e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs <= rhs);} ); 28 29 e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs >= rhs);} ); 29 30 e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs == rhs);} );

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

379 379 {} 0-ary create-empty-table 380 380 . 2-ary table-get 381 381 .? 2-ary table-has? 382 382 .= 3-ary table-set 383 383 384 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 && and ||.) 387 387 < > <= >= == != 2-ary generic comparison 388 + ~ 2-ary string concatenation (works also for non-string objects) 388 389 389 390 print 1-ary print-to-stdout 390 391 391 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()