Skip to content

Instantly share code, notes, and snippets.

View Cyan-in-20-minutes
package main
// import a Cyan package
import cyan.math
import cyan.reflect
// import a Java package
import java.lang
@doc{*
@joseoliv
joseoliv / RestrictTo
Last active Apr 24, 2021
restrict the values a variable can hold
View RestrictTo
package metaobjectTest
@doc{*
Annotation 'type' should be attached to a type. It checks whether
the value of the type obeys the expression that is attached to
the annotation. Use 'self' as the value of the object that is
being restricted. Therefore, the expressions can be something like:
self >= 0
self >= 0 && self < 5
View FSM_DSL_Methods.cyan
package metaobjectTest
@doc{*
Annotation 'fsmMethod' should be attached to a prototype. The parameter
name is optional. If present, it should be an identifer. The metaobject
creates a reset method with name 'resetFSM_Methods' or with the name
of the parameter. This method resets the FSM to the initial state.
q0, "q0q0:1" -> q0
@joseoliv
joseoliv / AddMethodTo.cyan
Last active Apr 23, 2021
Metaobject addMethodTo
View AddMethodTo.cyan
package metaobjectTest
@addMethodTo("func zero -> Int", "func zero -> Int = 0;")
@addMethodTo(
"""func len: String s less: Int -> Int""",
"""
func len: String s less: Int n -> Int { return s size - n}
""")
object AddMethodTo
@joseoliv
joseoliv / AddCodeFromMetaobject.cyan
Last active Apr 23, 2021
Metaobjects addCodeToGenericPrototype and AddCodeFromMetaobject
View AddCodeFromMetaobject.cyan
package metaobjectTest
/*
@addCodeFromMetaobject is in prototype Array. The annotation
addCodeToGenericPrototype gives the code to be added to the
array of prototype AddCodeFromMetaobject.
*/
@addCodeToGenericPrototype("Array", " func sumAll -> Int"){*
@joseoliv
joseoliv / AddBeforeMethod.cyan
Last active Apr 23, 2021
Metaobject addBeforeMethod
View AddBeforeMethod.cyan
package metaobjectTest
@addBeforeMethod("zero", "logStr = logStr ++ \"called zero\";")
object AddBeforeMethod
func run {
assert zero == 0;
assert logStr == "called zero";
}
@joseoliv
joseoliv / Helloworld.cyan
Last active Mar 12, 2021
Hello world program in Cyan
View Helloworld.cyan
package main
object Program
func run {
"Hello world" println
}
end
@joseoliv
joseoliv / Action_afti_dsa.cyan
Last active Apr 6, 2020
metaobject action_afti_dsa
View Action_afti_dsa.cyan
package metaobjectTest
// @doc{+* is used instead of @doc{* because '{*' is used
// in the text attached to @doc
@doc{+*
This metaobject can add fields and methods to the current prototype,
add code before methods, and add code inside methods (if the annotation
is inside a method). This text is in language Markdown. Unlike other
metaobjects, this one demands the understanding of the Cyan Metaobject
View Shout.cyan
package metaobjectTest
@doc{*
Annotation 'shout' can be attached to a method. It changes
all strings to uppercase letters. This is a demonstration
metaobject, of course.
*}
object Shout
View ReplaceCallBy.cyan
package metaobjectTest
@doc{*
Annotation 'replaceCallBy' should be attached to a final method. It
replaces any message passings to the method by the attached DSL code.
The DSL code may use the parameters, which are re-calculated if
parameter 'once' to the annotation is not used.
All methods of the prototype ReplaceCallBy are final because it is a