Chomik: Adding and Subtracting Integers


Today, let’s talk about adding and subtracting integers in Chomik — but before we dive into code, I’d like to introduce a certain feature of the language. Unfortunately it may seem “nasty”.
The Nasty Feature: Side Effects
Most of us don’t like side effects in code.
Why?
They change state outside of simply returning a value.
If something else changes that state (perhaps inside a function you call), your assumptions break.
They are not thread-safe.
Unfortunately, here’s the bad news: Chomik is built upon side effects.
Some operations require them, and integer arithmetic is a prime example.
Adding integers
Consider this example:
let x = value integer 10;
<add "integer" <x> 1>;
let x = <the add result "integer">;
<print <x>>;
Let’s unpack it:
let x = value integer 10;
Creates a variablex
holding the integer10
.<add "integer" <x> 1>;
Executes a built-in code variable whose name begins withadd "integer"
, followed by two integers: the value of thex
variable and1
.
This adds the two integers and stores the result in the predefined integer variablethe add result "integer"
.let x = <the add result "integer">;
If we want to store the sum inx
, we must explicitly assign it.<print <x>>;
Prints the final value.
Subtracting integers
Subtracting works in exactly the same way:
let x = value integer 10;
<subtract "integer" <x> 5>;
let x = <the subtract result "integer">;
<print <x>>;
Here we use the built-in family of code variables starting with subtract "integer"
.
The result is stored in the predefined integer variable the subtract result "integer"
.
This approach might feel cumbersome to those who are used to operators like x++
or x -= 5
.
I admit — Chomik is not highly expressive.
But it is extremely regular: there are almost no special cases in the language.
Summary:
Integer addition and subtraction in Chomik use built-in families of code variables (
add "integer"
,subtract "integer"
) that operate via side effects.Results are stored in predefined variables (
the add result "integer"
,the subtract result "integer"
) which you then assign where needed.This design may seem verbose, but it’s consistent.
Subscribe to my newsletter
Read articles from Pawel Biernacki directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Pawel Biernacki
Pawel Biernacki
I was born in 1973, in Cracow, Poland. I am a software engineer.