Differences From Artifact [274d10aca22ce25f]:
- File
sample/macro.pmy
- 2010-11-21 09:53:17 - part of checkin [435fa085ec] on branch trunk - refactored predefined layer names, and filled readme.txt. (user: kinaba) [annotate]
To Artifact [977cd2f997805f91]:
- File
sample/macro.pmy
- 2010-11-23 18:28:47 - part of checkin [ba11f1d551] on branch trunk - fixed the macro scoping rules concerning non-macro let (user: kinaba) [annotate]
1 @macro twice(x) { x; x }; | 1 @macro twice = fun(x) { x; x };
2 @macro max(x,y) { 2 @macro max(x,y) {
3 var _x = x; # no hygenic macro btw.... 3 var _x = x; # no hygenic macro btw....
4 var _y = y; # no hygenic macro btw.... 4 var _y = y; # no hygenic macro btw....
5 if(_x<_y){_y}else{_x} 5 if(_x<_y){_y}else{_x}
6 }; 6 };
7 def maxNormal(x,y) { 7 def maxNormal(x,y) {
8 if(x<y){y}else{x} 8 if(x<y){y}else{x}
................................................................................................................................................................................
11 if(x<y){y}else{x} 11 if(x<y){y}else{x}
12 }; 12 };
13 13
14 @macro LetItBe(x, y) { let it = x in y }; 14 @macro LetItBe(x, y) { let it = x in y };
15 15
16 @macro pow10(x) { 16 @macro pow10(x) {
17 @value( 17 @value(
> 18 def pow(y, n) {
> 19 if( n == 1 ) { y }
> 20 else {
> 21 @macro( @value(y) * @value(pow(y,n-1)) )
> 22 }
> 23 }
> 24 in
> 25 pow(@macro(x+1),10)
> 26 )
> 27 };
> 28
> 29 @macro pow10Hard(x) {
> 30 @value(
18 def pow(x, n) { 31 def pow(x, n) {
19 if( n == 1 ) { x } 32 if( n == 1 ) { x }
20 else { 33 else {
21 @macro( @value(x) * @value(pow(x,n-1)) ) 34 @macro( @value(x) * @value(pow(x,n-1)) )
22 } 35 }
23 } 36 }
24 in 37 in
25 pow(@macro(x),10) | 38 pow(@macro(x+1),10)
26 ) 39 )
27 }; 40 };
28 41
29 def printAndReturn(x) 42 def printAndReturn(x)
30 { 43 {
31 print(x); 44 print(x);
32 x 45 x
33 }; 46 };
34 <
35 <
36 <
37 <
38 47
39 def main() 48 def main()
40 { 49 {
41 twice( print("foo") ); 50 twice( print("foo") );
42 print("--------------"); 51 print("--------------");
43 print(max(printAndReturn(100),printAndReturn(200))); 52 print(max(printAndReturn(100),printAndReturn(200)));
44 print("--------------"); 53 print("--------------");
45 print(maxNormal(printAndReturn(100),printAndReturn(200))); 54 print(maxNormal(printAndReturn(100),printAndReturn(200)));
46 print("--------------"); 55 print("--------------");
47 print(maxBad(printAndReturn(100),printAndReturn(200))); 56 print(maxBad(printAndReturn(100),printAndReturn(200)));
48 print("--------------"); 57 print("--------------");
49 print( LetItBe( 1+2+3, it*it ) ); 58 print( LetItBe( 1+2+3, it*it ) );
50 print("--------------"); 59 print("--------------");
> 60 print(pow10(1));
51 print(pow10(2)); 61 print(pow10(2));
> 62 print("--------------");
> 63 print(pow10Hard(1));
> 64 print(pow10Hard(2));
> 65 };
> 66
> 67 main();
> 68
> 69 @macro foo(y)
> 70 {
> 71 fun(y){y}(300)
> 72 # let y = 300 in y
52 }; 73 };
53 74
54 main() | 75 print("--------------");
> 76 print(foo(200));