Umka: a statically typed embeddable scripting language
Umka is a statically typed embeddable scripting language. It combines simplicity and flexibility with compile-time type checking, following the principle Explicit is better than implicit.
400 x 400 matrix multiplication (AMD A4-3300M @ 1.9 GHz, Windows 7)
fn main() {
printf("Hello Umka!\n")
}
const a = 3
const b* = 2.38 // Exported identifier
const (
c = sin(b) / 5
d = "Hello" + " World"
)
type IntPtr = ^uint16 // Pointer
type Arr = [a]real // Array
type (
DynArr = []int // Dynamic array
String = str // String
Button = enum { // Enumeration
left
middle
right
}
MyMap = map[str]real // Map
Quat = struct { // Structure
q: [4]real
normalized: bool
}
Printable = interface { // Interface
print(): int
}
ErrFn = fn(code: int) // Function
)
var e: int
var f: String = d + "!"
var (
g: Arr = {2.3, -4.1 / 2, b}
h: DynArr
m: MyMap
)
q := Quat{{1, 0, 0, 0}, true}
fn tan(x: real): real {
return sin(x) / cos(x)
}
fn getValue(): (int, bool) {
return 42, true
}
callback := fn (event: int): bool |context| { // Closure capturing context
return context.ok && event > 0
}
fn (a: ^Arr) print(): int {
printf("Arr: %v\n", a^)
return 0
}
a = 42
m["Hello Umka"] = 3.14
alpha, beta = beta, alpha
sum := 0.0
h := make([]int, 3)
y := tan(30 * std::pi / 180)
h = append(h, []int{10, 20, 30, 40, 50})
h = delete(h, 1)
g.print()
if x, ok := getValue(); ok {
printf("Got %v\n", x)
}
switch a {
case 1, 3, 5, 7: printf("%d is odd\n", a)
case 2, 4, 6, 8: printf("%d is even\n", a)
default: printf("I don't know\n")
}
switch v := type(val) { // val is an interface
case int: printf("int: %d + 5 = %d\n", v, v + 5)
case str: printf("str: %s + 5 = %s\n", v, v + "5")
default: printf("unknown: %v\n", a)
}
for k := 1; k <= 128; k *= 2 {
printf("%v\n", k)
}
for i, x in g {
if fabs(x) > 1e12 {break}
if x < 0 {continue}
sum += x
}
a := new(int)
child := make(fiber, |a| { // a is captured
for i := 0; i < 5; i++ {
printf("Child : i = %d buf = %d\n", i, a^)
a^ = i * 3
resume() // Switch back to parent
}
})
for i := 0; i < 10; i++ {
printf("Parent: i = %d buf = %d\n", i, a^)
a^ = i * 7
if valid(child) {
resume(child) // Switch to child
}
}
While Go is a compiled systems programming language with a complex runtime library and big output binaries, Umka is a scripting language with a lightweight interpreter that can be easily embedded into any application as a shared library.
Umka is very similar to Go syntactically. However, in some aspects it’s different. It has shorter keywords: fn
for func
, str
for string
, in
for range
. For better readability, it requires a :
between variable names and types in declarations. It doesn’t follow the unfortunate C tradition of pointer dereferencing. Instead of *p
, it uses the Pascal syntax p^
. As the *
character is no longer used for pointers, it becomes the export mark, like in Oberon, so that a programmer can freely use upper/lower case letters in identifiers according to his/her own style. Type assertions don’t have any special syntax; they look like pointer type casts. Closure definitions require explicit lists of captured variables.
Umka allows implicit type casts and supports default parameters in function declarations. It features the ternary conditional operator deliberately omitted from Go. It doesn’t have slices as separate data types. Instead, it supports dynamic arrays, which are declared like Go’s slices and initialized by calling make()
. Method receivers must be pointers. The multithreading model in Umka is inspired by Lua and Wren rather than Go. It offers lightweight threads called fibers instead of goroutines and channels. The garbage collection mechanism is based on reference counting, so Umka needs to support weak
pointers. Full Unicode support is under development.