Files
kyn/docs/syntax.md

3.7 KiB

Kyn Syntax

This document details syntax of provided Kyn modules.

Call a module by writing it's name, then the arguments for the module.

Substitute the output of a module into another module's arguments by enclosing the call in (parenthases).

Access a variable by prefixing it's name with a $ dollar sign.

Data Types

Kyn supports several data types: strings, integers, doubles (floating-point numbers), and lists.

Strings

Strings are enclosed in double quotes. let myString = "Hello, World!"

Numbers

Numbers can be integers or doubles. let myInt = 42 let myDouble = 3.14

Lists

Lists are ordered collections of values, enclosed in square brackets. let myList = [1, "two", 3.0, ["nested", "list"]]

Input/Output

print

Prints all the arguments provided to the console.

Example: print "Hello!"

println

Prints all the arguments provided to theconsole, appending a newline after.

Example: println "Hello!"

input

Gets a line of input from the console.

Example: input (this does nothing but prompt the user)

Another example: let userInput = (input)

Variables and Data

let

Defines a variable to the value provided. If no value is provided, it defaults to an empty string.

Example: let myVar = "Hello!" Example: let anotherVar

concat

Concatenates multiple strings or numbers together.

Example: println (concat "There are " 3 " apples.") // Prints "There are 3 apples."

Indexed Operations

You can access elements of lists and characters of strings by their index (starting from 0).

Get element

Syntax: (variable index)

let myList = ["a", "b", "c"]
println (myList 1) // Prints "b"

let myString = "hello"
println (myString 2) // Prints "l"

Set element

Syntax: variable index = newValue

let myList = ["a", "b", "c"]
myList 1 = "z"
println $myList // Prints ["a", "z", "c"]

let myString = "hello"
myString 0 = "j"
println $myString // Prints "jello"

size

You can get the number of elements in a list or characters in a string using size.

Syntax: (variable size)

let myList = [1, 2, 3, 4]
println (myList size) // Prints 4

let myString = "hello"
println (myString size) // Prints 5

Computations

math

Computes a mathematical expression. Supports addition (+), subtraction (-), multiplication (*), division (/), power to (^) and mod (%). Follows order of operations.

Example: math 4 * 3 + 12 / 4 - 5 ^ 3

compare

Compares two values. Supports equal (==) and inequal (!=) on all values. Supports greater than (>), greater than or equal to (>=), lesser than (<), and lesser than or equal to (<=) for numbers. Returns "1" for true and "0" for false.

Control Flow

exit

Exits the program with a return code. If no return code or an invalid return code is provided, exits with code 0. Otherwise, exits with given code.

if

Runs a block of code if a condition is true. An optional else block can be provided. The condition is considered true if it's not 0, "false", or an empty string/list.

Example:

if compare 1 == 1 {
    println "1 is the same as 1"
} else {
    println "1 is not the same as 1"
}

while

Runs a block of code repeatedly as long as a condition is true.

Example:

let number = 0

while compare $number < 10 {
    number = (math $number + 1)
    println $number
}

fun

Defines a function with a name, a list of arguments, and a body.

fun multiply a b {
    return (math $a * $b)
}

let product = (multiply 7 6)
println $product # Prints 42

return

Exits a function, optionally returning a value.

fun myFunc {
    println "doing stuff..."
    return "done"
    println "this will not print"
}

let result = (myFunc)
println $result # Prints "doing stuff..." and then "done"