A quick overview of Bamya

Introduction

Bamya is a lightweight interpreter I built after reading Writing An Interpreter in Go to strengthen my fundamentals.

Table of Contents

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

Operator List

OperatorTypeDescription
+InfixAddition
-Infix and PrefixSubtraction
/InfixDivision
*InfixMultiplication
**InfixExponentiation
==InfixEquals
<InfixLess Than
<=InfixLess Than Or Equal
>InfixGreater Than
>=InfixGreater Than Or Equal
%InfixModulo
!PrefixTruthy or falsy

Data Types and Constructs

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 do the integers. No floats tho, we don't do 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.

> 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's 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