Skip to content

airlang/hava

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hava Programming Language

Hava is a small programming language project written in Python.

I first started working on Hava when I was 12. The first version was a simple interpreter, and over time I kept improving it as I learned more about how programming languages work behind the scenes.

Later, I changed the execution model and added a bytecode compiler with a small stack-based virtual machine. Some features from the legacy interpreter are not fully ported to the VM yet.

The language supports both Turkish-like and English keywords because I wanted to experiment with how programming languages feel with different keyword styles.

Why I Built This

My main goal with this project is learning and having some fun. The first version was very messy, but it helped me understand how interpreters actually work.

I wanted to understand:

  • how a parser understands syntax
  • how an AST can represent code
  • how an interpreter executes code
  • how bytecode and a virtual machine can be used instead of directly walking the AST

This is not meant to be a production language. It is a personal language implementation project.

How It Works

Hava's workflow looks like this:

Source Code -> Lexer -> Parser -> AST -> Bytecode Compiler -> Virtual Machine -> Output

Main Parts

Lexer

The lexer reads the source code and turns it into tokens.

For example, keywords, variable names, numbers, strings and operators are detected here.

Parser

The parser takes tokens and builds an AST based on the grammar rules of the language.

Bytecode Compiler

The compiler takes the AST and converts it into simple bytecode instructions.

For example:

x = 5:
yaz(x + 3):

can turn into something like:

LOAD_CONST 5
STORE_NAME x
LOAD_NAME x
LOAD_CONST 3
ADD
CALL ('yaz', 1)
POP

Virtual Machine

The virtual machine runs the generated bytecode instructions using a stack.

Features

  • experimental Turkish-like and English keyword syntax
  • variables and reassignment
  • integers and floats
  • strings
  • arrays
  • dictionaries
  • arithmetic operations
  • comparison operations
  • if statements
  • function definitions and calls
  • return values
  • native built-in functions
  • index access and index assignment
  • bytecode generation
  • stack-based virtual machine

Example

Basic example:

x = 12:

eğer x == 12 ::
    yaz("hava"):
:

Output:

hava

Function example:

fonksiyon topla(a, b) ::
    döndür a + b:
:

yaz(topla(5, 7)):

Output:

12

Array example:

array = ["h", "e", "l", "l", "o"]:

yaz(array):

array[0] = "y":
ekle(array, "w"):

yaz(array):
yaz(uzunluk(array)):

Output:

['h', 'e', 'l', 'l', 'o']
['y', 'e', 'l', 'l', 'o', 'w']
6

Dictionary example:

user = {
    "address" => {
        "city" => "Ankara"
    }
}:

yaz(user["address"]["city"]):
user["address"]["city"] = "İstanbul":
yaz(user["address"]["city"]):

Output:

Ankara
İstanbul

Running

Run the interactive console with:

python main.py

Then write Hava code in the console.

The interactive console is still simple. Single-line expressions and statements can be written directly in the console. Multi-line examples are usually better written in a .hava file. Multi-line REPL support is still experimental.

Running a file

You can run a Hava file with:

python main.py --file examples/math.hava

Example file:

## Hava can do maths! ##

number_1 = 50:
number_2 = 80:

yaz(number_1 + number_2):
yaz(number_1 * number_2):
yaz(number_1 - number_2):
yaz(number_1 / number_2):
yaz((number_1 + number_2) * 2):

Expected output:

130
4000
-30
0.625
260

Printing the AST and bytecode

You can print the generated AST, bytecode, or both while running a file:

python main.py --file examples/math.hava --ast       # print AST
python main.py --file examples/math.hava --bytecode  # print bytecode
python main.py --file examples/math.hava --debug     # print both

Status

Hava is still experimental.

Some parts are still rough and may change later. The project is mainly focused on learning compiler/interpreter concepts by building them from scratch.

Planned improvements:

  • better error messages
  • cleaner parser structure
  • local scopes
  • loop support in the bytecode VM
  • more examples
  • tests

Resources

Some resources, language reports and real-world source code references that helped me while building this project:

Interpreter and compiler resources

  • Crafting Interpreters — Robert Nystrom
  • Compilers: Principles, Techniques, and Tools — Aho, Lam, Sethi, Ullman
  • Engineering a Compiler — Keith Cooper and Linda Torczon

Language design and grammar references

About

Hava, Programming Language

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages