A quick overview of bamya

Introduction

Here's a more detailed introduction to Bamya. Bamya is a lightweight interpreter that offers a handful of handy features. I developed this interpreter by reading Writing An Interpreter In Go to advance my fundamentals. With Bamya, you can perform calculations, use functions, use if-else statements, and explore various other possibilities. To gain a deeper understanding of this interpreter, proceed to the next section :)

Operators

Bamya has two sorts of operators, prefix- and infix-operators.

Infix operators are placed between two expressions.

> 23 + 3 = 26

> 23 % 3 = 2

Prefix operators are placed before an expression.

> !true = false

> -23 = -23

Addition +

Infix-Operator

Subtraction -

Infix- and Prefix-Operator

Division /

Infix-Operator

Multiplication *

Infix-Operator

Exponentiation **

Infix-Operator

Equals ==

Infix-Operator

Less Than <

Infix-Operator

Less Than Or Equal <=

Infix-Operator

Greater Than >

Infix-Operator

Greater Than Or Equal >=

Infix-Operator

Modulo %

Infix-Operator

Truthy or falsy !

Prefix-Operator

Core Features

Bamya supports various types of constructs. Let's get started.

String

You might not believe it, but we do have strings.

> "foobar"

> foobar

Integer

We also to the integers. No floats tho, we don't to the floats.

> 1 + 1

> 2

Boolean

Booleans are a must. Some time in the future I want to implement a third bool called `maybe`.

> 0 <= 1

> true

Let Statements

Just as you know it from javascript, we can declare let variables and use them.
Use them carefully tho, we cannot redeclare the value.

> let foo = 23;

> foo

> 23

If-Else Statements

Support for if-else statements is also present. On the side note: There is no such thing as `elseif` in bamya.

> if (0 > 1) { "impossible" } else { "hello there!" }

> hello there!

Return Statements

Returns should be supported. Returning stuff is a matter of respect.

> return "hello"

> hello

Arrays

Arrays are beautiful, we do love our arrays. We support arrays, in any kind of form. We even do indexing!

> let arr = ["hi", 23, [1, [0, 3]], true]

> arr

> [hi, 23, [1, [0, 3]], true]

> arr[4 - 2]

> [1, [0, 3]]

Hashmaps

Hashmaps? We do that. Indexing? Of course, without it Bamya would be silly (not that it isn't already).

> let map = { "name": "furkan", "age": 23, "profession": "software engineer", "locatedInBerlin": true }

> map["locatedInBerlin"]

> true

Functions

Now the most fun part, we have functions. You can do some magical things with them!

You can initialize functions with `fn( ) {}`.

In the examples below we did some recursion, showed off closures and string concatenation, amazing right.

> let fibonacci = fn(x) { if (x <= 1) { return 1 } else { return fibonacci(x - 2) + fibonacci(x - 1) } }

> fibonacci(8)

> 34

> let outerFn = fn(x) { return fn(y) { return x + y } }

> outerFn(5)(15)

> 15

> let greeter = fn(name) { return "Hello there, " + name + "!" }

> greeter("visitor")

> Hello there, visitor!

Built in Functions

Now let's get to the built in functions that Bamya supports. There' only a handful, we're almost done.

len

Get the length of strings and arrays.

> len("foobar")

> 6

> len([0, 1])

> 2

first

Get the first element in an array.

> first([0, 1, 2, 3])

> 0

last

Get the last element in an array.

> last([0, 1, 2, 3])

> 3

rest

Get all elements except the first one in an array.

> rest([0, 1, 2, 3, 4])

> [1, 2, 3, 4]

push

Push an element to your liking into an array.

> push([0, 1, 2, 3], 4)

> [0, 1, 2, 3, 4]

log

Log something in the console and don't forget to look at the console!

> log("Hello console!")

> null

Back to homepage