Differences From Artifact [b441073a5b98679b]:
- File
sample/macro.pmy
- 2010-11-26 05:03:10 - part of checkin [207cea338a] on branch trunk - changed hiding mechanizem of x in let x = ... for @macro layer. Old: set(x,ValueLayer,undefined) Nee: set(x,NoopLayer,null) (user: kinaba) [annotate]
To Artifact [99466484fdc29835]:
- File
sample/macro.pmy
- 2010-11-26 15:13:58 - part of checkin [6760e0dd02] on branch trunk - evaluator refactoring done. x6 speed up. (user: kinaba) [annotate]
94 # Both prints "original". Macro respects the neutral layer's "let y=" 94 # Both prints "original". Macro respects the neutral layer's "let y="
95 # and "fun(y)". It does not alter the inner scope y 95 # and "fun(y)". It does not alter the inner scope y
96 @macro test1(y) { fun(y){y}("original") }; 96 @macro test1(y) { fun(y){y}("original") };
97 @macro test2(y) { let y = "original" in y }; 97 @macro test2(y) { let y = "original" in y };
98 print( test1("replaced?") ); 98 print( test1("replaced?") );
99 print( test2("replaced?") ); 99 print( test2("replaced?") );
100 100
101 ######################################## <
102 print("----------"); <
103 <
104 # Macro expansion is done only at the first call. <
105 # So by using @macro parameter, it can remember the argument <
106 # of the first call. <
107 def remember1( x @macro, y ) { if x == y then "yes" else "no" }; <
108 print( remember1(1, 1) ); # yes 1 == 1 <
109 print( remember1(2,1) ); # yes "1" == 1 <
110 print( remember1(2,2) ); # no "1" != 2 <
111 <
112 # exactly the same function, but called in different order <
113 def remember2( x @macro, y ) { if x == y then "yes" else "no" }; <
114 print( remember2(2, 2) ); # yes "2" == 2 <
115 print( remember2(2, 1) ); # no "2" != 1 <
116 print( remember2(1, 1) ); # no "2" != 1 <
117 <
118 # Is this a good thing or a bad thing?? <
119 <
120 ######################################## 101 ########################################
121 print("----------"); 102 print("----------");
122 103
123 # Trick to extract the AST of a function 104 # Trick to extract the AST of a function
124 def foo(x) { x + x }; 105 def foo(x) { x + x };
125 print( @macro(@value(foo)(arg1)) ); # prints AST for "arg1 + arg1" 106 print( @macro(@value(foo)(arg1)) ); # prints AST for "arg1 + arg1"
126 107
127 # If we wrote @macro(foo(arg1)), it is the AST of "foo(arg1)" 108 # If we wrote @macro(foo(arg1)), it is the AST of "foo(arg1)"
128 # Here, by @value(foo) we obtain the "usual" behavior of foo, 109 # Here, by @value(foo) we obtain the "usual" behavior of foo,
129 # not the macro-like bahavior to construct AST "foo(??)". 110 # not the macro-like bahavior to construct AST "foo(??)".
130 # But still, by @macro( ... ) layer, the "usual" function is run in 111 # But still, by @macro( ... ) layer, the "usual" function is run in
131 # macro mode. 112 # macro mode.