Last active
August 30, 2022 11:50
-
-
Save joseoliv/b8d5ef83e43eb3ca4d77c386d2fd7a68 to your computer and use it in GitHub Desktop.
onMethodMissing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package metaobjectTest | |
@doc{* | |
Annotations of this metaobject should be attached to a prototype. | |
The attached DSL code is made in Myan, which supports interpreted Cyan | |
within parameterless methods declared with 'func': | |
func afterResTypes_codeToAdd { | |
// interpreted Cyan code | |
} | |
According to the method name, the Cyan code is interpreted in phase | |
afterResTypes or semAn. Methods of interfaces | |
IAction_semAn | |
IActionNewPrototypes_semAn | |
IAction_afterResTypes | |
IActionNewPrototypes_afterResTypes | |
IParseWithCyanCompiler_parsing | |
ICommunicateInPrototype_afterResTypes_semAn_afterSemAn | |
IActionMethodMissing_semAn | |
can be implemented. To better understand this, see the documentation | |
for action_afterResTypes_semAn. | |
*} | |
@onMethodMissing{* | |
/* A method missing with two keywords and two parameters p1 and p2 | |
is replaced by | |
p1 ++ p2 ++ p1 | |
*/ | |
func semAn_missingKeywordMethod { | |
var String sss0 = ((message getkeywordParameterList get: 0) | |
getExprList get: 0) asString; | |
var String sss1 = ((message getkeywordParameterList get: 1) | |
getExprList get: 0) asString; | |
return [. sss0 ++ " ++ " ++ sss1 ++ " ++ " ++ sss0, "cyan.lang", "String" .]; | |
} | |
// a custom error message for unary message passings | |
// ending with 'Test' | |
func semAn_missingUnaryMethod { | |
var String name = unarySymbol getSymbolString; | |
if name endsWith: "Test" { | |
metaobject addError: "Methods ending with 'Test' have not been implemented yet"; | |
} | |
} | |
*} | |
object OnMethodMissing | |
func run { | |
var ssss = self use: "abc" append: 0; | |
printexpr ssss; | |
assert ssss == "abc0abc"; | |
// if uncommented, a compilation error with a custom error message | |
// self selfTest; | |
// if uncommented, a regular compilation error | |
// self alienMethod; | |
} | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment