Complete Language Tour

Master Keikaku from fundamentals to advanced features. This tutorial covers everything you need to write production-ready code.

1. Variables & Types

Keikaku uses := for declaration and = for reassignment. All variables are dynamically typed.

KEIKAKU Protocol
1# Variable declaration with :=
2name := "Keikaku"
3version := 1.0
4is_active := true
5count := 42
6
7# Reassignment with =
8count = count + 1
9
10# Lists (arrays)
11numbers := [1, 2, 3, 4, 5]
12mixed := ["text", 42, true]
13
14# Dictionaries
15config := {
16 "host": "localhost",
17 "port": 8080
18}
19
20# Accessing elements
21first := numbers[0] # 1
22host := config["host"] # "localhost"

Type Conversion

KEIKAKU Protocol
1# Convert to string
2text("42") # "42"
3text(3.14) # "3.14"
4
5# Convert to number
6number("42") # 42
7decimal("3.14") # 3.14
8
9# Check type
10classify(42) # "int"
11classify("hi") # "string"
12classify([1,2]) # "list"

2. Control Flow

Conditionals (foresee)

Use foresee for if-statements, alternate for else-if, and otherwise for else.

KEIKAKU Protocol
1score := 85
2
3foresee 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")
11
12# Output: Grade: B

Loops (cycle)

KEIKAKU Protocol
1# Range loop (exclusive end)
2cycle from 1 to 5 as i:
3 declare(i)
4# Output: 1, 2, 3, 4
5
6# List iteration
7items := ["apple", "banana", "cherry"]
8cycle through items as fruit:
9 declare("I like", fruit)
10
11# While loop
12count := 0
13cycle while count < 3:
14 declare("Count:", count)
15 count = count + 1
16
17# Infinite loop with break
18cycle:
19 val := inquire("Enter 'quit' to exit: ")
20 foresee val == "quit":
21 terminate

3. Protocols (Functions)

Functions in Keikaku are called protocols. They encapsulate reusable logic.

KEIKAKU Protocol
1# Basic protocol
2protocol greet(name):
3 declare("Hello,", name, "!")
4
5greet("World") # Output: Hello, World !
6
7# Protocol with return value
8protocol add(a, b):
9 yield a + b
10
11result := add(5, 3)
12declare(result) # Output: 8
13
14# Protocol with default parameters
15protocol power(base, exp := 2):
16 result := 1
17 cycle from 0 to exp as _:
18 result = result * base
19 yield result
20
21declare(power(3)) # 9 (3^2)
22declare(power(2, 8)) # 256 (2^8)

Lambda Expressions

KEIKAKU Protocol
1# Anonymous protocols for higher-order functions
2numbers := [1, 2, 3, 4, 5]
3
4# transform = map
5doubled := transform(numbers, protocol(x): yield x * 2)
6# [2, 4, 6, 8, 10]
7
8# select = filter
9evens := select(numbers, protocol(x): yield x % 2 == 0)
10# [2, 4]
11
12# fold = reduce
13sum := fold(numbers, protocol(acc, x): yield acc + x, 0)
14# 15

4. Sequences (Generators)

Sequences are lazy iterators that produce values on-demand. They're memory-efficient for large datasets.

KEIKAKU Protocol
1# Basic sequence
2sequence countdown(n):
3 cycle while n > 0:
4 yield n
5 n = n - 1
6 yield "Liftoff!"
7
8cycle through countdown(3) as val:
9 declare(val)
10# Output: 3, 2, 1, Liftoff!
11
12# Manual iteration with proceed()
13gen := countdown(2)
14declare(proceed(gen)) # 2
15declare(proceed(gen)) # 1
16declare(proceed(gen)) # Liftoff!

Delegation (yield from)

KEIKAKU Protocol
1sequence inner():
2 yield "A"
3 yield "B"
4
5sequence outer():
6 yield "Start"
7 delegate inner() # Yields A, B
8 yield "End"
9
10cycle through outer() as item:
11 declare(item)
12# Output: Start, A, B, End

