Skip to main content

Command Palette

Search for a command to run...

Chomik: Adding and Subtracting Integers

Updated
2 min read
Chomik: Adding and Subtracting Integers
P

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

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?

  1. They change state outside of simply returning a value.

  2. If something else changes that state (perhaps inside a function you call), your assumptions break.

  3. 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 variable x holding the integer 10.

  • <add "integer" <x> 1>;
    Executes a built-in code variable whose name begins with add "integer", followed by two integers: the value of the x variable and 1.
    This adds the two integers and stores the result in the predefined integer variable the add result "integer".

  • let x = <the add result "integer">;
    If we want to store the sum in x, 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.


More from this blog

C

Chomik

18 posts

Welcome to the official publication for Chomik, a new open-source programming language. Here, you'll find everything from development updates and tutorials to community projects.