CX is being revived in skycoin-cx. Work on the language stopped in 2023 and started again in November 2026, in one new repository that merges the old ones. There is no release yet, and CX chains, which were meant to run CX programs on a blockchain, are still unfinished. The language pages here follow the current code. The state of CX explains what has been done, what works, and what does not.
CX Examples
Table of Contents
Every program on this page was run with cx built from
skycoin-cx at commit
df4c9879. Save one as example.cx and run cx example.cx.
Hello World
A classic. string.PrintLine belongs to the built-in package
sys/string. Built-in packages still have to be imported, by their
sys/ path.
package main
import "sys/string"
func main () {
string.PrintLine("Hello World!")
}
Comments
Comments in CX follow the same syntax as in many other popular languages, like C and Go. Use // to comment out one line, and /* ... */ to comment out multiple lines in a source file.
package main
func main () {
// This won't be evaluated
// i32.print(i32.div(5, 0))
i32.print(i32.div(10, 5)) // prints 2
/*
Comment block
f32.print(f32.sub(3.33, 1.11))
f32.print(f32.sub(3.33, 1.11))
*/
}
Definitions
A definition is just a global variable. This means that any function in the current package or in those packages that have imported the current package can access the definition's value.
In this example, we can see that both printGlobals and main are accessing the definitions global1 and global2.
package main
var global1 i32 = 5
var global2 f32 = 3.14159
func printGlobals () {
i32.print(global1)
f32.print(global2)
}
func main () {
printGlobals()
f32.print(global2)
i32.print(global1)
}
Functions
The example defines two functions: one for calculating a circle's area, and another for calculating a circle's perimeter. Outputs are named, and a function returns whatever its named outputs hold when it ends.
package main
var PI f32 = 3.14159
func circleArea (radius f32) (area f32) {
area = radius * radius * PI
}
func circlePerimeter (radius f32) (perimeter f32) {
perimeter = 2.0 * radius * PI
}
func main () {
f32.print(circleArea(2.0))
f32.print(circlePerimeter(5.0))
}
Structs
User-defined types can be created by the use of structs. In this example, a type called "Point" is created. We want a point to be defined by a name, and its x and y coordinates on a plane.
The main function first prints the fields of an uninitialized Point. This won't crash the program, as CX initializes the fields to their zero value: an empty string for a str, false for a bool and 0 for an i32. Then it sets the fields of a second Point and prints their sum, 50.
package main
import "sys/string"
type Point struct {
name str
x i32
y i32
}
func main () {
var uPoint Point
string.PrintLine(uPoint.name)
i32.print(uPoint.x)
i32.print(uPoint.y)
var myPoint Point
myPoint.x = 5 + 10
myPoint.y = myPoint.x + 20
i32.print(myPoint.x + myPoint.y)
}
Arrays
Three slices hold the ids, ages and grades of four students. The bracket notation reads the values at index 1 of each.
package main
import "sys/string"
func main () {
var ids []i32
var ages []i32
var grades []f32
ids = []i32{433, 561, 652, 984}
ages = []i32{23, 21, 26, 31}
grades = []f32{8.8, 9.4, 9.3, 8.3}
string.PrintLine("Student's ID:")
i32.print(ids[1])
string.PrintLine("Student's age:")
i32.print(ages[1])
string.PrintLine("Student's grade:")
f32.print(grades[1])
string.PrintLine("done.")
}
Expressions
Four functions and a main function are defined in this example. sayHi takes no input and returns no output. sayMyName receives one parameter as input, but does not return anything. returnName receives no parameters as input, but returns one output.
multiReturn receives two input parameters and returns four output parameters: the sum, difference, product and quotient of two 32 bit integers. The main function receives all four in one assignment.
package main
import "sys/string"
func sayHi () {
string.PrintLine("Hi")
}
func sayMyName (name str) {
string.PrintLine(name)
}
func returnName () (name str) {
name = "Bart"
}
func multiReturn (num1 i32, num2 i32) (add i32, sub i32, mul i32, div i32) {
add = num1 + num2
sub = num1 - num2
mul = num1 * num2
div = num1 / num2
}
func main () {
sayHi()
sayMyName("Homer")
string.PrintLine(returnName())
var a i32
var s i32
var m i32
var d i32
a, s, m, d = multiReturn(20, 20)
printf("%d %d %d %d\n", a, s, m, d)
}
Go-to
goto should not be used often, as it can create programs that are very hard to debug. The compiler itself turns if and for into jumps.
If you really need a go-to in your program, you can use it. The example shows an if/else built from one conditional goto and a label.
package main
import "sys/string"
func basicIf (num i32) {
if num > 0 {
goto greater
}
string.PrintLine("Not greater than 0")
return
greater:
string.PrintLine("Greater than 0")
}
func main () {
basicIf(5)
basicIf(-5)
}
If and if/else
If and if/else statements in CX work as in any other programming language. If the condition evaluates to true, the first block of expressions will be executed; if the condition evaluates to false, the second block of expressions will be executed.
Like in Go, the condition doesn't need to be enclosed in parenthesis. Strings have no == operator, so they are compared with string.EqualityByByteValue.
package main
import "sys/string"
func main () {
if false {
string.PrintLine("This will never be printed")
}
if true {
string.PrintLine("This will always print")
}
if 5 > 3 {
string.PrintLine("5 is greater than 3")
}
if string.EqualityByByteValue("password123", "password123") {
string.PrintLine("Access granted")
} else {
string.PrintLine("Access denied")
}
var out i32
if 50 <= 5 {
out = 100
} else {
out = 200
}
i32.print(out)
}
Looping
You can use for to create a loop. The loop below evaluates its block while the condition is true, so it prints the numbers from 0 to 9.
package main
func main () {
var count i32
for count < 10 {
i32.print(count)
count = count + 1
}
}
Factorial
The factorial function shows how one can use recursion in CX. It prints 720.
package main
func factorial (num i32) (fact i32) {
if num == 1 {
fact = 1
} else {
fact = num * factorial(num - 1)
}
}
func main () {
i32.print(factorial(6))
}
More examples
The tests/examples
directory of skycoin-cx has more programs. The OpenGL ones need a cx
built with make build-gui.
This page used to show genetic programming with evolve, the REPL
meta-programming commands such as :dStack and :step, the
meta-programming functions remExpr, addExpr and affExpr, and a
robot simulator built on them. evolve and those functions are not
in skycoin-cx, and at commit df4c9879 a meta-programming command
such as :dStack true; stops the REPL with a syntax error, so those
examples are gone.