Skip to content

A Tour of CellScript

Installations

installation

CellScript by example

Hello World

package main
import "debug"
function main() {
    debug.Println("hello world")
}
$ cell -r riscv hello-world.cell
$ ls
hello-world.cell hello-world

$ ckb-debugger --bin hello-world
hello world

Value

package main
import "debug"
function main() {
    debug.Printf("cellscript")
    debug.Printf("1+1 = %d", 1+1)
    debug.Printf("true = %d", true)
	return 0
}
$ cell -r riscv value.cell
$ ckb-debugger --bin value
cellscript
1+1 = 2
true = 1

Variables

package main
import "debug"
function main() {
    var a = "initial"
    debug.Printf("%s", a)
    var b, c int64 = 1, 2
    debug.Printf("%d %d", b, c)
    var d = true
    debug.Printf("%d", d)
    var e int32
    debug.Printf("%d", e)
    f := "short"
    debug.Printf("%s", f)
	return 0
}
$ cell -r riscv value.cell
$ ckb-debugger --bin value
initial
1 2
1
0
short

Constants

package main
import (
    "debug"
)

const (
	s string = "constant"
	s2 = "string: s2"
)
function main() {
    debug.Printf("%s", s)
    debug.Printf("%s", s2)
    const n = 500000000
    debug.Printf("%d", n)
	return 0
}
$ cell -r riscv const.cell
$ ckb-debugger --bin const
constant
another constant
500000000

For Loop

package main
import "debug"
function main() {
    for j := 7; j <= 9; j++ {
        debug.Printf("%d", j)
    }

    for n := 0; n <= 5; n++ {
        if (n&1) == 0 {
            continue
        }
        debug.Printf("%d", n)
    }
    return 0
}
$ cell -r riscv for.cell
$ ckb-debugger --bin for
7
8
9
1
3
5

If/Else Branch

package main
import "debug"

function main() {
    if (7&1) == 0 {
        debug.Printf("7 is even")
    } else {
        debug.Printf("7 is odd")
    }

    num := 9
    if num < 0 {
        debug.Printf("9 is negative")
    } else if num < 10 {
        debug.Printf("9 has 1 digit")
    } else {
        debug.Printf("9 has multiple digits")
    }
    return 0
}
$ cell -r riscv for.cell
$ ckb-debugger --bin for
7 is odd
9 has 1 digit

Array

package main

import "fmt"

func main() {

    var a [5]int
    fmt.Println("emp:", a)

    a[4] = 100
    fmt.Println("set:", a)
    fmt.Println("get:", a[4])

    fmt.Println("len:", len(a))

    b := [5]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)

    var twoD [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD)
}

Interface


import (
	"errors"
	"debug"
)
type Demo table {
	start uint32
	end   uint32
}
func (b *Demo) st() (n uint32, err error) {
	return b.start, errors.None()
}
func (b *Demo) ed() uint32 {
	return b.end
}
func (b *Demo) reset() {
	b.start = uint32(0)
	b.end = uint32(0)
}
type Range interface {
	st() (uint32, error)
	ed() uint32
	reset()
}
func foo(b Range) {
	n, err := b.st()
	debug.Printf("%s", err.Error())
	end := b.ed()
	debug.Printf("%d", end)
	b.reset()
}
func main() {
	b := Demo {
		start: uint32(1),
		end: uint32(3),
	}
	foo(b)
	debug.Printf("reset%d%d", b.start, b.end)
	return 0
}