We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Khalzanka

From Esolang
Jump to navigation Jump to search

Khalzanka is a programming language designed by PSTF.

It's a strong and Turing-complete programming language that operates on both variables and a stack.

Overview

Each Khalzanka program is composed of the following three sections:

  1. Initialization. In this section, some variables will be initialized and one or more stacks will be set.
  2. Execution. The real code is in this section.
  3. Finalization. Recycle some used variables, then clear the stack.

This is the format of the program.

[[[
Document
]]]

|~~~~~PREDFUN
Functions here

|~~~~~INIT
===DEF
stack: n
variable: 
    a(?): xxx
    b(?): xxx
    ...
===IPT
Put anything here
|~~~~~EXEC
Put anything here
|~~~~~FINI
===RES
Put anything here
===CYC
recycle: blah blah blah blah 

Initialization

In the above program template, n represents the number of stacks. To reference a stack, you will need to use the 'snail shell' symbol (@) followed by the stack number. Attempting to reference a non-existent stack will cause an error.

The initialization format of the variable is as follows.

name(type): value

Next is the IPT module, which is used to set some user-dependent data. Variables newly defined in the EXEC phase will be destroyed when the program enters the FINI phase.

Execution

Command Table

Stack Operation

push x, @l

Push x onto the l-th stack.

pop @l, x

Pop the top element of the l-th stack, and then assign it to x. Leaving x empty means it will be destroyed directly.

transfer @m, @n

Pop the top element of the m-th stack and then push it onto the n-th stack.

clear @l

Clear the l-th stack.

I/O

print x

Print x to screen.

input x

Get x from keyboard by raw input.

read x

Get x from keyboard and evaluate it. Among the above three commands, if x is a stack, then output or input to its top element.

Variable Operation

let x, y

Assign x (temporary variable) the value of y.

make x, y

Assign x (predefined variable) the value of y.

index x, y

If x is a list, return y-th value of x. If x is a stack, peek down y slots from the stack top and return it.

x+y

This language supports operators, and binary operators are generally infix operators. Currently, the operators supported by this language are:

  • Binocular
    • Addition
    • Subtraction
    • Multiplication
    • Division
    • Integer Division
    • Modulo
    • Exponent
    • Bitwise AND
    • Bitwise OR
    • Bitwise XOR
    • Logical AND
    • Logical OR
    • Logical XOR
  • Monocular
    • Bitwise NOT
    • Logical NOT
    • Negation
  • Triocular
    • Conditional evaluation. The format is b?a:c. If b is true, return a; otherwise, return c.
  • Relation
    • Equal
    • Not equal
    • Above
    • Below
    • Over(≮, >=)
    • Under(≯, <=)
intersect x, y

Both parameters must be lists. Take the contents that are duplicated in the two lists, that is, the intersection of the two lists. Note: A ∩ ∅ = ∅, so as long as any parameter is an empty list, the result is an empty list.

union x, y

Both parameters must be lists. Return the result of concatenating the two lists and removing duplicates, that is, the union of the two lists. Note: A∪∅=A, so if one of the parameters is an empty list, return the other parameter.

range x, y, d, m

Return an arithmetic sequence starting from x, ending at y, with a step of d. Here, m represents the mode and must be one of 0 (closed interval), 1 (open-closed interval), 2 (closed-open interval), or 3 (open interval).

randint x, y

x and y must be integers. Return a random integer between x and y.

choice x

x must be a list. Returns a random element from x.

shuffle x

x must be a list. Return after shuffling x.

exist x, y

For x, if y ∈ x, return true; otherwise, return false.

sub x, y

For x, if y ⊆ x (all elements in y appear in x, but not all elements in x necessarily appear in y, and y is allowed to be exactly equal to x), return true; otherwise, return false.

swap x, y

Swap the values of two variables. If y is left blank and x is a number, take the reciprocal of x. Taking the reciprocal of 0 will return NaN.

Control Flow

for i x do blahblahblah end

For i in x, execute the contents within do and end.

if x do blahblahblah else do blahblahblah end

If-else conditional branch, where the 'else' code block can be omitted. If x is true, execute the first code block; otherwise, execute the second code block (if not specified, do nothing).

while x do blahblahblah end

Repeat the code block until x is not true.

until x do blahblahblah end

Equals to while !x do blahblahblah end.

do blahblahblah end

A code block.

call x [a1, a2, ...]

Call function x. The parameter field can be omitted (for functions without parameters, or subroutines).

pass

APLWSI interpreter.

pause x

Pause for x milliseconds. If x is left empty or is a negative number, output Press any key to continue... and then wait for the user to press any key.

halt

Exit program.

# I'm a comment

Single line comment.

#*
I'm also a comment
*#

Multiline comment.

Constant

true|false

Boolean.

null

Null value.

infinity

Infinity.

nan

Not a number.

Finalizataion

In the finalization stage, you can specify which preset variables need to be reclaimed. Leaving it blank or using an asterisk indicates reclaiming all preset variables. Before this, you may need to output some results.

Predefined Function

Predefined functions can be used in a program and will automatically be destroyed during the reclamation phase of the finalization stage. Its definition is like this:

function_name -> type (*args, **kwargs) do
    do something
end

Document

You can specify a document at the beginning of the program. The document is enclosed in triple square brackets. Before the document, you can specify the document format — text for plain text, wiki for Wikitext, md for Markdown, and rtf for rich text. After specifying the format, during the program compilation process, the document will be extracted separately and saved in txt, md, wikitext, or rtf format according to the format.

Example Programs

Add by yourself.

Categories