Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Created June 25, 2019 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseoliv/46f84facbb39f892e7d9ba71ce47bdf3 to your computer and use it in GitHub Desktop.
Save joseoliv/46f84facbb39f892e7d9ba71ce47bdf3 to your computer and use it in GitHub Desktop.
runPastCode
package metaobjectTest
import cyan.io
@doc{*
Annotation 'runPastCode'
*}
object RunPastCode
func run {
self testImportProgram;
self testPastCode;
}
func fat: Int n -> Int {
if n == 0 { return 1; } else { return n*(fat: n - 1); }
}
func testImportProgram {
var String color = favoriteColor;
var RunPastCode past = self;
var Int f5 = 0;
@runPastCode(false, past){*
var Int fatorial5 = past fat: 5;
return "f5 = " ++ fatorial5 ++ ";";
*}
if f5 == 120 {
"runPastCode was called with 'true'" println
}
else {
"runPastCode was called with 'false'" println
}
f5 = 0;
var RunPastCode past2 = self;
@runPastCode(true, past2){*
var Int fatorial5 = past2 fat: 5;
return "f5 = " ++ fatorial5 ++ ";";
*}
if f5 == 120 {
"runPastCode was called with 'true'" println
}
else {
"runPastCode was called with 'false'" println
}
}
func testPastCode {
var RunPastCode past3 = self;
favoriteColor = "blue";
@runPastCode(true, past3){*
return " favoriteColor = \"" ++
past3 getFavoriteColor ++ "\";";
*}
/* - change the assignment
favoriteColor = "blue";
to
favoriteColor = "cyan";
compile this prototype with the first
parameter to the annotation above set to 'false'
- change the assignment
favoriteColor = "cyan";
to
favoriteColor = "blue";
uncomment the next line and compile this prototype
with the first parameter to the annotation above set to 'true'
The annotation will generate an assignment to 'favoriteColor'
using the previous value of 'favoriteColor', which is 'cyan'
*/
assert favoriteColor == "cyan";
printexpr favoriteColor;
}
@property var String favoriteColor = "blue";
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment