Master Keikaku from fundamentals to advanced features. This tutorial covers everything you need to write production-ready code.
Keikaku uses := for declaration and = for reassignment. All variables are dynamically typed.
1# Variable declaration with :=2name := "Keikaku"3version := 1.04is_active := true5count := 4267# Reassignment with =8count = count + 1910# Lists (arrays)11numbers := [1, 2, 3, 4, 5]12mixed := ["text", 42, true]1314# Dictionaries15config := {16 "host": "localhost",17 "port": 808018}1920# Accessing elements21first := numbers[0] # 122host := config["host"] # "localhost"1# Convert to string2text("42") # "42"3text(3.14) # "3.14"45# Convert to number6number("42") # 427decimal("3.14") # 3.1489# Check type10classify(42) # "int"11classify("hi") # "string"12classify([1,2]) # "list"Use foresee for if-statements, alternate for else-if, and otherwise for else.
1score := 8523foresee score >= 90:4 declare("Grade: A")5alternate score >= 80:6 declare("Grade: B")7alternate score >= 70:8 declare("Grade: C")9otherwise:10 declare("Grade: F")1112# Output: Grade: B1# Range loop (exclusive end)2cycle from 1 to 5 as i:3 declare(i)4# Output: 1, 2, 3, 456# List iteration7items := ["apple", "banana", "cherry"]8cycle through items as fruit:9 declare("I like", fruit)1011# While loop12count := 013cycle while count < 3:14 declare("Count:", count)15 count = count + 11617# Infinite loop with break18cycle:19 val := inquire("Enter 'quit' to exit: ")20 foresee val == "quit":21 terminateFunctions in Keikaku are called protocols. They encapsulate reusable logic.
1# Basic protocol2protocol greet(name):3 declare("Hello,", name, "!")45greet("World") # Output: Hello, World !67# Protocol with return value8protocol add(a, b):9 yield a + b1011result := add(5, 3)12declare(result) # Output: 81314# Protocol with default parameters15protocol power(base, exp := 2):16 result := 117 cycle from 0 to exp as _:18 result = result * base19 yield result2021declare(power(3)) # 9 (3^2)22declare(power(2, 8)) # 256 (2^8)1# Anonymous protocols for higher-order functions2numbers := [1, 2, 3, 4, 5]34# transform = map5doubled := transform(numbers, protocol(x): yield x * 2)6# [2, 4, 6, 8, 10]78# select = filter9evens := select(numbers, protocol(x): yield x % 2 == 0)10# [2, 4]1112# fold = reduce13sum := fold(numbers, protocol(acc, x): yield acc + x, 0)14# 15Sequences are lazy iterators that produce values on-demand. They're memory-efficient for large datasets.
1# Basic sequence2sequence countdown(n):3 cycle while n > 0:4 yield n5 n = n - 16 yield "Liftoff!"78cycle through countdown(3) as val:9 declare(val)10# Output: 3, 2, 1, Liftoff!1112# Manual iteration with proceed()13gen := countdown(2)14declare(proceed(gen)) # 215declare(proceed(gen)) # 116declare(proceed(gen)) # Liftoff!1sequence inner():2 yield "A"3 yield "B"45sequence outer():6 yield "Start"7 delegate inner() # Yields A, B8 yield "End"910cycle through outer() as item:11 declare(item)12# Output: Start, A, B, End1# Generators can receive values2sequence accumulator():3 total := 04 cycle while true:5 received := receive()6 foresee received:7 total = total + received8 yield total910acc := accumulator()11proceed(acc) # Initialize12declare(transmit(acc, 10)) # 1013declare(transmit(acc, 5)) # 1514declare(transmit(acc, 25)) # 40Entities are Keikaku's object-oriented construct, supporting inheritance and encapsulation.
1# Define an entity2entity Player:3 protocol construct(name, health := 100):4 self._name := name5 self._health := health6 7 protocol take_damage(amount):8 self._health = self._health - amount9 foresee self._health <= 0:10 declare(self._name, "has been defeated!")11 otherwise:12 declare(self._name, "has", self._health, "HP left")13 14 protocol heal(amount):15 self._health = self._health + amount1617# Create instance with manifest18hero := manifest Player("Keikaku Master")19hero.take_damage(30) # Keikaku Master has 70 HP left20hero.heal(20)2122# Inheritance23entity Boss inherits Player:24 protocol construct(name, power):25 ascend construct(name, 500) # Call parent26 self._power := power27 28 protocol special_attack(target):29 target.take_damage(self._power)3031boss := manifest Boss("Dark Lord", 50)32boss.special_attack(hero) # Keikaku Master has 20 HP leftUse attempt/recover blocks to gracefully handle runtime errors.
1# Basic error handling2attempt:3 declare("Attempting risky operation...")4 result := 10 / 0 # This will error5 declare("This won't print")6recover as error:7 declare("Caught error:", error)89declare("Program continues normally")1011# Nested error handling12protocol safe_divide(a, b):13 attempt:14 foresee b == 0:15 yield "Cannot divide by zero"16 otherwise:17 yield a / b18 recover as e:19 yield "Error: " + text(e)2021declare(safe_divide(10, 2)) # 522declare(safe_divide(10, 0)) # Cannot divide by zeroKeikaku supports non-blocking operations with async protocol, await, and promises.
1# Async protocol2async protocol fetch_data(id):3 sleep(100) # Simulate network delay4 yield "Data for ID: " + text(id)56# Using async protocols7gen := fetch_data(42)8result := proceed(gen)9declare(result) # Data for ID: 421011# Promises12promise := resolve("completed")13value := await promise14declare(value) # completed1516# Delayed execution17protocol notify(msg):18 declare("Notification:", msg)1920defer(500, notify, "Task complete")21# Prints after 500ms delaydeclare(...) - Print outputinquire(prompt) - Read inputinscribe(file, content) - Write filedecipher(file) - Read filetext(x) - Convert to stringnumber(x) - Convert to integerdecimal(x) - Convert to floatclassify(x) - Get type namemeasure(x) - Length of list/stringpush(list, item) - Append to listreverse(list) - Reverse listspan(n) - Range [0..n-1]abs(x) - Absolute valuesqrt(x) - Square rootmin(a, b) - Minimummax(a, b) - Maximumrandom(min, max) - Random integeruppercase(s) - Convert to upperlowercase(s) - Convert to lowersplit(s, delim) - Split stringjoin(list, delim) - Join listcontains(s, substr) - Check substringproceed(gen) - Get next valuetransmit(gen, val) - Send valuereceive() - Receive sent valuedisrupt(gen, err) - Throw into generator