← Home

The Nari Programming Language Guide

Contents

About the Language

Nari is a language designed to be used from the Android chat application Telegram. It is a bot interpreter that receives commands you send it in the form of Nari commands and replies with the output of the executed program. You can find it under the @martubot username.

As an example, a valid Nari code would be Nari 1 2 + print, that will output 3 as the result of doing 1+2 and printing it.

The Stack

The use of Nari revolves heavily around the stack. The stack is a pile of values (either float point numbers and alphanumeric characters) to and from which you can push and pop values, respectively. Any numbers and any strings (between double quotation marks: "this is a string!") that you write in a Nari sequence is pushed to the stack. So, for example, if you execute the sequence nari 1 2 "hello" 455, the stack will end up looking like this:

(Top of the stack)
455
"hello"
2
1
(Bottom of the stack)

You can output the contents of the stack at any time using the command stack. For example, if you write the nari program nari 1 2 "hello" 455 stack, Nari will output:

Stack:
[1.0, 2.0, hello, 455.0]

Please note that the stack is reset for each program you execute, so if you run Nari 1 2 3 4 and then you run Nari stack, an empty stack will be printed instead.

Some Commands

With what we've learned up to this point you can make use of the following commands to write some simple programs. Bear in mind that, unless it says so, every command that takes an element from the stack to operate removes such element from the stack. Be sure to copy an element if you want to use it twice.

Conditionals

A conditional is a part of the code that only executes if a previous condition has been satisfied first. In Nari, conditionals start with the command if and end with the command endif, but then, fi and larry can be used to close the conditional as well.

Conditionals only execute if, when the code reaches the if command there is a non 0 element at the top of the stack (for example, a 1). If it happens to find a 0, the code will skip the if section completely and jump the corresponding end tag. Let's see some examples:

Nari 4 5 < if "4 is lower than 5!" print endif will output "4 is lower than 5!".

Nari 4 5 > if "4 is lower than 5!" print endif won't output anything.

Nari 4 5 > not if "4 is lower than 5!" print endif will output "4 is lower than 5!".

Nari 1 if "Nari is great!" print endif will output an obvious statement.

There is no else clause in Nari. This, though, can be constructed using the following excerpt of code:

Nari 4 5 <
copy if "4 is lower than 5!" print endif
not if "4 is greater than 5!" print endif

Loops

A loop is a part of the code that executes again and again while a condition is met. In Nari, you can open a loop with the do statement, and close a loop with either the loop or the loopdec statement.

When the execution reaches a loop statement, if will take the topmost element of the stack and, if it is not a 0, the execution will jump to the last do command and execute from there.
For example, Nari 0 do 1 + copy 10 != "Hello!" print loop will print "Hello!" ten times before the != condition is not met and loop finds a 0 on top of the stack.

On the other hand, the do ... loopdec (loop and decrease) cycle works like a normal for cycle. When the execution reaches the loopdec statement, it will loop if on top of the stack it finds anything else than 0; but also, loopdec will try to substract 1 from such element. So, e.g., if you run the code 10 do "Hello!" print loopdec, it will print "Hello!" ten times.

Variables

Having to work with all your values in the stack is less than nice. So Nari has a way of storing them into variables, with the commands toaux:variableName and aux:variableName. Basically toaux saves the topmost element of the stack in a variable of the name you chose -removing it from the stack, of course-, and aux pushes that value again to the stack. Note that pushing the value of a variable to the stack does not delete it from the variable and, thus, you can push it to the stack all the times you want. Also note that, like the stack, all variables are reset from one execution to the next, so if you store something in a variable, it won't be there when you execute another Nari sentence. Examples:

Nari 25 toaux:mynumber won't output anything but saves 25 to the variable "mynumber".

Nari 5 toaux:five aux:5 aux:5 * print will output "25".

Nari "Nari is great!" toaux:ofCourse aux:ofCourse print will again output a brillant quote.

There are also four DEPRECATED registers that you can use to store information (numbered from 1 to 4) that are accessed with the commands toaux1, aux1, toaux2, aux2, toaux3, aux3, toaux4 and aux4.

Comments

Comments in Nari are written between $. The Nari Interpreter won't try to execute any comments, so feel free to write whatever you want. For example:

Nari $Hey! This is a coment!$ 1 2 + print will just output 3.

Nari $This is a comment$ "$This is not$" print will output "$This is not$".


The Nari Programming Language
Martín del Río, 2016 - 2017
Home