This is the version 621ecc73bf7e921ea546b3c4 from 2022-03-02 01:46:27 comment: 'stuff...'
kaba syntax ed
Table of Contents
Basics ed
Kaba is a mix of c++ and python. From c++ we stole all the functional parts, like the type system, classes and virtual functions. Python gave us syntactic sugar and block indentation.
- simple hello world
func main()
# let's greet everyone
print("hello world")
- tabs for indentation!
- function main is automatically executed
- no "real" code outside of functions
- comments start with #
Types and values ed
Basic types from c++: int, float, bool, char
Variables ed
- local variables
func f()
# declare a variable "i" of type int
var i: int
# declare "k" and give it an initial value
var k: int = 7
# well, we now, what type that should be...
var l = 13 # int
var f = 1.7 # float
var b = true # bool
var name = "Bert" # string
var c = 'A' # char
Constants ed
- global constants
const MY_CONST = 13
- enums
enum
A # will be 0
B # will be 1
C = 20
D # will be 21
enum MyNamedEnum
X
print(MyNamedEnum.X)
Dynamic arrays ed
- dynamic arrays
var array: int[] = [10, 20, 30]
array.add(40)
print(array) # prints [10, 20, 30, 40]
print(array[0]) # prints 10
array[1] = -4
- slices
var array = range(10) # [0, 1, ..., 9]
# slices
print(array[2:6]) # [2, 3, 4, 5]
print(array[6:]) # [6, 7, 8, 9]
print(array[:3]) # [0, 1, 2]
# counting from the end
print(array[:-2]) # [0, 1, ..., 7]
# can be written to
array[2,4] = [13, 14]
Functions ed
Parameters ed
- with parameters
func some_cool_function(name: string, length: float)
# do something
# we can have a return value
func another_function(i: int) -> int
return 2*i
func call_the_others()
some_cool_function("hallo", 3.1415)
var x = another_function(7)
- parameters are declared as parameter-name: type
- return type is void, if omitted
- inside the function, parameters are constant!
deeper insights:
- small, simple types like int, float are passed by value
- more complex types, like string are passed by reference
- default values
func f(a: int, b: int = 13)
func meta()
f(1, 2)
f(1) # equivalent to f(1, 13)
Output parameters ed
- output parameters
func f(out x: int)
x = 13
func meta()
var x = 0
f(x)
print(x) # should print 13
Categories: Programmieren