Bidirectional Communication

KEIKAKU Protocol
1# Generators can receive values
2sequence accumulator():
3 total := 0
4 cycle while true:
5 received := receive()
6 foresee received:
7 total = total + received
8 yield total
9
10acc := accumulator()
11proceed(acc) # Initialize
12declare(transmit(acc, 10)) # 10
13declare(transmit(acc, 5)) # 15
14declare(transmit(acc, 25)) # 40

5. Entities (Classes)

Entities are Keikaku's object-oriented construct, supporting inheritance and encapsulation.

KEIKAKU Protocol
1# Define an entity
2entity Player:
3 protocol construct(name, health := 100):
4 self._name := name
5 self._health := health
6
7 protocol take_damage(amount):
8 self._health = self._health - amount
9 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 + amount
16
17# Create instance with manifest
18hero := manifest Player("Keikaku Master")
19hero.take_damage(30) # Keikaku Master has 70 HP left
20hero.heal(20)
21
22# Inheritance
23entity Boss inherits Player:
24 protocol construct(name, power):
25 ascend construct(name, 500) # Call parent
26 self._power := power
27
28 protocol special_attack(target):
29 target.take_damage(self._power)
30
31boss := manifest Boss("Dark Lord", 50)
32boss.special_attack(hero) # Keikaku Master has 20 HP left

6. Error Handling

Use attempt/recover blocks to gracefully handle runtime errors.

KEIKAKU Protocol
1# Basic error handling
2attempt:
3 declare("Attempting risky operation...")
4 result := 10 / 0 # This will error
5 declare("This won't print")
6recover as error:
7 declare("Caught error:", error)
8
9declare("Program continues normally")
10
11# Nested error handling
12protocol safe_divide(a, b):
13 attempt:
14 foresee b == 0:
15 yield "Cannot divide by zero"
16 otherwise:
17 yield a / b
18 recover as e:
19 yield "Error: " + text(e)
20
21declare(safe_divide(10, 2)) # 5
22declare(safe_divide(10, 0)) # Cannot divide by zero

7. Async Programming

Keikaku supports non-blocking operations with async protocol, await, and promises.

KEIKAKU Protocol
1# Async protocol
2async protocol fetch_data(id):
3 sleep(100) # Simulate network delay
4 yield "Data for ID: " + text(id)
5
6# Using async protocols
7gen := fetch_data(42)
8result := proceed(gen)
9declare(result) # Data for ID: 42
10
11# Promises
12promise := resolve("completed")
13value := await promise
14declare(value) # completed
15
16# Delayed execution
17protocol notify(msg):
18 declare("Notification:", msg)
19
20defer(500, notify, "Task complete")
21# Prints after 500ms delay

8. Built-in Functions Reference

I/O

  • declare(...) - Print output
  • inquire(prompt) - Read input
  • inscribe(file, content) - Write file
  • decipher(file) - Read file

Type Conversion

  • text(x) - Convert to string
  • number(x) - Convert to integer
  • decimal(x) - Convert to float
  • classify(x) - Get type name

Collections

  • measure(x) - Length of list/string
  • push(list, item) - Append to list
  • reverse(list) - Reverse list
  • span(n) - Range [0..n-1]

Math

  • abs(x) - Absolute value
  • sqrt(x) - Square root
  • min(a, b) - Minimum
  • max(a, b) - Maximum
  • random(min, max) - Random integer

Strings

  • uppercase(s) - Convert to upper
  • lowercase(s) - Convert to lower
  • split(s, delim) - Split string
  • join(list, delim) - Join list
  • contains(s, substr) - Check substring

Generators

  • proceed(gen) - Get next value
  • transmit(gen, val) - Send value
  • receive() - Receive sent value
  • disrupt(gen, err) - Throw into generator