Var

The Var: module covers primitive types, variable declaration, literals, string interpolation, multi-line strings, comments, the script entry point, and struct (UDT) definitions.


Types

Three primitive types, all stored in a Value variant:

Type Declaration keyword Default
Number Var:Number 0
String Var:String ""
Boolean Var:Boolean false

Variable declaration

Var:Number x;
Var:Number y = 42;
Var:String name = "hello";
Var:Boolean flag = true;
Var:Number a = 1, b = 2, c = 3;

Variables can be assigned without re-declaring:

x = x + 1;
name = "world";

Literals

42            // integer
3.14          // float
0xFF          // hex (stored as Number)
"hello\n"     // string — supports \n \t \" \\
"Hi ${name}!" // interpolated string — ${expr} is evaluated at runtime
true false    // booleans

String interpolation

Embed any expression inside ${ } within a double-quoted string:

Var:String name = "world";
Konsol:Print("Hello ${name}!");         // Hello world!

Var:Number a = 3;
Var:Number b = 4;
Konsol:Print("${a} + ${b} = ${a + b}"); // 3 + 4 = 7

Var:String msg = "Result: ${a * b}";
Konsol:Print(msg);                      // Result: 12

Full expressions inside ${}

${} accepts any expression the runtime can evaluate — not just variable names. Arithmetic, ternary, function calls, and even nested interpolation all work.

Konsol:Print("area     = ${width * height}");
Konsol:Print("midpoint = ${(a + b) / 2}");
Konsol:Print("sign     = ${x > 0 ? "pos" : x < 0 ? "neg" : "zero"}");
Konsol:Print("doubled  = ${timesTwo(n)}");

Multi-line strings (triple-quote)

Use """...""" for strings that span multiple lines or contain double-quote characters:

Var:String msg = """Hello,
World!
This is line three.""";
Konsol:Print(msg);

// Double-quotes inside are fine
Var:String code = """Konsol:Print("hi");""";

// ${} interpolation works inside triple-quoted strings too
Var:String name = "Alice";
Var:String greeting = """Dear ${name},
  Welcome!""";
Konsol:Print(greeting);

When the expression inside ${} gets complex, factor it into a variable first:

// discouraged — legal but harder to debug
Konsol:Print("${x > 0 ? "pos" : x < 0 ? "neg" : "zero"}");

// preferred
Var:String sign = x > 0 ? "pos" : x < 0 ? "neg" : "zero";
Konsol:Print("sign: ${sign}");

Comments

// single-line comment

/* multi-line
   comment */

Preprocessor#define and #include (file and plugin)

Structs (UDTs)

Simple data-only record types. No methods.

Var:Create(Point, Number x, Number y);

Var:Point p;
p.x = 10;
p.y = 20;
Konsol:Print(p.x + p.y);

Var:Create is processed at symbol-build time. Multiple fields are separated by commas:

Var:Create(Player, String name, Number hp, Boolean alive);

Var:Player hero;
hero.name  = "Link";
hero.hp    = 100;
hero.alive = true;
Konsol:Print(hero.name + " has " + hero.hp + " HP");

Fields can also be List:Type or Map — they are initialised to empty collections when the instance is declared:

Var:Create(Cart, String owner, List:String items, Map metadata);

Var:Cart order;
order.owner = "Alice";

// Assign a pre-populated list to the field
List:String picked;
List:Push("apple", picked);
List:Push("banana", picked);
order.items = picked;

Examples

Variables and types

// 01_vars.ks — Variables, types, literals, #define

#define GREETING "Hello, minks!"
#define MAX_SCORE 100

Konsol:Print("=== Variables & Types ===");

// Number
Var:Number x = 42;
Var:Number pi = 3.14159;
Var:Number hex = 0xFF;
Konsol:Print("x        = ${x}");
Konsol:Print("pi       = ${pi}");
Konsol:Print("0xFF     = ${hex}");  // prints 255

// String
Var:String name = "KonsolScript";
Var:String tab = "col1\t\tcol2";
Konsol:Print("name     = ${name}");
Konsol:Print("tab      = ${tab}");

// Boolean
Var:Boolean flag = true;
Var:Boolean off  = false;
Konsol:Print("flag     = ${flag}");
Konsol:Print("off      = ${off}");

// Multiple declarations on one line
Var:Number a = 1, b = 2, c = 3;
Konsol:Print("a b c    = ${a} ${b} ${c}");

// #define substitution
Konsol:Print(GREETING);
Konsol:Print("max score = ${MAX_SCORE}");

// Reassignment
x = 99;
name = "minks";
flag = false;
Konsol:Print("x after  = ${x}");
Konsol:Print("name now = ${name}");
Konsol:Print("flag now = ${flag}");

Structs

// 06_structs.ks — Var:Create (data-only record types)

Konsol:Print("=== Structs (UDTs) ===");

// Define a Point struct
Var:Create(Point, Number x, Number y);

// Define a Rectangle
Var:Create(Rect, Number left, Number top, Number right, Number bottom);

// Define a Person record
Var:Create(Person, String firstName, String lastName, Number age);

// ── Point usage ───────────────────────────────────────────────────────────────
Var:Point origin;
origin.x = 0;
origin.y = 0;
Konsol:Print("origin = ${origin.x} ${origin.y}");

Var:Point p;
p.x = 3;
p.y = 4;
Konsol:Print("p = ${p.x} ${p.y}");

