This is the version 62efc62a22c8286bb19870c4 from 2022-08-07 14:03:22 comment: 'stuff...'
Kaba syntax ed
My general purpose programming language.
Full lib reference: kaba.reference
Table of Contents
The basics ed
A basic main() function:
func main()
print("hi")
Parameter and return type syntax:
# compiler decides if call-by-value/reference, but always IMMUTABLE
func f(a: int, b: string) -> int
# squaring a
return a^2
# force call-by-reference and make i mutable
func g(out i: int)
i = 13
Local variables
func main()
# guess the type
let i = 13
let f = sin(pi)
let s = "hallo"
# explicit type
let v: vec3
Dynamic arrays
func f(i: int) -> int[]
let a = [1, 2, 3]
print(len(a)) # 3
print(sum(a)) # 6
print(str(a)) # "[1, 2, 3]"
let b: int[]
# append one element
b.add(i)
# sub-range
return a[1:]
For loops:
func main()
let a = [1, 2, 3]
for i in a
print(i)
# python-style set builder notation
let aa = [for i in a i^2]
let bb = [for i in a i^2 if i > 1]
Dictionaries:
func main()
let a = {"red": 13, "blue": 2}
let b: int{}
print(a["red"])
Classes ed
Some member variables and functions:
class A
i: int
s: string
x = 1.0 # auto assignment in constructor
func f()
print(s)
# by default, the "self" object is immutable!
func mut set_x(f: float)
x = f
# will be const/mutable when called on a const/mutable instance
func ref get_a_reference_to_member() -> string
# "ownership bleeding"
return s[2:4]
const N = 8
class B # is in namespace A
k: int
Instantiation on stack and heap:
func main()
let a: A
let b: A* = new A()
# always "." for members!
a.f()
b.f()
Virtual functions
class A
func virtual f()
print("A")
class B extends A
func override f()
print("B")
func main()
var a: A* = new B()
a.f() # prints "B"
Function pointers ed
"int->int" is the type of a function pointer taking an int and returning an int:
func do_something(f: int->int) -> int
return f(13)
func add_one(i: int) -> int
return i+1
func main()
do_something(add_one)
let x = 4
# a lambda function with auto-capture
do_something(func(i: int) i^2 + x)
Cool stuff
func main()
let f = [1.0, 2.0, 3.0]
print(map(sqrt, f))
let a = sorted([1, 5, 2, 6, 3, 4])
let v: vec3[] = [[1,2,3], [4,5,6], [7,8,9]]
let w = sorted(v, "-x") # sort by the x component, decreasing
Categories: Programmieren