// Distance from origin (manual sqrt via Math)
Var:Number _ret = 0;
Math:Sqrt(p.x * p.x + p.y * p.y, _ret);
Konsol:Print("distance from origin = ${_ret}");

// ── Rect usage ────────────────────────────────────────────────────────────────
Var:Rect box;
box.left   = 10;
box.top    = 20;
box.right  = 50;
box.bottom = 80;

Var:Number width  = box.right - box.left;
Var:Number height = box.bottom - box.top;
Konsol:Print("box width  = ${width}");
Konsol:Print("box height = ${height}");
Konsol:Print("box area   = ${width * height}");

// ── Person usage ──────────────────────────────────────────────────────────────
Var:Person alice;
alice.firstName = "Alice";
alice.lastName  = "Smith";
alice.age       = 30;

Var:Person bob;
bob.firstName = "Bob";
bob.lastName  = "Jones";
bob.age       = 25;

Konsol:Print("${alice.firstName} ${alice.lastName} age ${alice.age}");
Konsol:Print("${bob.firstName} ${bob.lastName} age ${bob.age}");

// Compare ages
if (alice.age > bob.age) {
    Konsol:Print("${alice.firstName} is older");
} else {
    Konsol:Print("${bob.firstName} is older");
}

String interpolation

// 29_interp.ks — string interpolation  "${expr}"

// ── Basic variable ────────────────────────────────────────────────────────────
Var:String name = "world";
Konsol:Print("Hello ${name}!");          // Hello world!

// ── Number variable ───────────────────────────────────────────────────────────
Var:Number x = 42;
Konsol:Print("x = ${x}");               // x = 42

// ── Arithmetic expression ─────────────────────────────────────────────────────
Var:Number a = 3;
Var:Number b = 4;
Konsol:Print("${a} + ${b} = ${a + b}"); // 3 + 4 = 7

// ── Multiple interpolations in one string ─────────────────────────────────────
Var:String first = "John";
Var:String last  = "Doe";
Konsol:Print("${first} ${last}");       // John Doe

// ── Expression with function call ─────────────────────────────────────────────
Var:Number n = 16;
Var:Number _ret = 0;
Math:Sqrt(n, _ret);
Var:Number sq = _ret;
Konsol:Print("sqrt(${n}) = ${sq}");     // sqrt(16) = 4

// ── Nested string concat inside interp ───────────────────────────────────────
Var:String greeting = "Hi";
Konsol:Print("${greeting + "!"} How are you?");   // Hi! How are you?

// ── Plain string not affected ─────────────────────────────────────────────────
Konsol:Print("no interp here");         // no interp here
Konsol:Print("price: $5.00");           // price: $5.00  ($ without { is literal)

// ── Interpolation in assignment ───────────────────────────────────────────────
Var:String msg = "Result: ${a * b}";
Konsol:Print(msg);                      // Result: 12

// ── Interpolation with boolean ────────────────────────────────────────────────
Var:Boolean flag = true;
Konsol:Print("flag is ${flag}");        // flag is true

// ── Chained ops ───────────────────────────────────────────────────────────────
Var:Number i = 1;
Var:Number j = 2;
Var:Number k = 3;
Konsol:Print("${i} ${j} ${k} sum=${i+j+k}");  // 1 2 3 sum=6

Konsol:Print("done");

getType on all variable kinds

// 33_gettype.ks — getType() on all variable kinds

// ── Primitives ────────────────────────────────────────────────────────────────
Var:Number  n = 42;
Var:String  s = "hello";
Var:Boolean b = true;

Var:String t;
t = n.getType();
Konsol:Print(t);        // Number

t = s.getType();
Konsol:Print(t);        // String

t = b.getType();
Konsol:Print(t);        // Boolean

// ── getType() used directly in expression ─────────────────────────────────────
Konsol:Print(n.getType());   // Number
Konsol:Print(s.getType());   // String

// ── Collections ───────────────────────────────────────────────────────────────
Array:Number arr;
Konsol:Print(arr.getType()); // Array:Number

List:String lst;
Konsol:Print(lst.getType()); // List:String

Map:New m;
Konsol:Print(m.getType());   // Map

// ── Struct (UDT) instance ─────────────────────────────────────────────────────
Var:Create(Point, Number x, Number y);
Var:Point p;
Konsol:Print(p.getType());   // Point

// ── Class instance ────────────────────────────────────────────────────────────
Class:Create(Dog)
{
    Var:String name;
}

Var:Dog d;
Konsol:Print(d.getType());   // Dog

// ── getType() on class instance via Object::getType ───────────────────────────
t = d.getType();
Konsol:Print(t);             // Dog

Konsol:Print("done");

Multi-line strings

// 43_multiline_string.ks — triple-quote multi-line strings

// Basic multi-line
Var:String msg = """Line one
Line two
Line three""";
Konsol:Print(msg);

// Contains double-quotes inside
Var:String code = """Konsol:Print("hello");""";
Konsol:Print(code);

// With ${} interpolation
Var:String lang = "KonsolScript";
Var:Number ver  = 1;
Var:String banner = """=== ${lang} v${ver} ===
Welcome!""";
Konsol:Print(banner);

// Concatenation with regular string
Var:String full = """multi
line""" + " suffix";
Konsol:Print(full);

// Empty triple-quote
Var:String empty = """""";
Konsol:Print(empty);          // (empty)

Konsol:Print("done");